source: oup/current/FileClasses/_FileTypes.pas@ 215

Last change on this file since 215 was 215, checked in by alloc, 17 years ago
File size: 5.9 KB
Line 
1unit _FileTypes;
2
3interface
4
5uses
6 TypeDefs, _DataTypes, Classes, Forms;
7
8
9type
10 TFile = class
11 protected
12 FConnectionID: Integer;
13 FFileID: Integer;
14 FFileName: String;
15 FFileExt: String;
16 FFileSize: Integer;
17 FFileStream: TMemoryStream;
18
19 FCached: Boolean;
20 FChanged: Boolean;
21
22 FDatLinks: TDatLinkList;
23 FDataFields: TBlock;
24 FRawParts: TRawDataList;
25
26 FEditor: TFrame;
27 FOpened: Boolean;
28
29 procedure InitDatLinks;
30 procedure InitRawList;
31
32 function GetDatLinkByOffset(Offset: Integer): TDatLink;
33 function GetDatLinkByIndex(ID: Integer): TDatLink;
34 function GetFieldByOffset(Offset: Integer): TDataField;
35 function GetFieldByIndex(ID: Integer): TDataField;
36 function GetRawPartByOffset(Offset: Integer): TRawDataInfo;
37 function GetRawPartByIndex(ID: Integer): TRawDataInfo;
38 function GetChildCount: Integer;
39 public
40 constructor Create(ConnectionID, FileID: Integer); virtual;
41 procedure Free;
42
43 procedure InitDataFields; virtual;
44 procedure InitEditor; virtual;
45
46 property FileStream: TMemoryStream read FFileStream;
47 property FileID: Integer read FFileID;
48 property FileName: String read FFileName;
49 property FileExt: String read FFileExt;
50 property FileSize: Integer read FFileSize;
51 property ConnectionID: Integer read FConnectionID;
52
53 property Cached: Boolean read FCached;
54 property Changed: Boolean read FChanged write FChanged;
55
56 property Editor: TFrame read FEditor;
57 property Opened: Boolean read FOpened write FOpened;
58
59 property ChildCount: Integer read GetChildCount;
60 property LinkByOffset[Offset: Integer]: TDatLink read GetDatLinkByOffset;
61 property LinkByIndex[ID: Integer]: TDatLink read GetDatLinkByIndex;
62
63 property FieldByOffset[Offset: Integer]: TDataField read GetFieldByOffset;
64 property FieldByIndex[ID: Integer]: TDataField read GetFieldByIndex;
65
66 property RawPartByOffset[Offset: Integer]: TRawDataInfo read GetRawPartByOffset;
67 property RawPartByIndex[ID: Integer]: TRawDataInfo read GetRawPartByIndex;
68 end;
69
70
71 TFileType = class of TFile;
72
73
74function GetDatLinkValue(stream: TStream; offset: Integer): Integer;
75
76
77
78implementation
79
80uses
81 DatLinks, RawList, ConnectionManager, Dialogs, _EmptyFile;
82
83{ TFileType }
84
85constructor TFile.Create(ConnectionID, FileID: Integer);
86var
87 fileinfo: TFileInfo;
88begin
89 FConnectionID := ConnectionID;
90 FFileID := FileID;
91 fileinfo := ConManager.Connection[ConnectionID].GetFileInfo(FileID);
92 FFileName := fileinfo.Name;
93 FFileExt := fileinfo.Extension;
94 FFileSize := fileinfo.Size;
95
96 FCached := False;
97 FChanged := False;
98
99 if not (Self is TFile_Empty) then
100 begin
101 FFileStream := TMemoryStream.Create;
102 ConManager.Connection[ConnectionID].LoadDatFile(FileID, TStream(FFileStream));
103
104 InitDatLinks();
105 InitRawList();
106 FDataFields := nil;
107 FEditor := nil;
108
109 FFileStream.Free;
110 FFileStream := nil;
111 end
112 else
113 begin
114 SetLength(FDatLinks, 0);
115 SetLength(FRawParts, 0);
116 FEditor := nil;
117 end;
118end;
119
120
121procedure TFile.Free;
122begin
123 FDataFields.Free;
124end;
125
126function TFile.GetDatLinkByIndex(ID: Integer): TDatLink;
127var
128 i: Integer;
129 valids: Integer;
130begin
131 if ID < GetChildCount then
132 begin
133 valids := 0;
134 i := 0;
135 repeat
136 if FDatLinks[i].DestID >= 0 then
137 begin
138 Inc(valids);
139 end;
140 Inc(i);
141 until valids > ID;
142 Result := FDatLinks[i - 1];
143 end
144 else
145 with Result do
146 begin
147 SrcOffset := -1;
148 DestID := -1;
149 PosDestExts := '';
150 end;
151end;
152
153function TFile.GetDatLinkByOffset(Offset: Integer): TDatLink;
154var
155 i: Integer;
156begin
157 Result.SrcOffset := -1;
158 Result.DestID := -1;
159 Result.PosDestExts := '';
160
161 if Length(FDatLinks) > 0 then
162 begin
163 for i := 0 to High(FDatLinks) do
164 if FDatLinks[i].SrcOffset = Offset then
165 break;
166 if i < Length(FDatLinks) then
167 Result := FDatLinks[i];
168 end;
169end;
170
171
172function TFile.GetFieldByIndex(ID: Integer): TDataField;
173begin
174 Result := FDataFields.FieldByIndex[ID];
175end;
176
177function TFile.GetFieldByOffset(Offset: Integer): TDataField;
178begin
179 Result := FDataFields.FieldByOffset[Offset];
180end;
181
182
183function TFile.GetChildCount: Integer;
184var
185 i: Integer;
186begin
187 Result := Length(FDatLinks);
188 if Result > 0 then
189 begin
190 Result := 0;
191 for i := 0 to High(FDatLinks) do
192 if FDatLinks[i].DestID >= 0 then
193 Inc(Result);
194 end;
195end;
196
197function TFile.GetRawPartByIndex(ID: Integer): TRawDataInfo;
198begin
199 if ID < Length(FRawParts) then
200 Result := FRawParts[ID]
201 else
202 with Result do
203 begin
204 SrcID := -1;
205 SrcOffset := -1;
206 RawAddr := -1;
207 RawSize := -1;
208 end;
209end;
210
211function TFile.GetRawPartByOffset(Offset: Integer): TRawDataInfo;
212var
213 i: Integer;
214begin
215 with Result do
216 begin
217 SrcID := -1;
218 SrcOffset := -1;
219 RawAddr := -1;
220 RawSize := -1;
221 end;
222
223 if Length(FRawParts) > 0 then
224 begin
225 for i := 0 to High(FRawParts) do
226 if FRawParts[i].SrcOffset = Offset then
227 break;
228 if i < Length(FRawParts) then
229 Result := FRawParts[i];
230 end;
231end;
232
233
234
235procedure TFile.InitDataFields;
236begin
237 if Assigned(FDataFields) then
238 Exit;
239end;
240
241procedure TFile.InitDatLinks;
242begin
243 FDatLinks := ConManager.Connection[FConnectionID].GetDatLinks(FFileID);
244end;
245
246procedure TFile.InitEditor;
247begin
248 Exit;
249end;
250
251procedure TFile.InitRawList;
252begin
253 FRawParts := ConManager.Connection[FConnectionID].GetRawList(FFileID);
254end;
255
256function GetDatLinkValue(stream: TStream; offset: Integer): Integer;
257begin
258 stream.Seek(Offset, soFromBeginning);
259 stream.Read(Result, 4);
260 if Result > 0 then
261 Result := Result div 256
262 else
263 Result := -1;
264end;
265
266end.
Note: See TracBrowser for help on using the repository browser.