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

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