source: oup/releases/0.18a/Unit8_binedit.pas@ 522

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