Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
Path.h
Go to the documentation of this file.
1#pragma once
2#include <array>
3#include <string>
4#include <filesystem>
5#include <ostream>
6#include <string_view>
7
8#include "Engine/Files/GeneratedAssetManifest.h"
9
10namespace Engine::Core {
11
12 template <size_t N>
14 {
15 public:
16 constexpr ConstexprPath() = default;
17
18 constexpr explicit ConstexprPath(const char (&literal)[N])
19 {
20 for (size_t i = 0; i < N; ++i) {
21 path_[i] = literal[i];
22 }
23 length_ = N - 1;
24 }
25
26 [[nodiscard]] constexpr std::string_view view() const { return {path_.data(), length_}; }
27
28 [[nodiscard]] std::filesystem::path fsPath() const
29 {
30 return std::filesystem::path(view());
31 }
32
33 [[nodiscard]] std::string string() const
34 {
35 return std::string(view());
36 }
37
38 [[nodiscard]] constexpr const char* c_str() const
39 {
40 return path_.data();
41 }
42
43 operator std::filesystem::path() const { return fsPath(); }
44
45 template <size_t M>
46 [[nodiscard]] constexpr auto operator/(const char (&other)[M]) const
47 {
48 constexpr size_t leftChars = N - 1;
49 constexpr size_t rightChars = M - 1;
50 constexpr size_t resultChars = leftChars + 1 + rightChars;
51
53 size_t out = 0;
54 for (size_t i = 0; i < length_; ++i) {
55 result.path_[out++] = path_[i];
56 }
57 if (length_ > 0 && result.path_[out - 1] != '/') {
58 result.path_[out++] = '/';
59 }
60 for (size_t i = 0; i < rightChars; ++i) {
61 result.path_[out++] = other[i];
62 }
63 result.path_[out] = '\0';
64 result.length_ = out;
65 return result;
66 }
67
68 std::filesystem::path operator/(const std::filesystem::path & other ) const
69 {
70 return fsPath() / other;
71 }
72
73 std::filesystem::path operator/(std::string_view other) const
74 {
75 return fsPath() / other;
76 }
77
78 private:
79 template <size_t>
80 friend class ConstexprPath;
81
82 std::array<char, N> path_{};
83 size_t length_ = 0;
84 };
85
86 template <size_t N>
87 ConstexprPath(const char (&)[N]) -> ConstexprPath<N>;
88
89 template <size_t N>
90 std::ostream& operator<<(std::ostream& stream, const ConstexprPath<N>& path)
91 {
92 return stream << path.view();
93 }
94
95 template <size_t N>
96 [[nodiscard]] consteval bool assetExists(const ConstexprPath<N>& path)
97 {
98 return Assets::GeneratedAssetManifest::contains(path.view());
99 }
100
101 class Path {
102 public:
103 static std::string getEngineRoot();
104
108 static constexpr std::string getProjectRoot();
112 static constexpr auto engineGeometry() {
113 return ConstexprPath( "assets/Engine/geometry/" );
114 }
115
119 static constexpr auto engineShaders() {
120 return ConstexprPath( "assets/Engine/shaders/" );
121 }
122
126 static constexpr auto engineTextures() {
127 return ConstexprPath( "assets/Engine/textures/" );
128 }
129
133 static constexpr auto engineFonts()
134 {
135 return ConstexprPath( "assets/Engine/fonts/" );
136 }
137
141 static constexpr auto engineSounds()
142 {
143 return ConstexprPath( "assets/Engine/sounds/" );
144 }
145
146 static bool doesFileExist(const std::filesystem::path &path);
147 static bool doesGltfFileExist(const std::filesystem::path &path);
148 static bool doesExrFileExist(const std::filesystem::path &path);
149 };
150
151}
152
153#define CREATE_ASSET_PATH(assetPath) \
154 [] { \
155 constexpr auto verifiedAssetPath = (assetPath); \
156 static_assert(::Engine::Core::assetExists(verifiedAssetPath), \
157 "Asset path is not present in GeneratedAssetManifest"); \
158 return verifiedAssetPath; \
159 }()
constexpr const char * c_str() const
Definition Path.h:38
std::filesystem::path operator/(std::string_view other) const
Definition Path.h:73
std::array< char, N > path_
Definition Path.h:82
constexpr ConstexprPath(const char(&literal)[N])
Definition Path.h:18
constexpr std::string_view view() const
Definition Path.h:26
std::string string() const
Definition Path.h:33
friend class ConstexprPath
Definition Path.h:80
constexpr auto operator/(const char(&other)[M]) const
Definition Path.h:46
std::filesystem::path fsPath() const
Definition Path.h:28
constexpr ConstexprPath()=default
std::filesystem::path operator/(const std::filesystem::path &other) const
Definition Path.h:68
static constexpr auto engineFonts()
Path to font assets.
Definition Path.h:133
static constexpr auto engineTextures()
Path to engine textures.
Definition Path.h:126
static bool doesExrFileExist(const std::filesystem::path &path)
static std::string getEngineRoot()
static bool doesGltfFileExist(const std::filesystem::path &path)
static constexpr auto engineGeometry()
Path to engine geometry assets.
Definition Path.h:112
static constexpr auto engineShaders()
Path to engine shaders.
Definition Path.h:119
static bool doesFileExist(const std::filesystem::path &path)
static constexpr auto engineSounds()
Path to engine audio files.
Definition Path.h:141
static constexpr std::string getProjectRoot()
Gets the root location of the whole game. This is.
Core audio subsystem owning the miniaudio engine and managing playback.
Definition AudioConfig.h:9
consteval bool assetExists(const ConstexprPath< N > &path)
Definition Path.h:96
ConstexprPath(const char(&)[N]) -> ConstexprPath< N >
std::ostream & operator<<(std::ostream &stream, const ConstexprPath< N > &path)
Definition Path.h:90