summaryrefslogtreecommitdiff
path: root/src/asset-manager.cpp
blob: 9fda1c19f5a47916a1d369ad17a9e2d90fefeb85 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

#include "asset-manager.hpp"

#include "thread_pool.hpp"

#include <chrono>
#include <filesystem>
#include <fstream>
#include <future>

const PkeHandleItemIndex_T maxAssetItemsPerBucket = 64;

struct AssetBucket {
	Asset assets[maxAssetItemsPerBucket];
};

BucketContainer<AssetBucket, AssetHandle> Asset_BucketContainer{};

ThreadPoolHandle assetThreadPool = ThreadPoolHandle_MAX;

void AM_Init() {
	Buckets_Init(Asset_BucketContainer);
	assetThreadPool = PkeThreads_Init(2, 255);
}

void AM_Load_Task(Asset &asset) {
	std::ifstream file(asset.basePath, std::ios::ate | std::ios::binary);
	if (!file.is_open()) {
		asset.state = PKE_ASSET_LOADING_STATE_FAILED;
		return;
	}
	asset.size = std::filesystem::file_size(asset.basePath);
	asset.ptr = Pke_New(asset.size, 64);
	file.seekg(0, std::ios::beg);
	file.read(static_cast<char *>(asset.ptr), asset.size);
	file.close();
	asset.state = PKE_ASSET_LOADING_STATE_LOADED;
}

AssetHandle AM_Register(const void *data, int64_t size, std::size_t alignment, const char *key) {
	assert(data != nullptr && "Attempt to register invalid asset data");
	assert(data != CAFE_BABE(void) && "Attempt to register invalid asset data");
	assert(size != 0 && "Attempt to register asset data of size 0");
	bool moved;
	AssetHandle assetHandle{Buckets_NewHandle<AssetBucket>(maxAssetItemsPerBucket, Asset_BucketContainer, moved)};
	Asset &asset = Asset_BucketContainer.buckets[assetHandle.bucketIndex].assets[assetHandle.itemIndex];
	new (&asset) Asset{};
	int64_t keyLen = strlen(key);
	int64_t keyOffset = (keyLen > 16 ? keyLen - 16 : 0);
	strncpy(asset.key, key + keyOffset, 16);
	asset.basePath = nullptr;
	asset.size = size;
	asset.ptr = Pke_New(size, alignment);
	memcpy(asset.ptr, data, size);
	asset.state = PKE_ASSET_LOADING_STATE_LOADED;
	return assetHandle;
}

AssetHandle AM_Register(const char *path) {
	bool moved = false;
	AssetHandle assetHandle{Buckets_NewHandle<AssetBucket>(maxAssetItemsPerBucket, Asset_BucketContainer, moved)};
	Asset &asset = Asset_BucketContainer.buckets[assetHandle.bucketIndex].assets[assetHandle.itemIndex];
	new (&asset) Asset{};
	int64_t pathLen = strlen(path);
	int64_t pathOffset = (pathLen > 16 ? pathLen - 16 : 0);
	strncpy(asset.key, path + pathOffset, 16);
	auto *copiedPath = Pke_New<char>(pathLen + 1);
	copiedPath[pathLen] = '\0';
	strncpy(copiedPath, path, pathLen);
	asset.basePath = copiedPath;
	asset.state = PKE_ASSET_LOADING_STATE_LOADING;
	std::packaged_task<void()> *task = Pke_New<std::packaged_task<void()>>();
	new (task) std::packaged_task<void()>( [&asset] {
		AM_Load_Task(asset);
	});
	asset.future = task->get_future();
	PkeThreads_Enqueue(assetThreadPool, task);
	return assetHandle;
}

void AM_Release(AssetHandle assetHandle) {
	assert(assetHandle != AssetHandle_MAX);
	Asset &asset = Asset_BucketContainer.buckets[assetHandle.bucketIndex].assets[assetHandle.itemIndex];
	asset.referenceCount -= 1;
	assert(asset.referenceCount >= 0 && "asset was unloaded more times than it was retrieved");
}

