Polyphase Game Engine
Loading...
Searching...
No Matches
AssetManager.h
Go to the documentation of this file.
1#pragma once
2
3#include "Asset.h"
4#include "AssetRef.h"
5#include "Log.h"
6
7#include "System/System.h"
8
9#include <string>
10#include <deque>
11#include <unordered_map>
12
13class Asset;
14class AssetDir;
15class Material;
16class ParticleSystem;
17
19{
20 std::string mName;
21 std::string mPath;
22 uint64_t mUuid = 0; // For UUID-based loading
23 std::vector<AssetRef*> mTargetRefs;
24 std::vector<AssetStub*> mDependentAssets;
25 const EmbeddedFile* mEmbeddedData = nullptr;
27 Asset* mAsset = nullptr;
28 int32_t mRequeueCount = 0;
29};
30
31#if EDITOR
32// Discovered non-.oct file (e.g. .mp4, .json, .png) tracked for packaging.
33// Raw files are invisible to the runtime asset system; this registry only
34// exists to drive packaging-time copy / embed decisions.
35//
36// mAbsolutePath follows the same convention as AssetStub::mPath:
37// - engine asset: "Engine/Assets/<...>" (relative to CWD; engine source dir is co-located)
38// - project asset: absolute path under projectDir, e.g. "M:/.../MyProject/Assets/<...>"
39// - addon asset: absolute path under projectDir/Packages, e.g. "M:/.../MyProject/Packages/<addon>/Assets/<...>"
40// The packaging code at ActionManager.cpp derives the destination by mirroring
41// saveDir's projectDir-prefix-strip rule: routing engine/project/addon paths
42// follows the same logic as cooked .oct files.
43struct RawAssetEntry
44{
45 std::string mAbsolutePath;
46 bool mEngineAsset = false;
47 uint32_t mPlatformMask = PlatformBit_All;
48 bool mEmbed = false;
49};
50
51// Result of reading an {asset}.meta sidecar. mExists is false when no sidecar
52// is present on disk; in that case the other fields hold their defaults.
53struct AssetMetaSidecar
54{
55 uint32_t mPlatformMask = PlatformBit_All;
56 bool mEmbed = false;
57 bool mExists = false;
58};
59#endif
60
61// Name-based lookup (backward compatible)
62POLYPHASE_API Asset* FetchAsset(const std::string& name);
63POLYPHASE_API Asset* LoadAsset(const std::string& name);
64POLYPHASE_API void UnloadAsset(const std::string& name);
65POLYPHASE_API void AsyncLoadAsset(const std::string& name, AssetRef* targetRef = nullptr);
66POLYPHASE_API AssetStub* FetchAssetStub(const std::string& name);
67
68// UUID-based lookup
70POLYPHASE_API Asset* LoadAssetByUuid(uint64_t uuid);
71POLYPHASE_API void AsyncLoadAssetByUuid(uint64_t uuid, AssetRef* targetRef = nullptr);
73
74#if EDITOR
75// Editor-only: addon-provided source-extension dispatch. Addons call this from
76// their plugin OnLoad to teach ActionManager::ImportAsset which Asset subclass
77// owns a given source-file extension (e.g. ".mp4" -> VideoClip). The extension
78// must include the leading dot and be lowercase; comparisons in the dispatcher
79// are case-sensitive to match the existing built-in branches.
80POLYPHASE_API void RegisterImportExtension(const std::string& ext, TypeId type);
81POLYPHASE_API TypeId LookupImportExtension(const std::string& ext);
82#endif
83
84template<typename T>
85T* FetchAsset(const std::string& name)
86{
87 Asset* asset = FetchAsset(name);
88
89 if (asset != nullptr &&
90 asset->GetType() != T::GetStaticType())
91 {
92 LogError("Type mismatch in FetchAsset<T>()");
93 asset = nullptr;
94 }
95
96 return (T*)asset;
97}
98
99template<typename T>
100T* LoadAsset(const std::string& name)
101{
102 Asset* asset = LoadAsset(name);
103
104 if (asset != nullptr &&
105 !asset->Is(T::ClassRuntimeId()))
106 {
107 LogError("Type mismatch in LoadAsset<T>()");
108 asset = nullptr;
109 }
110
111 return (T*)asset;
112}
113
114// Per-method POLYPHASE_API on the addon-facing surface only. A class-level
115// export expands to dllexport on every member (incl. implicit special-member
116// instantiations in TUs that only forward-decl AssetManager members), which
117// risks C4150 against forward-declared types. Addons reach into AssetManager
118// through Get() and RegisterTransientAsset() only.
120{
121public:
122
124
125 static void Create();
126 static void Destroy();
128
129 void Initialize();
130 void Update(float deltaTime);
131 void DiscoverDirectory(AssetDir* directory, bool engineDir);
132 void RefreshDirectory(AssetDir* directory);
133 void Discover(const char* directoryName, const char* directoryPath);
134 void DiscoverAssetRegistry(const char* registryPath);
135 void DiscoverEmbeddedAssets(struct EmbeddedFile* assets, uint32_t numAssets);
136 void Purge(bool purgeEngineAssets);
137 bool PurgeAsset(const char* name);
138 void RefSweep();
139 void LoadAll();
140
142
143 Asset* ImportEngineAsset(TypeId assetType, AssetDir* dir, const std::string& filename, ImportOptions* options = nullptr);
144 void ImportEngineAssets();
145
146 // Name-based lookup (backward compatible)
147 AssetStub* GetAssetStub(const std::string& name);
148 Asset* GetAsset(const std::string& name);
149 AssetStub* GetSceneAsset(const std::string& name);
150 Asset* LoadAsset(const std::string& name);
151 Asset* LoadAsset(AssetStub& stub);
152 void AsyncLoadAsset(const std::string& name, AssetRef* targetRef);
153
154 // UUID-based lookup (primary)
155 AssetStub* GetAssetStubByUuid(uint64_t uuid);
156 Asset* GetAssetByUuid(uint64_t uuid);
157 Asset* LoadAssetByUuid(uint64_t uuid);
158 void AsyncLoadAssetByUuid(uint64_t uuid, AssetRef* targetRef);
159
160 // Path-based lookup (e.g., "Assets/Models/SM_Plane" or "Models/SM_Plane")
161 AssetStub* GetAssetStubByPath(const std::string& path);
162 Asset* LoadAssetByPath(const std::string& path);
163 void SaveAsset(const std::string& name);
164 void SaveAsset(AssetStub& stub);
165 bool UnloadAsset(const std::string& name);
166 bool UnloadAsset(AssetStub& stub);
167
168 bool DoesAssetExist(const std::string& name);
169 bool RenameAsset(Asset* asset, const std::string& newName);
170 std::string GetParentDirectory(const std::string& path);
171 bool RenameDirectory(AssetDir* dir, const std::string& newName);
172 void GatherScriptFilesRecursive(const std::string& dirPath, const std::string& relativePath, std::vector<std::string>& scriptFiles);
173 std::vector<std::string> GetAvailableScriptFiles();
174 std::vector<std::string> GetAvailableFontFiles();
180 void DiscoverAddonPackages(const std::string& packagesDir);
181 std::string GetPolyphaseDirectory();
182 void GatherScriptFiles(const std::string &dir, std::vector<std::string> &outFiles);
183 void GatherFontFiles(const std::string& dir, std::vector<std::string>& outFiles);
185 std::string FindDefaultScenePath();
187 std::unordered_map<std::string, AssetStub*>& GetAssetMap();
188 std::vector<AssetStub*> GatherDirtyAssets();
189
190 AssetStub* RegisterAsset(const std::string& filename, TypeId type, AssetDir* directory, EmbeddedFile* embeddedAsset, bool engineAsset, uint64_t uuid = 0);
191 AssetStub* CreateAndRegisterAsset(TypeId assetType, AssetDir* directory, const std::string& filename, bool engineAsset);
192 AssetDir* GetAssetDirFromPath(const std::string& dirPath);
193
194 bool IsPurging() const;
195
196protected:
197
198 static ThreadFuncRet AsyncLoadThreadFunc(void* in);
199
201 AssetManager();
202
203 void UpdateEndLoadQueue();
204
205 std::unordered_map<std::string, AssetStub*> mAssetMap; // Name-based lookup (first wins)
206 std::unordered_map<std::string, AssetStub*> mAssetPathMap; // Path-based lookup (e.g., "Models/SM_Plane")
207 std::unordered_map<uint64_t, AssetStub*> mUuidMap; // UUID-based lookup (primary)
208 std::vector<Asset*> mTransientAssets;
210 bool mPurging = false;
211 bool mDestructing = false;
212 std::deque<AsyncLoadRequest*> mBeginLoadQueue;
213 std::deque<AsyncLoadRequest*> mEndLoadQueue;
214 ThreadObject* mAsyncLoadThread = {};
215 MutexObject* mMutex = {};
216
217#if EDITOR
218public:
219 glm::vec4 GetEditorAssetColor(TypeId type);
220 void InitAssetColorMap();
221
222 // Raw (non-.oct) asset packaging registry. Populated during DiscoverDirectory.
223 const std::vector<RawAssetEntry>& GetRawAssetEntries() const { return mRawAssetEntries; }
224
225 // Addon-provided source-extension dispatch (see free-function declarations
226 // above for usage). Stored on the manager so the table survives addon hot-
227 // reload as long as the addon re-registers in its OnLoad.
228 void RegisterImportExtension(const std::string& ext, TypeId type);
229 TypeId LookupImportExtension(const std::string& ext) const;
230
231 // Read the {asset}.meta sidecar at <assetPath>.meta. Returns defaults with
232 // mExists=false when the sidecar is missing or unparseable.
233 static AssetMetaSidecar LoadAssetMeta(const std::string& assetPath);
234
235 // Write the {asset}.meta sidecar. If platformMask == PlatformBit_All AND
236 // embed == false, the sidecar is deleted from disk instead of written so
237 // the source tree stays clean for assets at all-defaults.
238 static void SaveAssetMeta(const std::string& assetPath, uint32_t platformMask, bool embed);
239
240 // Persist new packaging flags to disk via SaveAssetMeta AND update in-memory
241 // AssetStub::mPlatformMask/mEmbed (if assetPath matches a known stub) AND
242 // RawAssetEntry::mPlatformMask/mEmbed (if assetPath matches a raw entry).
243 // Use this from the editor inspector so subsequent packaging sees the new
244 // flags without needing a full rediscover.
245 void ApplyAssetMetaFlags(const std::string& assetPath, uint32_t platformMask, bool embed);
246
247protected:
248 std::unordered_map<TypeId, glm::vec4> mAssetColorMap;
249 std::vector<RawAssetEntry> mRawAssetEntries;
250 std::unordered_map<std::string, TypeId> mImportExtensionMap;
251#endif
252};
253
254template<typename T>
256{
257 T* ret = new T();
259 return ret;
260
261 // Caller still needs to call Create() when ready!
262}
POLYPHASE_API void UnloadAsset(const std::string &name)
Definition AssetManager.cpp:100
POLYPHASE_API void AsyncLoadAsset(const std::string &name, AssetRef *targetRef=nullptr)
Definition AssetManager.cpp:105
POLYPHASE_API AssetStub * FetchAssetStubByUuid(uint64_t uuid)
Definition AssetManager.cpp:131
POLYPHASE_API AssetStub * FetchAssetStub(const std::string &name)
Definition AssetManager.cpp:110
POLYPHASE_API Asset * FetchAsset(const std::string &name)
Definition AssetManager.cpp:90
POLYPHASE_API Asset * LoadAsset(const std::string &name)
Definition AssetManager.cpp:95
T * NewTransientAsset()
Definition AssetManager.h:255
POLYPHASE_API Asset * LoadAssetByUuid(uint64_t uuid)
Definition AssetManager.cpp:121
POLYPHASE_API Asset * FetchAssetByUuid(uint64_t uuid)
Definition AssetManager.cpp:116
POLYPHASE_API void AsyncLoadAssetByUuid(uint64_t uuid, AssetRef *targetRef=nullptr)
Definition AssetManager.cpp:126
#define INVALID_TYPE_ID
Definition Constants.h:40
uint32_t TypeId
Definition EngineTypes.h:64
@ PlatformBit_All
Definition EngineTypes.h:53
bool Update()
Definition Engine.cpp:710
void LogError(const char *format,...)
Definition Log.cpp:281
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
Definition AssetDir.h:11
Definition AssetManager.h:120
bool DoesAssetExist(const std::string &name)
Definition AssetManager.cpp:1455
Asset * LoadAsset(const std::string &name)
Definition AssetManager.cpp:1125
void DiscoverDirectory(AssetDir *directory, bool engineDir)
Definition AssetManager.cpp:428
AssetDir * GetRootDirectory()
Definition AssetManager.cpp:1951
Asset * GetAssetByUuid(uint64_t uuid)
Definition AssetManager.cpp:1185
AssetStub * FindDefaultScene()
Definition AssetManager.cpp:1685
void DiscoverEmbeddedAssets(struct EmbeddedFile *assets, uint32_t numAssets)
Definition AssetManager.cpp:698
void UpdateEndLoadQueue()
Definition AssetManager.cpp:2068
Asset * LoadAssetByUuid(uint64_t uuid)
Definition AssetManager.cpp:1191
AssetDir * GetAssetDirFromPath(const std::string &dirPath)
Definition AssetManager.cpp:367
bool mPurging
Definition AssetManager.h:210
std::vector< std::string > GetAvailableFontFiles()
Definition AssetManager.cpp:1768
static void Create()
Definition AssetManager.cpp:161
void DiscoverAddonPackages(const std::string &packagesDir)
Definition AssetManager.cpp:1914
void AsyncLoadAsset(const std::string &name, AssetRef *targetRef)
Definition AssetManager.cpp:1286
void GatherScriptFiles(const std::string &dir, std::vector< std::string > &outFiles)
Definition AssetManager.cpp:1588
void Initialize()
Definition AssetManager.cpp:205
AssetStub * GetSceneAsset(const std::string &name)
Definition AssetManager.cpp:1112
void GatherFontFiles(const std::string &dir, std::vector< std::string > &outFiles)
Definition AssetManager.cpp:1638
bool PurgeAsset(const char *name)
Definition AssetManager.cpp:773
static AssetManager * sInstance
Definition AssetManager.h:200
void SaveAsset(const std::string &name)
Definition AssetManager.cpp:1358
static POLYPHASE_API AssetManager * Get()
Definition AssetManager.cpp:176
MutexObject * mMutex
Definition AssetManager.h:215
void Purge(bool purgeEngineAssets)
Definition AssetManager.cpp:722
std::vector< std::string > GetAvailableScriptFiles()
Definition AssetManager.cpp:1788
AssetManager()
Definition AssetManager.cpp:181
AssetStub * RegisterAsset(const std::string &filename, TypeId type, AssetDir *directory, EmbeddedFile *embeddedAsset, bool engineAsset, uint64_t uuid=0)
Definition AssetManager.cpp:218
bool mDestructing
Definition AssetManager.h:211
ThreadObject * mAsyncLoadThread
Definition AssetManager.h:214
bool RenameDirectory(AssetDir *dir, const std::string &newName)
Definition AssetManager.cpp:1552
bool RenameAsset(Asset *asset, const std::string &newName)
Definition AssetManager.cpp:1460
bool IsPurging() const
Definition AssetManager.cpp:423
std::deque< AsyncLoadRequest * > mEndLoadQueue
Definition AssetManager.h:213
std::string GetPolyphaseDirectory()
Definition AssetManager.cpp:1956
AssetStub * GetAssetStubByPath(const std::string &path)
Definition AssetManager.cpp:1246
void AsyncLoadAssetByUuid(uint64_t uuid, AssetRef *targetRef)
Definition AssetManager.cpp:1201
AssetStub * GetAssetStub(const std::string &name)
Definition AssetManager.cpp:1091
std::string GetParentDirectory(const std::string &path)
Definition AssetManager.cpp:1531
POLYPHASE_API void RegisterTransientAsset(Asset *asset)
Definition AssetManager.cpp:908
AssetStub * CreateAndRegisterAsset(TypeId assetType, AssetDir *directory, const std::string &filename, bool engineAsset)
Definition AssetManager.cpp:314
std::vector< AssetStub * > GatherDirtyAssets()
Definition AssetManager.cpp:1989
static ThreadFuncRet AsyncLoadThreadFunc(void *in)
Definition AssetManager.cpp:2007
~AssetManager()
Definition AssetManager.cpp:188
void LoadAll()
Definition AssetManager.cpp:889
void RefreshDirectory(AssetDir *directory)
Definition AssetManager.cpp:529
AssetDir * FindProjectDirectory()
Definition AssetManager.cpp:1866
AssetDir * FindPackagesDirectory()
Definition AssetManager.cpp:1898
std::unordered_map< std::string, AssetStub * > mAssetMap
Definition AssetManager.h:205
AssetStub * GetAssetStubByUuid(uint64_t uuid)
Definition AssetManager.cpp:1177
void UnloadProjectDirectory()
Definition AssetManager.cpp:1964
void DiscoverAssetRegistry(const char *registryPath)
Definition AssetManager.cpp:651
AssetDir * FindEngineDirectory()
Definition AssetManager.cpp:1882
std::unordered_map< std::string, AssetStub * > mAssetPathMap
Definition AssetManager.h:206
void GatherScriptFilesRecursive(const std::string &dirPath, const std::string &relativePath, std::vector< std::string > &scriptFiles)
void RefSweep()
Definition AssetManager.cpp:840
std::deque< AsyncLoadRequest * > mBeginLoadQueue
Definition AssetManager.h:212
bool UnloadAsset(const std::string &name)
Definition AssetManager.cpp:1412
std::vector< Asset * > mTransientAssets
Definition AssetManager.h:208
std::unordered_map< uint64_t, AssetStub * > mUuidMap
Definition AssetManager.h:207
AssetDir * mRootDirectory
Definition AssetManager.h:209
std::string FindDefaultScenePath()
Definition AssetManager.cpp:1698
static void Destroy()
Definition AssetManager.cpp:167
AssetDir * FindProjectRootDirectory()
std::unordered_map< std::string, AssetStub * > & GetAssetMap()
Definition AssetManager.cpp:1984
void ImportEngineAssets()
Definition AssetManager.cpp:969
Asset * GetAsset(const std::string &name)
Definition AssetManager.cpp:1099
Asset * LoadAssetByPath(const std::string &path)
Definition AssetManager.cpp:1276
Asset * ImportEngineAsset(TypeId assetType, AssetDir *dir, const std::string &filename, ImportOptions *options=nullptr)
Definition AssetManager.cpp:914
void Discover(const char *directoryName, const char *directoryPath)
Definition AssetManager.cpp:629
Definition AssetRef.h:18
Definition Asset.h:113
Definition Asset.h:102
Definition Material.h:48
virtual bool Is(RuntimeId id) const
Definition Object.h:29
Definition ParticleSystem.h:66
Definition Asset.h:82
Definition AssetManager.h:19
std::string mName
Definition AssetManager.h:20
const EmbeddedFile * mEmbeddedData
Definition AssetManager.h:25
Asset * mAsset
Definition AssetManager.h:27
int32_t mRequeueCount
Definition AssetManager.h:28
TypeId mType
Definition AssetManager.h:26
std::vector< AssetStub * > mDependentAssets
Definition AssetManager.h:24
std::vector< AssetRef * > mTargetRefs
Definition AssetManager.h:23
uint64_t mUuid
Definition AssetManager.h:22
std::string mPath
Definition AssetManager.h:21
Definition EmbeddedFile.h:6