source: oup/releases/0.21a/Unit8_binedit.pas@ 1043

Last change on this file since 1043 was 21, checked in by alloc, 18 years ago
File size: 13.4 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:Word; 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:Word; offset:LongWord):String;
72 VAR
73 data:Tdata;
74 i:Word;
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 10000..65535: BEGIN
95 Result:='';
96 FOR i:=1 TO datatype-10000 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 IF entries[i-1].datatype>10000 THEN
119 Self.structs.Cells[3,i]:='*String*#HINT:'+GetValue(entries[i-1].datatype,entries[i-1].offset)+'#'
120 ELSE
121 Self.structs.Cells[3,i]:=GetValue(entries[i-1].datatype,entries[i-1].offset);
122 Self.structs.Cells[4,i]:=entries[i-1].description;
123 END;
124 END;
125 END;
126 END;
127
128PROCEDURE TForm8.Recreatelist;
129 VAR
130 i:LongWord;
131 exts:TStringList;
132 BEGIN
133 combo_extension.Items.Clear;
134 combo_extension.Items.Add('_All files_ ('{+IntToStr(dat_header.Files)}+')');
135 exts:=GetExtensionsList;
136 FOR i:=0 TO High(exts) DO
137 combo_extension.Items.Add(exts[i]);
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 UpdateDatFile(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 IF Length(data)>0 THEN BEGIN
211 mem:=TMemoryStream.Create;
212 mem.Write(data[0],Length(data));
213 mem.Seek(0,soFromBeginning);
214 hex.LoadFromStream(mem);
215 mem.Free;
216 WriteStructureInfos(GetStructureInfoId(dat_files[fileid].Extension));
217 END;
218 END;
219
220PROCEDURE TForm8.ClearStructViewer;
221 VAR
222 x:Word;
223 BEGIN
224 structs.RowCount:=2;
225 FOR x:=0 TO structs.ColCount-1 DO structs.Cells[x,1]:='';
226 structs.Enabled:=False;
227 END;
228
229PROCEDURE TForm8.FormResize(Sender: TObject);
230 BEGIN
231 IF Self.Width>=650 THEN BEGIN
232 END ELSE Self.Width:=650;
233 IF Self.Height>=450 THEN BEGIN
234 END ELSE Self.Height:=450;
235 END;
236
237PROCEDURE TForm8.structsClick(Sender: TObject);
238 VAR
239 offset:LongWord;
240 length:Byte;
241 BEGIN
242 IF structs.Row>0 THEN BEGIN
243 offset:=structure_infos[GetStructureInfoId(dat_files[fileid].extension)].entries[structs.Row-1].offset;
244 length:=GetTypeDataLength(structure_infos[GetStructureInfoId(dat_files[fileid].extension)].entries[structs.Row-1].datatype);
245 hex.SelStart:=offset;
246 hex.SelEnd:=offset+length-1;
247{ IF structs.Cells[structs.Col,0]='Value' THEN
248 IF structure_infos[GetStructureInfoId(dat_files[fileid].Extension)].entries[structs.Row-1].datatype<=10 THEN
249 structs.Options:=structs.Options+[goEditing]
250 ELSE
251 structs.Options:=structs.Options-[goEditing];
252} END;
253 END;
254
255PROCEDURE TForm8.panel_dataResize(Sender: TObject);
256 BEGIN
257 structs.ColWidths[4]:=structs.Width-structs.ColWidths[0]-structs.ColWidths[1]-structs.ColWidths[2]-structs.ColWidths[3]-28;
258 END;
259
260PROCEDURE TForm8.hexChange(Sender: TObject);
261 BEGIN
262 IF hex.DataSize>0 THEN
263 WriteStructureInfos(GetStructureInfoId(dat_files[fileid].Extension));
264 END;
265
266PROCEDURE TForm8.hexSelectionChanged(Sender: TObject);
267 VAR
268 selstart:Integer;
269 i,j:Word;
270 BEGIN
271 FOR i:=1 TO structs.RowCount-1 DO BEGIN
272 FOR j:=0 TO structs.ColCount-1 DO BEGIN
273 structs.CellColors[j,i]:=clWhite;
274 structs.CellFontColors[j,i]:=clBlack;
275 END;
276 END;
277 IF hex.DataSize>0 THEN BEGIN
278 selstart:=hex.SelStart;
279 IF GetStructureInfoId(dat_files[fileid].Extension)>=0 THEN BEGIN
280 WITH structure_infos[GetStructureInfoId(dat_files[fileid].Extension)] DO BEGIN
281 FOR i:=0 TO High(entries) DO BEGIN
282 IF ((selstart-entries[i].offset)<GetTypeDataLength(entries[i].datatype)) AND ((selstart-entries[i].offset)>=0) THEN BEGIN
283 FOR j:=0 TO structs.ColCount-1 DO BEGIN
284 structs.CellColors[j,i+1]:=clBlue;
285 structs.CellFontColors[j,i+1]:=clWhite;
286 END;
287 structs.Row:=i+1;
288 END;
289 END;
290 END;
291 END;
292 END;
293 END;
294
295PROCEDURE TForm8.panel_extensionResize(Sender: TObject);
296 BEGIN
297 combo_extension.Width:=panel_extension.Width-5;
298 END;
299
300PROCEDURE TForm8.combo_extensionClick(Sender: TObject);
301 VAR
302 Extension:String[4];
303 files:TStringList;
304 i:LongWord;
305 BEGIN
306 Extension:=MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex],1,4);
307 list.Items.Clear;
308 IF Extension='_All' THEN
309 files:=GetFilesList('','',True)
310 ELSE
311 files:=GetFilesList(extension,'',True);
312 IF Length(files)>0 THEN
313 FOR i:=0 TO High(files) DO
314 list.Items.Add(files[i]);
315 END;
316
317PROCEDURE TForm8.FormClose(Sender: TObject; var Action: TCloseAction);
318 BEGIN
319 Action:=caFree;
320 Form1.close_window(Self.Name);
321 END;
322
323PROCEDURE TForm8.panel_imexportResize(Sender: TObject);
324 BEGIN
325 btn_import.Width:=panel_imexport.Width-8;
326 btn_export.Width:=panel_imexport.Width-8;
327 END;
328
329PROCEDURE TForm8.btn_exportClick(Sender: TObject);
330 BEGIN
331 saved.Filter:='Files of matching extension (*.'+dat_files[fileid].Extension+')|*.'+dat_files[fileid].Extension+'|All files|*.*';
332 saved.DefaultExt:=dat_files[fileid].Extension;
333 IF saved.Execute THEN BEGIN
334 ExportDatFile(fileid,saved.FileName);
335 END;
336 END;
337
338PROCEDURE TForm8.btn_importClick(Sender: TObject);
339 VAR
340 data:Tdata;
341 fs:TFileStream;
342 BEGIN
343 opend.Filter:='Files of matching extension (*.'+dat_files[fileid].Extension+')|*.'+dat_files[fileid].Extension+'|All files|*.*';
344 IF opend.Execute THEN BEGIN
345 fs:=TFileStream.Create(opend.FileName,fmOpenRead);
346 IF fs.Size<>dat_files[fileid].size THEN BEGIN
347 ShowMessage('Can''t import '+ExtractFilename(opend.FileName)+
348 ', file has to have same size as file in .dat.'+CrLf+
349 'Size of file in .dat: '+FormatFileSize(dat_files[fileid].size)+CrLf+
350 'Size of chosen file: '+FormatFileSize(fs.Size));
351 END ELSE BEGIN
352 SetLength(data,fs.Size);
353 fs.Read(data[0],fs.Size);
354 fs.Free;
355 fs:=TFileStream.Create(dat_filename,fmOpenReadWrite);
356 fs.Seek(dat_files[fileid].dataddr,soFromBeginning);
357 fs.Write(data[0],Length(data));
358 END;
359 fs.Free;
360 listClick(Self);
361 END;
362 END;
363
364PROCEDURE TForm8.FormActivate(Sender: TObject);
365 BEGIN
366 Form1.SetActiveWindow(Self.Name);
367 END;
368{
369PROCEDURE TForm8.structsKeyPress(Sender: TObject; VAR Key: Char);
370 VAR
371 dt:Byte;
372 BEGIN
373 IF structs.EditorMode THEN BEGIN
374 dt:=structure_infos[GetStructureInfoId(dat_files[fileid].Extension)].entries[structs.Row-1].datatype;
375 CASE dt OF
376 1..4: BEGIN
377 IF NOT (Key IN [#8,#13,#27,'0'..'9']) THEN Key:=#0;
378 END;
379 5..8: BEGIN
380 IF NOT (Key IN [#8,#13,#27,'0'..'9','A'..'F','a'..'f']) THEN Key:=#0;
381 IF Key IN ['a'..'f'] THEN Key:=UpCase(Key);
382 IF NOT (Key IN [#8,#13,#27]) AND ( Length(structs.Cells[structs.Col,structs.Row])>=2*(dt-4) ) THEN Key:=#0;
383 END;
384 9: BEGIN
385 IF (Key IN ['.']) AND (Pos('.',structs.Cells[structs.Col,structs.Row])>0) THEN Key:=#0;
386 IF NOT (Key IN [#8,#13,#27,'0'..'9','.','-']) THEN Key:=#0;
387 END;
388 10: BEGIN
389 IF NOT (Key IN [#8,#13,#27,'0'..'1']) THEN Key:=#0;
390 IF NOT (Key IN [#8,#13,#27]) AND ( Length(structs.Cells[structs.Col,structs.Row])>=8 ) THEN Key:=#0;
391 END;
392 END;
393 END;
394 END;
395
396PROCEDURE TForm8.structsSetEditText(Sender: TObject; ACol, ARow: Integer; CONST Value: string);
397 BEGIN
398 IF NOT TWrapGrid(Sender).EditorMode THEN
399 ShowMessage('['+IntToStr(ACol)+'|'+IntToStr(ARow)+']='+Value);
400 END;
401
402PROCEDURE TForm8.structsGetEditText(Sender: TObject; ACol, ARow: Integer; var Value: string);
403 BEGIN
404 IF structs.EditorMode THEN
405 ShowMessage('EditorMode - '+Value)
406 ELSE
407 ShowMessage('NOT EditorMode - '+Value);
408 END;
409}
410END.
Note: See TracBrowser for help on using the repository browser.