1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
#include "Assets.hpp"
#include <imgui.h>
#include <fstream>
#include <utility>
using namespace std::literals::string_view_literals;
namespace fs = std::filesystem;
Asset::Asset()
{
}
void AssetList::Reload()
{
DiscoverFiles([this](SavedAsset asset) -> void {
Create(std::move(asset));
});
}
const SavedAsset* AssetList::FindByName(std::string_view name) const
{
auto iter = mAssets.find(name);
if (iter != mAssets.end()) {
return &iter.value();
} else {
return nullptr;
}
}
const SavedAsset& AssetList::Create(SavedAsset asset)
{
auto [iter, DISCARD] = mAssets.insert(asset.Name, SavedAsset{});
auto& savedAsset = iter.value();
savedAsset = std::move(asset);
if (savedAsset.Uuid.is_nil()) {
savedAsset.Uuid = uuids::uuid_random_generator{}();
}
SaveEmptyInstance(savedAsset);
return savedAsset;
}
std::unique_ptr<Asset> AssetList::CreateAndLoad(SavedAsset assetIn)
{
auto& savedAsset = Create(std::move(assetIn));
auto asset = std::unique_ptr<Asset>(CreateEmptyInstance(savedAsset));
return asset;
}
std::unique_ptr<Asset> AssetList::Load(std::string_view name) const
{
if (auto savedAsset = FindByName(name)) {
auto asset = Load(*savedAsset);
return asset;
} else {
return nullptr;
}
}
std::unique_ptr<Asset> AssetList::Load(const SavedAsset& asset) const
{
return std::unique_ptr<Asset>(LoadImpl(asset));
}
const SavedAsset* AssetList::Rename(std::string_view oldName, std::string_view newName)
{
auto iter = mAssets.find(oldName);
if (iter == mAssets.end()) return nullptr;
auto info = std::move(iter.value());
info.Name = newName;
auto [newIter, DISCARD] = mAssets.insert(newName, std::move(info));
mAssets.erase(iter);
return &newIter.value();
}
bool AssetList::Remove(std::string_view name)
{
auto iter = mAssets.find(name);
if (iter == mAssets.end()) {
return false;
}
auto& asset = iter.value();
fs::remove(RetrievePathFromAsset(asset));
mAssets.erase(iter);
return true;
}
int AssetList::GetCacheSizeLimit() const
{
return mCacheSizeLimit;
}
void AssetList::SetCacheSizeLimit(int limit)
{
mCacheSizeLimit = limit;
}
void AssetList::DrawBigIcons(ListState& state)
{
// TODO
}
void AssetList::DrawDetails(ListState& state)
{
SetupDetailsTable("AssetDetailsTable");
for (auto& asset : mAssets) {
DrawDetailsTableRow(asset);
ImGui::TableNextRow();
}
ImGui::EndTable();
}
void AssetList::DiscoverFilesByExtension(const std::function<void(SavedAsset)>& callback, const fs::path& containerDir, std::string_view extension) const
{
for (auto entry : fs::directory_iterator(containerDir)) {
if (!entry.is_regular_file()) continue;
// If the caller provided an extension to match against, and it doesn't equal to current file extension, skip
if (!extension.empty() &&
entry.path().extension() != extension)
{
continue;
}
callback(SavedAsset{
.Name = RetrieveNameFromFile(entry.path()),
.Uuid = RetrieveUuidFromFile(entry.path()),
// TODO load payload
});
}
}
void AssetList::DiscoverFilesByHeader(const std::function<void(SavedAsset)>& callback, const fs::path& containerDir, const std::function<bool(std::istream&)>& validater) const
{
// TODO
}
|