source: oup/releases/0.29a2/Unit8_binedit.pas@ 717

Last change on this file since 717 was 30, checked in by alloc, 18 years ago
File size: 29.0 KB
Line 
1UNIT Unit8_binedit;
2INTERFACE
3USES
4 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
5 Dialogs, Wrapgrid, StdCtrls, Grids, StrUtils, MPHexEditor, ExtCtrls, Clipbrd,
6 Unit3_data, Unit2_functions, Unit9_data_structures, Unit4_exporters, Menus, Math,
7 VirtualTrees, VTHeaderPopup;
8
9TYPE
10 TForm8 = Class(TForm)
11 Splitter1: TSplitter;
12 panel_data: TPanel;
13 hex: TMPHexEditor;
14 Splitter2: TSplitter;
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 value_viewer: TWrapGrid;
27 Splitter3: TSplitter;
28 value_viewer_context: TPopupMenu;
29 value_viewer_context_copy: TMenuItem;
30 value_viewer_context_copyashex: TMenuItem;
31 value_viewer_context_copyasdec: TMenuItem;
32 value_viewer_context_copyasfloat: TMenuItem;
33 value_viewer_context_copyasbitset: TMenuItem;
34 value_viewer_context_copyasstring: TMenuItem;
35 check_zerobyte: TCheckBox;
36 edit_filtername: TEdit;
37 check_filtername: TCheckBox;
38 VST: TVirtualStringTree;
39 VTHPopup: TVTHeaderPopupMenu;
40 procedure VSTFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode;
41 Column: TColumnIndex);
42 procedure VSTDblClick(Sender: TObject);
43 procedure VTHPopupColumnChange(const Sender: TBaseVirtualTree;
44 const Column: TColumnIndex; Visible: Boolean);
45 procedure VSTHeaderDragged(Sender: TVTHeader; Column: TColumnIndex;
46 OldPosition: Integer);
47 procedure VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
48 Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
49 procedure hexKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
50 PROCEDURE LoadDat(_fileid:LongWord);
51 PROCEDURE LoadFileNames;
52 PROCEDURE check_filternameClick(Sender: TObject);
53 PROCEDURE check_zerobyteClick(Sender: TObject);
54 PROCEDURE combo_extensionClick(Sender: TObject);
55 PROCEDURE panel_extensionResize(Sender: TObject);
56 PROCEDURE listClick(Sender: TObject);
57 PROCEDURE Recreatelist;
58
59 PROCEDURE FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
60 PROCEDURE value_viewerDblClick(Sender: TObject);
61 PROCEDURE value_viewer_context_copyClick(Sender: TObject);
62 PROCEDURE value_viewerMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
63 PROCEDURE value_viewer_contextPopup(Sender: TObject);
64 PROCEDURE FormActivate(Sender: TObject);
65 PROCEDURE btn_importClick(Sender: TObject);
66 PROCEDURE btn_exportClick(Sender: TObject);
67 PROCEDURE panel_imexportResize(Sender: TObject);
68 FUNCTION Save:Boolean;
69 PROCEDURE FormClose(Sender: TObject; var Action: TCloseAction);
70 FUNCTION GetValue(datatype:Word; offset:LongWord):String;
71 PROCEDURE WriteStructureInfos; //(structinfoid:Integer);
72 PROCEDURE hexSelectionChanged(Sender: TObject);
73 PROCEDURE hexChange(Sender: TObject);
74 PROCEDURE FormResize(Sender: TObject);
75 PROCEDURE ClearStructViewer;
76 PROCEDURE FormCloseQuery(Sender: TObject; var CanClose: Boolean);
77 PROCEDURE FormCreate(Sender: TObject);
78 PROCEDURE ClearValues;
79 PROCEDURE WriteValues;
80 PROCEDURE SetNewValue(datatype:Word; offset:LongWord; value:String);
81 PRIVATE
82 PUBLIC
83 END;
84
85VAR
86 Form8: TForm8;
87
88IMPLEMENTATION
89{$R *.dfm}
90USES Unit1_main, Unit12_ValueEdit, Unit13_rawedit;
91VAR
92 fileid:LongWord;
93
94TYPE
95 PNodeData = ^TNodeData;
96 TNodeData = record
97 Caption:String;
98 Offset:LongInt;
99 DataType:Word;
100 Value:String;
101 Description:String;
102 end;
103
104
105function AddVSTEntry(AVST:TCustomVirtualStringTree; ANode:PVirtualNode; ARecord:TNodeData):PVirtualNode;
106 var
107 data:PNodeData;
108 begin
109 Result:=AVST.AddChild(ANode);
110 data:=AVST.GetNodeData(Result);
111 AVST.ValidateNode(Result,False);
112 data^:=ARecord;
113 end;
114
115
116
117PROCEDURE TForm8.LoadDat(_fileid:LongWord);
118 VAR
119 i:LongWord;
120 mem:TMemoryStream;
121 data:Tdata;
122 BEGIN
123 IF hex.Modified THEN BEGIN
124 IF NOT Save THEN BEGIN
125 FOR i:=0 TO list.Count-1 DO BEGIN
126 IF GetFileIDByName(list.Items.Strings[i])=fileid THEN BEGIN
127 list.ItemIndex:=i;
128 Exit;
129 END;
130 END;
131 END;
132 END;
133 fileid:=_fileid;
134 FOR i:=0 TO list.Count-1 DO
135 IF GetFileIDByName(list.Items.Strings[i])=fileid THEN
136 list.ItemIndex:=i;
137 Self.ClearStructViewer;
138 data:=LoadDatFile(fileid);
139 IF Length(data)>0 THEN BEGIN
140 mem:=TMemoryStream.Create;
141 mem.Write(data[0],Length(data));
142 mem.Seek(0,soFromBeginning);
143 hex.LoadFromStream(mem);
144 mem.Free;
145 WriteStructureInfos;
146 END ELSE BEGIN
147 ClearValues;
148 hex.DataSize:=0;
149 END;
150 END;
151
152PROCEDURE TForm8.Recreatelist;
153 VAR
154 i:LongWord;
155 exts:TStringList;
156 BEGIN
157 combo_extension.Items.Clear;
158 combo_extension.Items.Add('_All files_ ('+IntToStr(GetFilesCount)+')');
159 exts:=GetExtensionsList;
160 FOR i:=0 TO High(exts) DO
161 combo_extension.Items.Add(exts[i]);
162 combo_extension.ItemIndex:=0;
163 combo_extensionClick(Self);
164 END;
165
166PROCEDURE TForm8.LoadFileNames;
167 VAR
168 Extension:String[4];
169 no_zero_bytes:Boolean;
170 pattern:String;
171 files:TStringList;
172 i:LongWord;
173 BEGIN
174 Extension:=MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex],1,4);
175 no_zero_bytes:=NOT check_zerobyte.Checked;
176 pattern:='';
177 IF check_filtername.Checked THEN pattern:=edit_filtername.Text;
178 IF Extension='_All' THEN Extension:='';
179
180 files:=GetFilesList(extension,pattern,no_zero_bytes);
181 list.Items.Clear;
182 IF Length(files)>0 THEN
183 FOR i:=0 TO High(files) DO
184 list.Items.Add(files[i]);
185 END;
186
187PROCEDURE TForm8.panel_extensionResize(Sender: TObject);
188 BEGIN
189 combo_extension.Width:=panel_extension.Width-5;
190 edit_filtername.Width:=panel_extension.Width-5;
191 END;
192
193PROCEDURE TForm8.combo_extensionClick(Sender: TObject);
194 BEGIN
195 LoadFileNames;
196 END;
197
198PROCEDURE TForm8.check_zerobyteClick(Sender: TObject);
199 VAR
200 i:Byte;
201 BEGIN
202 LoadFileNames;
203 END;
204
205PROCEDURE TForm8.check_filternameClick(Sender: TObject);
206 BEGIN
207 edit_filtername.Enabled:=NOT check_filtername.Checked;
208 LoadFileNames;
209 END;
210
211PROCEDURE TForm8.listClick(Sender: TObject);
212 VAR
213 mem:TMemoryStream;
214 data:Tdata;
215 i:LongWord;
216 BEGIN
217 LoadDat(GetFileIDByName(list.Items.Strings[list.ItemIndex]));
218 END;
219
220
221
222
223FUNCTION IntToBin(value:Byte):String;
224 VAR i:Byte;
225 BEGIN
226 Result:='';
227 FOR i:=7 DOWNTO 0 DO BEGIN
228 Result:=Result+IntToStr((value SHR i) AND $01);
229 END;
230 END;
231
232FUNCTION TForm8.GetValue(datatype:Word; offset:LongWord):String;
233 VAR
234 data:Tdata;
235 i:Word;
236 BEGIN
237 CASE datatype OF
238 1: Result:=IntToStr(hex.data[offset]);
239 2: Result:=IntToStr(hex.data[offset]+hex.data[offset+1]*256);
240 3: Result:=IntToStr(hex.data[offset]+hex.data[offset+1]*256+hex.data[offset+2]*256*256);
241 4: Result:=IntToStr(hex.data[offset]+hex.data[offset+1]*256+hex.data[offset+2]*256*256+hex.data[offset+3]*256*256*256);
242 5: Result:='0x'+IntToHex(hex.data[offset],2);
243 6: Result:='0x'+IntToHex(hex.data[offset]+hex.data[offset+1]*256,4);
244 7: Result:='0x'+IntToHex(hex.data[offset]+hex.data[offset+1]*256+hex.data[offset+2]*256*256,6);
245 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);
246 9: BEGIN
247 SetLength(data,4);
248 data[0]:=hex.data[offset];
249 data[1]:=hex.data[offset+1];
250 data[2]:=hex.data[offset+2];
251 data[3]:=hex.data[offset+3];
252 Result:=FloatToStr(Decode_Float(data));
253 END;
254 10: Result:=IntToBin(hex.data[offset]);
255 11: Result:='0x'+IntToHex(GetRawInfo(fileid,offset).raw_addr,8);
256 12: Result:=FormatNumber(hex.data[offset+1]+hex.data[offset+2]*256+hex.data[offset+3]*256*256,5,'0');
257 13: Result:=IntToStr(hex.data[offset]);
258 14: Result:=IntToStr(hex.data[offset]+hex.data[offset+1]*256);
259 15: Result:=IntToStr(hex.data[offset]+hex.data[offset+1]*256+hex.data[offset+2]*256*256);
260 16: Result:=IntToStr(hex.data[offset]+hex.data[offset+1]*256+hex.data[offset+2]*256*256+hex.data[offset+3]*256*256*256);
261 1000..9999: BEGIN
262 Result:='';
263 FOR i:=1 TO datatype-1000 DO BEGIN
264 IF hex.Data[offset+i-1]>=32 THEN
265 Result:=Result+Chr(hex.Data[offset+i-1])
266 ELSE
267 Result:=Result+'.';
268 END;
269 END;
270 10000..65535: BEGIN
271 Result:='';
272 FOR i:=1 TO datatype-10000 DO BEGIN
273 IF hex.Data[offset+i-1]>=32 THEN
274 Result:=Result+Chr(hex.Data[offset+i-1])
275 ELSE
276 Result:=Result+'.';
277 END;
278 END;
279 END;
280 END;
281
282PROCEDURE TForm8.WriteStructureInfos;
283 VAR
284 i,j:LongWord;
285 pdata: PNodeData;
286 data: TNodeData;
287 node: PVirtualNode;
288 structs: TStructDef;
289 BEGIN
290 VST.BeginUpdate;
291 IF VST.RootNodeCount=0 THEN BEGIN
292 structs:=LoadStructureDefinition(fileid);
293 IF structs.data THEN BEGIN
294 IF Length(structs.Global)>0 THEN BEGIN
295 FOR i:=0 TO High(structs.Global) DO BEGIN
296 data.Caption:=structs.Global[i].name;
297 data.Offset:=structs.Global[i].offset;
298 data.DataType:=structs.Global[i].datatype;
299 data.Value:=GetValue(structs.Global[i].datatype, structs.Global[i].offset);
300 data.Description:=structs.Global[i].description;
301 AddVSTEntry(VST, nil, data);
302 END;
303 END;
304 IF Length(structs.Subs)>0 THEN BEGIN
305 FOR i:=0 TO High(structs.Subs) DO BEGIN
306 WITH structs.Subs[i] DO BEGIN
307 IF Length(Entries)>0 THEN BEGIN
308 IF Pos('#',SubName)>0 THEN BEGIN
309 data.Offset:=HexToLong(MidStr(SubName, Pos('#',SubName)+1, 8));
310 data.Value:=MidStr(SubName, PosEx('#',SubName,Pos('#',SubName)+1)+1, 8);
311 data.Caption:=MidStr(SubName, 1, Pos('#',SubName)-1);
312 END ELSE BEGIN
313 data.Caption:=SubName;
314 data.Offset:=0;
315 data.Value:='';
316 END;
317 data.DataType:=0;
318 data.Description:='';
319 node:=AddVSTEntry(VST, nil, data);
320 FOR j:=0 TO High(Entries) DO BEGIN
321 data.Caption:=Entries[j].name;
322 data.Offset:=Entries[j].offset;
323 data.DataType:=Entries[j].datatype;
324 data.Value:=GetValue(Entries[j].datatype, Entries[j].offset);
325 data.Description:=Entries[j].description;
326 AddVSTEntry(VST, node, data);
327 END;
328 END;
329 END;
330 END;
331 END;
332 END;
333 IF VST.RootNodeCount>0 THEN
334 VST.FocusedNode:=VST.GetFirst;
335 END ELSE BEGIN
336 Node:=VST.GetFirst;
337 WHILE Assigned(Node) DO BEGIN
338 pdata:=VST.GetNodeData(Node);
339 IF pdata.DataType>0 THEN
340 pdata.Value:=GetValue(pdata.Datatype, pdata.Offset);
341 Node:=VST.GetNext(Node);
342 END;
343 END;
344 VST.EndUpdate;
345 END;
346
347PROCEDURE TForm8.ClearValues;
348 VAR
349 i:Byte;
350 BEGIN
351 FOR i:=1 TO value_viewer.RowCount-1 DO BEGIN
352 value_viewer.Cells[1,i]:='';
353 END;
354 END;
355
356PROCEDURE TForm8.WriteValues;
357 VAR
358 i,j:Byte;
359 data:Tdata;
360 str:String;
361 value:LongWord;
362 BEGIN
363 FOR i:=1 TO value_viewer.RowCount-1 DO BEGIN
364 IF value_viewer.Cells[0,i]='1 byte, unsigned' THEN BEGIN
365 IF ((hex.SelCount=1) OR (hex.SelCount=0)) AND NOT ((hex.SelStart+1)>hex.DataSize) THEN BEGIN
366 value:=hex.Data[hex.SelStart];
367 value_viewer.Cells[1,i]:=IntToStr( value )+' / 0x'+IntToHex( value , 2 );
368 END ELSE
369 value_viewer.Cells[1,i]:='';
370 END;
371 IF value_viewer.Cells[0,i]='2 bytes, unsigned' THEN BEGIN
372 IF ((hex.SelCount=2) OR (hex.SelCount=0)) AND NOT ((hex.SelStart+2)>hex.DataSize) THEN BEGIN
373 value:=hex.Data[hex.SelStart] + hex.Data[hex.SelStart+1]*256;
374 value_viewer.Cells[1,i]:=IntToStr( value )+' / 0x'+IntToHex( value , 4 );
375 END ELSE
376 value_viewer.Cells[1,i]:='';
377 END;
378 IF value_viewer.Cells[0,i]='4 bytes, unsigned' THEN BEGIN
379 IF ((hex.SelCount=4) OR (hex.SelCount=0)) AND NOT ((hex.SelStart+4)>hex.DataSize) THEN BEGIN
380 value:=hex.Data[hex.SelStart]+hex.Data[hex.SelStart+1]*256+hex.Data[hex.SelStart+2]*256*256+hex.Data[hex.SelStart+3]*256*256*256;
381 value_viewer.Cells[1,i]:=IntToStr( value )+' / 0x'+IntToHex( value , 8 );
382 END ELSE
383 value_viewer.Cells[1,i]:='';
384 END;
385 IF value_viewer.Cells[0,i]='Bitset' THEN BEGIN
386 IF (hex.SelCount<=8) THEN BEGIN
387 IF hex.SelCount=0 THEN BEGIN
388 SetLength(data,1);
389 data[0]:=hex.Data[hex.SelStart];
390 END ELSE BEGIN
391 SetLength(data,hex.SelCount);
392 FOR j:=0 TO hex.SelCount-1 DO
393 data[j]:=hex.Data[hex.SelStart+j];
394 END;
395 value_viewer.Cells[1,i]:=DataToBin(data);
396 END ELSE
397 value_viewer.Cells[1,i]:='';
398 END;
399 IF value_viewer.Cells[0,i]='Float' THEN BEGIN
400 IF ((hex.SelCount=4) OR (hex.SelCount=0)) AND NOT ((hex.SelStart+4)>hex.DataSize) THEN BEGIN
401 SetLength(data,4);
402 FOR j:=0 TO 3 DO
403 data[j]:=hex.Data[hex.SelStart+j];
404 value_viewer.Cells[1,i]:=FloatToStr(Decode_Float(data));
405 END ELSE
406 value_viewer.Cells[1,i]:='';
407 END;
408 IF value_viewer.Cells[0,i]='Selected length' THEN BEGIN
409 value_viewer.Cells[1,i]:=IntToStr(hex.SelCount)+' bytes';
410 END;
411 IF value_viewer.Cells[0,i]='String' THEN BEGIN
412 j:=0;
413 str:='';
414 IF hex.SelCount=0 THEN BEGIN
415{ WHILE (hex.Data[hex.SelStart+j]>0) AND ((hex.SelStart+j)<hex.DataSize) DO BEGIN
416 IF hex.Data[hex.selstart+j]>=32 THEN
417 str:=str+Char(hex.Data[hex.SelStart+j])
418 ELSE
419 str:=str+'.';
420 Inc(j);
421 END;
422} END ELSE BEGIN
423 FOR j:=1 TO hex.SelCount DO
424 str:=str+Char(hex.Data[hex.SelStart+j-1]);
425 END;
426 value_viewer.Cells[1,i]:=str;
427 END;
428 END;
429 END;
430
431PROCEDURE TForm8.FormCreate(Sender: TObject);
432 BEGIN
433 Self.Caption:='';
434 fileid:=0;
435 VST.NodeDataSize:=SizeOf(TNodeData);
436 value_viewer.ColCount:=2;
437 value_viewer.RowCount:=8;
438 value_viewer.FixedRows:=1;
439 value_viewer.Cells[0,0]:='Type';
440 value_viewer.Cells[1,0]:='Value';
441 value_viewer.Cells[0,1]:='1 byte, unsigned';
442 value_viewer.Cells[0,2]:='2 bytes, unsigned';
443 value_viewer.Cells[0,3]:='4 bytes, unsigned';
444 value_viewer.Cells[0,4]:='Bitset';
445 value_viewer.Cells[0,5]:='Float';
446 value_viewer.Cells[0,6]:='String';
447 value_viewer.Cells[0,7]:='Selected length';
448 value_viewer.ColWidths[0]:=100;
449 hex.Height:=panel_data.Height-215;
450 END;
451
452FUNCTION TForm8.Save:Boolean;
453 VAR
454 mem:TMemoryStream;
455 data:Tdata;
456 i:LongWord;
457 BEGIN
458 CASE MessageBox(Self.Handle,PChar('Save changes to file '+GetFileInfo(fileid).FileName+'?'),PChar('Data changed...'),MB_YESNOCANCEL) OF
459 IDYES: BEGIN
460 mem:=TMemoryStream.Create;
461 hex.SaveToStream(mem);
462 mem.Seek(0,soFromBeginning);
463 SetLength(data,mem.Size);
464 mem.Read(data[0],mem.Size);
465 mem.Free;
466 UpdateDatFile(fileid,data);
467 hex.Modified:=False;
468 FOR i:=0 TO hex.Datasize-1 DO hex.ByteChanged[i]:=False;
469 Result:=True;
470 END;
471 IDNO: Result:=True;
472 IDCANCEL: BEGIN
473 Result:=False;
474 END;
475 END;
476 END;
477
478PROCEDURE TForm8.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
479 BEGIN
480 IF hex.Modified THEN BEGIN
481 IF NOT Save THEN CanClose:=False;
482 END;
483 END;
484
485PROCEDURE TForm8.ClearStructViewer;
486 BEGIN
487 VST.Clear;
488 END;
489
490PROCEDURE TForm8.FormResize(Sender: TObject);
491 BEGIN
492 IF Self.Width>=650 THEN BEGIN
493 END ELSE Self.Width:=650;
494 IF Self.Height>=450 THEN BEGIN
495 END ELSE Self.Height:=450;
496 END;
497
498PROCEDURE TForm8.hexChange(Sender: TObject);
499 BEGIN
500 ClearValues;
501 IF hex.DataSize>0 THEN BEGIN
502 WriteStructureInfos;
503 WriteValues;
504 END;
505 END;
506
507PROCEDURE TForm8.hexKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
508 VAR
509 temps: String;
510 BEGIN
511 IF (Shift=[ssCtrl]) AND (Key=Ord('C')) THEN BEGIN
512 IF hex.SelCount>0 THEN BEGIN
513 IF hex.InCharField THEN
514 Clipboard.AsText:=hex.SelectionAsText
515 ELSE
516 Clipboard.AsText:=hex.SelectionAsHex;
517 END;
518 END;
519 IF (Shift=[ssCtrl]) AND (Key=Ord('V')) THEN BEGIN
520{ temps:=Clipboard.AsText;
521 IF hex.SelStart+Length(temps)>hex.DataSize THEN
522 SetLength(temps, hex.DataSize-hex.SelStart);
523 hex.Sel
524 hex.SelCount:=Length(temps);
525 hex.ReplaceSelection(temps,Length(temps));
526} END;
527 END;
528
529PROCEDURE TForm8.hexSelectionChanged(Sender: TObject);
530 VAR
531 selstart:Integer;
532 i,j:Word;
533
534 node:PVirtualNode;
535 pdata:PNodeData;
536 BEGIN
537 IF hex.DataSize>0 THEN BEGIN
538 selstart:=hex.SelStart;
539 IF VST.RootNodeCount>0 THEN BEGIN
540 Node:=VST.GetFirst;
541 WHILE Assigned(Node) DO BEGIN
542 pdata:=VST.GetNodeData(Node);
543 IF pdata.DataType>0 THEN BEGIN
544 IF ((selstart-pdata.Offset)<GetTypeDataLength(pdata.DataType)) AND ((selstart-pdata.Offset)>=0) THEN BEGIN
545 VST.FocusedNode:=Node;
546 VST.Selected[Node]:=True;
547 Break;
548 END;
549 END;
550 Node:=VST.GetNext(Node);
551 END;
552 END;
553 END;
554 END;
555
556PROCEDURE TForm8.FormClose(Sender: TObject; var Action: TCloseAction);
557 BEGIN
558 Action:=caFree;
559 Form1.close_window(Self.Name);
560 END;
561
562PROCEDURE TForm8.panel_imexportResize(Sender: TObject);
563 BEGIN
564 btn_import.Width:=panel_imexport.Width-8;
565 btn_export.Width:=panel_imexport.Width-8;
566 END;
567
568PROCEDURE TForm8.btn_exportClick(Sender: TObject);
569 BEGIN
570 saved.Filter:='Files of matching extension (*.'+GetFileInfo(fileid).Extension+')|*.'+dat_files[fileid].Extension+'|All files|*.*';
571 saved.DefaultExt:=GetFileInfo(fileid).Extension;
572 IF saved.Execute THEN BEGIN
573 ExportDatFile(fileid,saved.FileName);
574 END;
575 END;
576
577PROCEDURE TForm8.btn_importClick(Sender: TObject);
578 VAR
579 data:Tdata;
580 fs:TFileStream;
581 BEGIN
582 opend.Filter:='Files of matching extension (*.'+GetFileInfo(fileid).Extension+')|*.'+dat_files[fileid].Extension+'|All files|*.*';
583 IF opend.Execute THEN BEGIN
584 fs:=TFileStream.Create(opend.FileName,fmOpenRead);
585 IF fs.Size<>hex.DataSize THEN BEGIN
586 ShowMessage('Can''t import '+ExtractFilename(opend.FileName)+
587 ', file has to have same size as file in .dat.'+CrLf+
588 'Size of file in .dat: '+FormatFileSize(hex.datasize)+CrLf+
589 'Size of chosen file: '+FormatFileSize(fs.Size));
590 END ELSE BEGIN
591 hex.LoadFromStream(fs);
592 hex.Modified:=True;
593 END;
594 fs.Free;
595 END;
596 END;
597
598PROCEDURE TForm8.FormActivate(Sender: TObject);
599 BEGIN
600 Form1.SetActiveWindow(Self.Name);
601 END;
602
603PROCEDURE TForm8.value_viewer_contextPopup(Sender: TObject);
604 VAR
605 i:Byte;
606 BEGIN
607 FOR i:=0 TO value_viewer_context.Items.Count-1 DO
608 value_viewer_context.Items.Items[i].Visible:=False;
609 WITH value_viewer DO BEGIN
610 IF (Col=1) AND (Row>0) AND (Length(Cells[Col,Row])>0) THEN BEGIN
611 IF Pos(' byte',Cells[0,Row])=2 THEN BEGIN
612 value_viewer_context.Items.Find('Copy to &clipboard').Visible:=True;
613 value_viewer_context.Items.Find('Copy to clipboard (as &dec)').Visible:=True;
614 value_viewer_context.Items.Find('Copy to clipboard (as &hex)').Visible:=True;
615 END;
616 IF Pos('Float',Cells[0,Row])=1 THEN
617 value_viewer_context.Items.Find('Copy to clipboard (as &float)').Visible:=True;
618 IF Pos('Bitset',Cells[0,Row])=1 THEN
619 value_viewer_context.Items.Find('Copy to clipboard (as &bitset)').Visible:=True;
620 IF Pos('String',Cells[0,Row])=1 THEN
621 value_viewer_context.Items.Find('Copy to clipboard (as &string)').Visible:=True;
622 IF Pos('Selected length',Cells[0,Row])=1 THEN
623 value_viewer_context.Items.Find('Copy to &clipboard').Visible:=True;
624 END;
625 END;
626 END;
627
628PROCEDURE TForm8.value_viewerMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
629 VAR
630 ACol,ARow:Integer;
631 BEGIN
632 IF Button=mbRight THEN BEGIN
633 value_viewer.MouseToCell(x,y,ACol,ARow);
634 IF ARow>0 THEN BEGIN
635 value_viewer.Col:=ACol;
636 value_viewer.Row:=ARow;
637 END;
638 END;
639 END;
640
641PROCEDURE TForm8.value_viewer_context_copyClick(Sender: TObject);
642 VAR
643 i:Byte;
644 name:String;
645 value:LongWord;
646 BEGIN
647 name:=TMenuItem(Sender).Name;
648 IF Pos('asstring',name)>0 THEN BEGIN
649 Clipboard.AsText:=value_viewer.Cells[value_viewer.Col,value_viewer.Row];
650 END ELSE
651 IF Pos('asfloat',name)>0 THEN BEGIN
652 Clipboard.AsText:=value_viewer.Cells[value_viewer.Col,value_viewer.Row];
653 END ELSE
654 IF Pos('asbitset',name)>0 THEN BEGIN
655 Clipboard.AsText:=value_viewer.Cells[value_viewer.Col,value_viewer.Row];
656 END ELSE
657 IF (Pos('ashex',name)>0) OR (Pos('asdec',name)>0) THEN BEGIN
658 IF value_viewer.Cells[0,value_viewer.Row]='1 byte, unsigned' THEN BEGIN
659 IF ((hex.SelCount=1) OR (hex.SelCount=0)) AND NOT ((hex.SelStart+1)>hex.DataSize) THEN
660 value:=hex.Data[hex.SelStart];
661 END;
662 IF value_viewer.Cells[0,value_viewer.Row]='2 bytes, unsigned' THEN BEGIN
663 IF ((hex.SelCount=2) OR (hex.SelCount=0)) AND NOT ((hex.SelStart+2)>hex.DataSize) THEN
664 value:=hex.Data[hex.SelStart] + hex.Data[hex.SelStart+1]*256;
665 END;
666 IF value_viewer.Cells[0,value_viewer.Row]='4 bytes, unsigned' THEN BEGIN
667 IF ((hex.SelCount=4) OR (hex.SelCount=0)) AND NOT ((hex.SelStart+4)>hex.DataSize) THEN
668 value:=hex.Data[hex.SelStart]+hex.Data[hex.SelStart+1]*256+hex.Data[hex.SelStart+2]*256*256+hex.Data[hex.SelStart+3]*256*256*256;
669 END;
670 IF Pos('asdec',name)>0 THEN BEGIN
671 Clipboard.AsText:=IntToStr(value);
672 END ELSE BEGIN
673 IF value_viewer.Cells[0,value_viewer.Row]='1 byte, unsigned' THEN
674 Clipboard.AsText:='0x'+IntToHex(value,2);
675 IF value_viewer.Cells[0,value_viewer.Row]='2 bytes, unsigned' THEN
676 Clipboard.AsText:='0x'+IntToHex(value,4);
677 IF value_viewer.Cells[0,value_viewer.Row]='4 bytes, unsigned' THEN
678 Clipboard.AsText:='0x'+IntToHex(value,8);
679 END;
680 END ELSE BEGIN
681 Clipboard.AsText:=value_viewer.Cells[value_viewer.Col,value_viewer.Row];
682 END;
683 END;
684
685procedure TForm8.VSTDblClick(Sender: TObject);
686 var
687 node:PVirtualNode;
688 nodedata:PNodeData;
689 begin
690 if VST.FocusedColumn=3 then begin
691 node:=VST.FocusedNode;
692 nodedata:=VST.GetNodeData(node);
693
694 IF NOT (nodedata.datatype IN [11,12]) THEN BEGIN
695 Form12.MakeVarInput(nodedata.Caption,nodedata.offset,nodedata.datatype,nodedata.value,Self);
696 END ELSE BEGIN
697 IF nodedata.DataType=11 THEN BEGIN
698 IF GetRawInfo(fileid,nodedata.offset).raw_size>0 THEN BEGIN
699 IF Form1.open_child('rawedit') THEN BEGIN
700 TForm13(Form1.ActiveMDIChild).LoadRaw(GetRawInfo(fileid,nodedata.offset));
701 END;
702 END;
703 END;
704 IF nodedata.DataType=12 THEN BEGIN
705 IF (StrToInt(nodedata.Value)<GetFilesCount) AND
706 (StrToInt(nodedata.Value)>0) AND
707 (StrToInt(nodedata.Value)<>fileid) THEN BEGIN
708 IF GetFileInfo(StrToInt(nodedata.Value)).Size>0 THEN BEGIN
709 IF Form1.open_child('binedit') THEN BEGIN
710 TForm8(Form1.ActiveMDIChild).LoadDat(StrToInt(nodedata.Value));
711 END;
712 END ELSE BEGIN
713 ShowMessage('Linked filed is a zero-byte-file');
714 END;
715 END;
716 END;
717 END;
718
719 end;
720 end;
721
722procedure TForm8.VSTFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode;
723 Column: TColumnIndex);
724 var
725 data:PNodeData;
726 begin
727 data:=VST.GetNodeData(node);
728 IF data.DataType>0 THEN BEGIN
729 hex.SelStart:=data.Offset;
730 hex.SelEnd:=data.Offset+GetTypeDataLength(data.DataType)-1;
731 END ELSE BEGIN
732 hex.SelStart:=data.Offset;
733 hex.SelEnd:=data.Offset+HexToLong(data.Value)-1;
734 END;
735end;
736
737procedure TForm8.VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
738 Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
739 var
740 data:PNodeData;
741 begin
742 data := Sender.GetNodeData(Node);
743 CellText := '';
744 if TextType = ttNormal then begin
745 case Column of
746 0: CellText := data.Caption;
747 1:
748 if data.DataType>0 then
749 CellText := '0x'+IntToHex(data.Offset,8);
750 2:
751 if data.DataType>0 then
752 CellText := GetDataType(data.DataType);
753 3:
754 CellText := GetValue(data.DataType, data.Offset);
755 4:
756 CellText := data.Description;
757 end;
758 end;
759 end;
760
761procedure TForm8.VSTHeaderDragged(Sender: TVTHeader; Column: TColumnIndex;
762 OldPosition: Integer);
763 begin
764 if Sender.Columns.Items[column].Position<1 then
765 Sender.Columns.Items[column].Position:=OldPosition;
766 end;
767
768procedure TForm8.VTHPopupColumnChange(const Sender: TBaseVirtualTree;
769 const Column: TColumnIndex; Visible: Boolean);
770 begin
771 if column=0 then
772 TVirtualStringTree(Sender).Header.Columns.Items[column].Options:=TVirtualStringTree(Sender).Header.Columns.Items[column].Options+[coVisible];
773 end;
774
775PROCEDURE TForm8.SetNewValue(datatype:Word; offset:LongWord; value:String);
776 VAR
777 data:Tdata;
778 value_int:LongWord;
779 value_float:Single;
780 i:Word;
781 BEGIN
782 CASE datatype OF
783 1..4: BEGIN
784 value_int:=StrToInt(value);
785 SetLength(data,datatype);
786 FOR i:=0 TO datatype-1 DO BEGIN
787 data[i]:=value_int MOD 256;
788 value_int:=value_int DIV 256;
789 END;
790 END;
791 5..8: BEGIN
792 value_int:=StrToInt('$'+value);
793 SetLength(data,datatype-4);
794 FOR i:=0 TO datatype-5 DO BEGIN
795 data[i]:=value_int MOD 256;
796 value_int:=value_int DIV 256;
797 END;
798 END;
799 9: BEGIN
800 value_float:=StrToFloat(value);
801 data:=Encode_Float(value_float);
802 END;
803 10: BEGIN
804 value_int:=BinToInt(value);
805 SetLength(data,1);
806 data[0]:=value_int;
807 END;
808 10000..65535: BEGIN
809 SetLength(data,datatype-10000);
810 FOR i:=1 TO datatype-10000 DO BEGIN
811 IF i<=Length(value) THEN
812 data[i-1]:=Ord(value[i])
813 ELSE
814 data[i-1]:=0;
815 END;
816 END;
817 END;
818 FOR i:=0 TO High(data) DO BEGIN
819 IF hex.Data[offset+i]<>data[i] THEN hex.ByteChanged[offset+i]:=True;
820 hex.Data[offset+i]:=data[i];
821 END;
822 hex.Modified:=True;
823 hexChange(Self);
824 hex.Repaint;
825 END;
826
827PROCEDURE TForm8.value_viewerDblClick(Sender: TObject);
828 VAR
829 offset:LongWord;
830 datatype:Word;
831 objectname:String;
832 value:String;
833 BEGIN
834 IF (value_viewer.Col=1) AND (Length(value_viewer.Cells[1,value_viewer.Row])>0) THEN BEGIN
835 offset:=hex.SelStart;
836 IF value_viewer.Cells[0,value_viewer.Row]='1 byte, unsigned' THEN
837 datatype:=1;
838 IF value_viewer.Cells[0,value_viewer.Row]='2 bytes, unsigned' THEN
839 datatype:=2;
840 IF value_viewer.Cells[0,value_viewer.Row]='4 bytes, unsigned' THEN
841 datatype:=4;
842 IF value_viewer.Cells[0,value_viewer.Row]='Bitset' THEN
843 datatype:=10;
844 IF value_viewer.Cells[0,value_viewer.Row]='Float' THEN
845 datatype:=9;
846 IF value_viewer.Cells[0,value_viewer.Row]='Selected length' THEN
847 Exit;
848 IF value_viewer.Cells[0,value_viewer.Row]='String' THEN BEGIN
849 IF hex.SelCount>0 THEN
850 datatype:=10000+hex.SelCount
851 ELSE
852 datatype:=10000+Length(value_viewer.Cells[1,value_viewer.Row]);
853 END;
854 objectname:='';
855 value:=GetValue(datatype,offset);
856 Form12.MakeVarInput(objectname,offset,datatype,value,Self);
857 END;
858 END;
859
860PROCEDURE TForm8.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
861 BEGIN
862 IF (Shift=[ssCtrl]) AND (Key=83) THEN
863 IF hex.Modified THEN
864 IF NOT Save THEN
865 Exit;
866 END;
867
868
869END.
Note: See TracBrowser for help on using the repository browser.