aboutsummaryrefslogtreecommitdiff
path: root/core/src/Model/Assets.cpp
blob: e4eee54704a6ed8240ee61a37b4004a57e97063e (plain)
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
#include "Assets.hpp"

#include <imgui.h>
#include <fstream>
#include <utility>

namespace fs = std::filesystem;

Asset::Asset()
{
}

std::unique_ptr<Asset> AssetCategory::CreateEmptyUnique(const SavedAsset& diskForm) const
{
	return std::unique_ptr<Asset>(CreateEmpty(diskForm));
}

std::unique_ptr<Asset> AssetCategory::LoadUnique(const SavedAsset& diskForm) const
{
	return std::unique_ptr<Asset>(Load(diskForm));
}

void AssetCategory::DiscoverFilesByExtension(const std::function<void(SavedAsset)>& callback, const std::filesystem::path& containerDir, std::string_view extension)
{
	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{
			.Path = entry.path(),
			.Name = entry.path().stem().string(),
		});
	}
}

void AssetCategory::DiscoverFilesByHeader(const std::function<void(SavedAsset)>& callback, const std::filesystem::__cxx11::path& containerDir, const std::function<bool(std::istream&)>& validater)
{
	// TODO
}

AssetList::AssetList(const AssetCategory& loader)
	: mLoader{ &loader }
{
}

void AssetList::Reload()
{
	mLoader->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();

	mLoader->CreateEmpty(asset);
	savedAsset = std::move(asset);

	return savedAsset;
}

std::unique_ptr<Asset> AssetList::CreateAndLoad(SavedAsset assetIn)
{
	auto& savedAsset = Create(std::move(assetIn));
	auto asset = std::unique_ptr<Asset>(mLoader->CreateEmpty(savedAsset));
	return asset;
}

std::unique_ptr<Asset> AssetList::LoadFromDisk(std::string_view name) const
{
	if (auto savedAsset = FindByName(name)) {
		auto asset = mLoader->LoadUnique(*savedAsset);
		return asset;
	}
}

bool AssetList::Rename(std::string_view oldName, std::string_view newName)
{
}

bool AssetList::Remove(std::string_view name)
{
}

int AssetList::GetCacheSizeLimit() const
{
	return mCacheSizeLimit;
}

void AssetList::SetCacheSizeLimit(int limit)
{
	mCacheSizeLimit = limit;
}

void AssetList::DrawBigIcons(DrawState& state)
{
	// TODO
}

void AssetList::DrawDetails(DrawState& state)
{
	mLoader->SetupDetailsTable("AssetDetailsTable");
	for (auto& asset : mAssets) {
		mLoader->DrawDetailsTableRow(asset);
		ImGui::TableNextRow();
	}
	ImGui::EndTable();
}