source: oup/releases/0.19a/Unit8_binedit.pas@ 864

Last change on this file since 864 was 21, checked in by alloc, 18 years ago
File size: 13.5 KB
RevLine 
[21]1UNIT Unit8_binedit;
2INTERFACE
3USES
4 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
5 Dialogs, Wrapgrid, StdCtrls, Grids, StrUtils, MPHexEditor, ExtCtrls,
6 Unit3_data, Unit2_functions, Unit9_data_structures, Unit4_exporters;
7
8TYPE
9 TForm8 = Class(TForm)
10 Splitter1: TSplitter;
11 panel_data: TPanel;
12 hex: TMPHexEditor;
13 Splitter2: TSplitter;
14 structs: TWrapGrid;
15 panel_files: TPanel;
16 list: TListBox;
17 panel_extension: TPanel;
18 lbl_filter: TLabel;
19 combo_extension: TComboBox;
20 Bevel1: TBevel;
21 panel_imexport: TPanel;
22 btn_export: TButton;
23 btn_import: TButton;
24 opend: TOpenDialog;
25 saved: TSaveDialog;
26{ PROCEDURE structsGetEditText(Sender: TObject; ACol, ARow: Integer; var Value: string);
27 PROCEDURE structsSetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string);
28 PROCEDURE structsKeyPress(Sender: TObject; var Key: Char);
29} PROCEDURE FormActivate(Sender: TObject);
30 PROCEDURE btn_importClick(Sender: TObject);
31 PROCEDURE btn_exportClick(Sender: TObject);
32 PROCEDURE panel_imexportResize(Sender: TObject);
33 FUNCTION Save:Boolean;
34 PROCEDURE FormClose(Sender: TObject; var Action: TCloseAction);
35 PROCEDURE combo_extensionClick(Sender: TObject);
36 PROCEDURE panel_extensionResize(Sender: TObject);
37 FUNCTION GetValue(datatype:Byte; offset:LongWord):String;
38 PROCEDURE WriteStructureInfos(structinfoid:Integer);
39 PROCEDURE hexSelectionChanged(Sender: TObject);
40 PROCEDURE hexChange(Sender: TObject);
41 PROCEDURE panel_dataResize(Sender: TObject);
42 PROCEDURE structsClick(Sender: TObject);
43 PROCEDURE FormResize(Sender: TObject);
44 PROCEDURE ClearStructViewer;
45 PROCEDURE listClick(Sender: TObject);
46 PROCEDURE FormCloseQuery(Sender: TObject; var CanClose: Boolean);
47 PROCEDURE FormCreate(Sender: TObject);
48 PROCEDURE Recreatelist;
49 PRIVATE
50 PUBLIC
51 END;
52
53VAR
54 Form8: TForm8;
55
56IMPLEMENTATION
57{$R *.dfm}
58USES Unit1_main;
59VAR
60 fileid:LongWord;
61
62FUNCTION IntToBin(value:Byte):String;
63 VAR i:Byte;
64 BEGIN
65 Result:='';
66 FOR i:=7 DOWNTO 0 DO BEGIN
67 Result:=Result+IntToStr((value SHR i) AND $01);
68 END;
69 END;
70
71FUNCTION TForm8.GetValue(datatype:Byte; offset:LongWord):String;
72 VAR
73 data:Tdata;
74 i:Byte;
75 BEGIN
76 CASE datatype OF
77 1: Result:=IntToStr(hex.data[offset]);
78 2: Result:=IntToStr(hex.data[offset]+hex.data[offset+1]*256);
79 3: Result:=IntToStr(hex.data[offset]+hex.data[offset+1]*256+hex.data[offset+2]*256*256);
80 4: Result:=IntToStr(hex.data[offset]+hex.data[offset+1]*256+hex.data[offset+2]*256*256+hex.data[offset+3]*256*256*256);
81 5: Result:='0x'+IntToHex(hex.data[offset],2);
82 6: Result:='0x'+IntToHex(hex.data[offset]+hex.data[offset+1]*256,4);
83 7: Result:='0x'+IntToHex(hex.data[offset]+hex.data[offset+1]*256+hex.data[offset+2]*256*256,6);
84 8: Result:='0x'+IntToHex(hex.data[offset]+hex.data[offset+1]*256+hex.data[offset+2]*256*256+hex.data[offset+3]*256*256*256,8);
85 9: BEGIN
86 SetLength(data,4);
87 data[0]:=hex.data[offset];
88 data[1]:=hex.data[offset+1];
89 data[2]:=hex.data[offset+2];
90 data[3]:=hex.data[offset+3];
91 Result:=FloatToStr(Decode_Float(data));
92 END;
93 10: Result:=IntToBin(hex.data[offset]);
94 11..255: BEGIN
95 Result:='';
96 FOR i:=1 TO datatype-10 DO BEGIN
97 IF hex.Data[offset+i-1]>=32 THEN
98 Result:=Result+Chr(hex.Data[offset+i-1])
99 ELSE
100 Result:=Result+'.';
101 END;
102 END;
103 END;
104 END;
105
106PROCEDURE TForm8.WriteStructureInfos(structinfoid:Integer);
107 VAR
108 i:Byte;
109 BEGIN
110 IF structinfoid>=0 THEN BEGIN
111 structs.Enabled:=True;
112 WITH structure_infos[structinfoid] DO BEGIN
113 Self.structs.RowCount:=Length(entries)+1;
114 FOR i:=1 TO Length(entries) DO BEGIN
115 Self.structs.Cells[0,i]:=entries[i-1].name;
116 Self.structs.Cells[1,i]:='0x'+IntToHex(entries[i-1].offset,6);
117 Self.structs.Cells[2,i]:=GetDataType(entries[i-1].datatype);
118 Self.structs.Cells[3,i]:=GetValue(entries[i-1].datatype,entries[i-1].offset);
119 Self.structs.Cells[4,i]:=entries[i-1].description;
120 END;
121 END;
122 END;
123 END;
124
125PROCEDURE TForm8.Recreatelist;
126 VAR
127 i:LongWord;
128 BEGIN
129 combo_extension.Items.Clear;
130 combo_extension.Items.Add('_All files_ ('+IntToStr(dat_header.Files)+')');
131 FOR i:=0 TO dat_header.Extensions-1 DO BEGIN
132 WITH dat_extensionsmap[i] DO BEGIN
133 combo_extension.Items.Add(
134 Extension[3]+Extension[2]+Extension[1]+Extension[0]+' ('+
135 IntToStr(ExtCount)+')');
136 END;
137 END;
138 combo_extension.ItemIndex:=0;
139 combo_extensionClick(Self);
140 END;
141
142PROCEDURE TForm8.FormCreate(Sender: TObject);
143 BEGIN
144 Self.Caption:='';
145 fileid:=0;
146 structs.ColCount:=5;
147 structs.RowCount:=2;
148 structs.FixedRows:=1;
149 structs.Cells[0,0]:='Name';
150 structs.Cells[1,0]:='Offset';
151 structs.Cells[2,0]:='Type';
152 structs.Cells[3,0]:='Value';
153 structs.Cells[4,0]:='Description';
154 structs.ColWidths[0]:=75;
155 structs.ColWidths[1]:=60;
156 structs.ColWidths[2]:=75;
157 structs.ColWidths[3]:=75;
158 Self.panel_dataResize(Self);
159 END;
160
161FUNCTION TForm8.Save:Boolean;
162 VAR
163 mem:TMemoryStream;
164 data:Tdata;
165 BEGIN
166 CASE MessageBox(Self.Handle,PChar('Save changes to file '+dat_files[fileid].FileName+'?'),PChar('Data changed...'),MB_YESNOCANCEL) OF
167 IDYES: BEGIN
168 mem:=TMemoryStream.Create;
169 hex.SaveToStream(mem);
170 mem.Seek(0,soFromBeginning);
171 SetLength(data,mem.Size);
172 mem.Read(data[0],mem.Size);
173 mem.Free;
174 SaveDatFile(fileid,data);
175 Result:=True;
176 END;
177 IDNO: Result:=True;
178 IDCANCEL: BEGIN
179 Result:=False;
180 END;
181 END;
182 END;
183
184PROCEDURE TForm8.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
185 BEGIN
186 IF hex.Modified THEN BEGIN
187 IF NOT Save THEN CanClose:=False;
188 END;
189 END;
190
191PROCEDURE TForm8.listClick(Sender: TObject);
192 VAR
193 mem:TMemoryStream;
194 data:Tdata;
195 i:LongWord;
196 BEGIN
197 IF hex.Modified THEN BEGIN
198 IF NOT Save THEN BEGIN
199 FOR i:=0 TO list.Count-1 DO BEGIN
200 IF StrToInt(MidStr(list.Items.Strings[i],1,5))=fileid THEN BEGIN
201 list.ItemIndex:=i;
202 Exit;
203 END;
204 END;
205 END;
206 END;
207 Self.ClearStructViewer;
208 fileid:=StrToInt(MidStr(list.Items.Strings[list.ItemIndex],1,5));
209 data:=LoadDatFile(fileid);
210 mem:=TMemoryStream.Create;
211 mem.Write(data[0],Length(data));
212 hex.LoadFromStream(mem);
213 mem.Free;
214 WriteStructureInfos(GetStructureInfoId(dat_files[fileid].Extension));
215 END;
216
217PROCEDURE TForm8.ClearStructViewer;
218 VAR
219 x:Word;
220 BEGIN
221 structs.RowCount:=2;
222 FOR x:=0 TO structs.ColCount-1 DO structs.Cells[x,1]:='';
223 structs.Enabled:=False;
224 END;
225
226PROCEDURE TForm8.FormResize(Sender: TObject);
227 BEGIN
228 IF Self.Width>=650 THEN BEGIN
229 END ELSE Self.Width:=650;
230 IF Self.Height>=450 THEN BEGIN
231 END ELSE Self.Height:=450;
232 END;
233
234PROCEDURE TForm8.structsClick(Sender: TObject);
235 VAR
236 offset:LongWord;
237 length:Byte;
238 BEGIN
239 IF structs.Row>0 THEN BEGIN
240 offset:=structure_infos[GetStructureInfoId(dat_files[fileid].extension)].entries[structs.Row-1].offset;
241 length:=GetTypeDataLength(structure_infos[GetStructureInfoId(dat_files[fileid].extension)].entries[structs.Row-1].datatype);
242 hex.SelStart:=offset;
243 hex.SelEnd:=offset+length-1;
244{ IF structs.Cells[structs.Col,0]='Value' THEN
245 IF structure_infos[GetStructureInfoId(dat_files[fileid].Extension)].entries[structs.Row-1].datatype<=10 THEN
246 structs.Options:=structs.Options+[goEditing]
247 ELSE
248 structs.Options:=structs.Options-[goEditing];
249} END;
250 END;
251
252PROCEDURE TForm8.panel_dataResize(Sender: TObject);
253 BEGIN
254 structs.ColWidths[4]:=structs.Width-structs.ColWidths[0]-structs.ColWidths[1]-structs.ColWidths[2]-structs.ColWidths[3]-28;
255 END;
256
257PROCEDURE TForm8.hexChange(Sender: TObject);
258 BEGIN
259 IF hex.DataSize>0 THEN
260 WriteStructureInfos(GetStructureInfoId(dat_files[fileid].Extension));
261 END;
262
263PROCEDURE TForm8.hexSelectionChanged(Sender: TObject);
264 VAR
265 selstart:Integer;
266 i,j:Word;
267 BEGIN
268 FOR i:=1 TO structs.RowCount-1 DO BEGIN
269 FOR j:=0 TO structs.ColCount-1 DO BEGIN
270 structs.CellColors[j,i]:=clWhite;
271 structs.CellFontColors[j,i]:=clBlack;
272 END;
273 END;
274 IF hex.DataSize>0 THEN BEGIN
275 selstart:=hex.SelStart;
276 IF GetStructureInfoId(dat_files[fileid].Extension)>=0 THEN BEGIN
277 WITH structure_infos[GetStructureInfoId(dat_files[fileid].Extension)] DO BEGIN
278 FOR i:=0 TO High(entries) DO BEGIN
279 IF ((selstart-entries[i].offset)<GetTypeDataLength(entries[i].datatype)) AND ((selstart-entries[i].offset)>=0) THEN BEGIN
280 FOR j:=0 TO structs.ColCount-1 DO BEGIN
281 structs.CellColors[j,i+1]:=clBlue;
282 structs.CellFontColors[j,i+1]:=clWhite;
283 END;
284 structs.Row:=i+1;
285 END;
286 END;
287 END;
288 END;
289 END;
290 END;
291
292PROCEDURE TForm8.panel_extensionResize(Sender: TObject);
293 BEGIN
294 combo_extension.Width:=panel_extension.Width-5;
295 END;
296
297PROCEDURE TForm8.combo_extensionClick(Sender: TObject);
298 VAR
299 Extension:String[4];
300 i:LongWord;
301 BEGIN
302 Extension:=MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex],1,4);
303 list.Items.Clear;
304 IF Extension='_All' THEN BEGIN
305 FOR i:=0 TO dat_header.Files-1 DO
306 IF (dat_files[i].FileType AND $02)=0 THEN
307 list.Items.Add(dat_files[i].FileName);
308 END ELSE BEGIN
309 FOR i:=0 TO dat_header.Files-1 DO
310 IF dat_files[i].Extension=Extension THEN
311 IF (dat_files[i].FileType AND $02)=0 THEN
312 list.Items.Add(dat_files[i].FileName);
313 END;
314 IF list.Count>0 THEN BEGIN
315 list.ItemIndex:=0;
316 listClick(Self);
317 END;
318 END;
319
320PROCEDURE TForm8.FormClose(Sender: TObject; var Action: TCloseAction);
321 BEGIN
322 Action:=caFree;
323 Form1.close_window(Self.Name);
324 END;
325
326PROCEDURE TForm8.panel_imexportResize(Sender: TObject);
327 BEGIN
328 btn_import.Width:=panel_imexport.Width-8;
329 btn_export.Width:=panel_imexport.Width-8;
330 END;
331
332PROCEDURE TForm8.btn_exportClick(Sender: TObject);
333 BEGIN
334 saved.Filter:='Files of matching extension (*.'+dat_files[fileid].Extension+')|*.'+dat_files[fileid].Extension+'|All files|*.*';
335 saved.DefaultExt:=dat_files[fileid].Extension;
336 IF saved.Execute THEN BEGIN
337 ExportDatFile(fileid,saved.FileName);
338 END;
339 END;
340
341PROCEDURE TForm8.btn_importClick(Sender: TObject);
342 VAR
343 data:Tdata;
344 fs:TFileStream;
345 BEGIN
346 opend.Filter:='Files of matching extension (*.'+dat_files[fileid].Extension+')|*.'+dat_files[fileid].Extension+'|All files|*.*';
347 IF opend.Execute THEN BEGIN
348 fs:=TFileStream.Create(opend.FileName,fmOpenRead);
349 IF fs.Size<>dat_files[fileid].size THEN BEGIN
350 ShowMessage('Can''t import '+ExtractFilename(opend.FileName)+
351 ', file has to have same size as file in .dat.'+CrLf+
352 'Size of file in .dat: '+FormatFileSize(dat_files[fileid].size)+CrLf+
353 'Size of chosen file: '+FormatFileSize(fs.Size));
354 END ELSE BEGIN
355 SetLength(data,fs.Size);
356 fs.Read(data[0],fs.Size);
357 fs.Free;
358 fs:=TFileStream.Create(dat_filename,fmOpenReadWrite);
359 fs.Seek(dat_files[fileid].dataddr,soFromBeginning);
360 fs.Write(data[0],Length(data));
361 END;
362 fs.Free;
363 listClick(Self);
364 END;
365 END;
366
367PROCEDURE TForm8.FormActivate(Sender: TObject);
368 BEGIN
369 Form1.SetActiveWindow(Self.Name);
370 END;
371{
372PROCEDURE TForm8.structsKeyPress(Sender: TObject; VAR Key: Char);
373 VAR
374 dt:Byte;
375 BEGIN
376 IF structs.EditorMode THEN BEGIN
377 dt:=structure_infos[GetStructureInfoId(dat_files[fileid].Extension)].entries[structs.Row-1].datatype;
378 CASE dt OF
379 1..4: BEGIN
380 IF NOT (Key IN [#8,#13,#27,'0'..'9']) THEN Key:=#0;
381 END;
382 5..8: BEGIN
383 IF NOT (Key IN [#8,#13,#27,'0'..'9','A'..'F','a'..'f']) THEN Key:=#0;
384 IF Key IN ['a'..'f'] THEN Key:=UpCase(Key);
385 IF NOT (Key IN [#8,#13,#27]) AND ( Length(structs.Cells[structs.Col,structs.Row])>=2*(dt-4) ) THEN Key:=#0;
386 END;
387 9: BEGIN
388 IF (Key IN ['.']) AND (Pos('.',structs.Cells[structs.Col,structs.Row])>0) THEN Key:=#0;
389 IF NOT (Key IN [#8,#13,#27,'0'..'9','.','-']) THEN Key:=#0;
390 END;
391 10: BEGIN
392 IF NOT (Key IN [#8,#13,#27,'0'..'1']) THEN Key:=#0;
393 IF NOT (Key IN [#8,#13,#27]) AND ( Length(structs.Cells[structs.Col,structs.Row])>=8 ) THEN Key:=#0;
394 END;
395 END;
396 END;
397 END;
398
399PROCEDURE TForm8.structsSetEditText(Sender: TObject; ACol, ARow: Integer; CONST Value: string);
400 BEGIN
401 IF NOT TWrapGrid(Sender).EditorMode THEN
402 ShowMessage('['+IntToStr(ACol)+'|'+IntToStr(ARow)+']='+Value);
403 END;
404
405PROCEDURE TForm8.structsGetEditText(Sender: TObject; ACol, ARow: Integer; var Value: string);
406 BEGIN
407 IF structs.EditorMode THEN
408 ShowMessage('EditorMode - '+Value)
409 ELSE
410 ShowMessage('NOT EditorMode - '+Value);
411 END;
412}
413END.
Note: See TracBrowser for help on using the repository browser.