Debian Codegraph @

src/files/castleinternaldirectoryinformation.pas

raw
1 {
2 Copyright 2019-2019 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Cached information about a directory (@link(TDirectoryInformation))
17 and in particular about the current castle-data:/ contents (@link(DataDirectoryInformation)). }
18 unit CastleInternalDirectoryInformation;
19
20 {$I castleconf.inc}
21
22 interface
23
24 uses DOM, Generics.Collections,
25 CastleFindFiles, CastleUtils;
26
27 type
28 { Generate, load, save to file an information about files under given path.
29 This is useful to store CastleDataInformation.xml file,
30 that can be used to perform FindFiles on platforms/filesystems that don't support
31 recursive searching for files/directories (like Android "assets"). }
32 TDirectoryInformation = class
33 public
34 type
35 TFile = class;
36 TDirectory = class;
37 TFiles = {$ifdef FPC}specialize{$endif} TObjectList<TFile>;
38 TDirectories = {$ifdef FPC}specialize{$endif} TObjectList<TDirectory>;
39
40 TEntry = class
41 private
42 procedure LoadFromFile(const Element: TDOMElement); virtual; abstract;
43 procedure SaveToFile(const ParentElement: TDOMElement); virtual; abstract;
44 procedure Sum(var DirsCount, FilesCount, FilesSize: QWord); virtual; abstract;
45 public
46 Name: String;
47 end;
48
49 TDirectory = class(TEntry)
50 strict private
51 procedure AddEntry(const FileInfo: TFileInfo; var StopSearch: boolean);
52 private
53 procedure Generate(const PathUrl: String);
54 procedure LoadFromFile(const Element: TDOMElement); override;
55 procedure SaveToFile(const ParentElement: TDOMElement); override;
56 procedure Sum(var DirsCount, FilesCount, FilesSize: QWord); override;
57 { Find file or directory (direct children, not recursive) of given name.
58 @nil if not found. }
59 function FindEntry(const AName: String): TEntry;
60 public
61 Directories: TDirectories;
62 Files: TFiles;
63 constructor Create;
64 destructor Destroy; override;
65 end;
66
67 TFile = class(TEntry)
68 private
69 procedure SaveToFile(const ParentElement: TDOMElement); override;
70 procedure LoadFromFile(const Element: TDOMElement); override;
71 procedure Sum(var DirsCount, FilesCount, FilesSize: QWord); override;
72 public
73 Size: QWord;
74 end;
75
76 var
77 RootDirectory: TDirectory;
78
79 destructor Destroy; override;
80 procedure Generate(const PathUrl: String);
81 procedure LoadFromFile(const Url: String);
82 procedure SaveToFile(const Url: String);
83 procedure Sum(out DirsCount, FilesCount, FilesSize: QWord);
84
85 { Find entry (file or directory) designated by given relative URL,
86 like 'subdir1/subdir2/my_file.txt'.
87 @nil if not found. }
88 function FindEntry(const Url: String): TEntry;
89 end;
90
91 var
92 { When <> 0, FindFiles and URIFileExists and URIExists don't look at cached
93 castle-data:/ contents in DataDirectoryInformation. }
94 DisableDataDirectoryInformation: Integer;
95
96 function DataDirectoryInformation: TDirectoryInformation;
97
98 implementation
99
100 uses SysUtils,
101 CastleXmlUtils, CastleUriUtils, CastleStringUtils, CastleLog;
102
103 { TDirectory ----------------------------------------------------------------- }
104
105 procedure TDirectoryInformation.TDirectory.AddEntry(
106 const FileInfo: TFileInfo; var StopSearch: boolean);
107 var
108 D: TDirectory;
109 F: TFile;
110 E: TEntry;
111 begin
112 if FileInfo.Directory then
113 begin
114 D := TDirectory.Create;
115 D.Generate(FileInfo.Url);
116 Directories.Add(D);
117 E := D;
118 end else
119 begin
120 F := TFile.Create;
121 F.Size := FileInfo.Size;
122 Files.Add(F);
123 E := F;
124 end;
125 E.Name := FileInfo.Name;
126 end;
127
128 procedure TDirectoryInformation.TDirectory.Generate(const PathUrl: String);
129 begin
130 Directories.Clear;
131 Files.Clear;
132 FindFiles(PathUrl, '*', true, {$ifdef FPC}@{$endif} AddEntry, []);
133 end;
134
135 constructor TDirectoryInformation.TDirectory.Create;
136 begin
137 inherited;
138 Directories := TDirectories.Create(true);
139 Files := TFiles.Create(true);
140 end;
141
142 destructor TDirectoryInformation.TDirectory.Destroy;
143 begin
144 FreeAndNil(Files);
145 FreeAndNil(Directories);
146 inherited;
147 end;
148
149 procedure TDirectoryInformation.TDirectory.LoadFromFile(const Element: TDOMElement);
150 var
151 D: TDirectory;
152 F: TFile;
153 I: TXMLElementIterator;
154 begin
155 Name := Element.AttributeStringDef('name', '');
156
157 I := Element.ChildrenIterator('directory');
158 try
159 while I.GetNext do
160 begin
161 D := TDirectory.Create;
162 Directories.Add(D);
163 D.LoadFromFile(I.Current);
164 end;
165 finally FreeAndNil(I) end;
166
167 I := Element.ChildrenIterator('file');
168 try
169 while I.GetNext do
170 begin
171 F := TFile.Create;
172 Files.Add(F);
173 F.LoadFromFile(I.Current);
174 end;
175 finally FreeAndNil(I) end;
176 end;
177
178 procedure TDirectoryInformation.TDirectory.SaveToFile(const ParentElement: TDOMElement);
179 var
180 Element: TDOMElement;
181 F: TFile;
182 D: TDirectory;
183 begin
184 Element := ParentElement.CreateChild('directory');
185 if Name <> '' then
186 Element.AttributeSet('name', Name);
187 for D in Directories do
188 D.SaveToFile(Element);
189 for F in Files do
190 F.SaveToFile(Element);
191 end;
192
193 procedure TDirectoryInformation.TDirectory.Sum(var DirsCount, FilesCount, FilesSize: QWord);
194 var
195 F: TFile;
196 D: TDirectory;
197 begin
198 Inc(DirsCount);
199 for D in Directories do
200 D.Sum(DirsCount, FilesCount, FilesSize);
201 for F in Files do
202 F.Sum(DirsCount, FilesCount, FilesSize);
203 end;
204
205 function TDirectoryInformation.TDirectory.FindEntry(const AName: String): TEntry;
206 var
207 F: TFile;
208 D: TDirectory;
209 begin
210 for D in Directories do
211 if SameFileName(D.Name, AName) then
212 Exit(D);
213 for F in Files do
214 if SameFileName(F.Name, AName) then
215 Exit(F);
216 Result := nil;
217 end;
218
219 { TFile ---------------------------------------------------------------------- }
220
221 procedure TDirectoryInformation.TFile.LoadFromFile(const Element: TDOMElement);
222 begin
223 Name := Element.AttributeString('name');
224 Size := Element.AttributeQWord('size');
225 end;
226
227 procedure TDirectoryInformation.TFile.SaveToFile(const ParentElement: TDOMElement);
228 var
229 E: TDOMElement;
230 begin
231 E := ParentElement.CreateChild('file');
232 E.AttributeSet('name', Name);
233 E.AttributeSet('size', Size);
234 end;
235
236 procedure TDirectoryInformation.TFile.Sum(var DirsCount, FilesCount, FilesSize: QWord);
237 begin
238 Inc(FilesCount);
239 FilesSize := FilesSize + Size;
240 end;
241
242 { TDirectoryInformation ---------------------------------------------- }
243
244 destructor TDirectoryInformation.Destroy;
245 begin
246 FreeAndNil(RootDirectory);
247 inherited;
248 end;
249
250 procedure TDirectoryInformation.Generate(const PathUrl: String);
251 begin
252 FreeAndNil(RootDirectory);
253 RootDirectory := TDirectory.Create;
254 // RootDirectory.Name deliberately left empty, don't save
255
256 Inc(DisableDataDirectoryInformation);
257 try
258 RootDirectory.Generate(PathUrl);
259 finally Dec(DisableDataDirectoryInformation) end;
260 end;
261
262 procedure TDirectoryInformation.LoadFromFile(const Url: String);
263 var
264 Doc: TXMLDocument;
265 DirectoryElement: TDOMElement;
266 begin
267 FreeAndNil(RootDirectory);
268
269 Doc := UrlReadXML(Url);
270
271 if Doc.DocumentElement.TagName <> 'directory_information' then
272 raise Exception.Create('Cannot load TDirectoryInformation from file, XML root element must be <directory_information>');
273
274 DirectoryElement := Doc.DocumentElement.Child('directory');
275 RootDirectory := TDirectory.Create;
276 RootDirectory.LoadFromFile(DirectoryElement);
277 end;
278
279 procedure TDirectoryInformation.SaveToFile(const Url: String);
280 var
281 Doc: TXMLDocument;
282 RootElement: TDOMElement;
283 Comment: TDOMComment;
284 begin
285 if RootDirectory = nil then
286 raise Exception.Create('Cannot save TDirectoryInformation to file, no information loaded');
287
288 Doc := TXMLDocument.Create;
289
290 Comment := Doc.CreateComment(
291 'DO NOT EDIT THIS FILE MANUALLY.' + NL +
292 'DO NOT COMMIT THIS FILE TO THE VERSION CONTROL.' + NL +
293 NL +
294 'This file is automatically generated to describe the directory contents.' + NL +
295 'This way we can access the directory information quicker,' + NL +
296 'and we can search data files regardless if the OS API allows it.' + NL);
297 Doc.AppendChild(Comment);
298
299 RootElement := Doc.CreateElement('directory_information');
300 Doc.AppendChild(RootElement);
301
302 RootDirectory.SaveToFile(RootElement);
303
304 UrlWriteXML(Doc, Url);
305 end;
306
307 procedure TDirectoryInformation.Sum(out DirsCount, FilesCount, FilesSize: QWord);
308 begin
309 DirsCount := 0;
310 FilesCount := 0;
311 FilesSize := 0;
312 if RootDirectory <> nil then
313 RootDirectory.Sum(DirsCount, FilesCount, FilesSize);
314 end;
315
316 function TDirectoryInformation.FindEntry(const Url: String): TEntry;
317 var
318 SeekPos: Integer;
319 Token: String;
320 begin
321 if RootDirectory = nil then
322 raise Exception.Create('Cannot search for entries in TDirectoryInformation, no information loaded');
323
324 SeekPos := 1;
325 Result := RootDirectory;
326 repeat
327 // although URL should only contain /, we tolerate here Windows \ also
328 Token := NextToken(Url, SeekPos, ['/', '\']);
329 if Token = '' then Break;
330
331 if Result is TFile then
332 Exit(nil); // path prefix is a name of a file, we cannot enter subdirectory of a file
333 Result := TDirectory(Result).FindEntry(Token);
334 if Result = nil then
335 Exit; // path component doesn't exist
336 until false;
337 end;
338
339 { globals -------------------------------------------------------------------- }
340
341 var
342 { We checked existence of CastleDataInformation.xml,
343 and created and loaded FDataDirectoryInformation if it exists. }
344 FDataDirectoryInformationInitialized: Boolean;
345 FDataDirectoryInformation: TDirectoryInformation;
346
347 procedure InitializeDataDirectoryInformation;
348 var
349 DataInfoUrl, DataInfoProtocol: String;
350 DirsCount, FilesCount, FilesSize: QWord;
351 begin
352 DataInfoUrl := ResolveCastleDataUrl('castle-data:/auto_generated/CastleDataInformation.xml');
353 DataInfoProtocol := URIProtocol(DataInfoUrl);
354
355 if (DataInfoProtocol = 'file') or
356 (DataInfoProtocol = 'castle-nx-contents') or
357 (DataInfoProtocol = '') then
358 begin
359 { To avoid exceptions during debugging,
360 follow a different approach when castle-data:/ resolves to something that
361 has reliable URIFileExists implementation: check by URIFileExists. }
362 Inc(DisableDataDirectoryInformation);
363 try
364 if URIFileExists(DataInfoUrl) then
365 begin
366 FDataDirectoryInformation := TDirectoryInformation.Create;
367 FDataDirectoryInformation.LoadFromFile(DataInfoUrl);
368 end else
369 FDataDirectoryInformation := nil;
370 finally Dec(DisableDataDirectoryInformation) end;
371 end else
372 begin
373 { With other protocols, the only way is to try opening
374 DataInfoUrl and see whether there's exception. }
375 FDataDirectoryInformation := TDirectoryInformation.Create;
376 try
377 FDataDirectoryInformation.LoadFromFile(DataInfoUrl);
378 except
379 on E: Exception do
380 begin
381 WritelnLog('Cannot read CastleDataInformation.xml: ' + ExceptMessage(E));
382 FreeAndNil(FDataDirectoryInformation);
383 end;
384 end;
385 end;
386
387 if FDataDirectoryInformation <> nil then
388 begin
389 FDataDirectoryInformation.Sum(DirsCount, FilesCount, FilesSize);
390 WritelnLog(Format('Loaded CastleDataInformation.xml information. Data contains %d directories, %d files, total (uncompressed) size %s.',
391 [DirsCount, FilesCount, SizeToStr(FilesSize)]));
392 end;
393 end;
394
395 function DataDirectoryInformation: TDirectoryInformation;
396 begin
397 if not FDataDirectoryInformationInitialized then
398 begin
399 InitializeDataDirectoryInformation;
400 FDataDirectoryInformationInitialized := true;
401 end;
402
403 Result := FDataDirectoryInformation;
404 end;
405
406 initialization
407 finalization
408 FreeAndNil(FDataDirectoryInformation);
409 end.