LkEngine 0.1.2
 
Loading...
Searching...
No Matches
RuntimeAssetManager.h
1#pragma once
2
3#include "LkEngine/Asset/IAssetManager.h"
4#include "LkEngine/Asset/AssetImporter.h"
5
6namespace LkEngine {
7
9 {
10 public:
13
14 virtual void Initialize() override;
15 virtual void Destroy() override;
16
17 template<typename AssetType>
18 TObjectPtr<AssetType> ImportAsset(const std::filesystem::path& Filepath);
19
20 virtual FAssetHandle ImportAsset(const std::filesystem::path& Filepath) override;
21 virtual void RemoveAsset(const FAssetHandle Handle) override;
22
23 virtual TObjectPtr<LAsset> GetAsset(const FAssetHandle Handle) override;
24 virtual EAssetType GetAssetType(const FAssetHandle Handle) override;
25
26 virtual bool ReloadData(const FAssetHandle Handle) override;
27 virtual void AddMemoryOnlyAsset(TObjectPtr<LAsset> Asset) override;
28 virtual bool IsAssetHandleValid(const FAssetHandle Handle) const override;
29 virtual bool IsAssetLoaded(const FAssetHandle Handle) const override;
30
31 virtual bool IsMemoryAsset(const FAssetHandle Handle) const override
32 {
33 return MemoryAssets.contains(Handle);
34 }
35
36 virtual bool LoadAssetRegistry() override;
37 virtual void WriteRegistryToDisk() override;
38
39 virtual const FAssetMetadata& GetMetadata(const FAssetHandle Handle) const override;
40 virtual const FAssetMetadata& GetMetadata(const TObjectPtr<LAsset>& Asset) const override;
41 virtual const FAssetMetadata& GetMetadata(const std::filesystem::path& InFilePath) const override;
42
43 virtual FAssetHandle GetAssetHandleFromFilePath(const std::filesystem::path& InFilePath) const override;
44 virtual EAssetType GetAssetTypeFromExtension(const std::string& Extension) const override;
45 virtual EAssetType GetAssetTypeFromPath(const std::filesystem::path& InFilePath) const override;
46
47 virtual std::unordered_set<FAssetHandle> GetAllAssetsWithType(const EAssetType AssetType) override;
48 virtual std::size_t GetAllAssetsWithType(const EAssetType AssetType, std::unordered_set<FAssetHandle>& AssetsOfType) override;
49
50 std::filesystem::path GetFileSystemPath(const FAssetMetadata& Metadata) const;
51 std::filesystem::path GetFileSystemPath(const FAssetHandle Handle) const;
52 std::filesystem::path GetRelativePath(const std::filesystem::path& InFilePath) const;
53
54 template<typename T>
55 TObjectPtr<T> GetAsset(const std::string& InFilePath)
56 {
57 TObjectPtr<LAsset> Asset = GetAsset(GetAssetHandleFromFilePath(InFilePath));
58 if (Asset)
59 {
60 return Asset->As<T>();
61 }
62
63 LK_CORE_ERROR_TAG("RuntimeAssetManager", "Failed to find asset with path: {}", InFilePath);
64 return nullptr;
65 }
66
67 template<typename T, typename... TArgs>
68 TObjectPtr<T> CreateNewAsset(const std::string& FileName, const std::string& DirectoryPath, TArgs&&... Args)
69 {
70 static_assert(std::is_base_of_v<LAsset, T>, "CreateNewAsset only works for assets derived from LAsset");
71 FAssetMetadata Metadata{};
72 Metadata.Handle = FAssetHandle();
73
74 if (DirectoryPath.empty() || DirectoryPath == ".")
75 {
76 Metadata.FilePath = FileName;
77 }
78 else
79 {
80 Metadata.FilePath = GetRelativePath(DirectoryPath + "/" + FileName);
81 }
82
83 Metadata.bIsDataLoaded = true;
84 Metadata.Type = T::GetStaticType();
85
86 AssetRegistry[Metadata.Handle] = Metadata;
87 WriteRegistryToDisk();
88
89 TObjectPtr<T> Asset = TObjectPtr<T>::Create(std::forward<TArgs>(Args)...);
90 Asset->Handle = Metadata.Handle;
91 LoadedAssets[Asset->Handle] = Asset;
92 LAssetImporter::Serialize(Metadata, Asset);
93
94 return Asset;
95 }
96
97 virtual const std::unordered_map<FAssetHandle, TObjectPtr<LAsset>>& GetLoadedAssets() const override
98 {
99 return LoadedAssets;
100 }
101
102 virtual const std::unordered_map<FAssetHandle, TObjectPtr<LAsset>>& GetMemoryOnlyAssets() const override
103 {
104 return MemoryAssets;
105 }
106
107 int GetTextures2D(std::vector<TObjectPtr<LTexture2D>>& TextureContainer);
108 int GetTextures2D(std::vector<std::pair<std::string, TObjectPtr<LTexture2D>>>& TextureContainer);
109 const std::vector<std::pair<std::string, TObjectPtr<LTexture2D>>>& GetTextures2D() const;
110
111 FORCEINLINE const LAssetRegistry& GetAssetRegistry() const { return AssetRegistry; }
112
113 private:
114 virtual FAssetMetadata& GetMetadataInternal(const FAssetHandle Handle) override;
115
116 void LoadBaseMaterials();
117 void LoadPrimitiveShapes();
118
119 private:
120 LAssetRegistry AssetRegistry;
121 bool bAssetRegistryValid = false;
122
123 std::unordered_map<FAssetHandle, TObjectPtr<LAsset>> LoadedAssets{};
124 std::unordered_map<FAssetHandle, TObjectPtr<LAsset>> MemoryAssets{};
125
126 std::unordered_map<std::string, TObjectPtr<LTexture2D>> Texture2DMap{};
127 };
128
129}
Definition IAssetManager.h:19
Definition AssetRegistry.h:11
Definition RuntimeAssetManager.h:9
virtual void Destroy() override
Destroy object, releasing all resources.
Definition RuntimeAssetManager.cpp:47
virtual void Initialize() override
Initialize object.
Definition RuntimeAssetManager.cpp:33
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 UUID.h:13