GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
ResourcesManager.h
1 /*
2  * GDevelop Core
3  * Copyright 2008-2016 Florian Rival ([email protected]). All rights
4  * reserved. This project is released under the MIT License.
5  */
6 #ifndef GDCORE_RESOURCESMANAGER_H
7 #define GDCORE_RESOURCESMANAGER_H
8 #include <map>
9 #include <memory>
10 #include <vector>
11 
12 #include "GDCore/String.h"
13 namespace gd {
14 class Project;
15 class ResourceFolder;
16 class SerializerElement;
17 class PropertyDescriptor;
18 } // namespace gd
19 
20 namespace gd {
21 
27 class GD_CORE_API Resource {
28  public:
29  Resource(){};
30  virtual ~Resource(){};
31  virtual Resource* Clone() const { return new Resource(*this); }
32 
35  virtual void SetName(const gd::String& name_) { name = name_; }
36 
39  virtual const gd::String& GetName() const { return name; }
40 
43  virtual void SetKind(const gd::String& newKind) { kind = newKind; }
44 
47  virtual const gd::String& GetKind() const { return kind; }
48 
51  virtual void SetUserAdded(bool isUserAdded) { userAdded = isUserAdded; }
52 
55  virtual bool IsUserAdded() const { return userAdded; }
56 
63  virtual bool UseFile() const { return false; }
64 
72  virtual const gd::String& GetFile() const { return badStr; };
73 
80  virtual void SetFile(const gd::String& newFile){};
81 
85  virtual void SetOrigin(const gd::String& originName_, const gd::String& originIdentifier_) {
86  originName = originName_;
87  originIdentifier = originIdentifier_;
88  }
89 
90  virtual const gd::String& GetOriginName() const { return originName; }
91  virtual const gd::String& GetOriginIdentifier() const { return originIdentifier; }
92 
98  virtual void SetMetadata(const gd::String& metadata_) {
99  metadata = metadata_;
100  }
101 
105  virtual const gd::String& GetMetadata() const { return metadata; }
106 
111 
126  virtual std::map<gd::String, gd::PropertyDescriptor> GetProperties() const;
127 
134  virtual bool UpdateProperty(const gd::String& name, const gd::String& value) {
135  return false;
136  };
138 
142  virtual void SerializeTo(SerializerElement& element) const {};
143 
147  virtual void UnserializeFrom(const SerializerElement& element){};
148 
149  private:
150  gd::String kind;
151  gd::String name;
152  gd::String metadata;
153  gd::String originName;
154  gd::String originIdentifier;
155  bool userAdded = false;
157 
158  static gd::String badStr;
159 };
160 
167 class GD_CORE_API ImageResource : public Resource {
168  public:
169  ImageResource() : Resource(), smooth(true) {
170  SetKind("image");
171  };
172  virtual ~ImageResource(){};
173  virtual ImageResource* Clone() const override {
174  return new ImageResource(*this);
175  }
176 
180  virtual const gd::String& GetFile() const override { return file; };
181 
185  virtual void SetFile(const gd::String& newFile) override;
186 
187  virtual bool UseFile() const override { return true; }
188 
189  std::map<gd::String, gd::PropertyDescriptor> GetProperties() const override;
190  bool UpdateProperty(const gd::String& name, const gd::String& value) override;
191 
195  void SerializeTo(SerializerElement& element) const override;
196 
200  void UnserializeFrom(const SerializerElement& element) override;
201 
205  bool IsSmooth() const { return smooth; }
206 
210  void SetSmooth(bool enable = true) { smooth = enable; }
211 
212  bool smooth;
213  private:
214  gd::String file;
215 };
216 
223 class GD_CORE_API AudioResource : public Resource {
224  public:
225  AudioResource() : Resource(), preloadAsMusic(false), preloadAsSound(false), preloadInCache(false) {
226  SetKind("audio");
227  };
228  virtual ~AudioResource(){};
229  virtual AudioResource* Clone() const override {
230  return new AudioResource(*this);
231  }
232 
233  virtual const gd::String& GetFile() const override { return file; };
234  virtual void SetFile(const gd::String& newFile) override;
235 
236  virtual bool UseFile() const override { return true; }
237 
238  std::map<gd::String, gd::PropertyDescriptor> GetProperties() const override;
239  bool UpdateProperty(const gd::String& name, const gd::String& value) override;
240 
241  void SerializeTo(SerializerElement& element) const override;
242 
243  void UnserializeFrom(const SerializerElement& element) override;
244 
248  bool PreloadAsMusic() const { return preloadAsMusic; }
249 
253  void SetPreloadAsMusic(bool enable = true) { preloadAsMusic = enable; }
254 
258  bool PreloadAsSound() const { return preloadAsSound; }
259 
263  void SetPreloadAsSound(bool enable = true) { preloadAsSound = enable; }
264 
268  bool PreloadInCache() const { return preloadInCache; }
269 
273  void SetPreloadInCache(bool enable = true) { preloadInCache = enable; }
274 
275  private:
276  gd::String file;
277  bool preloadAsSound;
278  bool preloadAsMusic;
279  bool preloadInCache;
280 };
281 
288 class GD_CORE_API FontResource : public Resource {
289  public:
290  FontResource() : Resource() { SetKind("font"); };
291  virtual ~FontResource(){};
292  virtual FontResource* Clone() const override {
293  return new FontResource(*this);
294  }
295 
296  virtual const gd::String& GetFile() const override { return file; };
297  virtual void SetFile(const gd::String& newFile) override;
298 
299  virtual bool UseFile() const override { return true; }
300  void SerializeTo(SerializerElement& element) const override;
301 
302  void UnserializeFrom(const SerializerElement& element) override;
303 
304  private:
305  gd::String file;
306 };
307 
314 class GD_CORE_API VideoResource : public Resource {
315  public:
316  VideoResource() : Resource() { SetKind("video"); };
317  virtual ~VideoResource(){};
318  virtual VideoResource* Clone() const override {
319  return new VideoResource(*this);
320  }
321 
322  virtual const gd::String& GetFile() const override { return file; };
323  virtual void SetFile(const gd::String& newFile) override;
324 
325  virtual bool UseFile() const override { return true; }
326  void SerializeTo(SerializerElement& element) const override;
327 
328  void UnserializeFrom(const SerializerElement& element) override;
329 
330  private:
331  gd::String file;
332 };
333 
340 class GD_CORE_API JsonResource : public Resource {
341  public:
342  JsonResource() : Resource(), disablePreload(false) { SetKind("json"); };
343  virtual ~JsonResource(){};
344  virtual JsonResource* Clone() const override {
345  return new JsonResource(*this);
346  }
347 
348  virtual const gd::String& GetFile() const override { return file; };
349  virtual void SetFile(const gd::String& newFile) override;
350 
351  virtual bool UseFile() const override { return true; }
352 
353  std::map<gd::String, gd::PropertyDescriptor> GetProperties() const override;
354  bool UpdateProperty(const gd::String& name, const gd::String& value) override;
355 
356  void SerializeTo(SerializerElement& element) const override;
357 
358  void UnserializeFrom(const SerializerElement& element) override;
359 
363  bool IsPreloadDisabled() const { return disablePreload; }
364 
368  void DisablePreload(bool disable = true) { disablePreload = disable; }
369 
370  private:
371  bool disablePreload;
372  gd::String file;
373 };
374 
381 class GD_CORE_API SpineResource : public JsonResource {
382  public:
383  SpineResource() : JsonResource() { SetKind("spine"); };
384  virtual ~SpineResource(){};
385  virtual SpineResource* Clone() const override {
386  return new SpineResource(*this);
387  }
388 };
389 
396 class GD_CORE_API TilemapResource : public Resource {
397  public:
398  TilemapResource() : Resource(), disablePreload(false) { SetKind("tilemap"); };
399  virtual ~TilemapResource(){};
400  virtual TilemapResource* Clone() const override {
401  return new TilemapResource(*this);
402  }
403 
404  virtual const gd::String& GetFile() const override { return file; };
405  virtual void SetFile(const gd::String& newFile) override;
406 
407  virtual bool UseFile() const override { return true; }
408 
409  std::map<gd::String, gd::PropertyDescriptor> GetProperties() const override;
410  bool UpdateProperty(const gd::String& name, const gd::String& value) override;
411 
412  void SerializeTo(SerializerElement& element) const override;
413 
414  void UnserializeFrom(const SerializerElement& element) override;
415 
419  bool IsPreloadDisabled() const { return disablePreload; }
420 
424  void DisablePreload(bool disable = true) { disablePreload = disable; }
425 
426  private:
427  bool disablePreload;
428  gd::String file;
429 };
430 
437 class GD_CORE_API TilesetResource : public Resource {
438  public:
439  TilesetResource() : Resource(), disablePreload(false) { SetKind("tileset"); };
440  virtual ~TilesetResource(){};
441  virtual TilesetResource* Clone() const override {
442  return new TilesetResource(*this);
443  }
444 
445  virtual const gd::String& GetFile() const override { return file; };
446  virtual void SetFile(const gd::String& newFile) override;
447 
448  virtual bool UseFile() const override { return true; }
449 
450  std::map<gd::String, gd::PropertyDescriptor> GetProperties() const override;
451  bool UpdateProperty(const gd::String& name, const gd::String& value) override;
452 
453  void SerializeTo(SerializerElement& element) const override;
454 
455  void UnserializeFrom(const SerializerElement& element) override;
456 
460  bool IsPreloadDisabled() const { return disablePreload; }
461 
465  void DisablePreload(bool disable = true) { disablePreload = disable; }
466 
467  private:
468  bool disablePreload;
469  gd::String file;
470 };
471 
478 class GD_CORE_API BitmapFontResource : public Resource {
479  public:
480  BitmapFontResource() : Resource() { SetKind("bitmapFont"); };
481  virtual ~BitmapFontResource(){};
482  virtual BitmapFontResource* Clone() const override {
483  return new BitmapFontResource(*this);
484  }
485 
486  virtual const gd::String& GetFile() const override { return file; };
487  virtual void SetFile(const gd::String& newFile) override;
488 
489  virtual bool UseFile() const override { return true; }
490  void SerializeTo(SerializerElement& element) const override;
491 
492  void UnserializeFrom(const SerializerElement& element) override;
493 
494  private:
495  gd::String file;
496 };
497 
504 class GD_CORE_API Model3DResource : public Resource {
505  public:
506  Model3DResource() : Resource() { SetKind("model3D"); };
507  virtual ~Model3DResource(){};
508  virtual Model3DResource* Clone() const override {
509  return new Model3DResource(*this);
510  }
511 
512  virtual const gd::String& GetFile() const override { return file; };
513  virtual void SetFile(const gd::String& newFile) override;
514 
515  virtual bool UseFile() const override { return true; }
516  void SerializeTo(SerializerElement& element) const override;
517 
518  void UnserializeFrom(const SerializerElement& element) override;
519 
520  private:
521  gd::String file;
522 };
523 
530 class GD_CORE_API AtlasResource : public Resource {
531  public:
532  AtlasResource() : Resource() { SetKind("atlas"); };
533  virtual ~AtlasResource(){};
534  virtual AtlasResource* Clone() const override {
535  return new AtlasResource(*this);
536  }
537 
538  virtual const gd::String& GetFile() const override { return file; };
539  virtual void SetFile(const gd::String& newFile) override;
540 
541  virtual bool UseFile() const override { return true; }
542  void SerializeTo(SerializerElement& element) const override;
543 
544  void UnserializeFrom(const SerializerElement& element) override;
545 
546  private:
547  gd::String file;
548 };
549 
556 class GD_CORE_API ResourcesManager {
557  public:
559  virtual ~ResourcesManager();
561  ResourcesManager& operator=(const ResourcesManager& rhs);
562 
566  bool HasResource(const gd::String& name) const;
567 
572  const gd::String& GetResourceNameWithOrigin(const gd::String& originName, const gd::String& originIdentifier) const;
573 
578  const gd::String& GetResourceNameWithFile(const gd::String& file) const;
579 
583  Resource& GetResource(const gd::String& name);
584 
588  const Resource& GetResource(const gd::String& name) const;
589 
593  std::shared_ptr<Resource> CreateResource(const gd::String& kind);
594 
598  const std::vector<std::shared_ptr<Resource>>& GetAllResources() const { return resources; };
599 
603  std::vector<gd::String> GetAllResourceNames() const;
604 
609  std::vector<gd::String> FindFilesNotInResources(const std::vector<gd::String>& filePathsToCheck) const;
610 
614  std::shared_ptr<gd::Resource> GetResourceSPtr(const gd::String& name);
615 
621  bool AddResource(const gd::Resource& resource);
622 
626  bool AddResource(const gd::String& name,
627  const gd::String& filename,
628  const gd::String& kind);
629 
633  void RemoveResource(const gd::String& name);
634 
638  void RenameResource(const gd::String& oldName, const gd::String& newName);
639 
643  std::size_t GetResourcePosition(const gd::String& name) const;
644 
648  bool MoveResourceUpInList(const gd::String& name);
649 
653  bool MoveResourceDownInList(const gd::String& name);
654 
658  void MoveResource(std::size_t oldIndex, std::size_t newIndex);
659 
663  bool HasFolder(const gd::String& name) const;
664 
668  const ResourceFolder& GetFolder(const gd::String& name) const;
669 
673  ResourceFolder& GetFolder(const gd::String& name);
674 
678  void RemoveFolder(const gd::String& name);
679 
683  void CreateFolder(const gd::String& name);
684 
688  bool MoveFolderUpInList(const gd::String& name);
689 
693  bool MoveFolderDownInList(const gd::String& name);
694 
698  std::vector<gd::String> GetAllFolderList();
699 
703  void SerializeTo(SerializerElement& element) const;
704 
708  static void SerializeResourceTo(gd::Resource& resource, SerializerElement& resourceElement);
709 
713  void UnserializeFrom(const SerializerElement& element);
714 
715  private:
716  void Init(const ResourcesManager& other);
717 
718  std::vector<std::shared_ptr<Resource> > resources;
719  std::vector<ResourceFolder> folders;
720 
721  static ResourceFolder badFolder;
722  static Resource badResource;
723  static gd::String badResourceName;
724 };
725 
726 class GD_CORE_API ResourceFolder {
727  public:
728  ResourceFolder(){};
729  virtual ~ResourceFolder(){};
731  ResourceFolder& operator=(const ResourceFolder& rhs);
732 
735  virtual void SetName(const gd::String& name_) { name = name_; }
736 
739  virtual const gd::String& GetName() const { return name; }
740 
744  virtual void AddResource(const gd::String& name,
745  gd::ResourcesManager& parentManager);
746 
750  virtual void RemoveResource(const gd::String& name);
751 
755  virtual bool HasResource(const gd::String& name) const;
756 
760  virtual Resource& GetResource(const gd::String& name);
761 
765  virtual const Resource& GetResource(const gd::String& name) const;
766 
770  virtual std::vector<gd::String> GetAllResourceNames();
771 
775  virtual bool MoveResourceUpInList(const gd::String& name);
776 
780  virtual bool MoveResourceDownInList(const gd::String& name);
781 
785  void SerializeTo(SerializerElement& element) const;
786 
790  void UnserializeFrom(const SerializerElement& element,
791  gd::ResourcesManager& parentManager);
792 
793  private:
794  gd::String name;
795  std::vector<std::shared_ptr<Resource> > resources;
796 
797  void Init(const ResourceFolder& other);
798  static Resource badResource;
799 };
800 
801 } // namespace gd
802 
803 #endif // GDCORE_RESOURCESMANAGER_H
Describe an atlas file used by a project.
Definition: ResourcesManager.h:530
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: ResourcesManager.h:538
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:541
Describe an audio file used by a project.
Definition: ResourcesManager.h:223
void SetPreloadAsMusic(bool enable=true)
Set if the audio resource should be preloaded as music.
Definition: ResourcesManager.h:253
bool PreloadAsSound() const
Return true if the audio resource should be preloaded as music.
Definition: ResourcesManager.h:258
bool PreloadAsMusic() const
Return true if the audio resource should be preloaded as music.
Definition: ResourcesManager.h:248
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:236
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: ResourcesManager.h:233
void SetPreloadAsSound(bool enable=true)
Set if the audio resource should be preloaded as music.
Definition: ResourcesManager.h:263
void SetPreloadInCache(bool enable=true)
Set if the audio resource should be preloaded in cache (without decoding into memory).
Definition: ResourcesManager.h:273
bool PreloadInCache() const
Return true if the audio resource should be preloaded in cache (without decoding into memory).
Definition: ResourcesManager.h:268
Describe a bitmap font file used by a project.
Definition: ResourcesManager.h:478
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:489
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: ResourcesManager.h:486
Describe a font file used by a project.
Definition: ResourcesManager.h:288
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: ResourcesManager.h:296
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:299
Describe an image/texture used by a project.
Definition: ResourcesManager.h:167
virtual const gd::String & GetFile() const override
Definition: ResourcesManager.h:180
bool IsSmooth() const
Return true if the image should be smoothed.
Definition: ResourcesManager.h:205
void SetSmooth(bool enable=true)
Set if the image should be smoothed in game.
Definition: ResourcesManager.h:210
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:187
bool smooth
True if smoothing filter is applied.
Definition: ResourcesManager.h:212
Describe a json file used by a project.
Definition: ResourcesManager.h:340
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: ResourcesManager.h:348
void DisablePreload(bool disable=true)
Set if the json preload at game startup must be disabled.
Definition: ResourcesManager.h:368
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:351
bool IsPreloadDisabled() const
Return true if the loading at game startup must be disabled.
Definition: ResourcesManager.h:363
Describe a 3D model file used by a project.
Definition: ResourcesManager.h:504
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: ResourcesManager.h:512
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:515
Definition: ResourcesManager.h:726
virtual const gd::String & GetName() const
Definition: ResourcesManager.h:739
virtual void SetName(const gd::String &name_)
Definition: ResourcesManager.h:735
Base class to describe a resource used by a game.
Definition: ResourcesManager.h:27
virtual void SerializeTo(SerializerElement &element) const
Serialize the object.
Definition: ResourcesManager.h:142
virtual const gd::String & GetMetadata() const
Return the (optional) metadata associated to the resource.
Definition: ResourcesManager.h:105
virtual void SetOrigin(const gd::String &originName_, const gd::String &originIdentifier_)
Definition: ResourcesManager.h:85
virtual void SetUserAdded(bool isUserAdded)
Change if the resource is user added or not.
Definition: ResourcesManager.h:51
virtual bool IsUserAdded() const
Return true if the resource was added by the user.
Definition: ResourcesManager.h:55
virtual const gd::String & GetName() const
Return the name of the resource.
Definition: ResourcesManager.h:39
virtual bool UseFile() const
Return true if the resource use a file.
Definition: ResourcesManager.h:63
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: ResourcesManager.h:134
virtual void SetFile(const gd::String &newFile)
Change, if applicable, the file of the resource.
Definition: ResourcesManager.h:80
virtual void SetName(const gd::String &name_)
Change the name of the resource with the name passed as parameter.
Definition: ResourcesManager.h:35
virtual void SetKind(const gd::String &newKind)
Change the kind of the resource.
Definition: ResourcesManager.h:43
virtual void SetMetadata(const gd::String &metadata_)
Set the metadata (any string) associated to the resource.
Definition: ResourcesManager.h:98
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: ResourcesManager.h:72
virtual void UnserializeFrom(const SerializerElement &element)
Unserialize the object.
Definition: ResourcesManager.h:147
virtual const gd::String & GetKind() const
Return the kind of the resource.
Definition: ResourcesManager.h:47
Inventory all resources used by a project.
Definition: ResourcesManager.h:556
const std::vector< std::shared_ptr< Resource > > & GetAllResources() const
Definition: ResourcesManager.h:598
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: ResourcesManager.h:381
String represents an UTF8 encoded string.
Definition: String.h:33
Describe a tilemap file used by a project.
Definition: ResourcesManager.h:396
void DisablePreload(bool disable=true)
Set if the tilemap preload at game startup must be disabled.
Definition: ResourcesManager.h:424
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: ResourcesManager.h:404
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:407
bool IsPreloadDisabled() const
Return true if the loading at game startup must be disabled.
Definition: ResourcesManager.h:419
Describe a tileset file used by a project.
Definition: ResourcesManager.h:437
void DisablePreload(bool disable=true)
Set if the tilemap preload at game startup must be disabled.
Definition: ResourcesManager.h:465
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:448
bool IsPreloadDisabled() const
Return true if the loading at game startup must be disabled.
Definition: ResourcesManager.h:460
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: ResourcesManager.h:445
Describe a video file used by a project.
Definition: ResourcesManager.h:314
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: ResourcesManager.h:322
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:325
Definition: CommonTools.h:24