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