migrate from std::filesystem::path to io::path (WIP)

This commit is contained in:
MihailRis
2025-01-30 22:23:13 +03:00
parent 1e22882284
commit e0314803c0
72 changed files with 1189 additions and 791 deletions
+95 -86
View File
@@ -9,9 +9,12 @@
#include "util/stringutil.hpp"
#include <utility>
#include "io/devices/StdfsDevice.hpp"
#include "world/files/WorldFiles.hpp"
#include "debug/Logger.hpp"
namespace fs = std::filesystem;
static debug::Logger logger("engine-paths");
static inline auto SCREENSHOTS_FOLDER = std::filesystem::u8path("screenshots");
@@ -22,15 +25,16 @@ static inline auto EXPORT_FOLDER = std::filesystem::u8path("export");
static inline auto CONTROLS_FILE = std::filesystem::u8path("controls.toml");
static inline auto SETTINGS_FILE = std::filesystem::u8path("settings.toml");
static std::filesystem::path toCanonic(std::filesystem::path path) {
static io::path toCanonic(io::path path) {
std::stack<std::string> parts;
path = path.lexically_normal();
path = std::filesystem::u8path(path.string()).lexically_normal().string();
do {
parts.push(path.filename().u8string());
path = path.parent_path();
parts.push(path.name());
path = path.parent();
} while (!path.empty());
path = fs::u8path("");
path = "";
while (!parts.empty()) {
const std::string part = parts.top();
@@ -48,44 +52,47 @@ static std::filesystem::path toCanonic(std::filesystem::path path) {
}
void EnginePaths::prepare() {
if (!fs::is_directory(resourcesFolder)) {
io::set_device("res", std::make_shared<io::StdfsDevice>(resourcesFolder));
io::set_device("user", std::make_shared<io::StdfsDevice>(userFilesFolder));
if (!io::is_directory("res:")) {
throw std::runtime_error(
resourcesFolder.u8string() + " is not a directory"
resourcesFolder.string() + " is not a directory"
);
}
if (!fs::is_directory(userFilesFolder)) {
fs::create_directories(userFilesFolder);
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();
auto contentFolder = userFilesFolder / CONTENT_FOLDER;
if (!fs::is_directory(contentFolder)) {
fs::create_directories(contentFolder);
auto contentFolder = io::path("user:") / CONTENT_FOLDER;
if (!io::is_directory(contentFolder)) {
io::create_directories(contentFolder);
}
auto exportFolder = userFilesFolder / EXPORT_FOLDER;
if (!fs::is_directory(exportFolder)) {
fs::create_directories(exportFolder);
auto exportFolder = io::path("user:") / EXPORT_FOLDER;
if (!io::is_directory(exportFolder)) {
io::create_directories(exportFolder);
}
auto configFolder = userFilesFolder / CONFIG_FOLDER;
if (!fs::is_directory(configFolder)) {
fs::create_directories(configFolder);
auto configFolder = io::path("user:") / CONFIG_FOLDER;
if (!io::is_directory(configFolder)) {
io::create_directories(configFolder);
}
}
std::filesystem::path EnginePaths::getUserFilesFolder() const {
const std::filesystem::path& EnginePaths::getUserFilesFolder() const {
return userFilesFolder;
}
std::filesystem::path EnginePaths::getResourcesFolder() const {
const std::filesystem::path& EnginePaths::getResourcesFolder() const {
return resourcesFolder;
}
std::filesystem::path EnginePaths::getNewScreenshotFile(const std::string& ext) {
auto folder = userFilesFolder / SCREENSHOTS_FOLDER;
if (!fs::is_directory(folder)) {
fs::create_directory(folder);
io::path EnginePaths::getNewScreenshotFile(const std::string& ext) {
auto folder = io::path("user:") / SCREENSHOTS_FOLDER;
if (!io::is_directory(folder)) {
io::create_directories(folder);
}
auto t = std::time(nullptr);
@@ -96,55 +103,55 @@ std::filesystem::path EnginePaths::getNewScreenshotFile(const std::string& ext)
ss << std::put_time(&tm, format);
std::string datetimestr = ss.str();
auto filename = folder / fs::u8path("screenshot-" + datetimestr + "." + ext);
auto file = folder / ("screenshot-" + datetimestr + "." + ext);
uint index = 0;
while (fs::exists(filename)) {
filename = folder / fs::u8path(
"screenshot-" + datetimestr + "-" +
std::to_string(index) + "." + ext
);
while (io::exists(file)) {
file = folder / fs::u8path(
"screenshot-" + datetimestr + "-" +
std::to_string(index) + "." + ext
);
index++;
}
return filename;
return file;
}
std::filesystem::path EnginePaths::getWorldsFolder() const {
return userFilesFolder / WORLDS_FOLDER;
io::path EnginePaths::getWorldsFolder() const {
return io::path("user:") / WORLDS_FOLDER;
}
std::filesystem::path EnginePaths::getConfigFolder() const {
return userFilesFolder / CONFIG_FOLDER;
io::path EnginePaths::getConfigFolder() const {
return io::path("user:") / CONFIG_FOLDER;
}
std::filesystem::path EnginePaths::getCurrentWorldFolder() {
io::path EnginePaths::getCurrentWorldFolder() {
return currentWorldFolder;
}
std::filesystem::path EnginePaths::getWorldFolderByName(const std::string& name) {
io::path EnginePaths::getWorldFolderByName(const std::string& name) {
return getWorldsFolder() / std::filesystem::path(name);
}
std::filesystem::path EnginePaths::getControlsFile() const {
return userFilesFolder / CONTROLS_FILE;
io::path EnginePaths::getControlsFile() const {
return io::path("user:") / CONTROLS_FILE;
}
std::filesystem::path EnginePaths::getSettingsFile() const {
return userFilesFolder / SETTINGS_FILE;
io::path EnginePaths::getSettingsFile() const {
return io::path("user:") / SETTINGS_FILE;
}
std::vector<std::filesystem::path> EnginePaths::scanForWorlds() const {
std::vector<std::filesystem::path> folders;
std::vector<io::path> EnginePaths::scanForWorlds() const {
std::vector<io::path> folders;
auto folder = getWorldsFolder();
if (!fs::is_directory(folder)) return folders;
if (!io::is_directory(folder)) return folders;
for (const auto& entry : fs::directory_iterator(folder)) {
for (const auto& entry : std::filesystem::directory_iterator(io::resolve(folder))) {
if (!entry.is_directory()) {
continue;
}
const auto& worldFolder = entry.path();
auto worldFile = worldFolder / fs::u8path(WorldFiles::WORLD_FILE);
if (!fs::is_regular_file(worldFile)) {
io::path worldFolder = folder / entry.path().filename().u8string();
auto worldFile = worldFolder / WorldFiles::WORLD_FILE;
if (!io::is_regular_file(worldFile)) {
continue;
}
folders.push_back(worldFolder);
@@ -152,10 +159,11 @@ std::vector<std::filesystem::path> EnginePaths::scanForWorlds() const {
std::sort(
folders.begin(),
folders.end(),
[](std::filesystem::path a, std::filesystem::path b) {
a = a / fs::u8path(WorldFiles::WORLD_FILE);
b = b / fs::u8path(WorldFiles::WORLD_FILE);
return fs::last_write_time(a) > fs::last_write_time(b);
[](io::path a, io::path b) {
a = a / WorldFiles::WORLD_FILE;
b = b / WorldFiles::WORLD_FILE;
return fs::last_write_time(io::resolve(a)) >
fs::last_write_time(io::resolve(b));
}
);
return folders;
@@ -170,11 +178,13 @@ void EnginePaths::setResourcesFolder(std::filesystem::path folder) {
}
void EnginePaths::setScriptFolder(std::filesystem::path folder) {
io::set_device("script", std::make_shared<io::StdfsDevice>(folder));
this->scriptFolder = std::move(folder);
}
void EnginePaths::setCurrentWorldFolder(std::filesystem::path folder) {
void EnginePaths::setCurrentWorldFolder(io::path folder) {
this->currentWorldFolder = std::move(folder);
io::create_subdevice("world", "user", currentWorldFolder);
}
void EnginePaths::setContentPacks(std::vector<ContentPack>* contentPacks) {
@@ -191,65 +201,64 @@ std::tuple<std::string, std::string> EnginePaths::parsePath(std::string_view pat
return {prefix, filename};
}
std::filesystem::path EnginePaths::resolve(
// 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(fs::u8path(filename)).u8string();
filename = toCanonic(filename).string();
if (prefix == "res" || prefix == "core") {
return resourcesFolder / fs::u8path(filename);
if (prefix == "core") {
return io::path("res:") / filename;
}
if (prefix == "user") {
return userFilesFolder / fs::u8path(filename);
if (prefix == "res" || prefix == "user" || prefix == "script") {
return prefix + ":" + filename;
}
if (prefix == "config") {
return getConfigFolder() / fs::u8path(filename);
return getConfigFolder() / filename;
}
if (prefix == "world") {
return currentWorldFolder / fs::u8path(filename);
return currentWorldFolder / filename;
}
if (prefix == "export") {
return userFilesFolder / EXPORT_FOLDER / fs::u8path(filename);
}
if (prefix == "script" && scriptFolder) {
return scriptFolder.value() / fs::u8path(filename);
return io::path("user:") / EXPORT_FOLDER / filename;
}
if (contentPacks) {
for (auto& pack : *contentPacks) {
if (pack.id == prefix) {
return pack.folder / fs::u8path(filename);
return pack.folder / filename;
}
}
}
if (throwErr) {
throw files_access_error("unknown entry point '" + prefix + "'");
}
return std::filesystem::path(filename);
return filename;
}
ResPaths::ResPaths(std::filesystem::path mainRoot, std::vector<PathsRoot> roots)
ResPaths::ResPaths(io::path mainRoot, std::vector<PathsRoot> roots)
: mainRoot(std::move(mainRoot)), roots(std::move(roots)) {
}
std::filesystem::path ResPaths::find(const std::string& filename) const {
io::path ResPaths::find(const std::string& filename) const {
for (int i = roots.size() - 1; i >= 0; i--) {
auto& root = roots[i];
auto file = root.path / fs::u8path(filename);
if (fs::exists(file)) {
auto file = root.path / filename;
if (io::exists(file)) {
return file;
}
}
return mainRoot / fs::u8path(filename);
return mainRoot / filename;
}
std::string ResPaths::findRaw(const std::string& filename) const {
for (int i = roots.size() - 1; i >= 0; i--) {
auto& root = roots[i];
if (fs::exists(root.path / std::filesystem::path(filename))) {
if (io::exists(root.path / filename)) {
return root.name + ":" + filename;
}
}
@@ -261,8 +270,8 @@ std::vector<std::string> ResPaths::listdirRaw(const std::string& folderName) con
for (int i = roots.size() - 1; i >= 0; i--) {
auto& root = roots[i];
auto folder = root.path / fs::u8path(folderName);
if (!fs::is_directory(folder)) continue;
for (const auto& entry : fs::directory_iterator(folder)) {
if (!io::is_directory(folder)) continue;
for (const auto& entry : fs::directory_iterator(io::resolve(folder))) {
auto name = entry.path().filename().u8string();
entries.emplace_back(root.name + ":" + folderName + "/" + name);
}
@@ -270,16 +279,16 @@ std::vector<std::string> ResPaths::listdirRaw(const std::string& folderName) con
return entries;
}
std::vector<std::filesystem::path> ResPaths::listdir(
std::vector<io::path> ResPaths::listdir(
const std::string& folderName
) const {
std::vector<std::filesystem::path> entries;
std::vector<io::path> entries;
for (int i = roots.size() - 1; i >= 0; i--) {
auto& root = roots[i];
std::filesystem::path folder = root.path / fs::u8path(folderName);
if (!fs::is_directory(folder)) continue;
for (const auto& entry : fs::directory_iterator(folder)) {
entries.push_back(entry.path());
io::path folder = root.path / folderName;
if (!io::is_directory(folder)) continue;
for (const auto& entry : fs::directory_iterator(io::resolve(folder))) {
entries.push_back(folder / entry.path().filename().u8string());
}
}
return entries;
@@ -288,8 +297,8 @@ std::vector<std::filesystem::path> ResPaths::listdir(
dv::value ResPaths::readCombinedList(const std::string& filename) const {
dv::value list = dv::list();
for (const auto& root : roots) {
auto path = root.path / fs::u8path(filename);
if (!fs::exists(path)) {
auto path = root.path / filename;
if (!io::exists(path)) {
continue;
}
try {
@@ -313,8 +322,8 @@ dv::value ResPaths::readCombinedList(const std::string& filename) const {
dv::value ResPaths::readCombinedObject(const std::string& filename) const {
dv::value object = dv::object();
for (const auto& root : roots) {
auto path = root.path / fs::u8path(filename);
if (!fs::exists(path)) {
auto path = root.path / filename;
if (!io::exists(path)) {
continue;
}
try {
@@ -335,6 +344,6 @@ dv::value ResPaths::readCombinedObject(const std::string& filename) const {
return object;
}
const std::filesystem::path& ResPaths::getMainRoot() const {
const io::path& ResPaths::getMainRoot() const {
return mainRoot;
}