28#include "utility/system_io/default_system_io.hpp"
32 std::string NormalizePath(
const std::string &path)
34 return std::filesystem::path(path).lexically_normal().generic_string();
40 std::make_unique<
utility::logging::DefaultLogger>(
"DefaultSystemIO"))
58 std::error_code error;
59 const std::filesystem::path directoryPath(directory);
60 if (!std::filesystem::exists(directoryPath, error)
61 || !std::filesystem::is_directory(directoryPath, error)) {
63 <<
"Asset directory does not exist or is invalid: " << directory;
68 for (
const auto &entry:
69 std::filesystem::directory_iterator(directoryPath, error)) {
72 <<
"Failed to iterate asset directory: " << directory;
76 if (entry.is_regular_file(error) && !error) {
77 const std::string path = entry.path().string();
78 if (this->add(path) ==
nullptr) {
79 getLogger().warning() <<
"Failed to load asset: " << path;
88std::shared_ptr<utility::File>
91 const std::string key = NormalizePath(path);
92 if (this->exists(key)) {
93 return this->open(key);
96 std::ifstream file(key, std::ios::binary | std::ios::ate);
98 if (!file.is_open()) {
99 getLogger().warning() <<
"Failed to open file: " << key;
103 const std::streamsize fileSize = file.tellg();
105 getLogger().warning() <<
"Failed to read file size: " << key;
110 content.resize(
static_cast<size_t>(fileSize));
111 file.seekg(0, std::ios::beg);
112 if (!content.empty()) {
113 file.read(content.data(),
static_cast<std::streamsize
>(content.size()));
115 getLogger().warning() <<
"Failed to read file content: " << key;
121 auto asset = std::make_shared<utility::File>(path, content);
123 getLogger().warning() <<
"Failed to create File for: " << key;
127 _assets[key] = asset;
133 const std::string key = NormalizePath(path);
134 auto it = _assets.find(key);
135 if (it != _assets.end()) {
141 getLogger().warning() <<
"Asset not found: " << key;
146 const std::string &newPath)
148 const std::string key = NormalizePath(path);
149 auto it = _assets.find(key);
150 if (it == _assets.end()) {
151 getLogger().warning() <<
"Asset not found: " << key;
155 const auto &content = it->second->data();
156 const std::string savePath = newPath.empty() ? key : newPath;
158 std::error_code error;
159 const auto parentPath = std::filesystem::path(savePath).parent_path();
160 if (!parentPath.empty()) {
161 std::filesystem::create_directories(parentPath, error);
163 getLogger().warning()
164 <<
"Failed to create parent directories for: " << savePath;
169 std::ofstream file(savePath, std::ios::binary);
170 if (!file.is_open()) {
171 getLogger().warning()
172 <<
"Failed to open file for writing: " << savePath;
176 file.write(
reinterpret_cast<const char *
>(content.data()), content.size());
178 getLogger().warning() <<
"Failed to write file content: " << savePath;
void remove(const std::string &path, bool save=true) override
Removes an asset from the manager.
bool loadDirectory(const std::string &directory) override
Loads assets from a directory.
DefaultSystemIO()
Constructs a DefaultSystemIO.
bool save(const std::string &path, const std::string &newPath="") override
Saves an asset to the file system.
std::shared_ptr< utility::File > add(const std::string &path) override
Adds an asset to the manager by loading it from the file system.
utility::logging::Logger & getLogger(void)
Get the internal logger.
~DefaultSystemIO() override
Default destructor for DefaultSystemIO.
Abstract logger interface defining stream-style logging operations.
Platform-specific default logger alias.
The utility namespace contains classes and functions for the utility project.