2#include <nlohmann/json.hpp>
7using namespace std::filesystem;
29 static_cast<void>(argc);
49 nlohmann::json appConfigData = nlohmann::json::parse(f);
51 if (appConfigData.contains(
"appName"))
52 cfg.
appName = appConfigData[
"appName"].get<std::string>();
54 if (appConfigData.contains(
"isFullScreen"))
55 cfg.
isFullScreen = appConfigData[
"isFullScreen"].get<
bool>();
57 if (appConfigData.contains(
"screenWidth"))
58 cfg.
screenWidth = appConfigData[
"screenWidth"].get<
float>();
60 if (appConfigData.contains(
"screenHeight"))
61 cfg.
screenHeight = appConfigData[
"screenHeight"].get<
float>();
63 if (appConfigData.contains(
"text"))
65 const auto& textNode = appConfigData[
"text"];
67 if (textNode.contains(
"scripts") && textNode[
"scripts"].is_array())
69 for (
const auto& script : textNode[
"scripts"])
71 if (script.is_string())
76 cfg.
textScripts.push_back(script.get<std::string>());
82 if (textNode.contains(
"fontSizePreload") && textNode[
"fontSizePreload"].is_array())
84 for (
const auto& fontSize : textNode[
"fontSizePreload"])
95 if (appConfigData.contains(
"logLevel"))
96 cfg.
logLevel = appConfigData[
"logLevel"].get<std::string>();
98 if (appConfigData.contains(
"useSmoothedFPS"))
101 if (appConfigData.contains(
"targetFPS"))
102 cfg.
targetFPS = appConfigData[
"targetFPS"].get<
float>();
104 if (appConfigData.contains(
"showStatsOverlay"))
108 catch (
const std::exception&)
118 std::ifstream file(filename, std::ios::ate | std::ios::binary);
122 throw std::runtime_error(
"failed to open shader binary file!");
125 const auto fileSize = file.tellg();
128 throw std::runtime_error(
"failed to determine shader binary file size!");
131 std::vector<char> buffer(
static_cast<size_t>(fileSize));
134 file.read(buffer.data(),
static_cast<std::streamsize
>(fileSize));
143 std::vector<std::string> shaderFileNames;
148 if( std::filesystem::exists( std::filesystem::path( directory ) ) && std::filesystem::is_directory(std::filesystem::path( directory ) ) )
150 std::filesystem::path pathToDirectory = std::filesystem::path( directory );
152 if(std::filesystem::is_directory( pathToDirectory ) )
154 for(
const std::filesystem::directory_entry& x : std::filesystem::directory_iterator( pathToDirectory ) )
156 size_t dot = x.path().string().find_last_of(
"." );
158 if( std::string{
"spv" } == x.path().string().substr( dot + 1 ) )
160 std::cout <<
"Shader binary file: " << x.path().string() <<
"\n";
161 shaderFileNames.push_back( x.path().string() );
167 catch(
const std::filesystem::filesystem_error& ex )
169 std::cout << ex.what() <<
'\n';
172 return shaderFileNames;
191std::filesystem::path Utility::FindPath(std::string fileOrFolderName, std::string searchingFrom)
193 std::filesystem::path result;
194 for(
const std::filesystem::directory_entry& entry : std::filesystem::directory_iterator(searchingFrom) )
196 if( entry.path().filename().string() == fileOrFolderName )
198 result = entry.path();
207 std::vector<std::string> imageFileNames;
212 if(std::filesystem::exists(std::filesystem::path(directory)) && std::filesystem::is_directory(std::filesystem::path(directory)) )
214 std::filesystem::path pathToDirectory = std::filesystem::path(directory);
216 if(std::filesystem::is_directory(pathToDirectory) )
218 for(
const std::filesystem::directory_entry& x : std::filesystem::directory_iterator(pathToDirectory) )
220 size_t dot = x.path().string().find_last_of(
".");
222 if( extToSearch == x.path().string().substr(dot + 1) )
224 imageFileNames.push_back(x.path().string());
230 catch(
const std::filesystem::filesystem_error& ex )
232 std::cout << ex.what() <<
'\n';
235 return imageFileNames;
240 auto exeDir = std::filesystem::canonical(std::filesystem::path(std::filesystem::current_path()));
241 if (exeDir.filename() ==
"Debug" || exeDir.filename() ==
"Release")
242 exeDir = exeDir.parent_path();
243 if (exeDir.filename() ==
"Binaries")
244 exeDir = exeDir.parent_path();
300 if (!std::filesystem::exists(jsonPath))
307 std::ifstream f(jsonPath.string());
310 std::cerr <<
"[ERROR] Failed to open Pack.json\n";
319 for (
auto it = j.begin(); it != j.end(); ++it)
322 entry.
offset = it.value().value(
"offset", std::size_t{ 0 });
323 entry.
size = it.value().value(
"size", std::size_t{ 0 });
333 std::vector<std::uint8_t> data;
341 if (!std::filesystem::exists(binPath) ||
342 !std::filesystem::exists(jsonPath))
344 std::cerr <<
"[Utility::ReadPackedFile] Missing Pack.bin or Pack.json\n";
353 std::cerr <<
"[Utility::ReadPackedFile] No such packed entry: "
354 << entryPath << std::endl;
360 std::ifstream bin(binPath.string(), std::ios::binary);
363 std::cerr <<
"[Utility::ReadPackedFile] Failed to open Pack.bin: "
364 << binPath.string() << std::endl;
370 data.resize(entry.
size);
372 if (entry.
offset >
static_cast<std::size_t
>(std::numeric_limits<std::streamoff>::max()))
374 std::cerr <<
"[Utility::ReadPackedFile] Entry offset exceeds supported file range: "
375 << entryPath << std::endl;
379 bin.seekg(
static_cast<std::streamoff
>(entry.
offset), std::ios::beg);
383 std::cerr <<
"[Utility::ReadPackedFile] Seek error for entry: "
384 << entryPath << std::endl;
389 static_cast<std::size_t
>(std::numeric_limits<std::streamsize>::max()))
391 std::cerr <<
"[Utility::ReadPackedFile] Entry size exceeds supported read range: "
392 << entryPath << std::endl;
397 reinterpret_cast<char*
>(data.data()),
398 static_cast<std::streamsize
>(entry.
size));
402 std::cerr <<
"[Utility::ReadPackedFile] Read error for entry: "
403 << entryPath << std::endl;
static std::vector< std::string > GetListOfFileNamesInDirectory(const char *directory, std::string extToSearch)
Returns a list of file names in a directory matching the specified extension.
static std::vector< char > ReadShaderBinaryFile(std::string const &filename)
Reads a binary shader file from disk.
static std::filesystem::path GetShadersBinaryPath()
Returns the directory path containing compiled shader binaries.
static std::filesystem::path sApplicationPath
static std::filesystem::path sBuildPath
static std::filesystem::path GetConfigFilePath()
Returns absolute path to Config/app_config.json.
static std::filesystem::path GetTextureFolderPath()
Returns absolute path to Content/Textures.
static std::filesystem::path GetFontsFolderPath()
Returns absolute path to Content/Fonts.
static std::filesystem::path ResolveProjectRoot()
Resolves project root folder (handles Release/Debug/Binaries layouts).
static std::filesystem::path const sContentRelativePathFolder
static std::filesystem::path GetLogsFolderPath()
Returns absolute path to Logs folder.
static std::filesystem::path const sDefaultShadersBinaryRelativePath
static std::filesystem::path const sAppConfigFilePath
static const PackEntries & GetPackEntries()
Returns the manifest of packed files.
static std::vector< std::string > GetListOfFilesInDirectory(std::string directory)
Returns a list of full file paths in the given directory.
static std::filesystem::path GetApplicationPath()
Returns the absolute path of the running application.
static std::filesystem::path const sContentPackageFilePath
static std::filesystem::path const sLogFolderPath
static std::filesystem::path GetContentFolderPath()
Returns absolute path to Content.
static AppConfig ReadConfigFile()
Reads application settings from the JSON config file.
static std::filesystem::path const sTextureRelativePathFolder
static std::filesystem::path const sContentPackEntriesFilePath
static std::filesystem::path const sShadersRelativePathFolder
static std::filesystem::path const sFontsRelativePathFolder
static std::filesystem::path const sModelsRelativePathFolder
static std::filesystem::path GetModelsFolderPath()
Returns absolute path to Content/Models.
static std::filesystem::path GetBuildPath()
Returns the build output directory path.
static std::filesystem::path sShadersBinaryPath
static std::vector< uint8_t > ReadPackedFile(const std::string &entryPath)
Reads raw bytes of a file stored inside Pack.bin.
static bool IsPackageProvided()
Checks whether packed assets (Pack.bin / Pack.json) exist.
static void InitializePaths(int argc, char *argv[])
Initializes engine paths based on the executable location.
static std::filesystem::path GetShadersFolderPath()
Returns absolute path to Content/Shaders.
static PackEntries sPackEntries
std::unordered_map< std::string, PackEntry > PackEntries
static bool sPackEntriesLoaded
Basic application settings loaded from a configuration file.
float targetFPS
Target frame rate (0 = uncapped).
float screenWidth
Desired window width in pixels (ignored in full-screen mode).
bool isFullScreen
Whether the application should start in full-screen mode.
std::string appName
Name of the application.
bool useSmoothedFPS
Enable FPS smoothing and frame pacing behavior.
bool showStatsOverlay
Enable on-screen statistics overlay.
std::vector< unsigned int > fontSizePreload
Font sizes to preload at startup.
float screenHeight
Desired window height in pixels (ignored in full-screen mode).
std::vector< std::string > textScripts
Unicode scripts to preload for text rendering.
std::string logLevel
Logging verbosity level ("Error", "Warning", "Info", "Debug").
Metadata describing one file stored inside a packed asset archive.