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
83// Returns the AssetDir the user is currently browsing in the asset browser —
84// i.e. the folder a right-click → "Create Asset" should write into. Returns
85// nullptr if no project is open or the editor isn't ready yet.
86//
87// Exposed for native addons that surface custom asset types in the asset
88// browser's New Asset menu (via EditorUIHooks::AddCreateAssetItem(s)) and
89// want the created asset to land in the user's current folder. Wraps
90// GetEditorState()->GetAssetDirectory() so addons don't have to take a
91// dependency on the non-exported EditorState class.
92POLYPHASE_API AssetDir* GetCurrentAssetDir();
93#endif
94
95template<typename T>
96T* FetchAsset(const std::string& name)
97{
98 Asset* asset = FetchAsset(name);
99
100 if (asset != nullptr &&
101 asset->GetType() != T::GetStaticType())
102 {
103 LogError("Type mismatch in FetchAsset<T>()");
104 asset = nullptr;
105 }
106
107 return (T*)asset;
108}
109
110template<typename T>
111T* LoadAsset(const std::string& name)
112{
113 Asset* asset = LoadAsset(name);
114
115 if (asset != nullptr &&
116 !asset->Is(T::ClassRuntimeId()))
117 {
118 LogError("Type mismatch in LoadAsset<T>()");
119 asset = nullptr;
120 }
121
122 return (T*)asset;
123}
124
125// Per-method POLYPHASE_API on the addon-facing surface only. A class-level
126// export expands to dllexport on every member (incl. implicit special-member
127// instantiations in TUs that only forward-decl AssetManager members), which
128// risks C4150 against forward-declared types. The addon-facing surface is:
129// - Get()
130// - RegisterTransientAsset()
131// - CreateAndRegisterAsset()
132// plus the file-scope POLYPHASE_API helpers near the top of this header
133// (FetchAsset, LoadAsset, RegisterImportExtension, GetCurrentAssetDir, …).
135{
136public:
137
139
140 static void Create();
141 static void Destroy();
143
144 void Initialize();
145 void Update(float deltaTime);
146 void DiscoverDirectory(AssetDir* directory, bool engineDir);
147 void RefreshDirectory(AssetDir* directory);
148 void Discover(const char* directoryName, const char* directoryPath);
149 void DiscoverAssetRegistry(const char* registryPath);
150 void DiscoverEmbeddedAssets(struct EmbeddedFile* assets, uint32_t numAssets);
151 void Purge(bool purgeEngineAssets);
152 POLYPHASE_API bool PurgeAsset(const char* name);
153 void RefSweep();
154 void LoadAll();
155
157
158 Asset* ImportEngineAsset(TypeId assetType, AssetDir* dir, const std::string& filename, ImportOptions* options = nullptr);
159 void ImportEngineAssets();
160
161 // Name-based lookup (backward compatible)
162 AssetStub* GetAssetStub(const std::string& name);
163 POLYPHASE_API Asset* GetAsset(const std::string& name);
164 AssetStub* GetSceneAsset(const std::string& name);
165 POLYPHASE_API Asset* LoadAsset(const std::string& name);
166 Asset* LoadAsset(AssetStub& stub);
167 void AsyncLoadAsset(const std::string& name, AssetRef* targetRef);
168
169 // UUID-based lookup (primary)
170 AssetStub* GetAssetStubByUuid(uint64_t uuid);
171 Asset* GetAssetByUuid(uint64_t uuid);
172 Asset* LoadAssetByUuid(uint64_t uuid);
173 void AsyncLoadAssetByUuid(uint64_t uuid, AssetRef* targetRef);
174
175 // Path-based lookup (e.g., "Assets/Models/SM_Plane" or "Models/SM_Plane")
176 AssetStub* GetAssetStubByPath(const std::string& path);
177 Asset* LoadAssetByPath(const std::string& path);
178 POLYPHASE_API void SaveAsset(const std::string& name);
180 bool UnloadAsset(const std::string& name);
181 bool UnloadAsset(AssetStub& stub);
182
183 bool DoesAssetExist(const std::string& name);
184 bool RenameAsset(Asset* asset, const std::string& newName);
185 std::string GetParentDirectory(const std::string& path);
186 bool RenameDirectory(AssetDir* dir, const std::string& newName);
187 void GatherScriptFilesRecursive(const std::string& dirPath, const std::string& relativePath, std::vector<std::string>& scriptFiles);
188 std::vector<std::string> GetAvailableScriptFiles();
189 std::vector<std::string> GetAvailableFontFiles();
195 void DiscoverAddonPackages(const std::string& packagesDir);
196 std::string GetPolyphaseDirectory();
197 void GatherScriptFiles(const std::string &dir, std::vector<std::string> &outFiles);
198 void GatherFontFiles(const std::string& dir, std::vector<std::string>& outFiles);
200 std::string FindDefaultScenePath();
202 POLYPHASE_API std::unordered_map<std::string, AssetStub*>& GetAssetMap();
203 std::vector<Asset*>& GetTransientAssets();
204 std::vector<AssetStub*> GatherDirtyAssets();
205
206 AssetStub* RegisterAsset(const std::string& filename, TypeId type, AssetDir* directory, EmbeddedFile* embeddedAsset, bool engineAsset, uint64_t uuid = 0);
207 // POLYPHASE_API: exposed to addons so a plugin-registered "Create Asset"
208 // menu entry can drop the .oct directly into the asset browser tree
209 // (RegisterAsset + SaveAsset under the hood) without bouncing through
210 // the import-extension pipeline. Pair with GetCurrentAssetDir() to
211 // land the asset in the user's currently-browsed folder.
212 POLYPHASE_API AssetStub* CreateAndRegisterAsset(TypeId assetType, AssetDir* directory, const std::string& filename, bool engineAsset);
213 POLYPHASE_API AssetDir* GetAssetDirFromPath(const std::string& dirPath);
214
215 bool IsPurging() const;
216
217 // Walks the asset map + transient list looking for a matching pointer
218 // identity without dereferencing the argument. Lets defensive call sites
219 // distinguish "live Asset" from "freed-but-not-yet-nulled raw pointer"
220 // when ASSET_LIVE_REF_TRACKING didn't catch the stale reference
221 // (e.g. across project-switch teardown). Linear scan, only intended for
222 // the rare paths that must defend against a dangling Asset*.
223 bool IsAssetLive(const Asset* asset) const;
224
225protected:
226
227 static ThreadFuncRet AsyncLoadThreadFunc(void* in);
228
230 AssetManager();
231
232 void UpdateEndLoadQueue();
233
234 std::unordered_map<std::string, AssetStub*> mAssetMap; // Name-based lookup (first wins)
235 std::unordered_map<std::string, AssetStub*> mAssetPathMap; // Path-based lookup (e.g., "Models/SM_Plane")
236 std::unordered_map<uint64_t, AssetStub*> mUuidMap; // UUID-based lookup (primary)
237 std::vector<Asset*> mTransientAssets;
239 bool mPurging = false;
240 bool mDestructing = false;
241 std::deque<AsyncLoadRequest*> mBeginLoadQueue;
242 std::deque<AsyncLoadRequest*> mEndLoadQueue;
243 ThreadObject* mAsyncLoadThread = {};
244 MutexObject* mMutex = {};
245
246#if EDITOR
247public:
248 glm::vec4 GetEditorAssetColor(TypeId type);
249 void InitAssetColorMap();
250
251 // Raw (non-.oct) asset packaging registry. Populated during DiscoverDirectory.
252 const std::vector<RawAssetEntry>& GetRawAssetEntries() const { return mRawAssetEntries; }
253
254 // Register a new loose-file entry at runtime (used by
255 // ActionManager::ImportLooseFile so the file ships with the next package
256 // without a full project rescan). Idempotent on mAbsolutePath.
257 void AddRawAssetEntry(const RawAssetEntry& entry);
258
259 // Addon-provided source-extension dispatch (see free-function declarations
260 // above for usage). Stored on the manager so the table survives addon hot-
261 // reload as long as the addon re-registers in its OnLoad.
262 void RegisterImportExtension(const std::string& ext, TypeId type);
263 TypeId LookupImportExtension(const std::string& ext) const;
264
265 // Read the {asset}.meta sidecar at <assetPath>.meta. Returns defaults with
266 // mExists=false when the sidecar is missing or unparseable.
267 static AssetMetaSidecar LoadAssetMeta(const std::string& assetPath);
268
269 // Write the {asset}.meta sidecar. If platformMask == PlatformBit_All AND
270 // embed == false, the sidecar is deleted from disk instead of written so
271 // the source tree stays clean for assets at all-defaults.
272 static void SaveAssetMeta(const std::string& assetPath, uint32_t platformMask, bool embed);
273
274 // Persist new packaging flags to disk via SaveAssetMeta AND update in-memory
275 // AssetStub::mPlatformMask/mEmbed (if assetPath matches a known stub) AND
276 // RawAssetEntry::mPlatformMask/mEmbed (if assetPath matches a raw entry).
277 // Use this from the editor inspector so subsequent packaging sees the new
278 // flags without needing a full rediscover.
279 void ApplyAssetMetaFlags(const std::string& assetPath, uint32_t platformMask, bool embed);
280
281protected:
282 std::unordered_map<TypeId, glm::vec4> mAssetColorMap;
283 std::vector<RawAssetEntry> mRawAssetEntries;
284 std::unordered_map<std::string, TypeId> mImportExtensionMap;
285#endif
286};
287
288template<typename T>
290{
291 T* ret = new T();
293 return ret;
294
295 // Caller still needs to call Create() when ready!
296}
POLYPHASE_API void UnloadAsset(const std::string &name)
Definition AssetManager.cpp:102
POLYPHASE_API void AsyncLoadAsset(const std::string &name, AssetRef *targetRef=nullptr)
Definition AssetManager.cpp:107
POLYPHASE_API AssetStub * FetchAssetStubByUuid(uint64_t uuid)
Definition AssetManager.cpp:133
POLYPHASE_API AssetStub * FetchAssetStub(const std::string &name)
Definition AssetManager.cpp:112
POLYPHASE_API Asset * FetchAsset(const std::string &name)
Definition AssetManager.cpp:92
POLYPHASE_API Asset * LoadAsset(const std::string &name)
Definition AssetManager.cpp:97
T * NewTransientAsset()
Definition AssetManager.h:289
POLYPHASE_API Asset * LoadAssetByUuid(uint64_t uuid)
Definition AssetManager.cpp:123
POLYPHASE_API Asset * FetchAssetByUuid(uint64_t uuid)
Definition AssetManager.cpp:118
POLYPHASE_API void AsyncLoadAssetByUuid(uint64_t uuid, AssetRef *targetRef=nullptr)
Definition AssetManager.cpp:128
#define INVALID_TYPE_ID
Definition Constants.h:40
uint32_t TypeId
Definition EngineTypes.h:71
@ PlatformBit_All
Definition EngineTypes.h:60
bool Update()
Definition Engine.cpp:790
void LogError(const char *format,...)
Definition Log.cpp:289
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
Definition AssetDir.h:11
Definition AssetManager.h:135
bool DoesAssetExist(const std::string &name)
Definition AssetManager.cpp:1649
POLYPHASE_API Asset * LoadAsset(const std::string &name)
Definition AssetManager.cpp:1302
void DiscoverDirectory(AssetDir *directory, bool engineDir)
Definition AssetManager.cpp:486
AssetDir * GetRootDirectory()
Definition AssetManager.cpp:2145
Asset * GetAssetByUuid(uint64_t uuid)
Definition AssetManager.cpp:1362
AssetStub * FindDefaultScene()
Definition AssetManager.cpp:1879
void DiscoverEmbeddedAssets(struct EmbeddedFile *assets, uint32_t numAssets)
Definition AssetManager.cpp:849
void UpdateEndLoadQueue()
Definition AssetManager.cpp:2272
Asset * LoadAssetByUuid(uint64_t uuid)
Definition AssetManager.cpp:1368
POLYPHASE_API AssetDir * GetAssetDirFromPath(const std::string &dirPath)
Definition AssetManager.cpp:405
bool mPurging
Definition AssetManager.h:239
std::vector< std::string > GetAvailableFontFiles()
Definition AssetManager.cpp:1962
static void Create()
Definition AssetManager.cpp:184
void DiscoverAddonPackages(const std::string &packagesDir)
Definition AssetManager.cpp:2108
void AsyncLoadAsset(const std::string &name, AssetRef *targetRef)
Definition AssetManager.cpp:1463
void GatherScriptFiles(const std::string &dir, std::vector< std::string > &outFiles)
Definition AssetManager.cpp:1782
void Initialize()
Definition AssetManager.cpp:228
AssetStub * GetSceneAsset(const std::string &name)
Definition AssetManager.cpp:1289
void GatherFontFiles(const std::string &dir, std::vector< std::string > &outFiles)
Definition AssetManager.cpp:1832
POLYPHASE_API bool PurgeAsset(const char *name)
Definition AssetManager.cpp:950
static AssetManager * sInstance
Definition AssetManager.h:229
POLYPHASE_API void SaveAsset(const std::string &name)
Definition AssetManager.cpp:1535
static POLYPHASE_API AssetManager * Get()
Definition AssetManager.cpp:199
MutexObject * mMutex
Definition AssetManager.h:244
void Purge(bool purgeEngineAssets)
Definition AssetManager.cpp:899
std::vector< std::string > GetAvailableScriptFiles()
Definition AssetManager.cpp:1982
AssetManager()
Definition AssetManager.cpp:204
AssetStub * RegisterAsset(const std::string &filename, TypeId type, AssetDir *directory, EmbeddedFile *embeddedAsset, bool engineAsset, uint64_t uuid=0)
Definition AssetManager.cpp:241
bool mDestructing
Definition AssetManager.h:240
ThreadObject * mAsyncLoadThread
Definition AssetManager.h:243
bool RenameDirectory(AssetDir *dir, const std::string &newName)
Definition AssetManager.cpp:1746
bool RenameAsset(Asset *asset, const std::string &newName)
Definition AssetManager.cpp:1654
bool IsPurging() const
Definition AssetManager.cpp:461
std::deque< AsyncLoadRequest * > mEndLoadQueue
Definition AssetManager.h:242
std::string GetPolyphaseDirectory()
Definition AssetManager.cpp:2150
AssetStub * GetAssetStubByPath(const std::string &path)
Definition AssetManager.cpp:1423
void AsyncLoadAssetByUuid(uint64_t uuid, AssetRef *targetRef)
Definition AssetManager.cpp:1378
AssetStub * GetAssetStub(const std::string &name)
Definition AssetManager.cpp:1268
std::string GetParentDirectory(const std::string &path)
Definition AssetManager.cpp:1725
std::vector< Asset * > & GetTransientAssets()
Definition AssetManager.cpp:2183
POLYPHASE_API void RegisterTransientAsset(Asset *asset)
Definition AssetManager.cpp:1085
POLYPHASE_API AssetStub * CreateAndRegisterAsset(TypeId assetType, AssetDir *directory, const std::string &filename, bool engineAsset)
Definition AssetManager.cpp:352
std::vector< AssetStub * > GatherDirtyAssets()
Definition AssetManager.cpp:2188
static ThreadFuncRet AsyncLoadThreadFunc(void *in)
Definition AssetManager.cpp:2211
~AssetManager()
Definition AssetManager.cpp:211
void LoadAll()
Definition AssetManager.cpp:1066
void RefreshDirectory(AssetDir *directory)
Definition AssetManager.cpp:633
POLYPHASE_API AssetDir * FindProjectDirectory()
Definition AssetManager.cpp:2060
AssetDir * FindPackagesDirectory()
Definition AssetManager.cpp:2092
std::unordered_map< std::string, AssetStub * > mAssetMap
Definition AssetManager.h:234
AssetStub * GetAssetStubByUuid(uint64_t uuid)
Definition AssetManager.cpp:1354
void UnloadProjectDirectory()
Definition AssetManager.cpp:2158
void DiscoverAssetRegistry(const char *registryPath)
Definition AssetManager.cpp:755
AssetDir * FindEngineDirectory()
Definition AssetManager.cpp:2076
std::unordered_map< std::string, AssetStub * > mAssetPathMap
Definition AssetManager.h:235
void GatherScriptFilesRecursive(const std::string &dirPath, const std::string &relativePath, std::vector< std::string > &scriptFiles)
void RefSweep()
Definition AssetManager.cpp:1017
std::deque< AsyncLoadRequest * > mBeginLoadQueue
Definition AssetManager.h:241
bool UnloadAsset(const std::string &name)
Definition AssetManager.cpp:1606
std::vector< Asset * > mTransientAssets
Definition AssetManager.h:237
std::unordered_map< uint64_t, AssetStub * > mUuidMap
Definition AssetManager.h:236
AssetDir * mRootDirectory
Definition AssetManager.h:238
std::string FindDefaultScenePath()
Definition AssetManager.cpp:1892
static void Destroy()
Definition AssetManager.cpp:190
AssetDir * FindProjectRootDirectory()
POLYPHASE_API std::unordered_map< std::string, AssetStub * > & GetAssetMap()
Definition AssetManager.cpp:2178
void ImportEngineAssets()
Definition AssetManager.cpp:1146
POLYPHASE_API Asset * GetAsset(const std::string &name)
Definition AssetManager.cpp:1276
Asset * LoadAssetByPath(const std::string &path)
Definition AssetManager.cpp:1453
Asset * ImportEngineAsset(TypeId assetType, AssetDir *dir, const std::string &filename, ImportOptions *options=nullptr)
Definition AssetManager.cpp:1091
bool IsAssetLive(const Asset *asset) const
Definition AssetManager.cpp:466
void Discover(const char *directoryName, const char *directoryPath)
Definition AssetManager.cpp:733
Definition AssetRef.h:18
Definition Asset.h:119
Definition Asset.h:108
Definition Material.h:48
virtual bool Is(RuntimeId id) const
Definition Object.h:29
Definition ParticleSystem.h:66
Definition Asset.h:88
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