LkEngine 0.1.2
 
Loading...
Searching...
No Matches
EditorAssetManager.h
1#pragma once
2
3#include "LkEngine/Core/IO/FileSystem.h"
4
5#include "LkEngine/Asset/IAssetManager.h"
6#include "LkEngine/Asset/AssetImporter.h"
7
8namespace LkEngine {
9
10 static constexpr const char* BASE_MATERIAL = "BaseMaterial";
11 static constexpr const char* BASE_MATERIAL2 = "BaseMaterial2";
12
14 {
15 static TObjectPtr<LTexture2D> Load(const std::filesystem::path& RelativePath, FTextureSpecification& Spec)
16 {
17 std::filesystem::path Path = std::filesystem::path("Assets") / RelativePath;
18 if (!LFileSystem::Exists(Path))
19 {
20 LK_CORE_VERIFY(false, "Failed to load texture '{}', the file does not exist", Path.string());
21 return nullptr;
22 }
23
24 Spec.Path = Path.string();
25
26 return LTexture2D::Create(Spec);
27 }
28 };
29
31 {
32 public:
35
36 virtual void Initialize() override;
37 virtual void Destroy() override;
38
39 template<typename AssetType>
40 TObjectPtr<AssetType> ImportAsset(const std::filesystem::path& Filepath);
41
42 virtual FAssetHandle ImportAsset(const std::filesystem::path& Filepath) override;
43 virtual void RemoveAsset(const FAssetHandle Handle) override;
44
45 virtual TObjectPtr<LAsset> GetAsset(const FAssetHandle Handle) override;
46 virtual EAssetType GetAssetType(const FAssetHandle Handle) override;
47
48 virtual bool ReloadData(const FAssetHandle Handle) override;
49 virtual void AddMemoryOnlyAsset(TObjectPtr<LAsset> Asset) override;
50 virtual bool IsAssetHandleValid(const FAssetHandle Handle) const override;
51 virtual bool IsAssetLoaded(const FAssetHandle Handle) const override;
52
53 virtual bool IsMemoryAsset(const FAssetHandle Handle) const override
54 {
55 return MemoryAssets.contains(Handle);
56 }
57
58 virtual bool LoadAssetRegistry() override;
59 virtual void WriteRegistryToDisk() override;
60
61 virtual const FAssetMetadata& GetMetadata(const FAssetHandle Handle) const override;
62 virtual const FAssetMetadata& GetMetadata(const TObjectPtr<LAsset>& Asset) const override;
63 virtual const FAssetMetadata& GetMetadata(const std::filesystem::path& InFilePath) const override;
64
65 virtual FAssetHandle GetAssetHandleFromFilePath(const std::filesystem::path& InFilePath) const override;
66 virtual EAssetType GetAssetTypeFromExtension(const std::string& Extension) const override;
67 virtual EAssetType GetAssetTypeFromPath(const std::filesystem::path& InFilePath) const override;
68
69 virtual std::unordered_set<FAssetHandle> GetAllAssetsWithType(const EAssetType AssetType) override;
70 virtual std::size_t GetAllAssetsWithType(const EAssetType AssetType, std::unordered_set<FAssetHandle>& AssetsOfType) override;
71
72 std::filesystem::path GetFileSystemPath(const FAssetMetadata& Metadata) const;
73 std::filesystem::path GetFileSystemPath(const FAssetHandle Handle) const;
74 std::filesystem::path GetRelativePath(const std::filesystem::path& InFilePath) const;
75
76 template<typename T>
77 TObjectPtr<T> GetAsset(const std::string& InFilePath)
78 {
79 TObjectPtr<LAsset> Asset = GetAsset(GetAssetHandleFromFilePath(InFilePath));
80 if (Asset)
81 {
82 return Asset->As<T>();
83 }
84
85 LK_CORE_ERROR_TAG("RuntimeAssetManager", "Failed to find asset with path: {}", InFilePath);
86 return nullptr;
87 }
88
89 template<typename T, typename... TArgs>
90 TObjectPtr<T> CreateNewAsset(const std::string& FileName, const std::string& DirectoryPath, TArgs&&... Args)
91 {
92 static_assert(std::is_base_of_v<LAsset, T>, "CreateNewAsset only works for Assets derived from LAsset");
93
94 FAssetMetadata Metadata{};
95 Metadata.Handle = FAssetHandle();
96
97 if (DirectoryPath.empty() || DirectoryPath == ".")
98 {
99 Metadata.FilePath = FileName;
100 }
101 else
102 {
103 Metadata.FilePath = GetRelativePath(DirectoryPath + "/" + FileName);
104 }
105
106 Metadata.bIsDataLoaded = true;
107 Metadata.Type = T::GetStaticType();
108
109 AssetRegistry[Metadata.Handle] = Metadata;
110 WriteRegistryToDisk();
111
112 TObjectPtr<T> Asset = TObjectPtr<T>::Create(std::forward<TArgs>(Args)...);
113 Asset->Handle = Metadata.Handle;
114 LoadedAssets[Asset->Handle] = Asset;
115 LAssetImporter::Serialize(Metadata, Asset);
116
117 return Asset;
118 }
119
120 virtual const std::unordered_map<FAssetHandle, TObjectPtr<LAsset>>& GetLoadedAssets() const override
121 {
122 return LoadedAssets;
123 }
124
125 virtual const std::unordered_map<FAssetHandle, TObjectPtr<LAsset>>& GetMemoryOnlyAssets() const override
126 {
127 return MemoryAssets;
128 }
129
130 int GetTextures2D(std::vector<TObjectPtr<LTexture2D>>& TextureContainer);
131 int GetTextures2D(std::vector<std::pair<std::string, TObjectPtr<LTexture2D>>>& TextureContainer);
132 const std::vector<std::pair<std::string, TObjectPtr<LTexture2D>>>& GetTextures2D() const;
133
134 FORCEINLINE const LAssetRegistry& GetAssetRegistry() const { return AssetRegistry; }
135
136 private:
137 virtual FAssetMetadata& GetMetadataInternal(const FAssetHandle Handle) override;
138
139 void LoadBaseMaterials();
140 void LoadPrimitiveShapes();
141
142 public:
143 inline static TObjectPtr<LMaterialAsset> BaseMaterial{};
144 inline static TObjectPtr<LMaterialAsset> BaseMaterial2{};
145
146 /* Base materials. */
147 inline static TObjectPtr<LMaterialAsset> Material_WoodContainer{};
148 inline static TObjectPtr<LMaterialAsset> Material_Metal{};
149 private:
150 LAssetRegistry AssetRegistry;
151 bool bAssetRegistryValid = false;
152
153 std::unordered_map<FAssetHandle, TObjectPtr<LAsset>> LoadedAssets{};
154 std::unordered_map<FAssetHandle, TObjectPtr<LAsset>> MemoryAssets{};
155
156 std::unordered_map<std::string, TObjectPtr<LTexture2D>> Texture2DMap{};
157 };
158
159}
Definition IAssetManager.h:19
Definition AssetRegistry.h:11
Definition EditorAssetManager.h:31
virtual void Initialize() override
Initialize object.
Definition EditorAssetManager.cpp:33
virtual void Destroy() override
Destroy object, releasing all resources.
Definition EditorAssetManager.cpp:100
Definition ObjectPtr.h:102
static TObjectPtr< T > Create(TArgs &&... Args)
Create new pointer instance of type T.
Definition Asset.h:11
EAssetType
Definition AssetTypes.h:24
Definition Asset.h:92
Definition EditorAssetManager.h:14
Definition Image.h:21
Definition UUID.h:13