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

Last change on this file since 214 was 214, checked in by alloc, 18 years ago
File size: 5.6 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; virtual; abstract;
30 procedure InitRawList; virtual; abstract;
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 end;
104
105 InitDatLinks();
106 InitRawList();
107 FDataFields := nil;
108 FEditor := nil;
109
110 if not (Self is TFile_Empty) then
111 FFileStream.Free;
112 FFileStream := nil;
113end;
114
115
116procedure TFile.Free;
117begin
118 FDataFields.Free;
119end;
120
121function TFile.GetDatLinkByIndex(ID: Integer): TDatLink;
122var
123 i: Integer;
124 valids: Integer;
125begin
126 if ID < GetChildCount then
127 begin
128 valids := 0;
129 i := 0;
130 repeat
131 if FDatLinks[i].DestID >= 0 then
132 begin
133 Inc(valids);
134 end;
135 Inc(i);
136 until valids > ID;
137 Result := FDatLinks[i - 1];
138 end
139 else
140 with Result do
141 begin
142 SrcOffset := -1;
143 DestID := -1;
144 PosDestExts := '';
145 end;
146end;
147
148function TFile.GetDatLinkByOffset(Offset: Integer): TDatLink;
149var
150 i: Integer;
151begin
152 Result.SrcOffset := -1;
153 Result.DestID := -1;
154 Result.PosDestExts := '';
155
156 if Length(FDatLinks) > 0 then
157 begin
158 for i := 0 to High(FDatLinks) do
159 if FDatLinks[i].SrcOffset = Offset then
160 break;
161 if i < Length(FDatLinks) then
162 Result := FDatLinks[i];
163 end;
164end;
165
166
167function TFile.GetFieldByIndex(ID: Integer): TDataField;
168begin
169 Result := FDataFields.FieldByIndex[ID];
170end;
171
172function TFile.GetFieldByOffset(Offset: Integer): TDataField;
173begin
174 Result := FDataFields.FieldByOffset[Offset];
175end;
176
177
178function TFile.GetChildCount: Integer;
179var
180 i: Integer;
181begin
182 Result := Length(FDatLinks);
183 if Result > 0 then
184 begin
185 Result := 0;
186 for i := 0 to High(FDatLinks) do
187 if FDatLinks[i].DestID >= 0 then
188 Inc(Result);
189 end;
190end;
191
192function TFile.GetRawPartByIndex(ID: Integer): TRawDataInfo;
193begin
194 if ID < Length(FRawParts) then
195 Result := FRawParts[ID]
196 else
197 with Result do
198 begin
199 SrcID := -1;
200 SrcOffset := -1;
201 RawAddr := -1;
202 RawSize := -1;
203 end;
204end;
205
206function TFile.GetRawPartByOffset(Offset: Integer): TRawDataInfo;
207var
208 i: Integer;
209begin
210 with Result do
211 begin
212 SrcID := -1;
213 SrcOffset := -1;
214 RawAddr := -1;
215 RawSize := -1;
216 end;
217
218 if Length(FRawParts) > 0 then
219 begin
220 for i := 0 to High(FRawParts) do
221 if FRawParts[i].SrcOffset = Offset then
222 break;
223 if i < Length(FRawParts) then
224 Result := FRawParts[i];
225 end;
226end;
227
228
229
230procedure TFile.InitDataFields;
231begin
232 if Assigned(FDataFields) then
233 Exit;
234end;
235
236procedure TFile.InitEditor;
237begin
238 Exit;
239end;
240
241function GetDatLinkValue(stream: TStream; offset: Integer): Integer;
242begin
243 stream.Seek(Offset, soFromBeginning);
244 stream.Read(Result, 4);
245 if Result > 0 then
246 Result := Result div 256
247 else
248 Result := -1;
249end;
250
251end.
Note: See TracBrowser for help on using the repository browser.