BsGameResourceManager.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisites.h"
  5. #include "BsModule.h"
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup Resources-Engine-Internal
  9. * @{
  10. */
  11. /**
  12. * Provides a way to map one resource path to another path. Useful if the resources are being referenced using a path
  13. * that is not the path to their physical location.
  14. */
  15. class BS_EXPORT ResourceMapping : public IReflectable
  16. {
  17. public:
  18. /** Returns the resource path map. */
  19. const UnorderedMap<Path, Path>& getMap() const { return mMapping; }
  20. /** Adds a new entry to the resource map. Translated from path @p from to path @p to. */
  21. void add(const Path& from, const Path& to);
  22. /** Creates a new empty resource mapping. */
  23. static SPtr<ResourceMapping> create();
  24. private:
  25. UnorderedMap<Path, Path> mMapping;
  26. /************************************************************************/
  27. /* RTTI */
  28. /************************************************************************/
  29. public:
  30. friend class ResourceMappingRTTI;
  31. static RTTITypeBase* getRTTIStatic();
  32. RTTITypeBase* getRTTI() const override;
  33. };
  34. /** Interface that can be implemented by the resource loaders required by GameResourceManager. */
  35. class BS_EXPORT IGameResourceLoader
  36. {
  37. public:
  38. virtual ~IGameResourceLoader() { }
  39. /** Loads the resource at the specified path. */
  40. virtual HResource load(const Path& path, bool keepLoaded) const = 0;
  41. /** @copydoc GameResourceManager::setMapping */
  42. virtual void setMapping(const SPtr<ResourceMapping>& mapping) { }
  43. };
  44. /** Handles loading of game resources when the standalone game is running. */
  45. class BS_EXPORT StandaloneResourceLoader : public IGameResourceLoader
  46. {
  47. public:
  48. /** @copydoc IGameResourceLoader::load */
  49. HResource load(const Path& path, bool keepLoaded) const override;
  50. /** @copydoc IGameResourceLoader::setMapping */
  51. void setMapping(const SPtr<ResourceMapping>& mapping) override;
  52. private:
  53. UnorderedMap<Path, Path> mMapping;
  54. };
  55. /**
  56. * Keeps track of resources that can be dynamically loaded during runtime. These resources will be packed with the game
  57. * build so that they're available on demand.
  58. *
  59. * Internal resource handle can be overridden so that editor or other systems can handle resource loading more directly.
  60. */
  61. class BS_EXPORT GameResourceManager : public Module<GameResourceManager>
  62. {
  63. public:
  64. GameResourceManager();
  65. /**
  66. * Loads the resource at the specified path.
  67. *
  68. * @see Resources::load
  69. */
  70. HResource load(const Path& path, bool keepLoaded) const;
  71. /** @copydoc load */
  72. template <class T>
  73. ResourceHandle<T> load(const Path& filePath, bool keepLoaded)
  74. {
  75. return static_resource_cast<T>(load(filePath, keepLoaded));
  76. }
  77. /**
  78. * Sets an optional mapping that be applied to any path provided to load(). This allows you to reference files
  79. * using different names and/or folder structure than they are actually in.
  80. *
  81. * For example normally in script code you would reference resources based on their path relative to the project
  82. * resoruces folder, but in standalone there is no such folder and we need to map the values.
  83. *
  84. * Provided paths should be relative to the working directory.
  85. */
  86. void setMapping(const SPtr<ResourceMapping>& mapping);
  87. /** Sets the resource loader implementation that determines how are the paths provided to load() loaded. */
  88. void setLoader(const SPtr<IGameResourceLoader>& loader);
  89. private:
  90. SPtr<IGameResourceLoader> mLoader;
  91. };
  92. /** @} */
  93. }