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