source: oup/current/FileClasses/_FileTypes.pas

Last change on this file was 248, checked in by alloc, 17 years ago
File size: 6.6 KB
RevLine 
[124]1unit _FileTypes;
2
3interface
4
5uses
[240]6 TypeDefs, _TreeElement, Classes, Forms, _DataTypes;
[124]7
8
9type
[245]10 TRawCache = record
11 DatOffset: Integer;
12 RawStream: TMemoryStream;
13 end;
14
[248]15 TResource = class(TTreeElement)
[206]16 protected
[231]17 FFileInfo: TFileInfo;
[210]18 FFileStream: TMemoryStream;
[245]19 FRawCaches: array of TRawCache;
[126]20
[212]21 FCached: Boolean;
22 FChanged: Boolean;
[248]23 FReplaced: Boolean;
[212]24
[206]25 FDataFields: TBlock;
[126]26
[214]27 FEditor: TFrame;
28 FOpened: Boolean;
29
[241]30 function GetChildCount: Integer; override;
31 function GetChild(ID: Integer): TTreeElement; override;
32 function GetCaption: String; override;
33 function GetType: String; override;
[240]34 function GetEditor: TFrame;
[126]35 function GetFieldByOffset(Offset: Integer): TDataField;
[245]36 function GetRawCacheID(Offset: Integer): Integer;
[240]37 procedure SetOpened(Opened: Boolean); virtual;
38 published
[124]39 public
[206]40 constructor Create(ConnectionID, FileID: Integer); virtual;
41 procedure Free;
42
[213]43 procedure InitDataFields; virtual;
[214]44 procedure InitEditor; virtual;
[213]45
[240]46 procedure ReCreateFile(fs: TStream);
47
48 procedure CacheFile;
49 procedure CacheRawFile(Offset: Integer);
[245]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;
[240]57
[210]58 property FileStream: TMemoryStream read FFileStream;
[231]59 property FileInfo: TFileInfo read FFileInfo;
[206]60
[212]61 property Cached: Boolean read FCached;
62 property Changed: Boolean read FChanged write FChanged;
63
[240]64 property Editor: TFrame read GetEditor;
65 property Opened: Boolean read FOpened write SetOpened;
[214]66
[126]67 property FieldByOffset[Offset: Integer]: TDataField read GetFieldByOffset;
[124]68 end;
69
[248]70type
71 TFileType = class of TResource;
[124]72
[207]73
74
[124]75implementation
76
[126]77uses
[241]78 DatLinks, RawList, ConnectionManager, Dialogs, _MetaTypes, _MetaManager,
79 SysUtils;
[126]80
[124]81{ TFileType }
82
[248]83procedure TResource.CacheFile;
[240]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
[248]93procedure TResource.CacheRawFile(Offset: Integer);
[240]94var
95 i: Integer;
96begin
[245]97 for i := 0 to High(FRawCaches) do
[240]98 begin
[245]99 if FRawCaches[i].DatOffset = Offset then
[240]100 begin
[245]101 if not Assigned(FRawCaches[i].RawStream) then
[240]102 begin
[245]103 FRawCaches[i].RawStream := TMemoryStream.Create;
104 ConManager.Connection[ConnectionID].LoadRawFile(FFileInfo.ID, Offset, TStream(FRawCaches[i].RawStream));
[240]105 end;
106 end;
107 end;
108end;
109
[248]110procedure TResource.CacheAllRawFiles;
[245]111var
112 i: Integer;
113begin
114 for i := 0 to High(FRawCaches) do
115 CacheRawFile(FRawCaches[i].DatOffset);
116end;
[240]117
[245]118
119
[248]120procedure TResource.DiscardFileCache;
[245]121begin
122 if Assigned(FFileStream) then
123 FFileStream.Free;
124 FFileStream := nil;
125end;
126
[248]127procedure TResource.DiscardRawCache(Offset: Integer);
[245]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
[248]142procedure TResource.DiscardAllRawCaches;
[245]143var
144 i: Integer;
145begin
146 for i := 0 to High(FRawCaches) do
147 DiscardRawCache(FRawCaches[i].DatOffset);
148end;
149
[248]150procedure TResource.StoreFileCache;
[245]151begin
152 Exit;
153end;
154
[248]155procedure TResource.StoreRawCache(Offset: Integer);
[245]156begin
157 Exit;
158end;
159
[248]160procedure TResource.StoreAllRawCaches;
[245]161var
162 i: Integer;
163begin
164 for i := 0 to High(FRawCaches) do
165 StoreRawCache(FRawCaches[i].DatOffset);
166end;
167
168
169
170
171
[248]172procedure TResource.SetOpened(Opened: Boolean);
[240]173begin
174 FOpened := Opened;
175end;
176
177
[248]178constructor TResource.Create(ConnectionID, FileID: Integer);
[245]179var
180 rawlist: TRawDataList;
181 i: Integer;
[126]182begin
183 FConnectionID := ConnectionID;
[231]184 FFileInfo := ConManager.Connection[ConnectionID].GetFileInfo(FileID);
[126]185
[245]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;
[240]190
[212]191 FCached := False;
192 FChanged := False;
[206]193
[238]194 FDataFields := nil;
195 FEditor := nil;
196
[212]197 if not (Self is TFile_Empty) then
[233]198 InitDataFields;
[126]199end;
200
201
[248]202procedure TResource.Free;
[206]203begin
204 FDataFields.Free;
205end;
206
[126]207
[248]208function TResource.GetFieldByOffset(Offset: Integer): TDataField;
[124]209begin
[206]210 Result := FDataFields.FieldByOffset[Offset];
[124]211end;
212
[126]213
[248]214function TResource.GetRawCacheID(Offset: Integer): Integer;
[245]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
[248]224function TResource.GetType: String;
[241]225begin
226 Result := FFileInfo.Extension;
227end;
228
[248]229function TResource.GetCaption: String;
[212]230begin
[233]231 Result := FFileInfo.Name;
232 if Result = '' then
233 Result := 'Unnamed';
[241]234 Result := Result + ' (' + IntToStr(FFileInfo.ID) + ')';
[212]235end;
236
[248]237function TResource.GetChild(ID: Integer): TTreeElement;
[232]238begin
[233]239 Result := FDataFields.Child[ID];
[232]240end;
241
[248]242function TResource.GetChildCount: Integer;
[126]243begin
[233]244 if FDataFields <> nil then
[238]245 Result := FDataFields.ChildCount
246 else
247 Result := 0;
[126]248end;
249
250
[248]251function TResource.GetEditor: TFrame;
[240]252begin
253 if not Assigned(FEditor) then
254 InitEditor;
255 Result := FEditor;
256end;
257
258
[248]259procedure TResource.InitDataFields;
[213]260begin
261 if Assigned(FDataFields) then
262 Exit;
[233]263 FFileStream := TMemoryStream.Create;
264 ConManager.Connection[FConnectionID].LoadDatFile(FFileInfo.ID, TStream(FFileStream));
[213]265end;
266
[215]267
[248]268procedure TResource.InitEditor;
[214]269begin
270 Exit;
271end;
272
[215]273
[240]274//*********************************************************
275//*********************************************************
276//*********************************************************
[248]277procedure TResource.ReCreateFile(fs: TStream);
[240]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
[124]296end.
Note: See TracBrowser for help on using the repository browser.