source: oup/releases/0.20a/Unit8_binedit.pas@ 413

Last change on this file since 413 was 21, checked in by alloc, 18 years ago
File size: 13.3 KB
Line 
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 exts:TStringList;
129 BEGIN
130 combo_extension.Items.Clear;
131 combo_extension.Items.Add('_All files_ ('{+IntToStr(dat_header.Files)}+')');
132 exts:=GetExtensionsList;
133 FOR i:=0 TO High(exts) DO
134 combo_extension.Items.Add(exts[i]);
135 combo_extension.ItemIndex:=0;
136 combo_extensionClick(Self);
137 END;
138
139PROCEDURE TForm8.FormCreate(Sender: TObject);
140 BEGIN
141 Self.Caption:='';
142 fileid:=0;
143 structs.ColCount:=5;
144 structs.RowCount:=2;
145 structs.FixedRows:=1;
146 structs.Cells[0,0]:='Name';
147 structs.Cells[1,0]:='Offset';
148 structs.Cells[2,0]:='Type';
149 structs.Cells[3,0]:='Value';
150 structs.Cells[4,0]:='Description';
151 structs.ColWidths[0]:=75;
152 structs.ColWidths[1]:=60;
153 structs.ColWidths[2]:=75;
154 structs.ColWidths[3]:=75;
155 Self.panel_dataResize(Self);
156 END;
157
158FUNCTION TForm8.Save:Boolean;
159 VAR
160 mem:TMemoryStream;
161 data:Tdata;
162 BEGIN
163 CASE MessageBox(Self.Handle,PChar('Save changes to file '+dat_files[fileid].FileName+'?'),PChar('Data changed...'),MB_YESNOCANCEL) OF
164 IDYES: BEGIN
165 mem:=TMemoryStream.Create;
166 hex.SaveToStream(mem);
167 mem.Seek(0,soFromBeginning);
168 SetLength(data,mem.Size);
169 mem.Read(data[0],mem.Size);
170 mem.Free;
171 UpdateDatFile(fileid,data);
172 Result:=True;
173 END;
174 IDNO: Result:=True;
175 IDCANCEL: BEGIN
176 Result:=False;
177 END;
178 END;
179 END;
180
181PROCEDURE TForm8.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
182 BEGIN
183 IF hex.Modified THEN BEGIN
184 IF NOT Save THEN CanClose:=False;
185 END;
186 END;
187
188PROCEDURE TForm8.listClick(Sender: TObject);
189 VAR
190 mem:TMemoryStream;
191 data:Tdata;
192 i:LongWord;
193 BEGIN
194 IF hex.Modified THEN BEGIN
195 IF NOT Save THEN BEGIN
196 FOR i:=0 TO list.Count-1 DO BEGIN
197 IF StrToInt(MidStr(list.Items.Strings[i],1,5))=fileid THEN BEGIN
198 list.ItemIndex:=i;
199 Exit;
200 END;
201 END;
202 END;
203 END;
204 Self.ClearStructViewer;
205 fileid:=StrToInt(MidStr(list.Items.Strings[list.ItemIndex],1,5));
206 data:=LoadDatFile(fileid);
207 IF Length(data)>0 THEN BEGIN
208 mem:=TMemoryStream.Create;
209 mem.Write(data[0],Length(data));
210 mem.Seek(0,soFromBeginning);
211 hex.LoadFromStream(mem);
212 mem.Free;
213 WriteStructureInfos(GetStructureInfoId(dat_files[fileid].Extension));
214 END;
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 files:TStringList;
301 i:LongWord;
302 BEGIN
303 Extension:=MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex],1,4);
304 list.Items.Clear;
305 IF Extension='_All' THEN
306 files:=GetFilesList('','',True)
307 ELSE
308 files:=GetFilesList(extension,'',True);
309 IF Length(files)>0 THEN
310 FOR i:=0 TO High(files) DO
311 list.Items.Add(files[i]);
312 END;
313
314PROCEDURE TForm8.FormClose(Sender: TObject; var Action: TCloseAction);
315 BEGIN
316 Action:=caFree;
317 Form1.close_window(Self.Name);
318 END;
319
320PROCEDURE TForm8.panel_imexportResize(Sender: TObject);
321 BEGIN
322 btn_import.Width:=panel_imexport.Width-8;
323 btn_export.Width:=panel_imexport.Width-8;
324 END;
325
326PROCEDURE TForm8.btn_exportClick(Sender: TObject);
327 BEGIN
328 saved.Filter:='Files of matching extension (*.'+dat_files[fileid].Extension+')|*.'+dat_files[fileid].Extension+'|All files|*.*';
329 saved.DefaultExt:=dat_files[fileid].Extension;
330 IF saved.Execute THEN BEGIN
331 ExportDatFile(fileid,saved.FileName);
332 END;
333 END;
334
335PROCEDURE TForm8.btn_importClick(Sender: TObject);
336 VAR
337 data:Tdata;
338 fs:TFileStream;
339 BEGIN
340 opend.Filter:='Files of matching extension (*.'+dat_files[fileid].Extension+')|*.'+dat_files[fileid].Extension+'|All files|*.*';
341 IF opend.Execute THEN BEGIN
342 fs:=TFileStream.Create(opend.FileName,fmOpenRead);
343 IF fs.Size<>dat_files[fileid].size THEN BEGIN
344 ShowMessage('Can''t import '+ExtractFilename(opend.FileName)+
345 ', file has to have same size as file in .dat.'+CrLf+
346 'Size of file in .dat: '+FormatFileSize(dat_files[fileid].size)+CrLf+
347 'Size of chosen file: '+FormatFileSize(fs.Size));
348 END ELSE BEGIN
349 SetLength(data,fs.Size);
350 fs.Read(data[0],fs.Size);
351 fs.Free;
352 fs:=TFileStream.Create(dat_filename,fmOpenReadWrite);
353 fs.Seek(dat_files[fileid].dataddr,soFromBeginning);
354 fs.Write(data[0],Length(data));
355 END;
356 fs.Free;
357 listClick(Self);
358 END;
359 END;
360
361PROCEDURE TForm8.FormActivate(Sender: TObject);
362 BEGIN
363 Form1.SetActiveWindow(Self.Name);
364 END;
365{
366PROCEDURE TForm8.structsKeyPress(Sender: TObject; VAR Key: Char);
367 VAR
368 dt:Byte;
369 BEGIN
370 IF structs.EditorMode THEN BEGIN
371 dt:=structure_infos[GetStructureInfoId(dat_files[fileid].Extension)].entries[structs.Row-1].datatype;
372 CASE dt OF
373 1..4: BEGIN
374 IF NOT (Key IN [#8,#13,#27,'0'..'9']) THEN Key:=#0;
375 END;
376 5..8: BEGIN
377 IF NOT (Key IN [#8,#13,#27,'0'..'9','A'..'F','a'..'f']) THEN Key:=#0;
378 IF Key IN ['a'..'f'] THEN Key:=UpCase(Key);
379 IF NOT (Key IN [#8,#13,#27]) AND ( Length(structs.Cells[structs.Col,structs.Row])>=2*(dt-4) ) THEN Key:=#0;
380 END;
381 9: BEGIN
382 IF (Key IN ['.']) AND (Pos('.',structs.Cells[structs.Col,structs.Row])>0) THEN Key:=#0;
383 IF NOT (Key IN [#8,#13,#27,'0'..'9','.','-']) THEN Key:=#0;
384 END;
385 10: BEGIN
386 IF NOT (Key IN [#8,#13,#27,'0'..'1']) THEN Key:=#0;
387 IF NOT (Key IN [#8,#13,#27]) AND ( Length(structs.Cells[structs.Col,structs.Row])>=8 ) THEN Key:=#0;
388 END;
389 END;
390 END;
391 END;
392
393PROCEDURE TForm8.structsSetEditText(Sender: TObject; ACol, ARow: Integer; CONST Value: string);
394 BEGIN
395 IF NOT TWrapGrid(Sender).EditorMode THEN
396 ShowMessage('['+IntToStr(ACol)+'|'+IntToStr(ARow)+']='+Value);
397 END;
398
399PROCEDURE TForm8.structsGetEditText(Sender: TObject; ACol, ARow: Integer; var Value: string);
400 BEGIN
401 IF structs.EditorMode THEN
402 ShowMessage('EditorMode - '+Value)
403 ELSE
404 ShowMessage('NOT EditorMode - '+Value);
405 END;
406}
407END.
Note: See TracBrowser for help on using the repository browser.