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 "BsCorePrerequisites.h" |
6 | #include "Reflection/BsIReflectable.h" |
7 | |
8 | namespace bs |
9 | { |
10 | /** @addtogroup Resources-Internal |
11 | * @{ |
12 | */ |
13 | |
14 | /** |
15 | * Serializable class that contains UUID <-> file path mapping for resources. |
16 | * |
17 | * @note |
18 | * This class allows you to reference resources between sessions. At the end of a session save the resource manifest, |
19 | * and then restore it at the start of a new session. This way ensures that resource UUIDs stay consistent and anything |
20 | * referencing them can find the resources. |
21 | * @note |
22 | * Thread safe. |
23 | */ |
24 | class BS_CORE_EXPORT BS_SCRIPT_EXPORT(m:Resources,api:bsf) ResourceManifest : public IReflectable |
25 | { |
26 | struct ConstructPrivately {}; |
27 | public: |
28 | explicit ResourceManifest(const ConstructPrivately& dummy); |
29 | ResourceManifest(const String& name); |
30 | |
31 | /** Returns an unique name of the resource manifest. */ |
32 | BS_SCRIPT_EXPORT(pr:getter,n:Name) |
33 | const String& getName() const { return mName; } |
34 | |
35 | /** Registers a new resource in the manifest. */ |
36 | BS_SCRIPT_EXPORT() |
37 | void registerResource(const UUID& uuid, const Path& filePath); |
38 | |
39 | /** Removes a resource from the manifest. */ |
40 | BS_SCRIPT_EXPORT() |
41 | void unregisterResource(const UUID& uuid); |
42 | |
43 | /** |
44 | * Attempts to find a resource with the provided UUID and outputs the path to the resource if found. Returns true |
45 | * if UUID was found, false otherwise. |
46 | */ |
47 | BS_SCRIPT_EXPORT() |
48 | bool uuidToFilePath(const UUID& uuid, Path& filePath) const; |
49 | |
50 | /** |
51 | * Attempts to find a resource with the provided path and outputs the UUID to the resource if found. Returns true |
52 | * if path was found, false otherwise. |
53 | */ |
54 | BS_SCRIPT_EXPORT() |
55 | bool filePathToUUID(const Path& filePath, UUID& outUUID) const; |
56 | |
57 | /** Checks if provided UUID exists in the manifest. */ |
58 | BS_SCRIPT_EXPORT() |
59 | bool uuidExists(const UUID& uuid) const; |
60 | |
61 | /** Checks if the provided path exists in the manifest. */ |
62 | BS_SCRIPT_EXPORT() |
63 | bool filePathExists(const Path& filePath) const; |
64 | |
65 | /** |
66 | * Saves the resource manifest to the specified location. |
67 | * |
68 | * @param[in] manifest Manifest to save. |
69 | * @param[in] path Full pathname of the file to save the manifest in. |
70 | * @param[in] relativePath If not empty, all pathnames in the manifest will be stored as if relative to this |
71 | * path. |
72 | */ |
73 | BS_SCRIPT_EXPORT() |
74 | static void save(const SPtr<ResourceManifest>& manifest, const Path& path, const Path& relativePath); |
75 | |
76 | /** |
77 | * Loads the resource manifest from the specified location. |
78 | * |
79 | * @param[in] path Full pathname of the file to load the manifest from. |
80 | * @param[in] relativePath If not empty, all loaded pathnames will have this path prepended. |
81 | */ |
82 | BS_SCRIPT_EXPORT() |
83 | static SPtr<ResourceManifest> load(const Path& path, const Path& relativePath); |
84 | |
85 | /** Creates a new empty resource manifest. Provided name should be unique among manifests. */ |
86 | BS_SCRIPT_EXPORT(ec:ResourceManifest) |
87 | static SPtr<ResourceManifest> create(const String& name); |
88 | |
89 | private: |
90 | String mName; |
91 | UnorderedMap<UUID, Path> mUUIDToFilePath; |
92 | UnorderedMap<Path, UUID> mFilePathToUUID; |
93 | |
94 | /************************************************************************/ |
95 | /* RTTI */ |
96 | /************************************************************************/ |
97 | |
98 | /** Creates a new empty resource manifest. */ |
99 | static SPtr<ResourceManifest> createEmpty(); |
100 | |
101 | public: |
102 | friend class ResourceManifestRTTI; |
103 | static RTTITypeBase* getRTTIStatic(); |
104 | RTTITypeBase* getRTTI() const override; |
105 | }; |
106 | |
107 | /** @} */ |
108 | } |