GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
ResourcesContainer.h
1 /*
2  * GDevelop Core
3  * Copyright 2008-2025 Florian Rival ([email protected]). All rights
4  * reserved. This project is released under the MIT License.
5  */
6 #pragma once
7 
8 #include <map>
9 #include <memory>
10 #include <vector>
11 
12 #include "GDCore/String.h"
13 
14 namespace gd {
15 class Project;
16 class SerializerElement;
17 class PropertyDescriptor;
18 } // namespace gd
19 
20 namespace gd {
21 
27 class GD_CORE_API Resource {
28 public:
29  static const gd::String imageType;
30  static const gd::String audioType;
31  static const gd::String fontType;
32  static const gd::String videoType;
33  static const gd::String jsonType;
34  static const gd::String tileMapType;
35  static const gd::String tileSetType;
36  static const gd::String bitmapType;
37  static const gd::String model3DType;
38  static const gd::String atlasType;
39  static const gd::String spineType;
40  static const gd::String javaScriptType;
41  static const gd::String internalInGameEditorOnlySvgType;
42 
43  Resource(){};
44  virtual ~Resource(){};
45  virtual Resource *Clone() const { return new Resource(*this); }
46 
49  virtual void SetName(const gd::String &name_) { name = name_; }
50 
53  virtual const gd::String &GetName() const { return name; }
54 
57  virtual void SetKind(const gd::String &newKind) { kind = newKind; }
58 
61  virtual const gd::String &GetKind() const { return kind; }
62 
65  virtual void SetUserAdded(bool isUserAdded) { userAdded = isUserAdded; }
66 
69  virtual bool IsUserAdded() const { return userAdded; }
70 
77  virtual bool UseFile() const { return false; }
78 
86  virtual const gd::String &GetFile() const { return badStr; };
87 
94  virtual void SetFile(const gd::String &newFile) {};
95 
99  virtual void SetOrigin(const gd::String &originName_,
100  const gd::String &originIdentifier_) {
101  originName = originName_;
102  originIdentifier = originIdentifier_;
103  }
104 
105  virtual const gd::String &GetOriginName() const { return originName; }
106  virtual const gd::String &GetOriginIdentifier() const {
107  return originIdentifier;
108  }
109 
115  virtual void SetMetadata(const gd::String &metadata_) {
116  metadata = metadata_;
117  }
118 
122  virtual const gd::String &GetMetadata() const { return metadata; }
123 
128 
143  virtual std::map<gd::String, gd::PropertyDescriptor> GetProperties() const;
144 
151  virtual bool UpdateProperty(const gd::String &name, const gd::String &value) {
152  return false;
153  };
155 
159  virtual void SerializeTo(SerializerElement &element) const {};
160 
164  virtual void UnserializeFrom(const SerializerElement &element) {};
165 
166 private:
167  gd::String kind;
168  gd::String name;
169  gd::String metadata;
170  gd::String originName;
171  gd::String originIdentifier;
172  bool userAdded = false;
174 
175  static gd::String badStr;
176 };
177 
184 class GD_CORE_API ImageResource : public Resource {
185 public:
186  ImageResource() : Resource(), smooth(true) { SetKind("image"); };
187  virtual ~ImageResource(){};
188  virtual ImageResource *Clone() const override {
189  return new ImageResource(*this);
190  }
191 
195  virtual const gd::String &GetFile() const override { return file; };
196 
200  virtual void SetFile(const gd::String &newFile) override;
201 
202  virtual bool UseFile() const override { return true; }
203 
204  std::map<gd::String, gd::PropertyDescriptor> GetProperties() const override;
205  bool UpdateProperty(const gd::String &name, const gd::String &value) override;
206 
210  void SerializeTo(SerializerElement &element) const override;
211 
215  void UnserializeFrom(const SerializerElement &element) override;
216 
220  bool IsSmooth() const { return smooth; }
221 
225  void SetSmooth(bool enable = true) { smooth = enable; }
226 
227  bool smooth;
228 private:
229  gd::String file;
230 };
231 
238 class GD_CORE_API AudioResource : public Resource {
239 public:
240  AudioResource()
241  : Resource(), preloadAsMusic(false), preloadAsSound(false),
242  preloadInCache(false) {
243  SetKind("audio");
244  };
245  virtual ~AudioResource(){};
246  virtual AudioResource *Clone() const override {
247  return new AudioResource(*this);
248  }
249 
250  virtual const gd::String &GetFile() const override { return file; };
251  virtual void SetFile(const gd::String &newFile) override;
252 
253  virtual bool UseFile() const override { return true; }
254 
255  std::map<gd::String, gd::PropertyDescriptor> GetProperties() const override;
256  bool UpdateProperty(const gd::String &name, const gd::String &value) override;
257 
258  void SerializeTo(SerializerElement &element) const override;
259 
260  void UnserializeFrom(const SerializerElement &element) override;
261 
265  bool PreloadAsMusic() const { return preloadAsMusic; }
266 
270  void SetPreloadAsMusic(bool enable = true) { preloadAsMusic = enable; }
271 
275  bool PreloadAsSound() const { return preloadAsSound; }
276 
280  void SetPreloadAsSound(bool enable = true) { preloadAsSound = enable; }
281 
286  bool PreloadInCache() const { return preloadInCache; }
287 
292  void SetPreloadInCache(bool enable = true) { preloadInCache = enable; }
293 
294 private:
295  gd::String file;
296  bool preloadAsSound;
297  bool preloadAsMusic;
298  bool preloadInCache;
299 };
300 
307 class GD_CORE_API FontResource : public Resource {
308 public:
309  FontResource() : Resource() { SetKind("font"); };
310  virtual ~FontResource(){};
311  virtual FontResource *Clone() const override {
312  return new FontResource(*this);
313  }
314 
315  virtual const gd::String &GetFile() const override { return file; };
316  virtual void SetFile(const gd::String &newFile) override;
317 
318  virtual bool UseFile() const override { return true; }
319  void SerializeTo(SerializerElement &element) const override;
320 
321  void UnserializeFrom(const SerializerElement &element) override;
322 
323 private:
324  gd::String file;
325 };
326 
333 class GD_CORE_API VideoResource : public Resource {
334 public:
335  VideoResource() : Resource() { SetKind("video"); };
336  virtual ~VideoResource(){};
337  virtual VideoResource *Clone() const override {
338  return new VideoResource(*this);
339  }
340 
341  virtual const gd::String &GetFile() const override { return file; };
342  virtual void SetFile(const gd::String &newFile) override;
343 
344  virtual bool UseFile() const override { return true; }
345  void SerializeTo(SerializerElement &element) const override;
346 
347  void UnserializeFrom(const SerializerElement &element) override;
348 
349 private:
350  gd::String file;
351 };
352 
359 class GD_CORE_API JsonResource : public Resource {
360 public:
361  JsonResource() : Resource(), disablePreload(false) { SetKind("json"); };
362  virtual ~JsonResource(){};
363  virtual JsonResource *Clone() const override {
364  return new JsonResource(*this);
365  }
366 
367  virtual const gd::String &GetFile() const override { return file; };
368  virtual void SetFile(const gd::String &newFile) override;
369 
370  virtual bool UseFile() const override { return true; }
371 
372  std::map<gd::String, gd::PropertyDescriptor> GetProperties() const override;
373  bool UpdateProperty(const gd::String &name, const gd::String &value) override;
374 
375  void SerializeTo(SerializerElement &element) const override;
376 
377  void UnserializeFrom(const SerializerElement &element) override;
378 
382  bool IsPreloadDisabled() const { return disablePreload; }
383 
387  void DisablePreload(bool disable = true) { disablePreload = disable; }
388 
389 private:
390  bool disablePreload;
391  gd::String file;
392 };
393 
400 class GD_CORE_API SpineResource : public JsonResource {
401 public:
402  SpineResource() : JsonResource() { SetKind("spine"); };
403  virtual ~SpineResource(){};
404  virtual SpineResource *Clone() const override {
405  return new SpineResource(*this);
406  }
407 };
408 
415 class GD_CORE_API TilemapResource : public Resource {
416 public:
417  TilemapResource() : Resource(), disablePreload(false) { SetKind("tilemap"); };
418  virtual ~TilemapResource(){};
419  virtual TilemapResource *Clone() const override {
420  return new TilemapResource(*this);
421  }
422 
423  virtual const gd::String &GetFile() const override { return file; };
424  virtual void SetFile(const gd::String &newFile) override;
425 
426  virtual bool UseFile() const override { return true; }
427 
428  std::map<gd::String, gd::PropertyDescriptor> GetProperties() const override;
429  bool UpdateProperty(const gd::String &name, const gd::String &value) override;
430 
431  void SerializeTo(SerializerElement &element) const override;
432 
433  void UnserializeFrom(const SerializerElement &element) override;
434 
438  bool IsPreloadDisabled() const { return disablePreload; }
439 
443  void DisablePreload(bool disable = true) { disablePreload = disable; }
444 
445 private:
446  bool disablePreload;
447  gd::String file;
448 };
449 
456 class GD_CORE_API TilesetResource : public Resource {
457 public:
458  TilesetResource() : Resource(), disablePreload(false) { SetKind("tileset"); };
459  virtual ~TilesetResource(){};
460  virtual TilesetResource *Clone() const override {
461  return new TilesetResource(*this);
462  }
463 
464  virtual const gd::String &GetFile() const override { return file; };
465  virtual void SetFile(const gd::String &newFile) override;
466 
467  virtual bool UseFile() const override { return true; }
468 
469  std::map<gd::String, gd::PropertyDescriptor> GetProperties() const override;
470  bool UpdateProperty(const gd::String &name, const gd::String &value) override;
471 
472  void SerializeTo(SerializerElement &element) const override;
473 
474  void UnserializeFrom(const SerializerElement &element) override;
475 
479  bool IsPreloadDisabled() const { return disablePreload; }
480 
484  void DisablePreload(bool disable = true) { disablePreload = disable; }
485 
486 private:
487  bool disablePreload;
488  gd::String file;
489 };
490 
497 class GD_CORE_API BitmapFontResource : public Resource {
498 public:
499  BitmapFontResource() : Resource() { SetKind("bitmapFont"); };
500  virtual ~BitmapFontResource(){};
501  virtual BitmapFontResource *Clone() const override {
502  return new BitmapFontResource(*this);
503  }
504 
505  virtual const gd::String &GetFile() const override { return file; };
506  virtual void SetFile(const gd::String &newFile) override;
507 
508  virtual bool UseFile() const override { return true; }
509  void SerializeTo(SerializerElement &element) const override;
510 
511  void UnserializeFrom(const SerializerElement &element) override;
512 
513 private:
514  gd::String file;
515 };
516 
523 class GD_CORE_API Model3DResource : public Resource {
524 public:
525  Model3DResource() : Resource() { SetKind("model3D"); };
526  virtual ~Model3DResource(){};
527  virtual Model3DResource *Clone() const override {
528  return new Model3DResource(*this);
529  }
530 
531  virtual const gd::String &GetFile() const override { return file; };
532  virtual void SetFile(const gd::String &newFile) override;
533 
534  virtual bool UseFile() const override { return true; }
535  void SerializeTo(SerializerElement &element) const override;
536 
537  void UnserializeFrom(const SerializerElement &element) override;
538 
539 private:
540  gd::String file;
541 };
542 
549 class GD_CORE_API AtlasResource : public Resource {
550 public:
551  AtlasResource() : Resource() { SetKind("atlas"); };
552  virtual ~AtlasResource(){};
553  virtual AtlasResource *Clone() const override {
554  return new AtlasResource(*this);
555  }
556 
557  virtual const gd::String &GetFile() const override { return file; };
558  virtual void SetFile(const gd::String &newFile) override;
559 
560  virtual bool UseFile() const override { return true; }
561  void SerializeTo(SerializerElement &element) const override;
562 
563  void UnserializeFrom(const SerializerElement &element) override;
564 
565 private:
566  gd::String file;
567 };
568 
575 class GD_CORE_API JavaScriptResource : public Resource {
576 public:
577  JavaScriptResource() : Resource() { SetKind("javascript"); };
578  virtual ~JavaScriptResource(){};
579  virtual JavaScriptResource *Clone() const override {
580  return new JavaScriptResource(*this);
581  }
582 
583  virtual const gd::String &GetFile() const override { return file; };
584  virtual void SetFile(const gd::String &newFile) override;
585 
586  virtual bool UseFile() const override { return true; }
587  void SerializeTo(SerializerElement &element) const override;
588 
589  void UnserializeFrom(const SerializerElement &element) override;
590 
591 private:
592  gd::String file;
593 };
594 
602 class GD_CORE_API InternalInGameEditorOnlySvgResource : public Resource {
603  public:
604  InternalInGameEditorOnlySvgResource() : Resource() { SetKind("internal-in-game-editor-only-svg"); };
606  virtual InternalInGameEditorOnlySvgResource* Clone() const override {
607  return new InternalInGameEditorOnlySvgResource(*this);
608  }
609 
610  virtual const gd::String& GetFile() const override { return file; };
611  virtual void SetFile(const gd::String& newFile) override;
612 
613  virtual bool UseFile() const override { return true; }
614  void SerializeTo(SerializerElement& element) const override;
615 
616  void UnserializeFrom(const SerializerElement& element) override;
617 
618  private:
619  gd::String file;
620 };
621 
628 class GD_CORE_API ResourcesContainer {
629 public:
630  enum SourceType {
631  Unknown,
632  Global,
633  Parameters,
634  Properties
635  };
636 
637  ResourcesContainer(const SourceType sourceType);
638  virtual ~ResourcesContainer();
640  ResourcesContainer &operator=(const ResourcesContainer &rhs);
641 
642  SourceType GetSourceType() const { return sourceType; }
643 
647  bool HasResource(const gd::String &name) const;
648 
652  Resource &GetResource(const gd::String &name);
653 
657  const Resource &GetResource(const gd::String &name) const;
658 
663  const gd::String &
664  GetResourceNameWithOrigin(const gd::String &originName,
665  const gd::String &originIdentifier) const;
666 
671  const gd::String &GetResourceNameWithFile(const gd::String &file) const;
672 
677  std::vector<gd::String> FindFilesNotInResources(
678  const std::vector<gd::String> &filePathsToCheck) const;
679 
683  std::shared_ptr<Resource> CreateResource(const gd::String &kind);
684 
688  const std::vector<std::shared_ptr<Resource>> &GetAllResources() const {
689  return resources;
690  };
691 
695  std::vector<gd::String> GetAllResourceNames() const;
696 
700  std::shared_ptr<gd::Resource> GetResourceSPtr(const gd::String &name);
701 
705  std::size_t Count() const { return resources.size(); };
706 
710  inline void Clear() { resources.clear(); }
711 
717  bool AddResource(const gd::Resource &resource);
718 
722  bool AddResource(const gd::String &name, const gd::String &filename,
723  const gd::String &kind);
724 
728  void RemoveResource(const gd::String &name);
729 
733  void RenameResource(const gd::String &oldName, const gd::String &newName);
734 
738  std::size_t GetResourcePosition(const gd::String &name) const;
739 
743  bool MoveResourceUpInList(const gd::String &name);
744 
748  bool MoveResourceDownInList(const gd::String &name);
749 
753  void MoveResource(std::size_t oldIndex, std::size_t newIndex);
754 
758  void SerializeTo(SerializerElement &element) const;
759 
763  static void SerializeResourceTo(gd::Resource &resource,
764  SerializerElement &resourceElement);
765 
769  void UnserializeFrom(const SerializerElement &element);
770 
771 private:
772  void Init(const ResourcesContainer &other);
773 
774  SourceType sourceType = Unknown;
775 
776  std::vector<std::shared_ptr<Resource>> resources;
777 
778  static Resource badResource;
779  static gd::String badResourceName;
780 };
781 
782 } // namespace gd
Describe an atlas file used by a project.
Definition: ResourcesContainer.h:549
virtual const gd::String & GetFile() const override
Return, if applicable, the String containing the file used by the resource. The file is relative to t...
Definition: ResourcesContainer.h:557
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesContainer.h:560
Describe an audio file used by a project.
Definition: ResourcesContainer.h:238
void SetPreloadAsMusic(bool enable=true)
Set if the audio resource should be preloaded as music.
Definition: ResourcesContainer.h:270
bool PreloadAsSound() const
Return true if the audio resource should be preloaded as music.
Definition: ResourcesContainer.h:275
bool PreloadAsMusic() const
Return true if the audio resource should be preloaded as music.
Definition: ResourcesContainer.h:265
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesContainer.h:253
virtual const gd::String & GetFile() const override
Return, if applicable, the String containing the file used by the resource. The file is relative to t...
Definition: ResourcesContainer.h:250
void SetPreloadAsSound(bool enable=true)
Set if the audio resource should be preloaded as music.
Definition: ResourcesContainer.h:280
void SetPreloadInCache(bool enable=true)
Set if the audio resource should be preloaded in cache (without decoding into memory).
Definition: ResourcesContainer.h:292
bool PreloadInCache() const
Return true if the audio resource should be preloaded in cache (without decoding into memory).
Definition: ResourcesContainer.h:286
Describe a bitmap font file used by a project.
Definition: ResourcesContainer.h:497
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesContainer.h:508
virtual const gd::String & GetFile() const override
Return, if applicable, the String containing the file used by the resource. The file is relative to t...
Definition: ResourcesContainer.h:505
Describe a font file used by a project.
Definition: ResourcesContainer.h:307
virtual const gd::String & GetFile() const override
Return, if applicable, the String containing the file used by the resource. The file is relative to t...
Definition: ResourcesContainer.h:315
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesContainer.h:318
Describe an image/texture used by a project.
Definition: ResourcesContainer.h:184
virtual const gd::String & GetFile() const override
Definition: ResourcesContainer.h:195
bool IsSmooth() const
Return true if the image should be smoothed.
Definition: ResourcesContainer.h:220
void SetSmooth(bool enable=true)
Set if the image should be smoothed in game.
Definition: ResourcesContainer.h:225
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesContainer.h:202
bool smooth
True if smoothing filter is applied.
Definition: ResourcesContainer.h:227
Describe a SVG file used by a project. Currently only used in the in-game editor.
Definition: ResourcesContainer.h:602
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesContainer.h:613
virtual const gd::String & GetFile() const override
Return, if applicable, the String containing the file used by the resource. The file is relative to t...
Definition: ResourcesContainer.h:610
Describe a JavaScript file used by a project.
Definition: ResourcesContainer.h:575
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesContainer.h:586
virtual const gd::String & GetFile() const override
Return, if applicable, the String containing the file used by the resource. The file is relative to t...
Definition: ResourcesContainer.h:583
Describe a json file used by a project.
Definition: ResourcesContainer.h:359
virtual const gd::String & GetFile() const override
Return, if applicable, the String containing the file used by the resource. The file is relative to t...
Definition: ResourcesContainer.h:367
void DisablePreload(bool disable=true)
Set if the json preload at game startup must be disabled.
Definition: ResourcesContainer.h:387
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesContainer.h:370
bool IsPreloadDisabled() const
Return true if the loading at game startup must be disabled.
Definition: ResourcesContainer.h:382
Describe a 3D model file used by a project.
Definition: ResourcesContainer.h:523
virtual const gd::String & GetFile() const override
Return, if applicable, the String containing the file used by the resource. The file is relative to t...
Definition: ResourcesContainer.h:531
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesContainer.h:534
Base class to describe a resource used by a game.
Definition: ResourcesContainer.h:27
virtual void SerializeTo(SerializerElement &element) const
Serialize the object.
Definition: ResourcesContainer.h:159
virtual const gd::String & GetMetadata() const
Return the (optional) metadata associated to the resource.
Definition: ResourcesContainer.h:122
virtual void SetOrigin(const gd::String &originName_, const gd::String &originIdentifier_)
Definition: ResourcesContainer.h:99
virtual void SetUserAdded(bool isUserAdded)
Change if the resource is user added or not.
Definition: ResourcesContainer.h:65
virtual bool IsUserAdded() const
Return true if the resource was added by the user.
Definition: ResourcesContainer.h:69
virtual const gd::String & GetName() const
Return the name of the resource.
Definition: ResourcesContainer.h:53
virtual bool UseFile() const
Return true if the resource use a file.
Definition: ResourcesContainer.h:77
virtual bool UpdateProperty(const gd::String &name, const gd::String &value)
Called when the IDE wants to update a custom property of the resource.
Definition: ResourcesContainer.h:151
virtual void SetFile(const gd::String &newFile)
Change, if applicable, the file of the resource.
Definition: ResourcesContainer.h:94
virtual void SetName(const gd::String &name_)
Change the name of the resource with the name passed as parameter.
Definition: ResourcesContainer.h:49
virtual void SetKind(const gd::String &newKind)
Change the kind of the resource.
Definition: ResourcesContainer.h:57
virtual void SetMetadata(const gd::String &metadata_)
Set the metadata (any string) associated to the resource.
Definition: ResourcesContainer.h:115
virtual const gd::String & GetFile() const
Return, if applicable, the String containing the file used by the resource. The file is relative to t...
Definition: ResourcesContainer.h:86
virtual void UnserializeFrom(const SerializerElement &element)
Unserialize the object.
Definition: ResourcesContainer.h:164
virtual const gd::String & GetKind() const
Return the kind of the resource.
Definition: ResourcesContainer.h:61
Inventory all resources used by a project.
Definition: ResourcesContainer.h:628
void Clear()
Clear all variables of the container.
Definition: ResourcesContainer.h:710
const std::vector< std::shared_ptr< Resource > > & GetAllResources() const
Definition: ResourcesContainer.h:688
std::size_t Count() const
Return the number of variables.
Definition: ResourcesContainer.h:705
A generic container that can represent a value ( containing a string, double, bool or int),...
Definition: SerializerElement.h:37
Describe a spine json file used by a project.
Definition: ResourcesContainer.h:400
String represents an UTF8 encoded string.
Definition: String.h:33
Describe a tilemap file used by a project.
Definition: ResourcesContainer.h:415
void DisablePreload(bool disable=true)
Set if the tilemap preload at game startup must be disabled.
Definition: ResourcesContainer.h:443
virtual const gd::String & GetFile() const override
Return, if applicable, the String containing the file used by the resource. The file is relative to t...
Definition: ResourcesContainer.h:423
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesContainer.h:426
bool IsPreloadDisabled() const
Return true if the loading at game startup must be disabled.
Definition: ResourcesContainer.h:438
Describe a tileset file used by a project.
Definition: ResourcesContainer.h:456
void DisablePreload(bool disable=true)
Set if the tilemap preload at game startup must be disabled.
Definition: ResourcesContainer.h:484
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesContainer.h:467
bool IsPreloadDisabled() const
Return true if the loading at game startup must be disabled.
Definition: ResourcesContainer.h:479
virtual const gd::String & GetFile() const override
Return, if applicable, the String containing the file used by the resource. The file is relative to t...
Definition: ResourcesContainer.h:464
Describe a video file used by a project.
Definition: ResourcesContainer.h:333
virtual const gd::String & GetFile() const override
Return, if applicable, the String containing the file used by the resource. The file is relative to t...
Definition: ResourcesContainer.h:341
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesContainer.h:344
Definition: CommonTools.h:24