source: oup/releases/0.17a/Unit8_binedit.pas

Last change on this file was 21, checked in by alloc, 17 years ago
File size: 7.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,
6  Unit3_data, Unit2_functions, Unit9_data_structures;
7
8TYPE
9  TForm8 = Class(TForm)
10    list: TListBox;
11    Splitter1: TSplitter;
12    panel_data: TPanel;
13    hex: TMPHexEditor;
14    Splitter2: TSplitter;
15    structs: TWrapGrid;
16    PROCEDURE hexSelectionChanged(Sender: TObject);
17    PROCEDURE hexChange(Sender: TObject);
18    PROCEDURE panel_dataResize(Sender: TObject);
19    PROCEDURE structsClick(Sender: TObject);
20    PROCEDURE FormResize(Sender: TObject);
21    PROCEDURE ClearStructViewer;
22    PROCEDURE listClick(Sender: TObject);
23    PROCEDURE FormCloseQuery(Sender: TObject; var CanClose: Boolean);
24    PROCEDURE FormCreate(Sender: TObject);
25    PROCEDURE Recreatelist;
26  PRIVATE
27  PUBLIC
28  END;
29
30VAR
31  Form8: TForm8;
32
33IMPLEMENTATION
34{$R *.dfm}
35VAR
36  fileid:LongWord;
37
38FUNCTION IntToBin(value:Byte):String;
39  VAR i:Byte;
40  BEGIN
41    Result:='';
42    FOR i:=7 DOWNTO 0 DO BEGIN
43      Result:=Result+IntToStr((value SHR i) AND $01);
44    END;
45  END;
46
47FUNCTION GetTypeDataLength(datatype:Byte):Byte;
48  BEGIN
49    CASE datatype OF
50      1..4: Result:=datatype;
51      5..8: Result:=datatype-4;
52      9: Result:=4;
53      10: Result:=1;
54      11..255: Result:=datatype-10;
55    END;
56  END;
57
58FUNCTION GetValue(datatype:Byte; offset:LongWord):String;
59  VAR
60    data:Tdata;
61  BEGIN
62    CASE datatype OF
63      1: Result:=IntToStr(Form8.hex.data[offset]);
64      2: Result:=IntToStr(Form8.hex.data[offset]+Form8.hex.data[offset+1]*256);
65      3: Result:=IntToStr(Form8.hex.data[offset]+Form8.hex.data[offset+1]*256+Form8.hex.data[offset+2]*256*256);
66      4: Result:=IntToStr(Form8.hex.data[offset]+Form8.hex.data[offset+1]*256+Form8.hex.data[offset+2]*256*256+Form8.hex.data[offset+3]*256*256*256);
67      5: Result:='0x'+IntToHex(Form8.hex.data[offset],2);
68      6: Result:='0x'+IntToHex(Form8.hex.data[offset]+Form8.hex.data[offset+1]*256,4);
69      7: Result:='0x'+IntToHex(Form8.hex.data[offset]+Form8.hex.data[offset+1]*256+Form8.hex.data[offset+2]*256*256,6);
70      8: Result:='0x'+IntToHex(Form8.hex.data[offset]+Form8.hex.data[offset+1]*256+Form8.hex.data[offset+2]*256*256+Form8.hex.data[offset+3]*256*256*256,8);
71      9: BEGIN
72          SetLength(data,4);
73          data[0]:=Form8.hex.data[offset];
74          data[1]:=Form8.hex.data[offset+1];
75          data[2]:=Form8.hex.data[offset+2];
76          data[3]:=Form8.hex.data[offset+3];
77          Result:=FloatToStr(Decode_Float(data));
78        END;
79      10: Result:=IntToBin(Form8.hex.data[offset]);
80    END;
81  END;
82
83PROCEDURE WriteStructureInfos(structinfoid:Integer);
84  VAR
85    i:Byte;
86  BEGIN
87    IF structinfoid>=0 THEN BEGIN
88      WITH structure_infos[structinfoid] DO BEGIN
89        Form8.structs.RowCount:=Length(entries)+1;
90        FOR i:=1 TO Length(entries) DO BEGIN
91          Form8.structs.Cells[0,i]:=entries[i-1].name;
92          Form8.structs.Cells[1,i]:='0x'+IntToHex(entries[i-1].offset,6);
93          Form8.structs.Cells[2,i]:=GetDataType(entries[i-1].datatype);
94          Form8.structs.Cells[3,i]:=GetValue(entries[i-1].datatype,entries[i-1].offset);
95          Form8.structs.Cells[4,i]:=entries[i-1].description;
96        END;
97      END;
98    END;
99  END;
100
101PROCEDURE TForm8.Recreatelist;
102  VAR
103    i:LongWord;
104  BEGIN
105    Form8.list.Items.Clear;
106    FOR i:=0 TO dat_header.Files-1 DO BEGIN
107      IF (dat_files[i].FileType AND $02)=0 THEN
108        Form8.list.Items.Add(dat_files[i].FileName);
109    END;
110  END;
111
112PROCEDURE TForm8.FormCreate(Sender: TObject);
113  BEGIN
114    Form8.Caption:='';
115    fileid:=0;
116    structs.ColCount:=5;
117    structs.RowCount:=2;
118    structs.FixedRows:=1;
119    structs.Cells[0,0]:='Name';
120    structs.Cells[1,0]:='Offset';
121    structs.Cells[2,0]:='Type';
122    structs.Cells[3,0]:='Value';
123    structs.Cells[4,0]:='Description';
124    structs.ColWidths[0]:=75;
125    structs.ColWidths[1]:=60;
126    structs.ColWidths[2]:=75;
127    structs.ColWidths[3]:=75;
128    Form8.panel_dataResize(Form8);
129  END;
130
131PROCEDURE TForm8.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
132  BEGIN
133    CanClose:=False;
134    Form8.Visible:=False;
135  END;
136
137PROCEDURE TForm8.listClick(Sender: TObject);
138  VAR
139    mem:TMemoryStream;
140    data:Tdata;
141  BEGIN
142    IF hex.Modified THEN BEGIN
143      IF MessageBox(Form8.Handle,PChar('Save changes to file '+dat_files[fileid].FileName+'?'),PChar('Data changed...'),MB_YESNO)=IDYES THEN BEGIN
144        mem:=TMemoryStream.Create;
145        hex.SaveToStream(mem);
146        mem.Seek(0,soFromBeginning);
147        SetLength(data,mem.Size);
148        mem.Read(data[0],mem.Size); 
149        mem.Free;
150        SaveDatFile(fileid,data);
151        ShowMessage('Changes saved...');
152      END;
153    END;
154    Form8.ClearStructViewer;
155    fileid:=StrToInt(MidStr(list.Items.Strings[list.ItemIndex],1,5));
156    data:=LoadDatFile(fileid);
157    mem:=TMemoryStream.Create;
158    mem.Write(data[0],Length(data));
159    hex.LoadFromStream(mem);
160    mem.Free;
161    WriteStructureInfos(GetStructureInfoId(dat_files[fileid].Extension));
162  END;
163
164PROCEDURE TForm8.ClearStructViewer;
165  VAR
166    x:Word;
167  BEGIN
168    structs.RowCount:=2;
169    FOR x:=0 TO structs.ColCount-1 DO structs.Cells[x,1]:='';
170  END;
171
172PROCEDURE TForm8.FormResize(Sender: TObject);
173  BEGIN
174    IF Form8.Width>=650 THEN BEGIN
175    END ELSE Form8.Width:=650;
176    IF Form8.Height>=450 THEN BEGIN
177    END ELSE Form8.Height:=450;
178  END;
179
180PROCEDURE TForm8.structsClick(Sender: TObject);
181  VAR
182    offset:LongWord;
183    length:Byte;
184  BEGIN
185    IF structs.Row>0 THEN BEGIN
186      offset:=structure_infos[GetStructureInfoId(dat_files[fileid].extension)].entries[structs.Row-1].offset;
187      length:=GetTypeDataLength(structure_infos[GetStructureInfoId(dat_files[fileid].extension)].entries[structs.Row-1].datatype);
188      hex.SelStart:=offset;
189      hex.SelEnd:=offset+length-1;
190    END;
191  END;
192
193PROCEDURE TForm8.panel_dataResize(Sender: TObject);
194  BEGIN
195    structs.ColWidths[4]:=structs.Width-structs.ColWidths[0]-structs.ColWidths[1]-structs.ColWidths[2]-structs.ColWidths[3]-12;
196  END;
197
198PROCEDURE TForm8.hexChange(Sender: TObject);
199  BEGIN
200    IF hex.DataSize>0 THEN
201      WriteStructureInfos(GetStructureInfoId(dat_files[fileid].Extension));
202  END;
203
204PROCEDURE TForm8.hexSelectionChanged(Sender: TObject);
205  VAR
206    selstart,sellength,selend:Integer;
207    i,j:Word;
208  BEGIN
209    FOR i:=1 TO structs.RowCount-1 DO BEGIN
210      FOR j:=0 TO structs.ColCount-1 DO BEGIN
211        structs.CellColors[j,i]:=clWhite;
212        structs.CellFontColors[j,i]:=clBlack;
213      END;
214    END;
215    IF hex.DataSize>0 THEN BEGIN
216      selstart:=hex.SelStart;
217      selend:=hex.SelEnd;
218      sellength:=hex.SelCount;
219      IF GetStructureInfoId(dat_files[fileid].Extension)>=0 THEN BEGIN
220        WITH structure_infos[GetStructureInfoId(dat_files[fileid].Extension)] DO BEGIN
221          FOR i:=0 TO High(entries) DO BEGIN
222            IF ((selstart-entries[i].offset)<GetTypeDataLength(entries[i].datatype)) AND ((selstart-entries[i].offset)>=0) THEN BEGIN
223              FOR j:=0 TO structs.ColCount-1 DO BEGIN
224                structs.CellColors[j,i+1]:=clBlue;
225                structs.CellFontColors[j,i+1]:=clWhite;
226              END;
227              structs.Row:=i+1;
228            END;
229          END;
230        END;
231      END;
232    END;
233  END;
234
235END.
Note: See TracBrowser for help on using the repository browser.