1 | unit _FileTypes;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | TypeDefs, _DataTypes, Classes;
|
---|
7 |
|
---|
8 |
|
---|
9 | type
|
---|
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 |
|
---|
67 | function GetDatLinkValue(stream: TStream; offset: Integer): Integer;
|
---|
68 |
|
---|
69 |
|
---|
70 |
|
---|
71 | implementation
|
---|
72 |
|
---|
73 | uses
|
---|
74 | DatLinks, RawList, ConnectionManager, Dialogs, _EmptyFile;
|
---|
75 |
|
---|
76 | { TFileType }
|
---|
77 |
|
---|
78 | constructor TFile.Create(ConnectionID, FileID: Integer);
|
---|
79 | var
|
---|
80 | i: Integer;
|
---|
81 | fileinfo: TFileInfo;
|
---|
82 | begin
|
---|
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;
|
---|
106 | end;
|
---|
107 |
|
---|
108 |
|
---|
109 | procedure TFile.Free;
|
---|
110 | begin
|
---|
111 | FDataFields.Free;
|
---|
112 | end;
|
---|
113 |
|
---|
114 | function TFile.GetDatLinkByIndex(ID: Integer): TDatLink;
|
---|
115 | var
|
---|
116 | i: Integer;
|
---|
117 | valids: Integer;
|
---|
118 | begin
|
---|
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;
|
---|
139 | end;
|
---|
140 |
|
---|
141 | function TFile.GetDatLinkByOffset(Offset: Integer): TDatLink;
|
---|
142 | var
|
---|
143 | i: Integer;
|
---|
144 | begin
|
---|
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;
|
---|
157 | end;
|
---|
158 |
|
---|
159 |
|
---|
160 | function TFile.GetFieldByIndex(ID: Integer): TDataField;
|
---|
161 | begin
|
---|
162 | Result := FDataFields.FieldByIndex[ID];
|
---|
163 | end;
|
---|
164 |
|
---|
165 | function TFile.GetFieldByOffset(Offset: Integer): TDataField;
|
---|
166 | var
|
---|
167 | i: Integer;
|
---|
168 | begin
|
---|
169 | Result := FDataFields.FieldByOffset[Offset];
|
---|
170 | end;
|
---|
171 |
|
---|
172 |
|
---|
173 | function TFile.GetChildCount: Integer;
|
---|
174 | var
|
---|
175 | i: Integer;
|
---|
176 | begin
|
---|
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;
|
---|
185 | end;
|
---|
186 |
|
---|
187 | function TFile.GetRawPartByIndex(ID: Integer): TRawDataInfo;
|
---|
188 | begin
|
---|
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;
|
---|
199 | end;
|
---|
200 |
|
---|
201 | function TFile.GetRawPartByOffset(Offset: Integer): TRawDataInfo;
|
---|
202 | var
|
---|
203 | i: Integer;
|
---|
204 | begin
|
---|
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;
|
---|
221 | end;
|
---|
222 |
|
---|
223 |
|
---|
224 |
|
---|
225 | procedure TFile.InitDataFields;
|
---|
226 | begin
|
---|
227 | if Assigned(FDataFields) then
|
---|
228 | Exit;
|
---|
229 | end;
|
---|
230 |
|
---|
231 | function GetDatLinkValue(stream: TStream; offset: Integer): Integer;
|
---|
232 | begin
|
---|
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;
|
---|
239 | end;
|
---|
240 |
|
---|
241 | end.
|
---|