1 | unit _FileTypes;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | TypeDefs, _TreeElement, Classes, Forms, _DataTypes;
|
---|
7 |
|
---|
8 |
|
---|
9 | type
|
---|
10 | TRawCache = record
|
---|
11 | DatOffset: Integer;
|
---|
12 | RawStream: TMemoryStream;
|
---|
13 | end;
|
---|
14 |
|
---|
15 | TResource = class(TTreeElement)
|
---|
16 | protected
|
---|
17 | FFileInfo: TFileInfo;
|
---|
18 | FFileStream: TMemoryStream;
|
---|
19 | FRawCaches: array of TRawCache;
|
---|
20 |
|
---|
21 | FCached: Boolean;
|
---|
22 | FChanged: Boolean;
|
---|
23 | FReplaced: Boolean;
|
---|
24 |
|
---|
25 | FDataFields: TBlock;
|
---|
26 |
|
---|
27 | FEditor: TFrame;
|
---|
28 | FOpened: Boolean;
|
---|
29 |
|
---|
30 | function GetChildCount: Integer; override;
|
---|
31 | function GetChild(ID: Integer): TTreeElement; override;
|
---|
32 | function GetCaption: String; override;
|
---|
33 | function GetType: String; override;
|
---|
34 | function GetEditor: TFrame;
|
---|
35 | function GetFieldByOffset(Offset: Integer): TDataField;
|
---|
36 | function GetRawCacheID(Offset: Integer): Integer;
|
---|
37 | procedure SetOpened(Opened: Boolean); virtual;
|
---|
38 | published
|
---|
39 | public
|
---|
40 | constructor Create(ConnectionID, FileID: Integer); virtual;
|
---|
41 | procedure Free;
|
---|
42 |
|
---|
43 | procedure InitDataFields; virtual;
|
---|
44 | procedure InitEditor; virtual;
|
---|
45 |
|
---|
46 | procedure ReCreateFile(fs: TStream);
|
---|
47 |
|
---|
48 | procedure CacheFile;
|
---|
49 | procedure CacheRawFile(Offset: Integer);
|
---|
50 | procedure CacheAllRawFiles;
|
---|
51 | procedure DiscardFileCache;
|
---|
52 | procedure DiscardRawCache(Offset: Integer);
|
---|
53 | procedure DiscardAllRawCaches;
|
---|
54 | procedure StoreFileCache;
|
---|
55 | procedure StoreRawCache(Offset: Integer);
|
---|
56 | procedure StoreAllRawCaches;
|
---|
57 |
|
---|
58 | property FileStream: TMemoryStream read FFileStream;
|
---|
59 | property FileInfo: TFileInfo read FFileInfo;
|
---|
60 |
|
---|
61 | property Cached: Boolean read FCached;
|
---|
62 | property Changed: Boolean read FChanged write FChanged;
|
---|
63 |
|
---|
64 | property Editor: TFrame read GetEditor;
|
---|
65 | property Opened: Boolean read FOpened write SetOpened;
|
---|
66 |
|
---|
67 | property FieldByOffset[Offset: Integer]: TDataField read GetFieldByOffset;
|
---|
68 | end;
|
---|
69 |
|
---|
70 | type
|
---|
71 | TFileType = class of TResource;
|
---|
72 |
|
---|
73 |
|
---|
74 |
|
---|
75 | implementation
|
---|
76 |
|
---|
77 | uses
|
---|
78 | DatLinks, RawList, ConnectionManager, Dialogs, _MetaTypes, _MetaManager,
|
---|
79 | SysUtils;
|
---|
80 |
|
---|
81 | { TFileType }
|
---|
82 |
|
---|
83 | procedure TResource.CacheFile;
|
---|
84 | begin
|
---|
85 | if not Assigned(FFileStream) then
|
---|
86 | begin
|
---|
87 | FFileStream := TMemoryStream.Create;
|
---|
88 | ConManager.Connection[FConnectionID].LoadDatFile(FFileInfo.ID, TStream(FFileStream));
|
---|
89 | end;
|
---|
90 | end;
|
---|
91 |
|
---|
92 |
|
---|
93 | procedure TResource.CacheRawFile(Offset: Integer);
|
---|
94 | var
|
---|
95 | i: Integer;
|
---|
96 | begin
|
---|
97 | for i := 0 to High(FRawCaches) do
|
---|
98 | begin
|
---|
99 | if FRawCaches[i].DatOffset = Offset then
|
---|
100 | begin
|
---|
101 | if not Assigned(FRawCaches[i].RawStream) then
|
---|
102 | begin
|
---|
103 | FRawCaches[i].RawStream := TMemoryStream.Create;
|
---|
104 | ConManager.Connection[ConnectionID].LoadRawFile(FFileInfo.ID, Offset, TStream(FRawCaches[i].RawStream));
|
---|
105 | end;
|
---|
106 | end;
|
---|
107 | end;
|
---|
108 | end;
|
---|
109 |
|
---|
110 | procedure TResource.CacheAllRawFiles;
|
---|
111 | var
|
---|
112 | i: Integer;
|
---|
113 | begin
|
---|
114 | for i := 0 to High(FRawCaches) do
|
---|
115 | CacheRawFile(FRawCaches[i].DatOffset);
|
---|
116 | end;
|
---|
117 |
|
---|
118 |
|
---|
119 |
|
---|
120 | procedure TResource.DiscardFileCache;
|
---|
121 | begin
|
---|
122 | if Assigned(FFileStream) then
|
---|
123 | FFileStream.Free;
|
---|
124 | FFileStream := nil;
|
---|
125 | end;
|
---|
126 |
|
---|
127 | procedure TResource.DiscardRawCache(Offset: Integer);
|
---|
128 | var
|
---|
129 | i: Integer;
|
---|
130 | begin
|
---|
131 | for i := 0 to High(FRawCaches) do
|
---|
132 | begin
|
---|
133 | if FRawCaches[i].DatOffset = Offset then
|
---|
134 | begin
|
---|
135 | if Assigned(FRawCaches[i].RawStream) then
|
---|
136 | FRawCaches[i].RawStream.Free;
|
---|
137 | FRawCaches[i].RawStream := nil;
|
---|
138 | end;
|
---|
139 | end;
|
---|
140 | end;
|
---|
141 |
|
---|
142 | procedure TResource.DiscardAllRawCaches;
|
---|
143 | var
|
---|
144 | i: Integer;
|
---|
145 | begin
|
---|
146 | for i := 0 to High(FRawCaches) do
|
---|
147 | DiscardRawCache(FRawCaches[i].DatOffset);
|
---|
148 | end;
|
---|
149 |
|
---|
150 | procedure TResource.StoreFileCache;
|
---|
151 | begin
|
---|
152 | Exit;
|
---|
153 | end;
|
---|
154 |
|
---|
155 | procedure TResource.StoreRawCache(Offset: Integer);
|
---|
156 | begin
|
---|
157 | Exit;
|
---|
158 | end;
|
---|
159 |
|
---|
160 | procedure TResource.StoreAllRawCaches;
|
---|
161 | var
|
---|
162 | i: Integer;
|
---|
163 | begin
|
---|
164 | for i := 0 to High(FRawCaches) do
|
---|
165 | StoreRawCache(FRawCaches[i].DatOffset);
|
---|
166 | end;
|
---|
167 |
|
---|
168 |
|
---|
169 |
|
---|
170 |
|
---|
171 |
|
---|
172 | procedure TResource.SetOpened(Opened: Boolean);
|
---|
173 | begin
|
---|
174 | FOpened := Opened;
|
---|
175 | end;
|
---|
176 |
|
---|
177 |
|
---|
178 | constructor TResource.Create(ConnectionID, FileID: Integer);
|
---|
179 | var
|
---|
180 | rawlist: TRawDataList;
|
---|
181 | i: Integer;
|
---|
182 | begin
|
---|
183 | FConnectionID := ConnectionID;
|
---|
184 | FFileInfo := ConManager.Connection[ConnectionID].GetFileInfo(FileID);
|
---|
185 |
|
---|
186 | rawlist := ConManager.Connection[ConnectionID].GetRawList(FileID);
|
---|
187 | SetLength(FRawCaches, Length(rawlist));
|
---|
188 | for i := 0 to High(rawlist) do
|
---|
189 | FRawCaches[i].DatOffset := rawlist[i].SrcOffset;
|
---|
190 |
|
---|
191 | FCached := False;
|
---|
192 | FChanged := False;
|
---|
193 |
|
---|
194 | FDataFields := nil;
|
---|
195 | FEditor := nil;
|
---|
196 |
|
---|
197 | if not (Self is TFile_Empty) then
|
---|
198 | InitDataFields;
|
---|
199 | end;
|
---|
200 |
|
---|
201 |
|
---|
202 | procedure TResource.Free;
|
---|
203 | begin
|
---|
204 | FDataFields.Free;
|
---|
205 | end;
|
---|
206 |
|
---|
207 |
|
---|
208 | function TResource.GetFieldByOffset(Offset: Integer): TDataField;
|
---|
209 | begin
|
---|
210 | Result := FDataFields.FieldByOffset[Offset];
|
---|
211 | end;
|
---|
212 |
|
---|
213 |
|
---|
214 | function TResource.GetRawCacheID(Offset: Integer): Integer;
|
---|
215 | var
|
---|
216 | i: Integer;
|
---|
217 | begin
|
---|
218 | Result := -1;
|
---|
219 | for i := 0 to High(FRawCaches) do
|
---|
220 | if FRawCaches[i].DatOffset = Offset then
|
---|
221 | Result := i;
|
---|
222 | end;
|
---|
223 |
|
---|
224 | function TResource.GetType: String;
|
---|
225 | begin
|
---|
226 | Result := FFileInfo.Extension;
|
---|
227 | end;
|
---|
228 |
|
---|
229 | function TResource.GetCaption: String;
|
---|
230 | begin
|
---|
231 | Result := FFileInfo.Name;
|
---|
232 | if Result = '' then
|
---|
233 | Result := 'Unnamed';
|
---|
234 | Result := Result + ' (' + IntToStr(FFileInfo.ID) + ')';
|
---|
235 | end;
|
---|
236 |
|
---|
237 | function TResource.GetChild(ID: Integer): TTreeElement;
|
---|
238 | begin
|
---|
239 | Result := FDataFields.Child[ID];
|
---|
240 | end;
|
---|
241 |
|
---|
242 | function TResource.GetChildCount: Integer;
|
---|
243 | begin
|
---|
244 | if FDataFields <> nil then
|
---|
245 | Result := FDataFields.ChildCount
|
---|
246 | else
|
---|
247 | Result := 0;
|
---|
248 | end;
|
---|
249 |
|
---|
250 |
|
---|
251 | function TResource.GetEditor: TFrame;
|
---|
252 | begin
|
---|
253 | if not Assigned(FEditor) then
|
---|
254 | InitEditor;
|
---|
255 | Result := FEditor;
|
---|
256 | end;
|
---|
257 |
|
---|
258 |
|
---|
259 | procedure TResource.InitDataFields;
|
---|
260 | begin
|
---|
261 | if Assigned(FDataFields) then
|
---|
262 | Exit;
|
---|
263 | FFileStream := TMemoryStream.Create;
|
---|
264 | ConManager.Connection[FConnectionID].LoadDatFile(FFileInfo.ID, TStream(FFileStream));
|
---|
265 | end;
|
---|
266 |
|
---|
267 |
|
---|
268 | procedure TResource.InitEditor;
|
---|
269 | begin
|
---|
270 | Exit;
|
---|
271 | end;
|
---|
272 |
|
---|
273 |
|
---|
274 | //*********************************************************
|
---|
275 | //*********************************************************
|
---|
276 | //*********************************************************
|
---|
277 | procedure TResource.ReCreateFile(fs: TStream);
|
---|
278 | const
|
---|
279 | temp0: array[0..31] of Byte =
|
---|
280 | ($AD,$DE,$AD,$DE,$AD,$DE,$AD,$DE,
|
---|
281 | $AD,$DE,$AD,$DE,$AD,$DE,$AD,$DE,
|
---|
282 | $AD,$DE,$AD,$DE,$AD,$DE,$AD,$DE,
|
---|
283 | $AD,$DE,$AD,$DE,$AD,$DE,$AD,$DE);
|
---|
284 | var
|
---|
285 | count: Integer;
|
---|
286 | begin
|
---|
287 | FDataFields.WriteData(fs);
|
---|
288 | count := fs.Size mod 32;
|
---|
289 | if count > 0 then
|
---|
290 | fs.Write(temp0[count], 32 - count);
|
---|
291 | end;
|
---|
292 | //*********************************************************
|
---|
293 | //*********************************************************
|
---|
294 | //*********************************************************
|
---|
295 |
|
---|
296 | end.
|
---|