Vulkan Schnee 0.0.1 (22840dc8)
High-performance rendering engine
Loading...
Searching...
No Matches
Path.h
1#pragma once
2#include <array>
3#include <string>
4#include <filesystem>
5#include <ostream>
6#include <string_view>
7
8#include "Engine/Files/AssetPath.h"
9#include "Engine/Files/GeneratedAssetManifest.h"
10
11#ifndef GAME_FOLDER_NAME
12#error "GAME_FOLDER_NAME must be defined by CMake"
13#endif
14
15namespace Engine::Core {
16
25 template <size_t N>
27 {
28 public:
32 constexpr ConstexprPath() = default;
33
38 constexpr explicit ConstexprPath(const char (&literal)[N])
39 {
40 for (size_t i = 0; i < N; ++i) {
41 path_[i] = literal[i];
42 }
43 length_ = N - 1;
44 }
45
50 [[nodiscard]] constexpr std::string_view view() const { return {path_.data(), length_}; }
51
56 [[nodiscard]] std::filesystem::path fsPath() const
57 {
58 return std::filesystem::path(view());
59 }
60
65 [[nodiscard]] std::string string() const
66 {
67 return std::string(view());
68 }
69
74 [[nodiscard]] constexpr const char* c_str() const
75 {
76 return path_.data();
77 }
78
83 operator std::filesystem::path() const { return fsPath(); }
84
91 template <size_t M>
92 [[nodiscard]] constexpr auto operator/(const char (&other)[M]) const
93 {
94 constexpr size_t leftChars = N - 1;
95 constexpr size_t rightChars = M - 1;
96 constexpr size_t resultChars = leftChars + 1 + rightChars;
97
99 size_t out = 0;
100 for (size_t i = 0; i < length_; ++i) {
101 result.path_[out++] = path_[i];
102 }
103 if (length_ > 0 && result.path_[out - 1] != '/') {
104 result.path_[out++] = '/';
105 }
106 for (size_t i = 0; i < rightChars; ++i) {
107 result.path_[out++] = other[i];
108 }
109 result.path_[out] = '\0';
110 result.length_ = out;
111 return result;
112 }
113
119 std::filesystem::path operator/(const std::filesystem::path & other ) const
120 {
121 return fsPath() / other;
122 }
123
129 std::filesystem::path operator/(std::string_view other) const
130 {
131 return fsPath() / other;
132 }
133
134 private:
135 template <size_t>
136 friend class ConstexprPath;
137
138 std::array<char, N> path_{};
139 size_t length_ = 0;
140 };
141
145 template <size_t N>
146 ConstexprPath(const char (&)[N]) -> ConstexprPath<N>;
147
154 template <size_t N>
155 std::ostream& operator<<(std::ostream& stream, const ConstexprPath<N>& path)
156 {
157 return stream << path.view();
158 }
159
165 template <size_t N>
166 [[nodiscard]] consteval bool assetExists(const ConstexprPath<N>& path)
167 {
168 return Assets::GeneratedAssetManifest::contains(path.view());
169 }
170
174 class Path {
175 public:
180 static std::string getEngineRoot();
181
185 static constexpr std::string getProjectRoot();
186
191 static constexpr auto engineGeometry()
192 {
193 return ConstexprPath( "assets/Engine/geometry/" );
194 }
195
200 static constexpr auto gameGeometry()
201 {
202 return ConstexprPath( "assets/" GAME_FOLDER_NAME "/Data/geometry/" );
203 }
204
209 static constexpr auto gameAudio()
210 {
211 return ConstexprPath( "assets/" GAME_FOLDER_NAME "/Data/sounds/" );
212 }
213
218 static constexpr auto gameTextures()
219 {
220 return ConstexprPath( "assets/" GAME_FOLDER_NAME "/Data/textures/" );
221 }
222
227 static constexpr auto engineShaders() {
228 return ConstexprPath( "assets/Engine/shaders/" );
229 }
230
235 static constexpr auto engineTextures() {
236 return ConstexprPath( "assets/Engine/textures/" );
237 }
238
243 static constexpr auto engineFonts()
244 {
245 return ConstexprPath( "assets/Engine/fonts/" );
246 }
247
252 static constexpr auto engineSounds()
253 {
254 return ConstexprPath( "assets/Engine/sounds/" );
255 }
256
262 static bool doesFileExist(const std::filesystem::path &path);
268 static bool doesGltfFileExist(const std::filesystem::path &path);
274 static bool doesExrFileExist(const std::filesystem::path &path);
275
282 static bool doesFileOfTypeExist(const std::filesystem::path &path, const std::string& extension);
283 };
284
285} // namespace Engine::Core
286
291#define CREATE_ASSET_PATH( assetPath ) \
292 [] \
293 { \
294 constexpr auto verifiedAssetPath = ( assetPath ); \
295 static_assert( \
296 ::Engine::Core::assetExists( verifiedAssetPath ), \
297 "Asset path is not present in GeneratedAssetManifest" \
298 ); \
299 return verifiedAssetPath; \
300 }()
301
308#define CREATE_MESH_ASSET_PATH( assetPath, objectName ) \
309 [] \
310 { \
311 constexpr auto path = CREATE_ASSET_PATH( assetPath ); \
312 Asset::MeshPath meshAsset( path.fsPath(), objectName ); \
313 return meshAsset; \
314 }()
Fixed-size path string that supports constexpr path composition.
Definition Path.h:27
constexpr const char * c_str() const
Gets a null-terminated character pointer.
Definition Path.h:74
std::filesystem::path operator/(std::string_view other) const
Appends a runtime string-view path segment.
Definition Path.h:129
std::array< char, N > path_
Definition Path.h:138
constexpr ConstexprPath(const char(&literal)[N])
Creates a constexpr path from a string literal.
Definition Path.h:38
constexpr std::string_view view() const
Gets this path as a string view.
Definition Path.h:50
std::string string() const
Converts this path to a string.
Definition Path.h:65
friend class ConstexprPath
Definition Path.h:136
constexpr auto operator/(const char(&other)[M]) const
Appends a string literal path segment.
Definition Path.h:92
std::filesystem::path fsPath() const
Converts this path to a filesystem path.
Definition Path.h:56
constexpr ConstexprPath()=default
Creates an empty constexpr path.
std::filesystem::path operator/(const std::filesystem::path &other) const
Appends a runtime filesystem path segment.
Definition Path.h:119
Centralized accessors for project and asset paths.
Definition Path.h:174
static constexpr auto engineFonts()
Path to font assets.
Definition Path.h:243
static constexpr auto engineTextures()
Path to engine textures.
Definition Path.h:235
static constexpr auto gameGeometry()
Path to game geometry assets.
Definition Path.h:200
static bool doesExrFileExist(const std::filesystem::path &path)
Checks if the specified filepath exists at the given path and if the file is an exr file.
static bool doesFileOfTypeExist(const std::filesystem::path &path, const std::string &extension)
Checks if the specified filepath exists and if the file at the path is of a file type.
static std::string getEngineRoot()
Gets the engine root directory at runtime.
static bool doesGltfFileExist(const std::filesystem::path &path)
Checks if the specified filepath exists at the given path and is a gltf file.
static constexpr auto gameTextures()
Path to game texture assets.
Definition Path.h:218
static constexpr auto engineGeometry()
Path to engine geometry assets.
Definition Path.h:191
static constexpr auto gameAudio()
Path to game Audio assets.
Definition Path.h:209
static constexpr auto engineShaders()
Path to engine shaders.
Definition Path.h:227
static bool doesFileExist(const std::filesystem::path &path)
Checks if a file at a filepath exists.
static constexpr auto engineSounds()
Path to engine audio files.
Definition Path.h:252
static constexpr std::string getProjectRoot()
Gets the root location of the whole game. This is.
consteval bool assetExists(const ConstexprPath< N > &path)
Checks whether a constexpr asset path exists in the generated manifest.
Definition Path.h:166
ConstexprPath(const char(&)[N]) -> ConstexprPath< N >
Deduces a ConstexprPath size from a string literal.
std::ostream & operator<<(std::ostream &stream, const ConstexprPath< N > &path)
Writes a constexpr path to an output stream.
Definition Path.h:155