aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/Asset.h
blob: b367a92af421bd4a271caec738b4f4ed71ea79ed (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
#pragma once

#include <string>

namespace crepe {

/**
 * \brief Asset location helper
 *
 * This class is used to locate game asset files, and should *always* be used
 * instead of reading file paths directly.
 */
class Asset {
public:
	/**
	 * \param src  Unique identifier to asset
	 */
	Asset(const std::string & src);
	/**
	 * \param src  Unique identifier to asset
	 */
	Asset(const char * src);

public:
	/**
	 * \brief Get the path to this asset
	 * \return path to this asset
	 */
	const std::string & get_path() const noexcept;

	/**
	 * \brief Comparison operator
	 * \param other Possibly different instance of \c Asset to test equality against
	 * \return True if \c this and \c other are equal
	 */
	bool operator==(const Asset & other) const noexcept;

private:
	//! path to asset
	std::string src;

private:
	/**
	 * \brief Locate asset path, or throw exception if it cannot be found
	 *
	 * This function resolves asset locations relative to crepe::Config::root_pattern if it is
	 * set and \p src is a relative path. If \p src is an absolute path, it is canonicalized.
	 * This function only returns if the file can be found.
	 *
	 * \param src Arbitrary path to resource file
	 *
	 * \returns \p src if crepe::Config::root_pattern is empty
	 * \returns Canonical path to \p src
	 *
	 * \throws std::runtime_error if root_pattern cannot be found
	 * \throws std::filesystem::filesystem_error if the resolved path does not exist
	 * \throws std::filesystem::filesystem_error if the path cannot be canonicalized
	 */
	std::string find_asset(const std::string & src) const;
	/**
	 * \returns The path to the current executable
	 */
	std::string whereami() const noexcept;
};

} // namespace crepe

namespace std {

//! Hash helper struct
template <>
struct hash<const crepe::Asset> {
	/**
	 * \brief Hash operator for crepe::Asset
	 *
	 * This function hashes a crepe::Asset instance, allowing it to be used as a key in an \c
	 * std::unordered_map.
	 *
	 * \returns Hash value
	 */
	size_t operator()(const crepe::Asset & asset) const noexcept;
};

} // namespace std