source: oup/releases/0.31a/Unit8_binedit.pas@ 1011

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