blob: 3fe3cebada6f156f24bacec0016cba861f49d536 (
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
|
#include <filesystem>
#include <stdexcept>
#include <whereami.h>
#include "Asset.h"
#include "api/Config.h"
using namespace crepe;
using namespace std;
Asset::Asset(const string & src) : src(find_asset(src)) {}
Asset::Asset(const char * src) : src(find_asset(src)) {}
const string & Asset::get_path() const noexcept { return this->src; }
string Asset::find_asset(const string & src) const {
auto & cfg = Config::get_instance();
auto & root_pattern = cfg.asset.root_pattern;
// if root_pattern is empty, find_asset must return all paths as-is
if (root_pattern.empty()) return src;
// absolute paths do not need to be resolved, only canonicalized
filesystem::path path = src;
if (path.is_absolute()) return filesystem::canonical(path);
// find directory matching root_pattern
filesystem::path root = this->whereami();
while (1) {
if (filesystem::exists(root / root_pattern)) break;
if (!root.has_parent_path())
throw runtime_error(format("Asset: Cannot find root pattern ({})", root_pattern));
root = root.parent_path();
}
// join path to root (base directory) and canonicalize
return filesystem::canonical(root / path);
}
string Asset::whereami() const noexcept {
string path;
size_t path_length = wai_getExecutablePath(NULL, 0, NULL);
path.resize(path_length + 1); // wai writes null byte
wai_getExecutablePath(path.data(), path_length, NULL);
path.resize(path_length);
return path;
}
bool Asset::operator==(const Asset & other) const noexcept { return this->src == other.src; }
size_t std::hash<const Asset>::operator()(const Asset & asset) const noexcept {
return std::hash<string>{}(asset.get_path());
};
|