source: oup/releases/0.18a2/Unit8_binedit.pas@ 1088

Last change on this file since 1088 was 21, checked in by alloc, 18 years ago
File size: 11.2 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 Label1: 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 FormActivate(Sender: TObject);
27 PROCEDURE btn_importClick(Sender: TObject);
28 PROCEDURE btn_exportClick(Sender: TObject);
29 PROCEDURE panel_imexportResize(Sender: TObject);
30 FUNCTION Save:Boolean;
31 PROCEDURE FormClose(Sender: TObject; var Action: TCloseAction);
32 PROCEDURE combo_extensionClick(Sender: TObject);
33 PROCEDURE panel_extensionResize(Sender: TObject);
34 FUNCTION GetValue(datatype:Byte; offset:LongWord):String;
35 PROCEDURE WriteStructureInfos(structinfoid:Integer);
36 PROCEDURE hexSelectionChanged(Sender: TObject);
37 PROCEDURE hexChange(Sender: TObject);
38 PROCEDURE panel_dataResize(Sender: TObject);
39 PROCEDURE structsClick(Sender: TObject);
40 PROCEDURE FormResize(Sender: TObject);
41 PROCEDURE ClearStructViewer;
42 PROCEDURE listClick(Sender: TObject);
43 PROCEDURE FormCloseQuery(Sender: TObject; var CanClose: Boolean);
44 PROCEDURE FormCreate(Sender: TObject);
45 PROCEDURE Recreatelist;
46 PRIVATE
47 PUBLIC
48 END;
49
50VAR
51 Form8: TForm8;
52
53IMPLEMENTATION
54{$R *.dfm}
55USES Unit1_main;
56VAR
57 fileid:LongWord;
58
59FUNCTION IntToBin(value:Byte):String;
60 VAR i:Byte;
61 BEGIN
62 Result:='';
63 FOR i:=7 DOWNTO 0 DO BEGIN
64 Result:=Result+IntToStr((value SHR i) AND $01);
65 END;
66 END;
67
68FUNCTION TForm8.GetValue(datatype:Byte; offset:LongWord):String;
69 VAR
70 data:Tdata;
71 BEGIN
72 CASE datatype OF
73 1: Result:=IntToStr(Self.hex.data[offset]);
74 2: Result:=IntToStr(Self.hex.data[offset]+Self.hex.data[offset+1]*256);
75 3: Result:=IntToStr(Self.hex.data[offset]+Self.hex.data[offset+1]*256+Self.hex.data[offset+2]*256*256);
76 4: Result:=IntToStr(Self.hex.data[offset]+Self.hex.data[offset+1]*256+Self.hex.data[offset+2]*256*256+Self.hex.data[offset+3]*256*256*256);
77 5: Result:='0x'+IntToHex(Self.hex.data[offset],2);
78 6: Result:='0x'+IntToHex(Self.hex.data[offset]+Self.hex.data[offset+1]*256,4);
79 7: Result:='0x'+IntToHex(Self.hex.data[offset]+Self.hex.data[offset+1]*256+Self.hex.data[offset+2]*256*256,6);
80 8: Result:='0x'+IntToHex(Self.hex.data[offset]+Self.hex.data[offset+1]*256+Self.hex.data[offset+2]*256*256+Self.hex.data[offset+3]*256*256*256,8);
81 9: BEGIN
82 SetLength(data,4);
83 data[0]:=Self.hex.data[offset];
84 data[1]:=Self.hex.data[offset+1];
85 data[2]:=Self.hex.data[offset+2];
86 data[3]:=Self.hex.data[offset+3];
87 Result:=FloatToStr(Decode_Float(data));
88 END;
89 10: Result:=IntToBin(Self.hex.data[offset]);
90 END;
91 END;
92
93PROCEDURE TForm8.WriteStructureInfos(structinfoid:Integer);
94 VAR
95 i:Byte;
96 BEGIN
97 IF structinfoid>=0 THEN BEGIN
98 WITH structure_infos[structinfoid] DO BEGIN
99 Self.structs.RowCount:=Length(entries)+1;
100 FOR i:=1 TO Length(entries) DO BEGIN
101 Self.structs.Cells[0,i]:=entries[i-1].name;
102 Self.structs.Cells[1,i]:='0x'+IntToHex(entries[i-1].offset,6);
103 Self.structs.Cells[2,i]:=GetDataType(entries[i-1].datatype);
104 Self.structs.Cells[3,i]:=Self.GetValue(entries[i-1].datatype,entries[i-1].offset);
105 Self.structs.Cells[4,i]:=entries[i-1].description;
106 END;
107 END;
108 END;
109 END;
110
111PROCEDURE TForm8.Recreatelist;
112 VAR
113 i:LongWord;
114 BEGIN
115 combo_extension.Items.Clear;
116 combo_extension.Items.Add('_All files_ ('+IntToStr(dat_header.Files)+')');
117 FOR i:=0 TO dat_header.Extensions-1 DO BEGIN
118 WITH dat_extensionsmap[i] DO BEGIN
119 combo_extension.Items.Add(
120 Extension[3]+Extension[2]+Extension[1]+Extension[0]+' ('+
121 IntToStr(ExtCount)+')');
122 END;
123 END;
124 combo_extension.ItemIndex:=0;
125 combo_extensionClick(Self);
126 END;
127
128PROCEDURE TForm8.FormCreate(Sender: TObject);
129 BEGIN
130 Self.Caption:='';
131 fileid:=0;
132 structs.ColCount:=5;
133 structs.RowCount:=2;
134 structs.FixedRows:=1;
135 structs.Cells[0,0]:='Name';
136 structs.Cells[1,0]:='Offset';
137 structs.Cells[2,0]:='Type';
138 structs.Cells[3,0]:='Value';
139 structs.Cells[4,0]:='Description';
140 structs.ColWidths[0]:=75;
141 structs.ColWidths[1]:=60;
142 structs.ColWidths[2]:=75;
143 structs.ColWidths[3]:=75;
144 Self.panel_dataResize(Self);
145 END;
146
147FUNCTION TForm8.Save:Boolean;
148 VAR
149 mem:TMemoryStream;
150 data:Tdata;
151 BEGIN
152 CASE MessageBox(Self.Handle,PChar('Save changes to file '+dat_files[fileid].FileName+'?'),PChar('Data changed...'),MB_YESNOCANCEL) OF
153 IDYES: BEGIN
154 mem:=TMemoryStream.Create;
155 hex.SaveToStream(mem);
156 mem.Seek(0,soFromBeginning);
157 SetLength(data,mem.Size);
158 mem.Read(data[0],mem.Size);
159 mem.Free;
160 SaveDatFile(fileid,data);
161 Result:=True;
162 END;
163 IDNO: Result:=True;
164 IDCANCEL: BEGIN
165 Result:=False;
166 END;
167 END;
168 END;
169
170PROCEDURE TForm8.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
171 BEGIN
172 IF hex.Modified THEN BEGIN
173 IF NOT Save THEN CanClose:=False;
174 END;
175 END;
176
177PROCEDURE TForm8.listClick(Sender: TObject);
178 VAR
179 mem:TMemoryStream;
180 data:Tdata;
181 i:LongWord;
182 BEGIN
183 IF hex.Modified THEN BEGIN
184 IF NOT Save THEN BEGIN
185 FOR i:=0 TO list.Count-1 DO BEGIN
186 IF StrToInt(MidStr(list.Items.Strings[i],1,5))=fileid THEN BEGIN
187 list.ItemIndex:=i;
188 Exit;
189 END;
190 END;
191 END;
192 END;
193 Self.ClearStructViewer;
194 fileid:=StrToInt(MidStr(list.Items.Strings[list.ItemIndex],1,5));
195 data:=LoadDatFile(fileid);
196 mem:=TMemoryStream.Create;
197 mem.Write(data[0],Length(data));
198 hex.LoadFromStream(mem);
199 mem.Free;
200 WriteStructureInfos(GetStructureInfoId(dat_files[fileid].Extension));
201 END;
202
203PROCEDURE TForm8.ClearStructViewer;
204 VAR
205 x:Word;
206 BEGIN
207 structs.RowCount:=2;
208 FOR x:=0 TO structs.ColCount-1 DO structs.Cells[x,1]:='';
209 END;
210
211PROCEDURE TForm8.FormResize(Sender: TObject);
212 BEGIN
213 IF Self.Width>=650 THEN BEGIN
214 END ELSE Self.Width:=650;
215 IF Self.Height>=450 THEN BEGIN
216 END ELSE Self.Height:=450;
217 END;
218
219PROCEDURE TForm8.structsClick(Sender: TObject);
220 VAR
221 offset:LongWord;
222 length:Byte;
223 BEGIN
224 IF structs.Row>0 THEN BEGIN
225 offset:=structure_infos[GetStructureInfoId(dat_files[fileid].extension)].entries[structs.Row-1].offset;
226 length:=GetTypeDataLength(structure_infos[GetStructureInfoId(dat_files[fileid].extension)].entries[structs.Row-1].datatype);
227 hex.SelStart:=offset;
228 hex.SelEnd:=offset+length-1;
229 END;
230 END;
231
232PROCEDURE TForm8.panel_dataResize(Sender: TObject);
233 BEGIN
234 structs.ColWidths[4]:=structs.Width-structs.ColWidths[0]-structs.ColWidths[1]-structs.ColWidths[2]-structs.ColWidths[3]-28;
235 END;
236
237PROCEDURE TForm8.hexChange(Sender: TObject);
238 BEGIN
239 IF hex.DataSize>0 THEN
240 WriteStructureInfos(GetStructureInfoId(dat_files[fileid].Extension));
241 END;
242
243PROCEDURE TForm8.hexSelectionChanged(Sender: TObject);
244 VAR
245 selstart:Integer;
246 i,j:Word;
247 BEGIN
248 FOR i:=1 TO structs.RowCount-1 DO BEGIN
249 FOR j:=0 TO structs.ColCount-1 DO BEGIN
250 structs.CellColors[j,i]:=clWhite;
251 structs.CellFontColors[j,i]:=clBlack;
252 END;
253 END;
254 IF hex.DataSize>0 THEN BEGIN
255 selstart:=hex.SelStart;
256 IF GetStructureInfoId(dat_files[fileid].Extension)>=0 THEN BEGIN
257 WITH structure_infos[GetStructureInfoId(dat_files[fileid].Extension)] DO BEGIN
258 FOR i:=0 TO High(entries) DO BEGIN
259 IF ((selstart-entries[i].offset)<GetTypeDataLength(entries[i].datatype)) AND ((selstart-entries[i].offset)>=0) THEN BEGIN
260 FOR j:=0 TO structs.ColCount-1 DO BEGIN
261 structs.CellColors[j,i+1]:=clBlue;
262 structs.CellFontColors[j,i+1]:=clWhite;
263 END;
264 structs.Row:=i+1;
265 END;
266 END;
267 END;
268 END;
269 END;
270 END;
271
272PROCEDURE TForm8.panel_extensionResize(Sender: TObject);
273 BEGIN
274 combo_extension.Width:=panel_extension.Width-5;
275 END;
276
277PROCEDURE TForm8.combo_extensionClick(Sender: TObject);
278 VAR
279 Extension:String[4];
280 i:LongWord;
281 BEGIN
282 Extension:=MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex],1,4);
283 list.Items.Clear;
284 IF Extension='_All' THEN BEGIN
285 FOR i:=0 TO dat_header.Files-1 DO
286 IF (dat_files[i].FileType AND $02)=0 THEN
287 list.Items.Add(dat_files[i].FileName);
288 END ELSE BEGIN
289 FOR i:=0 TO dat_header.Files-1 DO
290 IF dat_files[i].Extension=Extension THEN
291 IF (dat_files[i].FileType AND $02)=0 THEN
292 list.Items.Add(dat_files[i].FileName);
293 END;
294 IF list.Count>0 THEN BEGIN
295 list.ItemIndex:=0;
296 listClick(Self);
297 END;
298 END;
299
300PROCEDURE TForm8.FormClose(Sender: TObject; var Action: TCloseAction);
301 BEGIN
302 Action:=caFree;
303 Form1.close_window(Self.Name);
304 END;
305
306PROCEDURE TForm8.panel_imexportResize(Sender: TObject);
307 BEGIN
308 btn_import.Width:=panel_imexport.Width-8;
309 btn_export.Width:=panel_imexport.Width-8;
310 END;
311
312PROCEDURE TForm8.btn_exportClick(Sender: TObject);
313 BEGIN
314 saved.Filter:='Files of matching extension (*.'+dat_files[fileid].Extension+')|*.'+dat_files[fileid].Extension+'|All files|*.*';
315 saved.DefaultExt:=dat_files[fileid].Extension;
316 IF saved.Execute THEN BEGIN
317 ExportDatFile(fileid,saved.FileName);
318 END;
319 END;
320
321PROCEDURE TForm8.btn_importClick(Sender: TObject);
322 VAR
323 data:Tdata;
324 fs:TFileStream;
325 BEGIN
326 opend.Filter:='Files of matching extension (*.'+dat_files[fileid].Extension+')|*.'+dat_files[fileid].Extension+'|All files|*.*';
327 IF opend.Execute THEN BEGIN
328 fs:=TFileStream.Create(opend.FileName,fmOpenRead);
329 IF fs.Size<>dat_files[fileid].size THEN BEGIN
330 ShowMessage('Can''t import '+ExtractFilename(opend.FileName)+
331 ', file has to have same size as file in .dat.'+CrLf+
332 'Size of file in .dat: '+FormatFileSize(dat_files[fileid].size)+CrLf+
333 'Size of chosen file: '+FormatFileSize(fs.Size));
334 END ELSE BEGIN
335 SetLength(data,fs.Size);
336 fs.Read(data[0],fs.Size);
337 fs.Free;
338 fs:=TFileStream.Create(dat_filename,fmOpenReadWrite);
339 fs.Seek(dat_files[fileid].dataddr,soFromBeginning);
340 fs.Write(data[0],Length(data));
341 END;
342 fs.Free;
343 listClick(Self);
344 END;
345 END;
346
347PROCEDURE TForm8.FormActivate(Sender: TObject);
348 BEGIN
349 Form1.SetActiveWindow(Self.Name);
350 END;
351
352END.
Note: See TracBrowser for help on using the repository browser.