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

Last change on this file since 256 was 248, checked in by alloc, 17 years ago
File size: 6.6 KB
Line 
1unit _FileTypes;
2
3interface
4
5uses
6 TypeDefs, _TreeElement, Classes, Forms, _DataTypes;
7
8
9type
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
70type
71 TFileType = class of TResource;
72
73
74
75implementation
76
77uses
78 DatLinks, RawList, ConnectionManager, Dialogs, _MetaTypes, _MetaManager,
79 SysUtils;
80
81{ TFileType }
82
83procedure TResource.CacheFile;
84begin
85 if not Assigned(FFileStream) then
86 begin
87 FFileStream := TMemoryStream.Create;
88 ConManager.Connection[FConnectionID].LoadDatFile(FFileInfo.ID, TStream(FFileStream));
89 end;
90end;
91
92
93procedure TResource.CacheRawFile(Offset: Integer);
94var
95 i: Integer;
96begin
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;
108end;
109
110procedure TResource.CacheAllRawFiles;
111var
112 i: Integer;
113begin
114 for i := 0 to High(FRawCaches) do
115 CacheRawFile(FRawCaches[i].DatOffset);
116end;
117
118
119
120procedure TResource.DiscardFileCache;
121begin
122 if Assigned(FFileStream) then
123 FFileStream.Free;
124 FFileStream := nil;
125end;
126
127procedure TResource.DiscardRawCache(Offset: Integer);
128var
129 i: Integer;
130begin
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;
140end;
141
142procedure TResource.DiscardAllRawCaches;
143var
144 i: Integer;
145begin
146 for i := 0 to High(FRawCaches) do
147 DiscardRawCache(FRawCaches[i].DatOffset);
148end;
149
150procedure TResource.StoreFileCache;
151begin
152 Exit;
153end;
154
155procedure TResource.StoreRawCache(Offset: Integer);
156begin
157 Exit;
158end;
159
160procedure TResource.StoreAllRawCaches;
161var
162 i: Integer;
163begin
164 for i := 0 to High(FRawCaches) do
165 StoreRawCache(FRawCaches[i].DatOffset);
166end;
167
168
169
170
171
172procedure TResource.SetOpened(Opened: Boolean);
173begin
174 FOpened := Opened;
175end;
176
177
178constructor TResource.Create(ConnectionID, FileID: Integer);
179var
180 rawlist: TRawDataList;
181 i: Integer;
182begin
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;
199end;
200
201
202procedure TResource.Free;
203begin
204 FDataFields.Free;
205end;
206
207
208function TResource.GetFieldByOffset(Offset: Integer): TDataField;
209begin
210 Result := FDataFields.FieldByOffset[Offset];
211end;
212
213
214function TResource.GetRawCacheID(Offset: Integer): Integer;
215var
216 i: Integer;
217begin
218 Result := -1;
219 for i := 0 to High(FRawCaches) do
220 if FRawCaches[i].DatOffset = Offset then
221 Result := i;
222end;
223
224function TResource.GetType: String;
225begin
226 Result := FFileInfo.Extension;
227end;
228
229function TResource.GetCaption: String;
230begin
231 Result := FFileInfo.Name;
232 if Result = '' then
233 Result := 'Unnamed';
234 Result := Result + ' (' + IntToStr(FFileInfo.ID) + ')';
235end;
236
237function TResource.GetChild(ID: Integer): TTreeElement;
238begin
239 Result := FDataFields.Child[ID];
240end;
241
242function TResource.GetChildCount: Integer;
243begin
244 if FDataFields <> nil then
245 Result := FDataFields.ChildCount
246 else
247 Result := 0;
248end;
249
250
251function TResource.GetEditor: TFrame;
252begin
253 if not Assigned(FEditor) then
254 InitEditor;
255 Result := FEditor;
256end;
257
258
259procedure TResource.InitDataFields;
260begin
261 if Assigned(FDataFields) then
262 Exit;
263 FFileStream := TMemoryStream.Create;
264 ConManager.Connection[FConnectionID].LoadDatFile(FFileInfo.ID, TStream(FFileStream));
265end;
266
267
268procedure TResource.InitEditor;
269begin
270 Exit;
271end;
272
273
274//*********************************************************
275//*********************************************************
276//*********************************************************
277procedure TResource.ReCreateFile(fs: TStream);
278const
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);
284var
285 count: Integer;
286begin
287 FDataFields.WriteData(fs);
288 count := fs.Size mod 32;
289 if count > 0 then
290 fs.Write(temp0[count], 32 - count);
291end;
292//*********************************************************
293//*********************************************************
294//*********************************************************
295
296end.
Note: See TracBrowser for help on using the repository browser.