source: trunk/Packages/CoolStreaming/UDynNumber.pas

Last change on this file was 2, checked in by chronos, 9 years ago
  • Added: TPakFile class for extraction PAK files from original game.
File size: 1.7 KB
Line 
1unit UDynNumber;
2
3{$mode objfpc}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, UBitStream, Math;
9
10type
11
12 { TDynamicNumber }
13
14 TDynamicNumber = class
15 Stream: TBitStream;
16 procedure WriteNumber(Value: QWord);
17 function ReadNumber: QWord;
18 constructor Create;
19 destructor Destroy; override;
20 private
21 function ReadNumber2: QWord;
22 end;
23
24implementation
25
26{ TDynamicNumber }
27
28procedure TDynamicNumber.WriteNumber(Value: QWord);
29var
30 Length: Integer;
31begin
32 Length := Floor(Log2(Value)) + 1;
33 if Length > 1 then begin
34 Stream.WriteNumber(1, 1);
35 WriteNumber(Length - 2);
36 end else Stream.WriteNumber(0, 1);
37 if Length > 1 then Length := Length - 1;
38 Stream.WriteNumber(Value, Length);
39end;
40
41function TDynamicNumber.ReadNumber: QWord;
42var
43 Bit: Byte;
44 Length: Integer;
45begin
46 Length := 0;
47 Bit := Stream.ReadNumber(1);
48 if Bit = 0 then Length := 1
49 else Length := ReadNumber2 + 2;
50 if Length > 1 then Result := Stream.ReadNumber(Length - 1)
51 else Result := Stream.ReadNumber(Length);
52 if Length > 1 then Result := Result or (QWord(1) shl (Length - 1));
53end;
54
55function TDynamicNumber.ReadNumber2: QWord;
56var
57 Bit: Byte;
58 Length: Integer;
59begin
60 Length := 0;
61 Bit := Stream.ReadNumber(1);
62 if Bit = 0 then Length := 1
63 else Length := ReadNumber + 2;
64 if Length > 1 then Result := Stream.ReadNumber(Length - 1)
65 else Result := Stream.ReadNumber(Length);
66 if Length > 1 then Result := Result or (QWord(1) shl (Length - 1));
67end;
68
69constructor TDynamicNumber.Create;
70begin
71 Stream := TMemoryBitStream.Create;
72end;
73
74destructor TDynamicNumber.Destroy;
75begin
76 Stream.Free;
77 inherited Destroy;
78end;
79
80end.
81
Note: See TracBrowser for help on using the repository browser.