Finality Engine
Loading...
Searching...
No Matches
AssetManager.h
Go to the documentation of this file.
1
6#pragma once
7#include <Assets/Asset.h>
8#include <unordered_map>
9#include <memory>
10#include <string>
11
12#include <Core/Core.h>
13#include "AssetRegistry.h"
14
15namespace FINALITY
16{
17 class Texture;
20 {
21 public:
22 static void Initialize();
23 static void Shutdown();
24
25 template<typename T, typename... Args>
26 static std::shared_ptr<T> CreateAsset(const std::string& filePath, Args&&... args)
27 {
28 const std::string resolved = AssetRegistry::Resolve(filePath);
29 if (resolved.empty()) return nullptr;
30 auto it = s_PathToHandleRegistry.find(resolved);
31 if (it != s_PathToHandleRegistry.end())
32 {
33 return std::dynamic_pointer_cast<T>(s_LoadedAssets[it->second]);
34 }
35
36 std::shared_ptr<T> asset;
37 try { asset = T::Create(resolved, std::forward<Args>(args)...); }
38 catch (const std::exception&) { return nullptr; }
39 if (!asset) return nullptr;
40
41 const auto* record = AssetRegistry::FindPath(resolved);
42 UUID handle = record ? UUID(record->ID) : UUID();
43 asset->Handle = handle;
44
45 s_LoadedAssets[handle] = asset;
46 s_PathToHandleRegistry[resolved] = handle;
47
48 return asset;
49 }
50
51 static std::shared_ptr<Asset> GetAsset(UUID handle);
52 static bool HasAsset(UUID handle);
53 static size_t ReloadMaterialShaders(bool force = false);
54 static void InvalidateAssetPath(const std::string& filePath);
55 static std::shared_ptr<Texture> ReloadTexture(const std::string& filePath);
56
57 private:
58 static std::unordered_map<UUID, std::shared_ptr<Asset>> s_LoadedAssets;
59 static std::unordered_map<std::string, UUID> s_PathToHandleRegistry;
60 };
61}
Tracks project assets, metadata and reference resolution.
#define FAPI
Definition Core.h:22
static void Initialize()
static bool HasAsset(UUID handle)
static std::shared_ptr< Texture > ReloadTexture(const std::string &filePath)
static std::shared_ptr< Asset > GetAsset(UUID handle)
static std::shared_ptr< T > CreateAsset(const std::string &filePath, Args &&... args)
static void InvalidateAssetPath(const std::string &filePath)
static size_t ReloadMaterialShaders(bool force=false)
static void Shutdown()
static const AssetRecord * FindPath(const std::filesystem::path &path)
static std::string Resolve(const std::string &reference)