const Asset *AM_Get(AssetHandle assetHandle) {
	auto validationResult = ValidateHandle(assetHandle, Asset_BucketContainer.pkeHandle, maxAssetItemsPerBucket);
	assert(validationResult == 0);
	auto &asset = Asset_BucketContainer.buckets[assetHandle.bucketIndex].assets[assetHandle.itemIndex];
	if (asset.state == PKE_ASSET_LOADING_STATE_LOADED) {
		asset.referenceCount += 1;
		return &asset;
	}
	if (asset.state == PKE_ASSET_LOADING_STATE_UNLOADED) {
		if (asset.basePath == nullptr) {
			return nullptr;
		}
		asset.state = PKE_ASSET_LOADING_STATE_LOADING;
		std::packaged_task<void()> *task = Pke_New<std::packaged_task<void()>>();
		new (task) std::packaged_task<void()>( [&asset] {
			AM_Load_Task(asset);
		});
		asset.future = task->get_future();
		PkeThreads_Enqueue(assetThreadPool, task);
	}
	if (asset.state == PKE_ASSET_LOADING_STATE_LOADING) {
		if (asset.future.valid()) {
			asset.future.wait();
		} else {
			char buf[256];
			buf[255] = '\0';
			snprintf(buf, 255, "[AM_Get] Attempting to retrieve asset: '%.16s' which had a state of '%hhu', but did not have a valid future and thus cannot wait for completion", asset.key, asset.state);
			throw buf;
		}
	}
	if (asset.state == PKE_ASSET_LOADING_STATE_LOADED)
		asset.referenceCount += 1;
	return &asset;
}

void AM_DebugPrint() {
	printf("Asset Manager printout:\n");
	for (uint64_t b = 0; b <= Asset_BucketContainer.pkeHandle.bucketIndex; ++b) {
		auto &bkt = Asset_BucketContainer.buckets[b];
		uint64_t counter = b == Asset_BucketContainer.pkeHandle.bucketIndex ? Asset_BucketContainer.pkeHandle.itemIndex : maxAssetItemsPerBucket;
		for (uint64_t i = 0; i < counter; ++i) {
			auto &asset = bkt.assets[i];
			/*
			if (asset.size == 0)
				continue;
			*/
			printf("-Asset: 0x%016lX\n", (b << 32) + i);
			printf("\tkey:  %.16s\n", asset.key);
			if (asset.basePath != nullptr) {
				printf("\tbasePath:  %s\n", asset.basePath);
			} else {
				printf("\tbasePath:  %p\n", nullptr);
			}
			printf("\tsize: %ld\n", asset.size);
			printf("\tptr   %p\n", asset.ptr);
			printf("\tfuture: %i\n", asset.future.valid());
			printf("\treferenceCount: %i\n", asset.referenceCount);
			printf("\tAssetLoadingState: %hhu\n", asset.state);
		}
	}
}

void AM_GC() {
	for (long b = 0; b <= Asset_BucketContainer.pkeHandle.bucketIndex; ++b) {
		for (long i = 0; i < Asset_BucketContainer.pkeHandle.itemIndex; ++i) {
			Asset &asset = Asset_BucketContainer.buckets[b].assets[i];
			if (asset.referenceCount > 0)
				continue;
			if (asset.ptr != nullptr && asset.ptr != CAFE_BABE(void)) {
				Pke_Delete(asset.ptr, asset.size);
			}
			asset.size = 0;
			asset.ptr = CAFE_BABE(void);
			asset.future = std::future<void>{};
			asset.state = PKE_ASSET_LOADING_STATE_UNLOADED;
		}
	}
}

void AM_Teardown() {
	AM_GC();
	Buckets_Destroy(Asset_BucketContainer);
	PkeThreads_Teardown(assetThreadPool);
}