completely remove EnginePaths::resolve

This commit is contained in:
MihailRis
2025-02-04 12:35:19 +03:00
parent b955ae59c1
commit a653b063e6
12 changed files with 67 additions and 106 deletions
+19 -1
View File
@@ -10,6 +10,18 @@ namespace fs = std::filesystem;
static debug::Logger logger("io-stdfs");
StdfsDevice::StdfsDevice(fs::path root, bool createDirectory)
: root(std::move(root)) {
if (createDirectory && !fs::is_directory(this->root)) {
std::error_code ec;
fs::create_directories(this->root, ec);
if (ec) {
logger.error() << "error creating root directory " << this->root
<< ": " << ec.message();
}
}
}
fs::path StdfsDevice::resolve(std::string_view path) {
return root / fs::u8path(path);
}
@@ -54,7 +66,13 @@ bool StdfsDevice::isfile(std::string_view path) {
void StdfsDevice::mkdirs(std::string_view path) {
auto resolved = resolve(path);
fs::create_directories(resolved);
std::error_code ec;
fs::create_directories(resolved, ec);
if (ec) {
logger.error() << "error creating directory " << resolved << ": "
<< ec.message();
}
}
bool StdfsDevice::remove(std::string_view path) {
+1 -1
View File
@@ -3,7 +3,7 @@
namespace io {
class StdfsDevice : public Device {
public:
StdfsDevice(std::filesystem::path root) : root(std::move(root)) {}
StdfsDevice(std::filesystem::path root, bool createDirectory = true);
std::filesystem::path resolve(std::string_view path) override;
void write(std::string_view path, const void* data, size_t size) override;
+16 -54
View File
@@ -20,8 +20,8 @@ static debug::Logger logger("engine-paths");
static inline auto SCREENSHOTS_FOLDER = std::filesystem::u8path("screenshots");
static inline auto CONTENT_FOLDER = std::filesystem::u8path("content");
static inline auto WORLDS_FOLDER = std::filesystem::u8path("worlds");
static inline auto CONFIG_FOLDER = std::filesystem::u8path("config");
static inline auto EXPORT_FOLDER = std::filesystem::u8path("export");
static inline auto CONFIG_FOLDER = io::path("config");
static inline auto EXPORT_FOLDER = io::path("export");
static inline auto CONTROLS_FILE = std::filesystem::u8path("controls.toml");
static inline auto SETTINGS_FILE = std::filesystem::u8path("settings.toml");
@@ -52,7 +52,7 @@ static io::path toCanonic(io::path path) {
}
void EnginePaths::prepare() {
io::set_device("res", std::make_shared<io::StdfsDevice>(resourcesFolder));
io::set_device("res", std::make_shared<io::StdfsDevice>(resourcesFolder, false));
io::set_device("user", std::make_shared<io::StdfsDevice>(userFilesFolder));
if (!io::is_directory("res:")) {
@@ -60,10 +60,6 @@ void EnginePaths::prepare() {
resourcesFolder.string() + " is not a directory"
);
}
if (!io::is_directory("user:")) {
io::create_directories("user:");
}
logger.info() << "resources folder: " << fs::canonical(resourcesFolder).u8string();
logger.info() << "user files folder: " << fs::canonical(userFilesFolder).u8string();
@@ -71,14 +67,10 @@ void EnginePaths::prepare() {
if (!io::is_directory(contentFolder)) {
io::create_directories(contentFolder);
}
auto exportFolder = io::path("user:") / EXPORT_FOLDER;
if (!io::is_directory(exportFolder)) {
io::create_directories(exportFolder);
}
auto configFolder = io::path("user:") / CONFIG_FOLDER;
if (!io::is_directory(configFolder)) {
io::create_directories(configFolder);
}
io::create_subdevice("core", "res", "");
io::create_subdevice("export", "user", EXPORT_FOLDER);
io::create_subdevice("config", "user", CONFIG_FOLDER);
}
const std::filesystem::path& EnginePaths::getUserFilesFolder() const {
@@ -187,7 +179,16 @@ void EnginePaths::setCurrentWorldFolder(io::path folder) {
}
void EnginePaths::setContentPacks(std::vector<ContentPack>* contentPacks) {
// Remove previous content entry-points
for (const auto& pack : *this->contentPacks) {
io::remove_device(pack.id);
}
this->contentPacks = contentPacks;
// Create content devices
for (const auto& pack : *contentPacks) {
auto parent = pack.folder.entryPoint();
io::create_subdevice(pack.id, parent, pack.folder);
}
}
std::tuple<std::string, std::string> EnginePaths::parsePath(std::string_view path) {
@@ -200,45 +201,6 @@ std::tuple<std::string, std::string> EnginePaths::parsePath(std::string_view pat
return {prefix, filename};
}
// TODO: remove
io::path EnginePaths::resolve(
const std::string& path, bool throwErr
) const {
auto [prefix, filename] = EnginePaths::parsePath(path);
if (prefix.empty()) {
throw files_access_error("no entry point specified");
}
filename = toCanonic(filename).string();
if (prefix == "core") {
return io::path("res:") / filename;
}
if (prefix == "res" || prefix == "user" || prefix == "script") {
return prefix + ":" + filename;
}
if (prefix == "config") {
return getConfigFolder() / filename;
}
if (prefix == "world") {
return currentWorldFolder / filename;
}
if (prefix == "export") {
return io::path("user:") / EXPORT_FOLDER / filename;
}
if (contentPacks) {
for (auto& pack : *contentPacks) {
if (pack.id == prefix) {
return pack.folder / filename;
}
}
}
if (throwErr) {
throw files_access_error("unknown entry point '" + prefix + "'");
}
return filename;
}
ResPaths::ResPaths(io::path mainRoot, std::vector<PathsRoot> roots)
: mainRoot(std::move(mainRoot)), roots(std::move(roots)) {
}
-2
View File
@@ -43,8 +43,6 @@ public:
std::vector<io::path> scanForWorlds() const;
io::path resolve(const std::string& path, bool throwErr = true) const;
static std::tuple<std::string, std::string> parsePath(std::string_view view);
static inline auto CONFIG_DEFAULTS =
+4
View File
@@ -23,6 +23,10 @@ void io::set_device(const std::string& name, std::shared_ptr<io::Device> device)
devices[name] = device;
}
void io::remove_device(const std::string& name) {
devices.erase(name);
}
std::shared_ptr<io::Device> io::get_device(const std::string& name) {
const auto& found = devices.find(name);
if (found == devices.end()) {
+1
View File
@@ -16,6 +16,7 @@ namespace io {
class Device;
void set_device(const std::string& name, std::shared_ptr<Device> device);
void remove_device(const std::string& name);
std::shared_ptr<Device> get_device(const std::string& name);
Device& require_device(const std::string& name);