source: trunk/Packages/CoolStreaming/Demo/VarBlockSerializer/UMainForm.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: 9.0 KB
Line 
1unit UMainForm;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
9 Spin, UVarBlockSerializer, StrUtils, ComCtrls, ExtCtrls;
10
11type
12
13 { TMainForm }
14
15 TMainForm = class(TForm)
16 published
17 ButtonEncodeIndexed: TButton;
18 ButtonDecodeIndexed: TButton;
19 ButtonDecodeRaw: TButton;
20 ButtonDecodeString: TButton;
21 ButtonEncodeRaw: TButton;
22 ButtonEncodeString: TButton;
23 ButtonFloatDecode1: TButton;
24 ButtonFloatEncode1: TButton;
25 ButtonSIntDecode: TButton;
26 ButtonSIntEncode: TButton;
27 ButtonUIntDecode: TButton;
28 ButtonUIntEncode: TButton;
29 CheckBoxMask1: TCheckBox;
30 CheckBoxMask2: TCheckBox;
31 CheckBoxMask3: TCheckBox;
32 EditIndexedItem1: TEdit;
33 EditIndexedItem2: TEdit;
34 EditIndexedItem3: TEdit;
35 EditIndexed: TEdit;
36 EditFloat: TEdit;
37 EditRaw: TEdit;
38 EditRawData: TEdit;
39 EditSInt: TEdit;
40 EditString: TEdit;
41 EditStringData: TEdit;
42 EditUInt: TEdit;
43 FloatSpinEdit1: TFloatSpinEdit;
44 GroupBox1: TGroupBox;
45 GroupBox2: TGroupBox;
46 GroupBox3: TGroupBox;
47 GroupBox4: TGroupBox;
48 GroupBox5: TGroupBox;
49 Label1: TLabel;
50 Label2: TLabel;
51 Label3: TLabel;
52 Label4: TLabel;
53 Label5: TLabel;
54 Label6: TLabel;
55 PageControl1: TPageControl;
56 SpinEditFloat: TSpinEdit;
57 SpinEditSInt: TSpinEdit;
58 SpinEditUInt: TSpinEdit;
59 TabSheet1: TTabSheet;
60 TabSheet2: TTabSheet;
61 procedure ButtonDecodeIndexedClick(Sender: TObject);
62 procedure ButtonEncodeIndexedClick(Sender: TObject);
63 procedure ButtonDecodeRawClick(Sender: TObject);
64 procedure ButtonDecodeStringClick(Sender: TObject);
65 procedure ButtonEncodeRawClick(Sender: TObject);
66 procedure ButtonEncodeStringClick(Sender: TObject);
67 procedure ButtonFloatDecode1Click(Sender: TObject);
68 procedure ButtonFloatEncode1Click(Sender: TObject);
69 procedure ButtonSIntDecodeClick(Sender: TObject);
70 procedure ButtonSIntEncodeClick(Sender: TObject);
71 procedure ButtonUIntDecodeClick(Sender: TObject);
72 procedure ButtonUIntEncodeClick(Sender: TObject);
73 procedure FormShow(Sender: TObject);
74 public
75 function StreamToString(Stream: TStream): string;
76 procedure StringToStream(Text: string; Stream: TStream);
77 end;
78
79var
80 MainForm: TMainForm;
81
82implementation
83
84{$R *.lfm}
85
86{ TMainForm }
87
88procedure TMainForm.ButtonDecodeRawClick(Sender: TObject);
89var
90 Block: TVarBlockSerializer;
91 Stream: TMemoryStream;
92begin
93 try
94 Block := TVarBlockSerializer.Create;
95 Stream := TMemoryStream.Create;
96 StringToStream(EditRawData.Text, Block.Stream);
97 Block.Stream.Position := 0;
98 Block.ReadVarStream(Stream);
99 EditRaw.Text := StreamToString(Stream);
100 finally
101 Stream.Free;
102 Block.Free;
103 end;
104end;
105
106procedure TMainForm.ButtonEncodeIndexedClick(Sender: TObject);
107var
108 IndexedBlock: TVarBlockIndexed;
109 Stream: TMemoryStream;
110begin
111 try
112 IndexedBlock := TVarBlockIndexed.Create;
113 Stream := TMemoryStream.Create;
114 if CheckBoxMask1.Checked then begin
115 StringToStream(EditIndexedItem1.Text, Stream);
116 IndexedBlock.WriteVarStream(0, Stream);
117 end;
118 if CheckBoxMask2.Checked then begin
119 StringToStream(EditIndexedItem2.Text, Stream);
120 IndexedBlock.WriteVarStream(1, Stream);
121 end;
122 if CheckBoxMask3.Checked then begin
123 StringToStream(EditIndexedItem3.Text, Stream);
124 IndexedBlock.WriteVarStream(2, Stream);
125 end;
126 Stream.Clear;
127 IndexedBlock.WriteToStream(Stream);
128 EditIndexed.Text := StreamToString(Stream);
129 finally
130 IndexedBlock.Free;
131 Stream.Free;
132 end;
133end;
134
135procedure TMainForm.ButtonDecodeIndexedClick(Sender: TObject);
136var
137 IndexedBlock: TVarBlockIndexed;
138 Stream: TMemoryStream;
139begin
140 try
141 IndexedBlock := TVarBlockIndexed.Create;
142 Stream := TMemoryStream.Create;
143 StringToStream(EditIndexed.Text, Stream);
144 IndexedBlock.ReadFromStream(Stream);
145
146 CheckBoxMask1.Checked := IndexedBlock.TestIndex(0);
147 CheckBoxMask2.Checked := IndexedBlock.TestIndex(1);
148 CheckBoxMask3.Checked := IndexedBlock.TestIndex(2);
149
150 Stream.Clear;
151 if IndexedBlock.TestIndex(0) then begin
152 IndexedBlock.ReadVarStream(0, Stream);
153 EditIndexedItem1.Text := StreamToString(Stream);
154 end else EditIndexedItem1.Text := '';
155 if IndexedBlock.TestIndex(1) then begin
156 IndexedBlock.ReadVarStream(1, Stream);
157 EditIndexedItem2.Text := StreamToString(Stream);
158 end else EditIndexedItem2.Text := '';
159 if IndexedBlock.TestIndex(2) then begin
160 IndexedBlock.ReadVarStream(2, Stream);
161 EditIndexedItem3.Text := StreamToString(Stream);
162 end else EditIndexedItem3.Text := '';
163 finally
164 IndexedBlock.Free;
165 Stream.Free;
166 end;
167end;
168
169procedure TMainForm.ButtonDecodeStringClick(Sender: TObject);
170var
171 Block: TVarBlockSerializer;
172begin
173 try
174 Block := TVarBlockSerializer.Create;
175 StringToStream(EditStringData.Text, Block.Stream);
176 Block.Stream.Position := 0;
177 EditString.Text := Block.ReadVarString;
178 finally
179 Block.Free;
180 end;
181end;
182
183procedure TMainForm.ButtonEncodeRawClick(Sender: TObject);
184var
185 Block: TVarBlockSerializer;
186 Stream: TMemoryStream;
187begin
188 try
189 Block := TVarBlockSerializer.Create;
190 Stream := TMemoryStream.Create;
191 StringToStream(EditRaw.Text, Stream);
192 Block.WriteVarStream(Stream);
193 EditRawData.Text := StreamToString(Block.Stream);
194 finally
195 Stream.Free;
196 Block.Free;
197 end;
198end;
199
200procedure TMainForm.ButtonEncodeStringClick(Sender: TObject);
201var
202 Block: TVarBlockSerializer;
203begin
204 try
205 Block := TVarBlockSerializer.Create;
206 Block.WriteVarString(EditString.Text);
207 EditStringData.Text := StreamToString(Block.Stream);
208 finally
209 Block.Free;
210 end;
211end;
212
213procedure TMainForm.ButtonFloatDecode1Click(Sender: TObject);
214var
215 Block: TVarBlockSerializer;
216begin
217 try
218 Block := TVarBlockSerializer.Create;
219 StringToStream(EditFloat.Text, Block.Stream);
220 Block.Stream.Position := 0;
221 FloatSpinEdit1.Value := Block.ReadVarFloat(SpinEditFloat.Value);
222 finally
223 Block.Free;
224 end;
225end;
226
227procedure TMainForm.ButtonFloatEncode1Click(Sender: TObject);
228var
229 Block: TVarBlockSerializer;
230begin
231 try
232 Block := TVarBlockSerializer.Create;
233 Block.WriteVarFloat(FloatSpinEdit1.Value, SpinEditFloat.Value);
234 EditFloat.Text := StreamToString(Block.Stream);
235 finally
236 Block.Free;
237 end;
238end;
239
240procedure TMainForm.ButtonSIntDecodeClick(Sender: TObject);
241var
242 Block: TVarBlockSerializer;
243begin
244 try
245 Block := TVarBlockSerializer.Create;
246 StringToStream(EditSInt.Text, Block.Stream);
247 Block.Stream.Position := 0;
248 SpinEditSInt.Value := Block.ReadVarSInt;
249 finally
250 Block.Free;
251 end;
252end;
253
254procedure TMainForm.ButtonSIntEncodeClick(Sender: TObject);
255var
256 Block: TVarBlockSerializer;
257begin
258 try
259 Block := TVarBlockSerializer.Create;
260 Block.WriteVarSInt(SpinEditSInt.Value);
261 EditSInt.Text := StreamToString(Block.Stream);
262 finally
263 Block.Free;
264 end;
265end;
266
267procedure TMainForm.ButtonUIntDecodeClick(Sender: TObject);
268var
269 Block: TVarBlockSerializer;
270begin
271 try
272 Block := TVarBlockSerializer.Create;
273 StringToStream(EditUInt.Text, Block.Stream);
274 Block.Stream.Position := 0;
275 SpinEditUInt.Value := Block.ReadVarUInt;
276 finally
277 Block.Free;
278 end;
279end;
280
281procedure TMainForm.ButtonUIntEncodeClick(Sender: TObject);
282var
283 Block: TVarBlockSerializer;
284begin
285 try
286 Block := TVarBlockSerializer.Create;
287 Block.WriteVarUInt(SpinEditUInt.Value);
288 EditUInt.Text := StreamToString(Block.Stream);
289 finally
290 Block.Free;
291 end;
292end;
293
294procedure TMainForm.FormShow(Sender: TObject);
295begin
296 PageControl1.TabIndex := 0;
297end;
298
299function TMainForm.StreamToString(Stream: TStream): string;
300begin
301 Result := '';
302 Stream.Position := 0;
303 while Stream.Position < Stream.Size do
304 Result := Result + IntToHex(Stream.ReadByte, 2) + ' ';
305end;
306
307function TryHexToInt(Data: string; var Value: Integer): Boolean;
308var
309 I: Integer;
310begin
311 Data := UpperCase(Data);
312 Result := True;
313 Value := 0;
314 for I := 0 to Length(Data) - 1 do begin
315 if (Data[I + 1] >= '0') and (Data[I + 1] <= '9') then
316 Value := Value or (Ord(Data[I + 1]) - Ord('0')) shl ((Length(Data) - I - 1) * 4)
317 else if (Data[I + 1] >= 'A') and (Data[I + 1] <= 'F') then
318 Value := Value or (Ord(Data[I + 1]) - Ord('A') + 10) shl ((Length(Data) - I - 1) * 4)
319 else Result := False;
320 end;
321end;
322
323procedure TMainForm.StringToStream(Text: string; Stream: TStream);
324var
325 Part: string;
326 Number: Integer;
327begin
328 Stream.Size := 0;
329 Text := Trim(Text);
330 while Pos(' ', Text) > 0 do begin
331 Part := Copy(Text, 1, Pos(' ', Text) - 1);
332 if TryHexToInt(Part, Number) and (Number < 256) then
333 Stream.WriteByte(Number);
334 Delete(Text, 1, Pos(' ', Text));
335 end;
336 if TryHexToInt(Text, Number) and (Number < 256) and (Text <> '') then
337 Stream.WriteByte(Number);
338end;
339
340end.
341
Note: See TracBrowser for help on using the repository browser.