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
+1 -1
View File
@@ -260,7 +260,7 @@ static int l_load_texture(lua::State* L) {
static int l_open_folder(lua::State* L) {
auto path = engine->getPaths().resolve(lua::require_string(L, 1));
platform::open_folder(path);
platform::open_folder(io::resolve(path));
return 0;
}
+38 -38
View File
@@ -13,13 +13,13 @@
namespace fs = std::filesystem;
using namespace scripting;
static fs::path resolve_path(const std::string& path) {
static io::path resolve_path(const std::string& path) {
return engine->getPaths().resolve(path);
}
static fs::path resolve_path_soft(const std::string& path) {
static io::path resolve_path_soft(const std::string& path) {
if (path.find(':') == std::string::npos) {
return fs::u8path("");
return "";
}
return engine->getPaths().resolve(path, false);
}
@@ -34,17 +34,17 @@ static int l_find(lua::State* L) {
}
static int l_resolve(lua::State* L) {
fs::path path = resolve_path(lua::require_string(L, 1));
return lua::pushstring(L, path.u8string());
io::path path = resolve_path(lua::require_string(L, 1));
return lua::pushstring(L, path.string());
}
static int l_read(lua::State* L) {
fs::path path = resolve_path(lua::require_string(L, 1));
if (fs::is_regular_file(path)) {
io::path path = resolve_path(lua::require_string(L, 1));
if (io::is_regular_file(path)) {
return lua::pushstring(L, io::read_string(path));
}
throw std::runtime_error(
"file does not exists " + util::quote(path.u8string())
"file does not exists " + util::quote(path.string())
);
}
@@ -52,9 +52,9 @@ static std::set<std::string> writeable_entry_points {
"world", "export", "config"
};
static fs::path get_writeable_path(lua::State* L) {
static io::path get_writeable_path(lua::State* L) {
std::string rawpath = lua::require_string(L, 1);
fs::path path = resolve_path(rawpath);
io::path path = resolve_path(rawpath);
auto entryPoint = rawpath.substr(0, rawpath.find(':'));
if (writeable_entry_points.find(entryPoint) == writeable_entry_points.end()) {
throw std::runtime_error("access denied");
@@ -63,7 +63,7 @@ static fs::path get_writeable_path(lua::State* L) {
}
static int l_write(lua::State* L) {
fs::path path = get_writeable_path(L);
io::path path = get_writeable_path(L);
std::string text = lua::require_string(L, 2);
io::write_string(path, text);
return 1;
@@ -71,62 +71,62 @@ static int l_write(lua::State* L) {
static int l_remove(lua::State* L) {
std::string rawpath = lua::require_string(L, 1);
fs::path path = resolve_path(rawpath);
io::path path = resolve_path(rawpath);
auto entryPoint = rawpath.substr(0, rawpath.find(':'));
if (writeable_entry_points.find(entryPoint) == writeable_entry_points.end()) {
throw std::runtime_error("access denied");
}
return lua::pushboolean(L, fs::remove(path));
return lua::pushboolean(L, io::remove(path));
}
static int l_remove_tree(lua::State* L) {
std::string rawpath = lua::require_string(L, 1);
fs::path path = resolve_path(rawpath);
io::path path = resolve_path(rawpath);
auto entryPoint = rawpath.substr(0, rawpath.find(':'));
if (writeable_entry_points.find(entryPoint) == writeable_entry_points.end()) {
throw std::runtime_error("access denied");
}
return lua::pushinteger(L, fs::remove_all(path));
return lua::pushinteger(L, io::remove_all(path));
}
static int l_exists(lua::State* L) {
fs::path path = resolve_path_soft(lua::require_string(L, 1));
return lua::pushboolean(L, fs::exists(path));
io::path path = resolve_path_soft(lua::require_string(L, 1));
return lua::pushboolean(L, io::exists(path));
}
static int l_isfile(lua::State* L) {
fs::path path = resolve_path_soft(lua::require_string(L, 1));
return lua::pushboolean(L, fs::is_regular_file(path));
io::path path = resolve_path_soft(lua::require_string(L, 1));
return lua::pushboolean(L, io::is_regular_file(path));
}
static int l_isdir(lua::State* L) {
fs::path path = resolve_path_soft(lua::require_string(L, 1));
return lua::pushboolean(L, fs::is_directory(path));
io::path path = resolve_path_soft(lua::require_string(L, 1));
return lua::pushboolean(L, io::is_directory(path));
}
static int l_length(lua::State* L) {
fs::path path = resolve_path(lua::require_string(L, 1));
if (fs::exists(path)) {
return lua::pushinteger(L, fs::file_size(path));
io::path path = resolve_path(lua::require_string(L, 1));
if (io::exists(path)) {
return lua::pushinteger(L, io::file_size(path));
} else {
return lua::pushinteger(L, -1);
}
}
static int l_mkdir(lua::State* L) {
fs::path path = resolve_path(lua::require_string(L, 1));
return lua::pushboolean(L, fs::create_directory(path));
io::path path = resolve_path(lua::require_string(L, 1));
return lua::pushboolean(L, io::create_directories(path)); // FIXME
}
static int l_mkdirs(lua::State* L) {
fs::path path = resolve_path(lua::require_string(L, 1));
return lua::pushboolean(L, fs::create_directories(path));
io::path path = resolve_path(lua::require_string(L, 1));
return lua::pushboolean(L, io::create_directories(path));
}
static int l_read_bytes(lua::State* L) {
fs::path path = resolve_path(lua::require_string(L, 1));
if (fs::is_regular_file(path)) {
size_t length = static_cast<size_t>(fs::file_size(path));
io::path path = resolve_path(lua::require_string(L, 1));
if (io::is_regular_file(path)) {
size_t length = static_cast<size_t>(io::file_size(path));
auto bytes = io::read_bytes(path, length);
@@ -140,12 +140,12 @@ static int l_read_bytes(lua::State* L) {
return 1;
}
throw std::runtime_error(
"file does not exists " + util::quote(path.u8string())
"file does not exists " + util::quote(path.string())
);
}
static int l_write_bytes(lua::State* L) {
fs::path path = get_writeable_path(L);
io::path path = get_writeable_path(L);
if (auto bytearray = lua::touserdata<lua::LuaBytearray>(L, 2)) {
auto& bytes = bytearray->data();
@@ -176,15 +176,15 @@ static int l_list(lua::State* L) {
if (dirname.find(':') == std::string::npos) {
return l_list_all_res(L, dirname);
}
fs::path path = resolve_path(dirname);
if (!fs::is_directory(path)) {
io::path path = resolve_path(dirname);
if (!io::is_directory(path)) {
throw std::runtime_error(
util::quote(path.u8string()) + " is not a directory"
util::quote(path.string()) + " is not a directory"
);
}
lua::createtable(L, 0, 0);
size_t index = 1;
for (auto& entry : fs::directory_iterator(path)) {
for (auto& entry : fs::directory_iterator(io::resolve(path))) {
auto name = entry.path().filename().u8string();
auto file = dirname + "/" + name;
lua::pushstring(L, file);
@@ -240,7 +240,7 @@ static int l_read_combined_object(lua::State* L) {
static int l_is_writeable(lua::State* L) {
std::string rawpath = lua::require_string(L, 1);
fs::path path = resolve_path(rawpath);
io::path path = resolve_path(rawpath);
auto entryPoint = rawpath.substr(0, rawpath.find(':'));
if (writeable_entry_points.find(entryPoint) == writeable_entry_points.end()) {
return lua::pushboolean(L, false);
@@ -38,8 +38,8 @@ static int l_load_fragment(lua::State* L) {
const auto& paths = engine->getPaths();
auto filename = lua::require_string(L, 1);
auto path = paths.resolve(filename);
if (!std::filesystem::exists(path)) {
throw std::runtime_error("file "+path.u8string()+" does not exist");
if (!io::exists(path)) {
throw std::runtime_error("file "+path.string()+" does not exist");
}
auto map = io::read_binary_json(path);
+7 -8
View File
@@ -135,12 +135,12 @@ static int l_is_pressed(lua::State* L) {
}
}
static void resetPackBindings(fs::path& packFolder) {
auto configFolder = packFolder/fs::path("config");
auto bindsFile = configFolder/fs::path("bindings.toml");
if (fs::is_regular_file(bindsFile)) {
static void reset_pack_bindings(const io::path& packFolder) {
auto configFolder = packFolder / "config";
auto bindsFile = configFolder / "bindings.toml";
if (io::is_regular_file(bindsFile)) {
Events::loadBindings(
bindsFile.u8string(),
bindsFile.string(),
io::read_string(bindsFile),
BindType::REBIND
);
@@ -148,10 +148,9 @@ static void resetPackBindings(fs::path& packFolder) {
}
static int l_reset_bindings(lua::State*) {
auto resFolder = engine->getPaths().getResourcesFolder();
resetPackBindings(resFolder);
reset_pack_bindings("res:");
for (auto& pack : engine->getContentPacks()) {
resetPackBindings(pack.folder);
reset_pack_bindings(pack.folder);
}
return 0;
}
+6 -6
View File
@@ -21,7 +21,7 @@ static int l_pack_get_folder(lua::State* L) {
for (auto& pack : packs) {
if (pack.id == packName) {
return lua::pushstring(L, pack.folder.u8string() + "/");
return lua::pushstring(L, pack.folder.string() + "/");
}
}
return lua::pushstring(L, "");
@@ -40,7 +40,7 @@ static int l_pack_get_installed(lua::State* L) {
/// @brief pack.get_available() -> array<string>
static int l_pack_get_available(lua::State* L) {
fs::path worldFolder("");
io::path worldFolder;
if (level) {
worldFolder = level->getWorld()->wfile->getFolder();
}
@@ -88,7 +88,7 @@ static int l_pack_get_info(
auto assets = engine->getAssets();
std::string icon = pack.id + ".icon";
if (!AssetsLoader::loadExternalTexture(
assets, icon, {pack.folder / fs::path("icon.png")}
assets, icon, {pack.folder / "icon.png"}
)) {
icon = "gui/no_icon";
}
@@ -146,7 +146,7 @@ static int pack_get_infos(lua::State* L) {
}
}
if (!ids.empty()) {
fs::path worldFolder("");
io::path worldFolder;
if (level) {
worldFolder = level->getWorld()->wfile->getFolder();
}
@@ -188,7 +188,7 @@ static int l_pack_get_info(lua::State* L) {
return pack.id == packid;
});
if (found == packs.end()) {
fs::path worldFolder("");
io::path worldFolder;
if (level) {
worldFolder = level->getWorld()->wfile->getFolder();
}
@@ -225,7 +225,7 @@ static int l_pack_assemble(lua::State* L) {
ids.push_back(lua::require_string(L, -1));
lua::pop(L);
}
fs::path worldFolder("");
io::path worldFolder;
if (level) {
worldFolder = level->getWorld()->wfile->getFolder();
}
+2 -2
View File
@@ -49,7 +49,7 @@ static int l_get_list(lua::State* L) {
int versionMajor = versionMap["major"].asInteger();
int versionMinor = versionMap["minor"].asInteger();
auto name = folder.filename().u8string();
auto name = folder.name();
lua::pushstring(L, name);
lua::setfield(L, "name");
@@ -105,7 +105,7 @@ static int l_get_seed(lua::State* L) {
static int l_exists(lua::State* L) {
auto name = lua::require_string(L, 1);
auto worldsDir = engine->getPaths().getWorldFolderByName(name);
return lua::pushboolean(L, fs::is_directory(worldsDir));
return lua::pushboolean(L, io::is_directory(worldsDir));
}
static int l_is_day(lua::State* L) {
+2 -3
View File
@@ -125,7 +125,7 @@ void lua::initialize(const EnginePaths& paths, const CoreParameters& params) {
main_thread = create_state(
paths, params.headless ? StateType::SCRIPT : StateType::BASE
);
lua::pushstring(main_thread, params.scriptFile.stem().u8string());
lua::pushstring(main_thread, params.scriptFile.stem());
lua::setglobal(main_thread, "__VC_SCRIPT_NAME");
}
@@ -159,8 +159,7 @@ State* lua::create_state(const EnginePaths& paths, StateType stateType) {
}
init_state(L, stateType);
auto resDir = paths.getResourcesFolder();
auto file = resDir / fs::u8path("scripts/stdmin.lua");
auto file = "res:scripts/stdmin.lua";
auto src = io::read_string(file);
lua::pop(L, lua::execute(L, 0, src, "core:scripts/stdmin.lua"));
return L;
@@ -1,4 +1,5 @@
#include <iostream>
#include <iomanip>
#include "libs/api_lua.hpp"
#include "debug/Logger.hpp"
@@ -68,7 +68,7 @@ static int l_dump(lua::State* L) {
raster[i*3 + 2] = val;
}
}
imageio::write(file.u8string(), &image);
imageio::write(file, &image);
}
return 0;
}