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
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
17 template <size_t N>
19 {
20 public:
21 constexpr ConstexprPath() = default;
22
23 constexpr explicit ConstexprPath(const char (&literal)[N])
24 {
25 for (size_t i = 0; i < N; ++i) {
26 path_[i] = literal[i];
27 }
28 length_ = N - 1;
29 }
30
31 [[nodiscard]] constexpr std::string_view view() const { return {path_.data(), length_}; }
32
33 [[nodiscard]] std::filesystem::path fsPath() const
34 {
35 return std::filesystem::path(view());
36 }
37
38 [[nodiscard]] std::string string() const
39 {
40 return std::string(view());
41 }
42
43 [[nodiscard]] constexpr const char* c_str() const
44 {
45 return path_.data();
46 }
47
48 operator std::filesystem::path() const { return fsPath(); }
49
50 template <size_t M>
51 [[nodiscard]] constexpr auto operator/(const char (&other)[M]) const
52 {
53 constexpr size_t leftChars = N - 1;
54 constexpr size_t rightChars = M - 1;
55 constexpr size_t resultChars = leftChars + 1 + rightChars;
56
58 size_t out = 0;
59 for (size_t i = 0; i < length_; ++i) {
60 result.path_[out++] = path_[i];
61 }
62 if (length_ > 0 && result.path_[out - 1] != '/') {
63 result.path_[out++] = '/';
64 }
65 for (size_t i = 0; i < rightChars; ++i) {
66 result.path_[out++] = other[i];
67 }
68 result.path_[out] = '\0';
69 result.length_ = out;
70 return result;
71 }
72
73 std::filesystem::path operator/(const std::filesystem::path & other ) const
74 {
75 return fsPath() / other;
76 }
77
78 std::filesystem::path operator/(std::string_view other) const
79 {
80 return fsPath() / other;
81 }
82
83 private:
84 template <size_t>
85 friend class ConstexprPath;
86
87 std::array<char, N> path_{};
88 size_t length_ = 0;
89 };
90
91 template <size_t N>
92 ConstexprPath(const char (&)[N]) -> ConstexprPath<N>;
93
94 template <size_t N>
95 std::ostream& operator<<(std::ostream& stream, const ConstexprPath<N>& path)
96 {
97 return stream << path.view();
98 }
99
100 template <size_t N>
101 [[nodiscard]] consteval bool assetExists(const ConstexprPath<N>& path)
102 {
103 return Assets::GeneratedAssetManifest::contains(path.view());
104 }
105
109 class Path {
110 public:
115 static std::string getEngineRoot();
116
120 static constexpr std::string getProjectRoot();
121
126 static constexpr auto engineGeometry()
127 {
128 return ConstexprPath( "assets/Engine/geometry/" );
129 }
130
135 static constexpr auto gameGeometry()
136 {
137 return ConstexprPath( "assets/" GAME_FOLDER_NAME "/Data/geometry/" );
138 }
139
144 static constexpr auto gameAudio()
145 {
146 return ConstexprPath( "assets/" GAME_FOLDER_NAME "/Data/Audio/" );
147 }
148
153 static constexpr auto engineShaders() {
154 return ConstexprPath( "assets/Engine/shaders/" );
155 }
156
161 static constexpr auto engineTextures() {
162 return ConstexprPath( "assets/Engine/textures/" );
163 }
164
169 static constexpr auto engineFonts()
170 {
171 return ConstexprPath( "assets/Engine/fonts/" );
172 }
173
178 static constexpr auto engineSounds()
179 {
180 return ConstexprPath( "assets/Engine/sounds/" );
181 }
182
188 static bool doesFileExist(const std::filesystem::path &path);
194 static bool doesGltfFileExist(const std::filesystem::path &path);
200 static bool doesExrFileExist(const std::filesystem::path &path);
201
208 static bool doesFileOfTypeExist(const std::filesystem::path &path, const std::string& extension);
209 };
210
211} // namespace Engine::Core
212
217#define CREATE_ASSET_PATH( assetPath ) \
218 [] \
219 { \
220 constexpr auto verifiedAssetPath = ( assetPath ); \
221 static_assert( \
222 ::Engine::Core::assetExists( verifiedAssetPath ), \
223 "Asset path is not present in GeneratedAssetManifest" \
224 ); \
225 return verifiedAssetPath; \
226 }()
227
234#define CREATE_MESH_ASSET_PATH( assetPath, objectName ) \
235 [] \
236 { \
237 constexpr auto path = CREATE_ASSET_PATH( assetPath ); \
238 Asset::MeshPath meshAsset( path.fsPath(), objectName ); \
239 return meshAsset; \
240 }()
constexpr const char * c_str() const
Definition Path.h:43
std::filesystem::path operator/(std::string_view other) const
Definition Path.h:78
std::array< char, N > path_
Definition Path.h:87
constexpr ConstexprPath(const char(&literal)[N])
Definition Path.h:23
constexpr std::string_view view() const
Definition Path.h:31
std::string string() const
Definition Path.h:38
friend class ConstexprPath
Definition Path.h:85
constexpr auto operator/(const char(&other)[M]) const
Definition Path.h:51
std::filesystem::path fsPath() const
Definition Path.h:33
constexpr ConstexprPath()=default
std::filesystem::path operator/(const std::filesystem::path &other) const
Definition Path.h:73
Centralized accessors for project and asset paths.
Definition Path.h:109
static constexpr auto engineFonts()
Path to font assets @lsp_source_path Engine/fonts.
Definition Path.h:169
static constexpr auto engineTextures()
Path to engine textures @lsp_source_path Engine/textures.
Definition Path.h:161
static constexpr auto gameGeometry()
Path to game geometry assets @lsp_source_path AirHockey/Data/geometry.
Definition Path.h:135
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 engineGeometry()
Path to engine geometry assets @lsp_source_path Engine/geometry.
Definition Path.h:126
static constexpr auto gameAudio()
Path to game Audio assets @lsp_source_path AirHockey/Data/Audio.
Definition Path.h:144
static constexpr auto engineShaders()
Path to engine shaders @lsp_source_path Engine/shaders.
Definition Path.h:153
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 @lsp_source_path Engine/sounds.
Definition Path.h:178
static constexpr std::string getProjectRoot()
Gets the root location of the whole game. This is.
Manages IBL (Image-Based Lighting) resources for specular reflections.
consteval bool assetExists(const ConstexprPath< N > &path)
Definition Path.h:101
ConstexprPath(const char(&)[N]) -> ConstexprPath< N >
std::ostream & operator<<(std::ostream &stream, const ConstexprPath< N > &path)
Definition Path.h:95