Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
AssetImporter.hpp
1#pragma once
2
3#include <string>
4#include <map>
5
6#include <Common/ResourcePipeline/Uuid.hpp>
7#include <Common/ResourcePipeline/AssetType.hpp>
8
9namespace Grindstone {
11 public:
12 virtual ~AssetImporter() {};
13
14 virtual void QueueReloadAsset(Uuid uuid) = 0;
15 virtual void* ProcessLoadedFile(Uuid uuid) = 0;
16 virtual bool TryGetIfLoaded(Uuid uuid, void*& output) = 0;
17 virtual void* ProcessLoadedFile(std::string_view address) { return nullptr; };
18 virtual bool TryGetIfLoaded(std::string_view address, void*& output) { return false; };
19 virtual void* IncrementAssetUse(Uuid uuid) = 0;
20 virtual void DecrementAssetUse(Uuid uuid) = 0;
21 virtual AssetType GetAssetType() { return assetType; }
22
23 protected:
24 AssetType assetType = AssetType::Undefined;
25
26 };
27
28 template<typename AssetStructType, AssetType internalAssetType>
30 public:
31
32 SpecificAssetImporter() { assetType = internalAssetType; }
33 static AssetType GetStaticAssetType() { return internalAssetType; }
34
35 virtual void* IncrementAssetUse(Uuid uuid) override {
36 void* output = nullptr;
37 if (TryGetIfLoaded(uuid, output)) {
38 AssetStructType* asset = static_cast<AssetStructType*>(output);
39 ++asset->referenceCount;
40 return asset;
41 }
42 else {
43 return ProcessLoadedFile(uuid);
44 }
45
46 return nullptr;
47 }
48
49 virtual void DecrementAssetUse(Uuid uuid) override {
50 void* output = nullptr;
51 if (TryGetIfLoaded(uuid, output) && output != nullptr) {
52 AssetStructType* asset = static_cast<AssetStructType*>(output);
53 if (asset->referenceCount <= 1) {
54 assets.erase(uuid);
55 }
56 else {
57 --asset->referenceCount;
58 }
59 }
60 }
61
62 virtual bool TryGetIfLoaded(Uuid uuid, void*& output) override {
63 auto assetInMap = assets.find(uuid);
64 if (assetInMap != assets.end()) {
65 output = &assetInMap->second;
66 return true;
67 }
68
69 return false;
70 }
71
72 size_t AssetCount() const { return assets.size(); }
73 bool HasAssets() const { return assets.size() != 0; }
74
75 auto begin() noexcept { return assets.begin(); }
76 auto cbegin() const noexcept { return assets.begin(); }
77 auto end() noexcept { return assets.end(); }
78 auto cend() const noexcept { return assets.cend(); }
79
80 protected:
81 std::map<Uuid, AssetStructType> assets;
82
83 };
84}
Definition AssetImporter.hpp:10
Definition AssetImporter.hpp:29
Definition Uuid.hpp:6