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

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