Polyphase Game Engine
Loading...
Searching...
No Matches
Asset.h
Go to the documentation of this file.
1#pragma once
2
3#include "PolyphaseAPI.h"
4#include "EngineTypes.h"
5#include "EmbeddedFile.h"
6#include "Factory.h"
7#include "Object.h"
8#include "Maths.h"
9
10#include <string>
11#include <vector>
12#include <unordered_map>
13
14class Stream;
15class Property;
16class AssetDir;
17
18#define ASSET_MAGIC_NUMBER 0x4f435421
19
20//----------------------------------------------------
21// --------------- ASSET VERSIONING ------------------
22// ---------------------------------------------------
23#define ASSET_VERSION_BASE 1
24#define ASSET_VERSION_SCENE_EXTRA_DATA 2
25#define ASSET_VERSION_PARTICLE_RADIAL_SPAWN 3
26#define ASSET_VERSION_PROPERTY_EXTRA 4
27#define ASSET_VERSION_SCENE_SUB_SCENE_OVERRIDE 5
28#define ASSET_VERSION_FONT_TTF_FLAG 6
29#define ASSET_VERSION_TEXTURE_COOKED_PROPERTIES 7
30#define ASSET_VERSION_TEXTURE_LOW_QUALITY 8
31#define ASSET_VERSION_MATERIAL_LITE_TEXTURE_COUNT 9
32#define ASSET_VERSION_STATIC_MESH_3D_HAS_BAKED_LIGHTING 10
33#define ASSET_VERSION_SCENE_SUBSCENE_INSTANCE_COLORS 11
34#define ASSET_VERSION_UUID_SUPPORT 12
35#define ASSET_VERSION_UUID_WITH_NAME_FALLBACK 13
36#define ASSET_VERSION_NODE_PERSISTENT_UUID 14
37#define ASSET_VERSION_MATERIAL_LITE_NODE_GRAPH 15
38#define ASSET_VERSION_NODE_GRAPH_FUNCTIONS 16
39#define ASSET_VERSION_NODE_GRAPH_VARIABLES 17
40#define ASSET_VERSION_SCENE_ICON_OVERRIDE 18
41#define ASSET_VERSION_SCENE_MENU_OVERRIDE 19
42#define ASSET_VERSION_VOXEL3D 20
43#define ASSET_VERSION_VOXEL3D_ATLAS 21
44#define ASSET_VERSION_VOXEL3D_ATLAS_INT32 22
45#define ASSET_VERSION_TERRAIN3D 23
46#define ASSET_VERSION_TERRAIN3D_MATSLOTS 24
47#define ASSET_VERSION_TERRAIN3D_ATLAS 25
48#define ASSET_VERSION_TERRAIN3D_BAKE 26
49#define ASSET_VERSION_TERRAIN3D_BAKEDMAP 27
50#define ASSET_VERSION_TILESET_BASE 28
51#define ASSET_VERSION_TILEMAP_BASE 29
52#define ASSET_VERSION_TILESET_METADATA 30
53#define ASSET_VERSION_TILESET_AUTOTILE 31
54#define ASSET_VERSION_TRANSFORM_KEYFRAME_SIGNAL 32
55#define ASSET_VERSION_INPUT_PROMPT_MAP 33
56#define ASSET_VERSION_INPUT_PROMPT_STYLE 34
57#define ASSET_VERSION_MATERIAL_LITE_UV_SOURCE 35
58#define ASSET_VERSION_SKELETAL_MESH_SECTIONS 36
59#define ASSET_VERSION_BONE_MASK 37
60#define ASSET_VERSION_SOUNDWAVE_STREAMING 38
61#define ASSET_VERSION_BONE_SOCKETS 39
62#define ASSET_VERSION_SCENE_OCCLUSION 40
63#define ASSET_VERSION_SCENE_OCCLUSION_DYNAMIC 41
64#define ASSET_VERSION_SCENE_OCCLUSION_ALGO 42
65#define ASSET_VERSION_INPUT_PROMPT_MAP_PLATFORM_MAC 43
66#define ASSET_VERSION_CURRENT 43
67// ----------------------------------------------------
68
69#define DECLARE_ASSET(Base, Parent) DECLARE_FACTORY(Base, Asset); DECLARE_OBJECT(Base, Parent);
70#define DEFINE_ASSET(Base) DEFINE_FACTORY(Base, Asset); DEFINE_OBJECT(Base);
71
72POLYPHASE_API extern bool HandleAssetPropChange(Datum* datum, uint32_t index, const void* newValue);
73
75{
79 Loaded,
80
81 Count
82};
83
85{
89 uint8_t mEmbedded = false;
90 uint64_t mUuid = 0; // Added in ASSET_VERSION_UUID_SUPPORT (12)
91};
92
94{
95 Asset* mAsset = nullptr;
96 const EmbeddedFile* mEmbeddedData = nullptr;
97 std::string mPath;
99 bool mEngineAsset = false;
100 uint64_t mUuid = 0; // Primary identifier
101
102 // Per-asset packaging metadata, sourced from the {asset}.meta sidecar at discovery time.
103 // Defaults preserve pre-meta behaviour: ship to all platforms, loose on disk.
105 bool mEmbed = false;
106
107#if EDITOR
108 std::string mName;
109 AssetDir* mDirectory = nullptr;
110#endif
111};
112
114{
115public:
116 bool HasOption(const std::string& key);
117 Datum GetOptionValue(const std::string& key);
118 void SetOptionValue(const std::string& key, const Datum& value);
119
120protected:
121 std::unordered_map<std::string, Datum> mOptions;
122};
123
125{
126public:
127
131
132 Asset();
133 virtual ~Asset();
134
135 virtual void Create();
136 virtual void Destroy();
137
138 virtual void Copy(Asset* srcAsset);
139
140 // Reset the loaded flag without destroying the asset or erasing live
141 // AssetRefs. Lets a caller tear down the GPU/physics side of an asset
142 // and then call Create() again to rebuild from new data, while any
143 // scene references to this asset stay intact. Used by editor-time
144 // rebuilds such as StaticMesh::CreateRaw being invoked on an asset
145 // that AssetManager already auto-Create()'d at registration time.
146 void ResetLoadedFlag();
147
148 const std::string& GetName() const;
149 bool IsLoaded() const;
150 bool IsRefCounted() const;
151 int32_t GetRefCount() const;
152
153 bool IsEmbedded() const;
154 void SetEmbedded(bool embed);
155
156 bool IsEngineAsset() const;
157 void SetEngineAsset(bool engineAsset);
158
159 void SetName(const std::string& name);
160 void IncrementRefCount();
161 void DecrementRefCount();
162
163 void LoadFile(const char* path, AsyncLoadRequest* request = nullptr);
164 void LoadEmbedded(const EmbeddedFile* embeddedAsset, AsyncLoadRequest* request = nullptr);
165 void SaveFile(const char* path, Platform platform);
166
167 virtual void LoadStream(Stream& stream, Platform platform);
168 virtual void SaveStream(Stream& stream, Platform platform);
169 virtual bool Import(const std::string& path, ImportOptions* options = nullptr);
170
171 virtual void GatherProperties(std::vector<Property>& outProps) override;
172 virtual glm::vec4 GetTypeColor();
173 virtual const char* GetTypeName();
174 virtual const char* GetTypeImportExt();
175
176 // Editor-only behavior: when true, EditorState snapshots this asset's
177 // serialized state (and dirty flag) at PIE start and restores it at PIE
178 // stop, so script mutations during play don't leak into the editor view.
179 // Kept unconditional to keep vtable layout identical across editor and
180 // runtime builds; the runtime never calls this.
181 virtual bool ShouldSnapshotForPie() const { return false; }
182
183 bool IsTransient() const;
184 void SetTransient(bool transient);
185
186 uint64_t GetUuid() const;
187 void SetUuid(uint64_t uuid);
188 void EnsureUuid(); // Generate UUID if not already assigned
189
190 static AssetHeader ReadHeader(Stream& stream);
191 void WriteHeader(Stream& stream);
192
193 static std::string GetNameFromPath(const std::string& path);
194 static std::string GetDirectoryFromPath(const std::string& path);
195
196 static const char* GetNameFromTypeId(TypeId id);
197 static TypeId GetTypeIdFromName(const char* name);
198
199protected:
200
201 uint32_t mVersion = 0;
203 uint64_t mUuid = 0;
204 bool mEmbedded = false;
205 bool mLoaded = false;
206 bool mEnableRefCount = true;
207 bool mEngineAsset = false;
208 bool mTransient = false;
209
210 std::string mName = "Asset";
211 int32_t mRefCount = 0;
212
213#if EDITOR
214public:
215 void ClearDirtyFlag();
216 void SetDirtyFlag();
217 bool GetDirtyFlag();
218
219protected:
220 bool mDirty = false;
221#endif
222};
#define ASSET_MAGIC_NUMBER
Definition Asset.h:18
POLYPHASE_API bool HandleAssetPropChange(Datum *datum, uint32_t index, const void *newValue)
Definition Asset.cpp:34
AssetLoadState
Definition Asset.h:75
#define ASSET_VERSION_CURRENT
Definition Asset.h:66
#define INVALID_TYPE_ID
Definition Constants.h:40
Platform
Definition EngineTypes.h:32
uint32_t TypeId
Definition EngineTypes.h:75
@ PlatformBit_All
Definition EngineTypes.h:64
Export macros for Polyphase Engine symbols.
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
Definition AssetDir.h:13
Definition Asset.h:125
virtual bool ShouldSnapshotForPie() const
Definition Asset.h:181
DECLARE_OBJECT(Asset, Object)
DECLARE_FACTORY(Asset, Asset)
DECLARE_FACTORY_MANAGER(Asset)
Definition Datum.h:169
Definition Asset.h:114
void SetOptionValue(const std::string &key, const Datum &value)
Definition Asset.cpp:63
Datum GetOptionValue(const std::string &key)
Definition Asset.cpp:52
bool HasOption(const std::string &key)
Definition Asset.cpp:45
std::unordered_map< std::string, Datum > mOptions
Definition Asset.h:121
Definition Object.h:13
virtual void GatherProperties(std::vector< Property > &props)
Definition Object.h:41
Definition Property.h:14
Definition Stream.h:21
Definition Asset.h:85
TypeId mType
Definition Asset.h:88
uint64_t mUuid
Definition Asset.h:90
uint32_t mVersion
Definition Asset.h:87
uint8_t mEmbedded
Definition Asset.h:89
uint32_t mMagic
Definition Asset.h:86
Definition Asset.h:94
TypeId mType
Definition Asset.h:98
uint64_t mUuid
Definition Asset.h:100
bool mEmbed
Definition Asset.h:105
Asset * mAsset
Definition Asset.h:95
const EmbeddedFile * mEmbeddedData
Definition Asset.h:96
uint32_t mPlatformMask
Definition Asset.h:104
std::string mPath
Definition Asset.h:97
bool mEngineAsset
Definition Asset.h:99
Definition AssetManager.h:19
Definition EmbeddedFile.h:6