Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
FileSystem.cpp
Go to the documentation of this file.
1#include <filesystem>
2#include <fstream>
3#include <vector>
4
5#include "io/FileSystem.h"
6
7#include "Log.h"
8#include "config.h"
9
10#if defined _WIN32
11# include "windows.h"
12#elif defined(__APPLE__)
13# include <mach-o/dyld.h>
14#elif defined(__linux__)
15# include "limits.h"
16# include "unistd.h"
17#endif
18
19namespace mnd
20{
21
22std::filesystem::path FileSystem::GetExecutableFolder() const
23{
24#if defined _WIN32
25 wchar_t buf[MAX_PATH];
26 GetModuleFileNameW(NULL, buf, MAX_PATH);
27 return std::filesystem::path(buf).remove_filename();
28#elif defined(__APPLE__)
29 uint32_t size = 0;
30 _NSGetExecutablePath(nullptr, &size);
31 std::string tmp(size, '\0');
32 _NSGetExecutablePath(tmp.data(), &size);
33 return std::filesystem::weakly_canonical(std::filesystem::path(tmp)).remove_filename();
34#elif defined(__linux__)
35 return std::filesystem::weakly_canonical(std::filesystem::read_symlink("/proc/self/exe")).remove_filename();
36#else
37 return std::filesystem::current_path();
38#endif
39}
40
41std::filesystem::path FileSystem::GetAssetsFolder() const
42{
43#if defined(ASSETS_ROOT)
44 auto path = std::filesystem::path(std::string(ASSETS_ROOT));
45 if (std::filesystem::exists(path)) {
46 return path;
47 }
48#endif
49 return std::filesystem::weakly_canonical(GetExecutableFolder() / kAssetsDirName);
50}
51
52std::vector<char> FileSystem::LoadFile(const std::filesystem::path &path)
53{
54 std::ifstream file(path, std::ios::binary | std::ios::ate);
55 if (!file.is_open()) {
56 LOG_ERROR("Could not open file: %s", path.c_str());
57 return {};
58 }
59
60 auto size = file.tellg();
61 file.seekg(0);
62
63 std::vector<char> buffer(size);
64
65 if (!file.read(buffer.data(), size)) {
66 LOG_ERROR("Could not open file: %s", path.c_str());
67 return {};
68 }
69
70 return buffer;
71}
72
73std::vector<char> FileSystem::LoadAssetFile(const std::string &relativePath)
74{
75 return LoadFile(GetAssetsFolder() / relativePath);
76}
77
78std::string FileSystem::LoadAssetFileText(const std::string &relativePath)
79{
80 auto buffer = LoadAssetFile(relativePath);
81 return std::string(buffer.begin(), buffer.end());
82}
83
84} // namespace mnd
constexpr const char * kAssetsDirName
Definition Constants.h:104
Console logging macros for all engine and game code.
#define LOG_ERROR(fmt,...)
Definition Log.h:81
std::filesystem::path GetExecutableFolder() const
Return the directory containing the running executable.
std::vector< char > LoadFile(const std::filesystem::path &path)
Load an arbitrary file into a byte buffer.
std::string LoadAssetFileText(const std::string &relativePath)
Load a text file from the assets directory into a string.
std::vector< char > LoadAssetFile(const std::string &relativePath)
Load a file from the assets directory into a byte buffer.
std::filesystem::path GetAssetsFolder() const
Return the runtime assets directory.