Ignore:
Timestamp:
Dec 23, 2006, 1:03:50 AM (18 years ago)
Author:
alloc
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • oup/current/Tool_BinEdit.pas

    r43 r45  
    11unit Tool_BinEdit;
    2 
    32interface
    4 
    53uses
    64  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    7   Dialogs, Wrapgrid, StdCtrls, Grids, StrUtils, MPHexEditor, ExtCtrls, Clipbrd,
    8   Data, Code_Functions, Code_DataStructures, Code_Exporters, Code_OniDataClass,
    9   Menus, Math, VirtualTrees, VTHeaderPopup;
     5  Dialogs, Tool_Template, StdCtrls, ExtCtrls, VirtualTrees, Grids, Wrapgrid,
     6  MPHexEditor, VTHeaderPopup, Menus, StrUtils, Clipbrd,
     7  Data, Code_OniDataClass, Code_Functions, Code_DataStructures, Code_Exporters;
    108
    119type
    12   TForm_BinEdit = class(TForm)
    13     Splitter1: TSplitter;
    14     panel_data: TPanel;
    15     hex:      TMPHexEditor;
     10  TForm_BinEdit = class(TForm_ToolTemplate)
     11    hex: TMPHexEditor;
    1612    Splitter2: TSplitter;
    17     panel_files: TPanel;
    18     list:     TListBox;
    19     panel_extension: TPanel;
    20     lbl_filter: TLabel;
    21     combo_extension: TComboBox;
    22     Bevel1:   TBevel;
    23     panel_imexport: TPanel;
    24     btn_export: TButton;
    25     btn_import: TButton;
    26     opend:    TOpenDialog;
    27     saved:    TSaveDialog;
    2813    value_viewer: TWrapGrid;
     14    VST: TVirtualStringTree;
    2915    Splitter3: TSplitter;
    3016    value_viewer_context: TPopupMenu;
    3117    value_viewer_context_copy: TMenuItem;
    32     value_viewer_context_copyashex: TMenuItem;
    3318    value_viewer_context_copyasdec: TMenuItem;
    3419    value_viewer_context_copyasfloat: TMenuItem;
    3520    value_viewer_context_copyasbitset: TMenuItem;
    3621    value_viewer_context_copyasstring: TMenuItem;
    37     check_zerobyte: TCheckBox;
    38     edit_filtername: TEdit;
    39     check_filtername: TCheckBox;
    40     VST:      TVirtualStringTree;
     22    value_viewer_context_copyashex: TMenuItem;
    4123    VTHPopup: TVTHeaderPopupMenu;
     24    procedure FormCreate(Sender: TObject);
     25    procedure NewFile(fileinfo: TFileInfo);
     26
     27    procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
     28    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
     29
     30    procedure hexKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
     31    procedure hexSelectionChanged(Sender: TObject);
     32    procedure hexChange(Sender: TObject);
     33
     34    procedure LoadDat(_fileid: LongWord);
     35    function Save: Boolean;
     36    function GetValue(datatype: Word; offset: LongWord): String;
     37    procedure SetNewValue(datatype: Word; offset: LongWord; Value: String);
     38
     39    procedure WriteStructureInfos;
     40    procedure ClearStructViewer;
     41    procedure VSTDblClick(Sender: TObject);
    4242    procedure VSTFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode;
    4343      Column: TColumnIndex);
    44     procedure VSTDblClick(Sender: TObject);
    4544    procedure VTHPopupColumnChange(const Sender: TBaseVirtualTree;
    4645      const Column: TColumnIndex; Visible: Boolean);
     
    4948    procedure VSTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
    5049      Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
    51     procedure hexKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    52     procedure LoadDat(_fileid: LongWord);
    53     procedure LoadFileNames;
    54     procedure check_filternameClick(Sender: TObject);
    55     procedure check_zerobyteClick(Sender: TObject);
    56     procedure combo_extensionClick(Sender: TObject);
    57     procedure panel_extensionResize(Sender: TObject);
    58     procedure listClick(Sender: TObject);
    59     procedure Recreatelist;
    60 
    61     procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
     50
     51    procedure ClearValues;
     52    procedure WriteValues;
    6253    procedure value_viewerDblClick(Sender: TObject);
    6354    procedure value_viewer_context_copyClick(Sender: TObject);
     
    6556      Shift: TShiftState; X, Y: Integer);
    6657    procedure value_viewer_contextPopup(Sender: TObject);
    67     procedure btn_importClick(Sender: TObject);
    68     procedure btn_exportClick(Sender: TObject);
    69     procedure panel_imexportResize(Sender: TObject);
    70     function Save: Boolean;
    71     procedure FormClose(Sender: TObject; var Action: TCloseAction);
    72     function GetValue(datatype: Word; offset: LongWord): String;
    73     procedure WriteStructureInfos; //(structinfoid:Integer);
    74     procedure hexSelectionChanged(Sender: TObject);
    75     procedure hexChange(Sender: TObject);
    76     procedure FormResize(Sender: TObject);
    77     procedure ClearStructViewer;
    78     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    79     procedure FormCreate(Sender: TObject);
    80     procedure ClearValues;
    81     procedure WriteValues;
    82     procedure SetNewValue(datatype: Word; offset: LongWord; Value: String);
    83     procedure listMouseDown(Sender: TObject; Button: TMouseButton;
    84       Shift: TShiftState; X, Y: Integer);
    8558  private
    8659    fileid: LongWord;
     
    9366implementation
    9467
     68uses Helper_ValueEdit, Main, Tool_RawEdit;
    9569{$R *.dfm}
    96 
    97 uses Main, Helper_ValueEdit, Tool_Rawedit;
    9870
    9971type
     
    11082
    11183
     84procedure TForm_BinEdit.FormCreate(Sender: TObject);
     85begin
     86  inherited;
     87  Self.OnNewFileSelected := NewFile;
     88
     89  Self.Caption := '';
     90  fileid     := 0;
     91  VST.NodeDataSize := SizeOf(TNodeData);
     92  value_viewer.ColCount := 2;
     93  value_viewer.RowCount := 8;
     94  value_viewer.FixedRows := 1;
     95  value_viewer.Cells[0, 0] := 'Type';
     96  value_viewer.Cells[1, 0] := 'Value';
     97  value_viewer.Cells[0, 1] := '1 byte, unsigned';
     98  value_viewer.Cells[0, 2] := '2 bytes, unsigned';
     99  value_viewer.Cells[0, 3] := '4 bytes, unsigned';
     100  value_viewer.Cells[0, 4] := 'Bitset';
     101  value_viewer.Cells[0, 5] := 'Float';
     102  value_viewer.Cells[0, 6] := 'String';
     103  value_viewer.Cells[0, 7] := 'Selected length';
     104//  value_viewer.ColWidths[0] := 100;
     105//  value_viewer.ColWidths[1] := value_viewer.Width - 150;
     106//  hex.Height := content.Height - 215;
     107  //
     108  value_viewer.Font.Charset := AppSettings.CharSet;
     109  VST.Font.Charset := AppSettings.CharSet;
     110  hex.Translation := tkAsIs;
     111  hex.Font.Charset := AppSettings.CharSet;
     112  //
     113end;
     114
     115procedure TForm_BinEdit.NewFile(fileinfo: TFileInfo);
     116begin
     117  LoadDat(fileinfo.ID);
     118end;
     119
     120
     121
    112122
    113123function AddVSTEntry(AVST: TCustomVirtualStringTree; ANode: PVirtualNode;
     
    127137procedure TForm_BinEdit.LoadDat(_fileid: LongWord);
    128138var
    129   i:    LongWord;
    130139  mem:  TMemoryStream;
    131140  Data: Tdata;
     
    135144    if not Save then
    136145    begin
    137       for i := 0 to list.Count - 1 do
    138       begin
    139         if OniDataConnection.ExtractFileID(list.Items.Strings[i]) = fileid then
    140         begin
    141           list.ItemIndex := i;
    142           Exit;
    143         end;
    144       end;
     146      Self.SelectFileID(fileid);
     147      Exit;
    145148    end;
    146149  end;
    147150  fileid := _fileid;
    148   for i := 0 to list.Count - 1 do
    149     if OniDataConnection.ExtractFileID(list.Items.Strings[i]) = fileid then
    150     begin
    151       list.ItemIndex := i;
    152       Break;
    153     end;
     151  if OniDataConnection.ExtractFileID(
     152        filelist.Items.Strings[filelist.ItemIndex]) <> fileid then
     153    Self.SelectFileID(fileid);
    154154  Self.ClearStructViewer;
    155155  Data := OniDataConnection.LoadDatFile(fileid);
     
    172172
    173173
    174 
    175 procedure TForm_BinEdit.Recreatelist;
    176 var
    177   i:    LongWord;
    178   exts: TStringArray;
    179 begin
    180   combo_extension.Items.Clear;
    181   combo_extension.Items.Add('_All files_ (' + IntToStr(
    182     OniDataConnection.GetFilesCount) + ')');
    183   exts := OniDataConnection.GetExtensionsList;
    184   for i := 0 to High(exts) do
    185     combo_extension.Items.Add(exts[i]);
    186   combo_extension.ItemIndex := 0;
    187   combo_extensionClick(Self);
    188 end;
    189 
    190 
    191 
    192 
    193 procedure TForm_BinEdit.LoadFileNames;
    194 var
    195   Extension: String[4];
    196   no_zero_bytes: Boolean;
    197   pattern: String;
    198   files: TStringArray;
    199   i: LongWord;
    200 begin
    201   Extension := MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex], 1, 4);
    202   no_zero_bytes := not check_zerobyte.Checked;
    203   pattern := '';
    204   if check_filtername.Checked then
    205     pattern := edit_filtername.Text;
    206   if Extension = '_All' then
    207     Extension := '';
    208 
    209   files := OniDataConnection.GetFilesList(extension, pattern, no_zero_bytes);
    210   list.Items.Clear;
    211   if Length(files) > 0 then
    212     for i := 0 to High(files) do
    213       list.Items.Add(files[i]);
    214 end;
    215 
    216 
    217 
    218 
    219 procedure TForm_BinEdit.panel_extensionResize(Sender: TObject);
    220 begin
    221   combo_extension.Width := panel_extension.Width - 5;
    222   edit_filtername.Width := panel_extension.Width - 5;
    223 end;
    224 
    225 
    226 
    227 
    228 procedure TForm_BinEdit.combo_extensionClick(Sender: TObject);
    229 begin
    230   LoadFileNames;
    231 end;
    232 
    233 
    234 
    235 
    236 procedure TForm_BinEdit.check_zerobyteClick(Sender: TObject);
    237 begin
    238   LoadFileNames;
    239 end;
    240 
    241 
    242 
    243 
    244 procedure TForm_BinEdit.check_filternameClick(Sender: TObject);
    245 begin
    246   edit_filtername.Enabled := not check_filtername.Checked;
    247   LoadFileNames;
    248 end;
    249 
    250 
    251 
    252 
    253 procedure TForm_BinEdit.listClick(Sender: TObject);
    254 begin
    255   LoadDat(OniDataConnection.ExtractFileID(list.Items.Strings[list.ItemIndex]));
    256 end;
    257 
    258 
    259 
    260 
    261 procedure TForm_BinEdit.listMouseDown(Sender: TObject; Button: TMouseButton;
    262   Shift: TShiftState; X, Y: Integer);
    263 var
    264   pt: TPoint;
    265 begin
    266   pt.X := x;
    267   pt.Y := y;
    268   list.ItemIndex := list.ItemAtPos(pt, true);
    269   if list.ItemIndex > -1 then
    270     Self.listClick(Self);
    271 end;
    272174
    273175function IntToBin(Value: Byte): String;
     
    588490
    589491
    590 procedure TForm_BinEdit.FormCreate(Sender: TObject);
    591 begin
    592   Self.Caption := '';
    593   fileid     := 0;
    594   VST.NodeDataSize := SizeOf(TNodeData);
    595   value_viewer.ColCount := 2;
    596   value_viewer.RowCount := 8;
    597   value_viewer.FixedRows := 1;
    598   value_viewer.Cells[0, 0] := 'Type';
    599   value_viewer.Cells[1, 0] := 'Value';
    600   value_viewer.Cells[0, 1] := '1 byte, unsigned';
    601   value_viewer.Cells[0, 2] := '2 bytes, unsigned';
    602   value_viewer.Cells[0, 3] := '4 bytes, unsigned';
    603   value_viewer.Cells[0, 4] := 'Bitset';
    604   value_viewer.Cells[0, 5] := 'Float';
    605   value_viewer.Cells[0, 6] := 'String';
    606   value_viewer.Cells[0, 7] := 'Selected length';
    607   value_viewer.ColWidths[0] := 100;
    608   value_viewer.ColWidths[1] := value_viewer.Width - 150;
    609   hex.Height := panel_data.Height - 215;
    610   //
    611   value_viewer.Font.Charset := AppSettings.CharSet;
    612   VST.Font.Charset := AppSettings.CharSet;
    613   hex.Translation := tkAsIs;
    614   hex.Font.Charset := AppSettings.CharSet;
    615   //
    616 end;
    617 
    618 
    619 
    620 
    621492function TForm_BinEdit.Save: Boolean;
    622493var
     
    672543
    673544
    674 
    675 
    676 procedure TForm_BinEdit.FormResize(Sender: TObject);
    677 begin
    678   if Self.Width >= 650 then
    679   begin
    680   end
    681   else
    682     Self.Width := 650;
    683   if Self.Height >= 450 then
    684   begin
    685   end
    686   else
    687     Self.Height := 450;
    688 end;
    689545
    690546
     
    767623
    768624
    769 procedure TForm_BinEdit.FormClose(Sender: TObject; var Action: TCloseAction);
    770 begin
    771   Action := caFree;
    772 end;
    773 
    774 
    775 
    776 
    777 procedure TForm_BinEdit.panel_imexportResize(Sender: TObject);
    778 begin
    779   btn_import.Width := panel_imexport.Width - 8;
    780   btn_export.Width := panel_imexport.Width - 8;
    781 end;
    782 
    783 
    784 
    785 
    786 procedure TForm_BinEdit.btn_exportClick(Sender: TObject);
    787 begin
    788   saved.Filter     := 'Files of matching extension (*.' + OniDataConnection.GetFileInfo(
    789     fileid).Extension + ')|*.' + OniDataConnection.GetFileInfo(fileid).Extension +
    790     '|All files|*.*';
    791   saved.DefaultExt := OniDataConnection.GetFileInfo(fileid).Extension;
    792   if saved.Execute then
    793   begin
    794     ExportDatFile(fileid, saved.FileName);
    795   end;
    796 end;
    797 
    798 
    799 
    800 
    801 procedure TForm_BinEdit.btn_importClick(Sender: TObject);
    802 var
    803   fs: TFileStream;
    804 begin
    805   opend.Filter := 'Files of matching extension (*.' + OniDataConnection.GetFileInfo(
    806     fileid).Extension + ')|*.' + OniDataConnection.GetFileInfo(fileid).Extension +
    807     '|All files|*.*';
    808   if opend.Execute then
    809   begin
    810     fs := TFileStream.Create(opend.FileName, fmOpenRead);
    811     if fs.Size <> hex.DataSize then
    812     begin
    813       ShowMessage('Can''t import ' + ExtractFilename(opend.FileName) +
    814         ', file has to have same size as file in .dat.' + CrLf +
    815         'Size of file in .dat: ' + FormatFileSize(hex.datasize) + CrLf +
    816         'Size of chosen file: ' + FormatFileSize(fs.Size));
    817     end
    818     else
    819     begin
    820       hex.LoadFromStream(fs);
    821       hex.Modified := True;
    822     end;
    823     fs.Free;
    824   end;
    825 end;
    826 
    827 
    828 
    829 
    830625procedure TForm_BinEdit.value_viewer_contextPopup(Sender: TObject);
    831626var
     
    964759      begin
    965760        if OniDataConnection.GetRawInfo(fileid, nodedata.offset).raw_size > 0 then
    966         begin
    967           if Form_Main.open_child('rawedit') then
    968           begin
    969             TForm_RawEdit(Form_Main.ActiveMDIChild).LoadRaw(
    970               OniDataConnection.GetRawInfo(fileid, nodedata.offset));
    971           end;
    972         end;
     761          Form_Main.open_child('rawedit', fileid);
    973762      end;
    974763      if nodedata.DataType = 12 then
     
    979768        begin
    980769          if OniDataConnection.GetFileInfo(StrToInt(nodedata.Value)).Size > 0 then
    981           begin
    982             if Form_Main.open_child('binedit') then
    983             begin
    984               TForm_BinEdit(Form_Main.ActiveMDIChild).LoadDat(StrToInt(nodedata.Value));
    985             end;
    986           end
     770            Form_Main.open_child('binedit', StrToInt(nodedata.Value))
    987771          else
    988           begin
    989772            ShowMessage('Linked filed is a zero-byte-file');
    990           end;
    991773        end;
    992774      end;
    993775      if (nodedata.DataType >= 100) and (nodedata.DataType <= 300) then
    994776      begin
    995         if Form_Main.open_child('binedit') then
     777        if Form_Main.open_child('binedit', -1) then
    996778        begin
    997779          TForm_BinEdit(Form_Main.ActiveMDIChild).edit_filtername.Text := nodedata.Value;
     
    1196978
    1197979
     980begin
     981  AddToolListEntry('binedit', 'Binary .dat-Editor', '');
    1198982end.
Note: See TracChangeset for help on using the changeset viewer.