Debian Codegraph @

src/images/castleinternalautogenerated.pas

raw
1 {
2 Copyright 2018-2022 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 { Information about auto-generated resources (TAutoGenerated). }
17 unit CastleInternalAutoGenerated;
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses Generics.Collections, DOM,
24 CastleImages, CastleUtils;
25
26 type
27 { Information about auto-generated resources, right now: auto-generated
28 compressed and downscaled textures.
29 See https://castle-engine.io/creating_data_auto_generated_textures.php .
30 This information is read and written by the CGE build tool.
31 It may also be accessed during the game by the engine, automatically
32 (e.g. by CastleInternalMaterialProperties). }
33 TAutoGenerated = class
34 public
35 type
36 TGeneratedTexture = class
37 { URL of the texture.
38 Relative to the CastleAutoGenerated.xml file,
39 which also always means: relative to the game data directory. }
40 Url: String;
41
42 Compressed: Boolean;
43 Compression: TTextureCompression; //< only relevant if Compressed
44 Scale: Cardinal;
45
46 { Platforms for which this is created and used.
47 If the current @link(Platform) isn't included in this set,
48 then the file may not exist, because "castle-engine package"
49 could exclude it from distributing for this platform. }
50 Platforms: TCastlePlatforms;
51
52 { Flag used at generation by ToolTextureGeneration to indicate that
53 this generated texture is still mentioned in material_properties.xml
54 (e.g. the original was not excluded, or given compression/scale was not excluded). }
55 Used: Boolean;
56
57 procedure Save(const ParentElement: TDOMElement);
58 procedure Load(const E: TDOMElement);
59 end;
60
61 TGeneratedTextureList = {$ifdef FPC}specialize{$endif} TObjectList<TGeneratedTexture>;
62
63 TTexture = class
64 private
65 { Generated output files for this texture. }
66 FGenerated: TGeneratedTextureList;
67 public
68 { URL of the texture.
69 Relative to the CastleAutoGenerated.xml file,
70 which also always means: relative to the game data directory. }
71 Url: String;
72
73 { The hash associated with each resource URL,
74 used to detect whether the auto-generated things need updating. }
75 Hash: string;
76
77 { Dimensions of the texture, just like for TCastleImage.
78
79 These are dimensions before eventual downscaling (downscaling which may be requested
80 by data/material_properties.xml and done by "castle-engine auto-generate-texture",
81 see https://castle-engine.io/creating_data_auto_generated_textures.php ). }
82 Width, Height, Depth: Cardinal;
83
84 { Platforms for which the original texture is packaged. }
85 OriginalPlatforms: TCastlePlatforms;
86
87 { Flag used at generation by ToolTextureGeneration to indicate that
88 this generated texture is still mentioned in material_properties.xml
89 (e.g. the original was not excluded, or given compression/scale was not excluded). }
90 Used: Boolean;
91
92 { How many mipmaps should be generated in all @link(Generated) of this texture. }
93 MipmapsLevel: Cardinal;
94
95 constructor Create;
96 destructor Destroy; override;
97
98 { Creates a new TGeneratedTexture for this compression and scale, if necessary. }
99 function Generated(const Compressed: Boolean; const Compression: TTextureCompression;
100 const Scale: Cardinal): TGeneratedTexture;
101 procedure Save(const ParentElement: TDOMElement);
102 procedure Load(const E: TDOMElement);
103 end;
104 private
105 type
106 TTextureList = {$ifdef FPC}specialize{$endif} TObjectList<TTexture>;
107 var
108 Textures: TTextureList;
109 public
110 const
111 FileName = 'CastleAutoGenerated.xml';
112 AutoGeneratedDirName = 'auto_generated';
113
114 constructor Create;
115 destructor Destroy; override;
116
117 { Load information from CastleAutoGenerated.xml file.
118 Loading non-existing URL is allowed and ignored
119 (it is equivalent to loading an empty file, so it will clear current knowledge). }
120 procedure LoadFromFile(const Url: String);
121 procedure SaveToFile(const Url: String);
122
123 { Remove the entries for no longer used files (original and generated textures).
124 This cleans up the CastleAutoGenerated.xml from files that have been excluded,
125 or no longer exist, or their compression/scale have changed and thus they
126 are no longer used by the material_properties.xml.
127
128 Relies on TTexture.Used, TGeneratedTexture.Used flags set. }
129 procedure CleanUnused(const Verbose: boolean);
130
131 { Remove the entries for no longer existing input files.
132 TODO: In the future, it should also remove the auto-generated output
133 for the no-longer-existing input files. }
134 procedure CleanNotExistingFiles(const ProjectDataUrl: String; const Verbose: boolean);
135
136 { Get existing, or create new, Texture for this Url.
137
138 Given Url must be relative to the data directory.
139 Inside CastleAutoGenerated.xml we only store information about files
140 in the data directory.
141 If necessary use @link(RelativeToCastleDataURL) to transform it.
142
143 The OriginalPlatforms value will be stored in CastleAutoGenerated.xml,
144 if we will create the TTexture instance by this method.
145 When CreateIfNotExists = false, then OriginalPlatforms is always ignored. }
146 function Texture(const Url: String; const OriginalPlatforms: TCastlePlatforms;
147 const CreateIfNotExists: Boolean): TTexture;
148
149 { According to the CastleAutoGenerated.xml information, should the given data file
150 be distrubuted for given platform? }
151 function IncludeDataForPlatform(const TargetPlatform: TCastlePlatform;
152 const UrlInData: String): Boolean;
153
154 { Is file at given URL used by the auto-generated mechanism.
155 Given Url must be relative to the data directory. }
156 function Used(const Url: String): Boolean;
157 end;
158
159 function PlatformsToStr(const Platforms: TCastlePlatforms): String;
160 function StrToPlatforms(const S: String): TCastlePlatforms;
161
162 implementation
163
164 uses SysUtils,
165 CastleXmlUtils, CastleUriUtils, CastleStringUtils, CastleLog;
166
167 function PlatformsToStr(const Platforms: TCastlePlatforms): String;
168 var
169 P: TCastlePlatform;
170 begin
171 { Special result when all platforms are included.
172 This way when adding new platforms in the future,
173 they'll automatically be part of this. }
174 if Platforms = AllPlatforms then
175 Exit('all');
176
177 Result := '';
178 for P in Platforms do
179 Result := SAppendPart(Result, ';', PlatformToStr(P));
180 end;
181
182 function StrToPlatforms(const S: String): TCastlePlatforms;
183 var
184 PlatformsStr: TCastleStringList;
185 PlatformStr: String;
186 begin
187 if S = 'all' then
188 Exit(AllPlatforms);
189
190 Result := [];
191
192 if S <> '' then // S = '' means "no platforms"
193 begin
194 PlatformsStr := SplitString(S, ';');
195 try
196 for PlatformStr in PlatformsStr do
197 Include(Result, StrToPlatform(PlatformStr));
198 finally FreeAndNil(PlatformsStr) end;
199 end;
200 end;
201
202 { TAutoGenerated.TGeneratedTexture ------------------------------------------- }
203
204 procedure TAutoGenerated.TGeneratedTexture.Save(const ParentElement: TDOMElement);
205 var
206 E: TDOMElement;
207 begin
208 E := ParentElement.CreateChild('generated');
209 E.AttributeSet('url', Url);
210 E.AttributeSet('compressed', Compressed);
211 if Compressed then
212 E.AttributeSet('compression', TextureCompressionToString(Compression));
213 E.AttributeSet('scale', Scale);
214 E.AttributeSet('platforms', PlatformsToStr(Platforms));
215 end;
216
217 procedure TAutoGenerated.TGeneratedTexture.Load(const E: TDOMElement);
218 begin
219 Url := E.AttributeString('url');
220 Compressed := E.AttributeBoolean('compressed');
221 if Compressed then
222 Compression := StringToTextureCompression(E.AttributeString('compression'));
223 Scale := E.AttributeCardinal('scale');
224 Platforms := StrToPlatforms(E.AttributeString('platforms'));
225 end;
226
227 { TAutoGenerated.TTexture ---------------------------------------------------- }
228
229 constructor TAutoGenerated.TTexture.Create;
230 begin
231 FGenerated := TGeneratedTextureList.Create(true);
232 inherited;
233 end;
234
235 destructor TAutoGenerated.TTexture.Destroy;
236 begin
237 FreeAndNil(FGenerated);
238 inherited;
239 end;
240
241 procedure TAutoGenerated.TTexture.Save(const ParentElement: TDOMElement);
242 var
243 E: TDOMElement;
244 G: TGeneratedTexture;
245 begin
246 E := ParentElement.CreateChild('texture');
247 E.AttributeSet('url', Url);
248 E.AttributeSet('last_processed_content_hash', Hash);
249 E.AttributeSet('width', Width);
250 E.AttributeSet('height', Height);
251 E.AttributeSet('depth', Depth);
252 E.AttributeSet('original_platforms', PlatformsToStr(OriginalPlatforms));
253 if MipmapsLevel <> 0 then
254 E.AttributeSet('mipmaps_level', MipmapsLevel);
255
256 for G in FGenerated do
257 G.Save(E);
258 end;
259
260 procedure TAutoGenerated.TTexture.Load(const E: TDOMElement);
261 var
262 I: TXMLElementIterator;
263 G: TGeneratedTexture;
264 begin
265 Url := E.AttributeString('url');
266 Hash := E.AttributeString('last_processed_content_hash');
267 Width := E.AttributeCardinalDef('width', 0);
268 Height := E.AttributeCardinalDef('height', 0);
269 Depth := E.AttributeCardinalDef('depth', 0);
270 MipmapsLevel := E.AttributeCardinalDef('mipmaps_level', 0);
271 if E.HasAttribute('original_platforms') then
272 OriginalPlatforms := StrToPlatforms(E.AttributeString('original_platforms'))
273 else
274 begin
275 // DEPRECATED!
276 WriteLnWarning('Autogenerated texture info is missing "original_platforms" attribute. '+'It means CastleAutoGenerated.xml was build by an old version of Castle Game Engine. Currently this behavior is deprecated. Consider deleting the old versions and regenerating all of the auto-generated textures. Assuming OriginalPlatforms = AllPlatforms.');
277 OriginalPlatforms := AllPlatforms;
278 end;
279
280 FGenerated.Clear;
281 I := E.ChildrenIterator('generated');
282 try
283 while I.GetNext do
284 begin
285 G := TGeneratedTexture.Create;
286 FGenerated.Add(G);
287 G.Load(I.Current);
288 end;
289 finally FreeAndNil(I) end;
290 end;
291
292 function TAutoGenerated.TTexture.Generated(const Compressed: Boolean; const Compression: TTextureCompression;
293 const Scale: Cardinal): TGeneratedTexture;
294 var
295 G: TGeneratedTexture;
296 begin
297 for G in FGenerated do
298 begin
299 if (G.Compressed = Compressed) and
300 (G.Compression = Compression) and
301 (G.Scale = Scale) then
302 Exit(G);
303 end;
304
305 Result := TGeneratedTexture.Create;
306 FGenerated.Add(Result);
307 Result.Compressed := Compressed;
308 Result.Compression := Compression;
309 Result.Scale := Scale;
310 end;
311
312 { TAutoGenerated ------------------------------------------------------------- }
313
314 constructor TAutoGenerated.Create;
315 begin
316 inherited;
317 Textures := TTextureList.Create;
318 end;
319
320 destructor TAutoGenerated.Destroy;
321 begin
322 FreeAndNil(Textures);
323 inherited;
324 end;
325
326 function TAutoGenerated.Texture(const Url: String; const OriginalPlatforms: TCastlePlatforms;
327 const CreateIfNotExists: Boolean): TTexture;
328 begin
329 for Result in Textures do
330 if Result.Url = Url then
331 Exit;
332
333 if CreateIfNotExists then
334 begin
335 Result := TTexture.Create;
336 Result.Url := Url;
337 Result.OriginalPlatforms := OriginalPlatforms;
338 Textures.Add(Result);
339 end else
340 Result := nil;
341 end;
342
343 procedure TAutoGenerated.SaveToFile(const Url: String);
344 var
345 Doc: TXMLDocument;
346 RootElement, TexturesElement: TDOMElement;
347 T: TTexture;
348 Comment: TDOMComment;
349 begin
350 Doc := TXMLDocument.Create;
351 try
352 Comment := Doc.CreateComment(
353 'DO NOT EDIT THIS FILE MANUALLY.' + NL +
354 'It is automatically created/updated.' + NL +
355 NL +
356 'This file describes the state of files inside auto_generated' + NL +
357 'subdirectories of game data.' + NL +
358 'This file should be created and updated only by the Castle Game Engine' + NL +
359 'build tool, using commands like "castle-engie auto-generate-textures".' + NL +
360 'These commands update things inside auto_generated subdirectories,' + NL +
361 '*and* they update the ' + FileName + ' file.' + NL +
362 NL +
363 'If you transfer the auto_generated subdirectories (e.g. if you commit' + NL +
364 'them to the version control repository), then you should also' + NL +
365 'transfer (commit) the appropriate ' + FileName + ' file along.' + NL +
366 'Otherwise (e.g. if you ignore the auto_generated subdirectories' + NL +
367 'in version control), then ignore this file too.' + NL +
368 NL +
369 'Game code may assume that if some aute-generated files are present,' + NL +
370 'then the ''castle-data:/' + FileName + ''' file exists,' + NL +
371 'describing them.' + NL);
372 Doc.AppendChild(Comment);
373
374 RootElement := Doc.CreateElement('auto_generated');
375 Doc.AppendChild(RootElement);
376
377 TexturesElement := RootElement.CreateChild('textures');
378
379 for T in Textures do
380 T.Save(TexturesElement);
381
382 URLWriteXML(Doc, Url);
383 finally FreeAndNil(Doc) end;
384 end;
385
386 procedure TAutoGenerated.LoadFromFile(const Url: String);
387 var
388 Doc: TXMLDocument;
389 T: TTexture;
390 I: TXMLElementIterator;
391 begin
392 Textures.Clear;
393
394 { If the file does not exist yet,
395 then we have no information about auto-generated resources
396 (all last processed Hash is ''), which is OK. }
397 if not URIFileExists(Url) then
398 Exit;
399
400 Doc := URLReadXML(Url);
401 try
402 I := Doc.DocumentElement.Child('textures').ChildrenIterator('texture');
403 try
404 while I.GetNext do
405 begin
406 T := TTexture.Create;
407 Textures.Add(T);
408 T.Load(I.Current);
409 end;
410 finally FreeAndNil(I) end;
411 finally FreeAndNil(Doc) end;
412 end;
413
414 procedure TAutoGenerated.CleanUnused(const Verbose: boolean);
415 var
416 T: TTexture;
417 I, J: Integer;
418 begin
419 for I := Textures.Count - 1 downto 0 do
420 begin
421 if not Textures[I].Used then
422 begin
423 Writeln(Format(
424 'Original texture file "%s" is no longer affected by automatic texture processing.' + NL +
425 'Be sure to also remove any outputs for this texture inside the auto_generated directory (if exists). Using "castle-engine auto-generate-clean" should clean them.',
426 [Textures[I].Url]));
427 Textures.Delete(I);
428 end else
429 begin
430 T := Textures[I];
431 for J := T.FGenerated.Count - 1 downto 0 do
432 if not T.FGenerated[J].Used then
433 begin
434 Writeln(Format(
435 'Generated texture file "%s" is no longer affected by automatic texture processing.' + NL +
436 'Be sure to also remove this texture file (if exists). Using "castle-engine auto-generate-clean" should clean them.',
437 [T.FGenerated[J].Url]));
438 T.FGenerated.Delete(J);
439 end;
440 end;
441 end;
442 end;
443
444 procedure TAutoGenerated.CleanNotExistingFiles(const ProjectDataUrl: String;
445 const Verbose: boolean);
446 var
447 I: Integer;
448 T: TTexture;
449 TextureUrl, AutoGeneratedDirUrl: String;
450 begin
451 I := 0;
452 while I < Textures.Count do
453 begin
454 T := Textures[I];
455 TextureUrl := CombineURI(ProjectDataUrl, T.Url);
456 if not URIFileExists(TextureUrl) then
457 begin
458 AutoGeneratedDirUrl := ExtractURIPath(TextureUrl) + AutoGeneratedDirName + '/';
459 if Verbose then
460 Writeln(Format(
461 'Texture file "%s" no longer exists, removing the record about it from "%s".' + NL +
462 'Be sure to also remove the output for this texture inside the "%s" directory (if exists). Using "castle-engine auto-generate-clean" should clean them.',
463 [TextureUrl, FileName, AutoGeneratedDirUrl]));
464 Textures.Delete(I);
465 end else
466 Inc(I);
467 end;
468 end;
469
470 function TAutoGenerated.IncludeDataForPlatform(const TargetPlatform: TCastlePlatform;
471 const UrlInData: String): Boolean;
472 var
473 T: TTexture;
474 G: TGeneratedTexture;
475 begin
476 { Exclude this file, if it is autogenerated and it is not for TargetPlatform. }
477 for T in Textures do
478 begin
479 if (T.Url = UrlInData) and (not (TargetPlatform in T.OriginalPlatforms)) then
480 Exit(false);
481 for G in T.FGenerated do
482 begin
483 if (G.Url = UrlInData) and (not (TargetPlatform in G.Platforms)) then
484 Exit(false);
485 end;
486 end;
487 Result := true;
488 end;
489
490 function TAutoGenerated.Used(const Url: String): Boolean;
491 var
492 T: TTexture;
493 G: TGeneratedTexture;
494 begin
495 for T in Textures do
496 begin
497 if T.Url = Url then
498 Exit(true);
499 for G in T.FGenerated do
500 if G.Url = Url then
501 Exit(true);
502 end;
503 Result := false;
504 end;
505
506 end.