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;
157 
158  static gd::String badStr;
159 };
160 
167 class GD_CORE_API ImageResource : public Resource {
168  public:
169  ImageResource() : Resource(), smooth(true), alwaysLoaded(false) {
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;
214  private:
215  gd::String file;
216 };
217 
224 class GD_CORE_API AudioResource : public Resource {
225  public:
226  AudioResource() : Resource(), preloadAsMusic(false), preloadAsSound(false), preloadInCache(false) {
227  SetKind("audio");
228  };
229  virtual ~AudioResource(){};
230  virtual AudioResource* Clone() const override {
231  return new AudioResource(*this);
232  }
233 
234  virtual const gd::String& GetFile() const override { return file; };
235  virtual void SetFile(const gd::String& newFile) override;
236 
237  virtual bool UseFile() const override { return true; }
238 
239  std::map<gd::String, gd::PropertyDescriptor> GetProperties() const override;
240  bool UpdateProperty(const gd::String& name, const gd::String& value) override;
241 
242  void SerializeTo(SerializerElement& element) const override;
243 
244  void UnserializeFrom(const SerializerElement& element) override;
245 
249  bool PreloadAsMusic() const { return preloadAsMusic; }
250 
254  void SetPreloadAsMusic(bool enable = true) { preloadAsMusic = enable; }
255 
259  bool PreloadAsSound() const { return preloadAsSound; }
260 
264  void SetPreloadAsSound(bool enable = true) { preloadAsSound = enable; }
265 
269  bool PreloadInCache() const { return preloadInCache; }
270 
274  void SetPreloadInCache(bool enable = true) { preloadInCache = enable; }
275 
276  private:
277  gd::String file;
278  bool preloadAsSound;
279  bool preloadAsMusic;
280  bool preloadInCache;
281 };
282 
289 class GD_CORE_API FontResource : public Resource {
290  public:
291  FontResource() : Resource() { SetKind("font"); };
292  virtual ~FontResource(){};
293  virtual FontResource* Clone() const override {
294  return new FontResource(*this);
295  }
296 
297  virtual const gd::String& GetFile() const override { return file; };
298  virtual void SetFile(const gd::String& newFile) override;
299 
300  virtual bool UseFile() const override { return true; }
301  void SerializeTo(SerializerElement& element) const override;
302 
303  void UnserializeFrom(const SerializerElement& element) override;
304 
305  private:
306  gd::String file;
307 };
308 
315 class GD_CORE_API VideoResource : public Resource {
316  public:
317  VideoResource() : Resource() { SetKind("video"); };
318  virtual ~VideoResource(){};
319  virtual VideoResource* Clone() const override {
320  return new VideoResource(*this);
321  }
322 
323  virtual const gd::String& GetFile() const override { return file; };
324  virtual void SetFile(const gd::String& newFile) override;
325 
326  virtual bool UseFile() const override { return true; }
327  void SerializeTo(SerializerElement& element) const override;
328 
329  void UnserializeFrom(const SerializerElement& element) override;
330 
331  private:
332  gd::String file;
333 };
334 
341 class GD_CORE_API JsonResource : public Resource {
342  public:
343  JsonResource() : Resource(), disablePreload(false) { SetKind("json"); };
344  virtual ~JsonResource(){};
345  virtual JsonResource* Clone() const override {
346  return new JsonResource(*this);
347  }
348 
349  virtual const gd::String& GetFile() const override { return file; };
350  virtual void SetFile(const gd::String& newFile) override;
351 
352  virtual bool UseFile() const override { return true; }
353 
354  std::map<gd::String, gd::PropertyDescriptor> GetProperties() const override;
355  bool UpdateProperty(const gd::String& name, const gd::String& value) override;
356 
357  void SerializeTo(SerializerElement& element) const override;
358 
359  void UnserializeFrom(const SerializerElement& element) override;
360 
364  bool IsPreloadDisabled() const { return disablePreload; }
365 
369  void DisablePreload(bool disable = true) { disablePreload = disable; }
370 
371  private:
372  bool disablePreload;
373  gd::String file;
374 };
375 
382 class GD_CORE_API SpineResource : public JsonResource {
383  public:
384  SpineResource() : JsonResource() { SetKind("spine"); };
385  virtual ~SpineResource(){};
386  virtual SpineResource* Clone() const override {
387  return new SpineResource(*this);
388  }
389 };
390 
397 class GD_CORE_API TilemapResource : public Resource {
398  public:
399  TilemapResource() : Resource(), disablePreload(false) { SetKind("tilemap"); };
400  virtual ~TilemapResource(){};
401  virtual TilemapResource* Clone() const override {
402  return new TilemapResource(*this);
403  }
404 
405  virtual const gd::String& GetFile() const override { return file; };
406  virtual void SetFile(const gd::String& newFile) override;
407 
408  virtual bool UseFile() const override { return true; }
409 
410  std::map<gd::String, gd::PropertyDescriptor> GetProperties() const override;
411  bool UpdateProperty(const gd::String& name, const gd::String& value) override;
412 
413  void SerializeTo(SerializerElement& element) const override;
414 
415  void UnserializeFrom(const SerializerElement& element) override;
416 
420  bool IsPreloadDisabled() const { return disablePreload; }
421 
425  void DisablePreload(bool disable = true) { disablePreload = disable; }
426 
427  private:
428  bool disablePreload;
429  gd::String file;
430 };
431 
438 class GD_CORE_API TilesetResource : public Resource {
439  public:
440  TilesetResource() : Resource(), disablePreload(false) { SetKind("tileset"); };
441  virtual ~TilesetResource(){};
442  virtual TilesetResource* Clone() const override {
443  return new TilesetResource(*this);
444  }
445 
446  virtual const gd::String& GetFile() const override { return file; };
447  virtual void SetFile(const gd::String& newFile) override;
448 
449  virtual bool UseFile() const override { return true; }
450 
451  std::map<gd::String, gd::PropertyDescriptor> GetProperties() const override;
452  bool UpdateProperty(const gd::String& name, const gd::String& value) override;
453 
454  void SerializeTo(SerializerElement& element) const override;
455 
456  void UnserializeFrom(const SerializerElement& element) override;
457 
461  bool IsPreloadDisabled() const { return disablePreload; }
462 
466  void DisablePreload(bool disable = true) { disablePreload = disable; }
467 
468  private:
469  bool disablePreload;
470  gd::String file;
471 };
472 
479 class GD_CORE_API BitmapFontResource : public Resource {
480  public:
481  BitmapFontResource() : Resource() { SetKind("bitmapFont"); };
482  virtual ~BitmapFontResource(){};
483  virtual BitmapFontResource* Clone() const override {
484  return new BitmapFontResource(*this);
485  }
486 
487  virtual const gd::String& GetFile() const override { return file; };
488  virtual void SetFile(const gd::String& newFile) override;
489 
490  virtual bool UseFile() const override { return true; }
491  void SerializeTo(SerializerElement& element) const override;
492 
493  void UnserializeFrom(const SerializerElement& element) override;
494 
495  private:
496  gd::String file;
497 };
498 
505 class GD_CORE_API Model3DResource : public Resource {
506  public:
507  Model3DResource() : Resource() { SetKind("model3D"); };
508  virtual ~Model3DResource(){};
509  virtual Model3DResource* Clone() const override {
510  return new Model3DResource(*this);
511  }
512 
513  virtual const gd::String& GetFile() const override { return file; };
514  virtual void SetFile(const gd::String& newFile) override;
515 
516  virtual bool UseFile() const override { return true; }
517  void SerializeTo(SerializerElement& element) const override;
518 
519  void UnserializeFrom(const SerializerElement& element) override;
520 
521  private:
522  gd::String file;
523 };
524 
531 class GD_CORE_API AtlasResource : public Resource {
532  public:
533  AtlasResource() : Resource() { SetKind("atlas"); };
534  virtual ~AtlasResource(){};
535  virtual AtlasResource* Clone() const override {
536  return new AtlasResource(*this);
537  }
538 
539  virtual const gd::String& GetFile() const override { return file; };
540  virtual void SetFile(const gd::String& newFile) override;
541 
542  virtual bool UseFile() const override { return true; }
543  void SerializeTo(SerializerElement& element) const override;
544 
545  void UnserializeFrom(const SerializerElement& element) override;
546 
547  private:
548  gd::String file;
549 };
550 
557 class GD_CORE_API ResourcesManager {
558  public:
560  virtual ~ResourcesManager();
562  ResourcesManager& operator=(const ResourcesManager& rhs);
563 
567  bool HasResource(const gd::String& name) const;
568 
573  const gd::String& GetResourceNameWithOrigin(const gd::String& originName, const gd::String& originIdentifier) const;
574 
579  const gd::String& GetResourceNameWithFile(const gd::String& file) const;
580 
584  Resource& GetResource(const gd::String& name);
585 
589  const Resource& GetResource(const gd::String& name) const;
590 
594  std::shared_ptr<Resource> CreateResource(const gd::String& kind);
595 
599  const std::vector<std::shared_ptr<Resource>>& GetAllResources() const { return resources; };
600 
604  std::vector<gd::String> GetAllResourceNames() const;
605 
610  std::vector<gd::String> FindFilesNotInResources(const std::vector<gd::String>& filePathsToCheck) const;
611 
615  std::shared_ptr<gd::Resource> GetResourceSPtr(const gd::String& name);
616 
622  bool AddResource(const gd::Resource& resource);
623 
627  bool AddResource(const gd::String& name,
628  const gd::String& filename,
629  const gd::String& kind);
630 
634  void RemoveResource(const gd::String& name);
635 
639  void RenameResource(const gd::String& oldName, const gd::String& newName);
640 
644  std::size_t GetResourcePosition(const gd::String& name) const;
645 
649  bool MoveResourceUpInList(const gd::String& name);
650 
654  bool MoveResourceDownInList(const gd::String& name);
655 
659  void MoveResource(std::size_t oldIndex, std::size_t newIndex);
660 
664  bool HasFolder(const gd::String& name) const;
665 
669  const ResourceFolder& GetFolder(const gd::String& name) const;
670 
674  ResourceFolder& GetFolder(const gd::String& name);
675 
679  void RemoveFolder(const gd::String& name);
680 
684  void CreateFolder(const gd::String& name);
685 
689  bool MoveFolderUpInList(const gd::String& name);
690 
694  bool MoveFolderDownInList(const gd::String& name);
695 
699  std::vector<gd::String> GetAllFolderList();
700 
704  void SerializeTo(SerializerElement& element) const;
705 
709  static void SerializeResourceTo(gd::Resource& resource, SerializerElement& resourceElement);
710 
714  void UnserializeFrom(const SerializerElement& element);
715 
716  private:
717  void Init(const ResourcesManager& other);
718 
719  std::vector<std::shared_ptr<Resource> > resources;
720  std::vector<ResourceFolder> folders;
721 
722  static ResourceFolder badFolder;
723  static Resource badResource;
724  static gd::String badResourceName;
725 };
726 
727 class GD_CORE_API ResourceFolder {
728  public:
729  ResourceFolder(){};
730  virtual ~ResourceFolder(){};
732  ResourceFolder& operator=(const ResourceFolder& rhs);
733 
736  virtual void SetName(const gd::String& name_) { name = name_; }
737 
740  virtual const gd::String& GetName() const { return name; }
741 
745  virtual void AddResource(const gd::String& name,
746  gd::ResourcesManager& parentManager);
747 
751  virtual void RemoveResource(const gd::String& name);
752 
756  virtual bool HasResource(const gd::String& name) const;
757 
761  virtual Resource& GetResource(const gd::String& name);
762 
766  virtual const Resource& GetResource(const gd::String& name) const;
767 
771  virtual std::vector<gd::String> GetAllResourceNames();
772 
776  virtual bool MoveResourceUpInList(const gd::String& name);
777 
781  virtual bool MoveResourceDownInList(const gd::String& name);
782 
786  void SerializeTo(SerializerElement& element) const;
787 
791  void UnserializeFrom(const SerializerElement& element,
792  gd::ResourcesManager& parentManager);
793 
794  private:
795  gd::String name;
796  std::vector<std::shared_ptr<Resource> > resources;
797 
798  void Init(const ResourceFolder& other);
799  static Resource badResource;
800 };
801 
802 } // namespace gd
803 
804 #endif // GDCORE_RESOURCESMANAGER_H
Describe an atlas file used by a project.
Definition: ResourcesManager.h:531
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:539
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:542
Describe an audio file used by a project.
Definition: ResourcesManager.h:224
void SetPreloadAsMusic(bool enable=true)
Set if the audio resource should be preloaded as music.
Definition: ResourcesManager.h:254
bool PreloadAsSound() const
Return true if the audio resource should be preloaded as music.
Definition: ResourcesManager.h:259
bool PreloadAsMusic() const
Return true if the audio resource should be preloaded as music.
Definition: ResourcesManager.h:249
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:237
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:234
void SetPreloadAsSound(bool enable=true)
Set if the audio resource should be preloaded as music.
Definition: ResourcesManager.h:264
void SetPreloadInCache(bool enable=true)
Set if the audio resource should be preloaded in cache (without decoding into memory).
Definition: ResourcesManager.h:274
bool PreloadInCache() const
Return true if the audio resource should be preloaded in cache (without decoding into memory).
Definition: ResourcesManager.h:269
Describe a bitmap font file used by a project.
Definition: ResourcesManager.h:479
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:490
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:487
Describe a font file used by a project.
Definition: ResourcesManager.h:289
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:297
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:300
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
bool alwaysLoaded
True if the image must always be loaded in memory.
Definition: ResourcesManager.h:213
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:341
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:349
void DisablePreload(bool disable=true)
Set if the json preload at game startup must be disabled.
Definition: ResourcesManager.h:369
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:352
bool IsPreloadDisabled() const
Return true if the loading at game startup must be disabled.
Definition: ResourcesManager.h:364
Describe a 3D model file used by a project.
Definition: ResourcesManager.h:505
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:513
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:516
Definition: ResourcesManager.h:727
virtual const gd::String & GetName() const
Definition: ResourcesManager.h:740
virtual void SetName(const gd::String &name_)
Definition: ResourcesManager.h:736
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:557
const std::vector< std::shared_ptr< Resource > > & GetAllResources() const
Definition: ResourcesManager.h:599
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:382
String represents an UTF8 encoded string.
Definition: String.h:31
Describe a tilemap file used by a project.
Definition: ResourcesManager.h:397
void DisablePreload(bool disable=true)
Set if the tilemap preload at game startup must be disabled.
Definition: ResourcesManager.h:425
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:405
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:408
bool IsPreloadDisabled() const
Return true if the loading at game startup must be disabled.
Definition: ResourcesManager.h:420
Describe a tileset file used by a project.
Definition: ResourcesManager.h:438
void DisablePreload(bool disable=true)
Set if the tilemap preload at game startup must be disabled.
Definition: ResourcesManager.h:466
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:449
bool IsPreloadDisabled() const
Return true if the loading at game startup must be disabled.
Definition: ResourcesManager.h:461
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:446
Describe a video file used by a project.
Definition: ResourcesManager.h:315
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:323
virtual bool UseFile() const override
Return true if the resource use a file.
Definition: ResourcesManager.h:326
Definition: CommonTools.h:24