lua filesystem access fix
This commit is contained in:
@@ -1,12 +1,13 @@
|
|||||||
#include "engine_paths.h"
|
#include "engine_paths.h"
|
||||||
|
|
||||||
#include <filesystem>
|
#include <stack>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
#include "../typedefs.h"
|
#include "../typedefs.h"
|
||||||
#include "WorldFiles.h"
|
#include "WorldFiles.h"
|
||||||
|
|
||||||
#define SCREENSHOTS_FOLDER "screenshots"
|
const fs::path SCREENSHOTS_FOLDER {"screenshots"};
|
||||||
|
|
||||||
fs::path EnginePaths::getUserfiles() const {
|
fs::path EnginePaths::getUserfiles() const {
|
||||||
return userfiles;
|
return userfiles;
|
||||||
@@ -93,22 +94,48 @@ void EnginePaths::setContentPacks(std::vector<ContentPack>* contentPacks) {
|
|||||||
this->contentPacks = contentPacks;
|
this->contentPacks = contentPacks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
static fs::path toCanonic(fs::path path) {
|
||||||
|
std::stack<std::string> parts;
|
||||||
|
path = path.lexically_normal();
|
||||||
|
while (true) {
|
||||||
|
parts.push(path.filename().u8string());
|
||||||
|
path = path.parent_path();
|
||||||
|
if (path.empty())
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
path = fs::u8path("");
|
||||||
|
while (!parts.empty()) {
|
||||||
|
const std::string part = parts.top();
|
||||||
|
parts.pop();
|
||||||
|
if (part == ".") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (part == "..") {
|
||||||
|
throw files_access_error("entry point reached");
|
||||||
|
}
|
||||||
|
|
||||||
|
path = path / fs::path(part);
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
fs::path EnginePaths::resolve(std::string path) {
|
fs::path EnginePaths::resolve(std::string path) {
|
||||||
size_t separator = path.find(':');
|
size_t separator = path.find(':');
|
||||||
if (separator == std::string::npos) {
|
if (separator == std::string::npos) {
|
||||||
return fs::u8path(path);
|
throw files_access_error("no entry point specified");
|
||||||
}
|
}
|
||||||
std::string prefix = path.substr(0, separator);
|
std::string prefix = path.substr(0, separator);
|
||||||
std::string filename = path.substr(separator+1);
|
std::string filename = path.substr(separator+1);
|
||||||
|
filename = toCanonic(fs::u8path(filename)).u8string();
|
||||||
|
|
||||||
if (prefix == "res" || prefix == "core") {
|
if (prefix == "res" || prefix == "core") {
|
||||||
return resources/fs::u8path(filename);
|
return resources/fs::u8path(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prefix == "user") {
|
if (prefix == "user") {
|
||||||
return userfiles/fs::u8path(filename);
|
return userfiles/fs::u8path(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prefix == "world") {
|
if (prefix == "world") {
|
||||||
return worldFolder/fs::u8path(filename);
|
return worldFolder/fs::u8path(filename);
|
||||||
}
|
}
|
||||||
@@ -120,7 +147,7 @@ fs::path EnginePaths::resolve(std::string path) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fs::u8path("./"+filename);
|
throw files_access_error("unknown entry point '"+prefix+"'");
|
||||||
}
|
}
|
||||||
|
|
||||||
ResPaths::ResPaths(fs::path mainRoot, std::vector<fs::path> roots)
|
ResPaths::ResPaths(fs::path mainRoot, std::vector<fs::path> roots)
|
||||||
|
|||||||
@@ -3,12 +3,18 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <stdexcept>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
#include "../content/ContentPack.h"
|
#include "../content/ContentPack.h"
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
class files_access_error : public std::runtime_error {
|
||||||
|
public:
|
||||||
|
files_access_error(const std::string& msg) : std::runtime_error(msg) {}
|
||||||
|
};
|
||||||
|
|
||||||
class EnginePaths {
|
class EnginePaths {
|
||||||
fs::path userfiles {"."};
|
fs::path userfiles {"."};
|
||||||
fs::path resources {"res"};
|
fs::path resources {"res"};
|
||||||
|
|||||||
@@ -24,17 +24,23 @@
|
|||||||
#include "../../../window/Window.h"
|
#include "../../../window/Window.h"
|
||||||
#include "../../../engine.h"
|
#include "../../../engine.h"
|
||||||
|
|
||||||
|
fs::path resolve_path(lua_State* L, const std::string& path) {
|
||||||
|
try {
|
||||||
|
return scripting::engine->getPaths()->resolve(path);
|
||||||
|
} catch (const files_access_error& err) {
|
||||||
|
luaL_error(L, err.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* == file library == */
|
/* == file library == */
|
||||||
int l_file_resolve(lua_State* L) {
|
int l_file_resolve(lua_State* L) {
|
||||||
std::string path = lua_tostring(L, 1);
|
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||||
fs::path resolved = scripting::engine->getPaths()->resolve(path);
|
lua_pushstring(L, path.u8string().c_str());
|
||||||
lua_pushstring(L, resolved.u8string().c_str());
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int l_file_read(lua_State* L) {
|
int l_file_read(lua_State* L) {
|
||||||
auto paths = scripting::engine->getPaths();
|
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||||
fs::path path = paths->resolve(lua_tostring(L, 1));
|
|
||||||
if (fs::is_regular_file(path)) {
|
if (fs::is_regular_file(path)) {
|
||||||
lua_pushstring(L, files::read_string(path).c_str());
|
lua_pushstring(L, files::read_string(path).c_str());
|
||||||
return 1;
|
return 1;
|
||||||
@@ -43,37 +49,32 @@ int l_file_read(lua_State* L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int l_file_write(lua_State* L) {
|
int l_file_write(lua_State* L) {
|
||||||
auto paths = scripting::engine->getPaths();
|
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||||
fs::path path = paths->resolve(lua_tostring(L, 1));
|
|
||||||
const char* text = lua_tostring(L, 2);
|
const char* text = lua_tostring(L, 2);
|
||||||
files::write_string(path, text);
|
files::write_string(path, text);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int l_file_exists(lua_State* L) {
|
int l_file_exists(lua_State* L) {
|
||||||
auto paths = scripting::engine->getPaths();
|
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||||
fs::path path = paths->resolve(lua_tostring(L, 1));
|
|
||||||
lua_pushboolean(L, fs::exists(path));
|
lua_pushboolean(L, fs::exists(path));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int l_file_isfile(lua_State* L) {
|
int l_file_isfile(lua_State* L) {
|
||||||
auto paths = scripting::engine->getPaths();
|
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||||
fs::path path = paths->resolve(lua_tostring(L, 1));
|
|
||||||
lua_pushboolean(L, fs::is_regular_file(path));
|
lua_pushboolean(L, fs::is_regular_file(path));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int l_file_isdir(lua_State* L) {
|
int l_file_isdir(lua_State* L) {
|
||||||
auto paths = scripting::engine->getPaths();
|
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||||
fs::path path = paths->resolve(lua_tostring(L, 1));
|
|
||||||
lua_pushboolean(L, fs::is_directory(path));
|
lua_pushboolean(L, fs::is_directory(path));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int l_file_length(lua_State* L) {
|
int l_file_length(lua_State* L) {
|
||||||
auto paths = scripting::engine->getPaths();
|
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||||
fs::path path = paths->resolve(lua_tostring(L, 1));
|
|
||||||
if (fs::exists(path)){
|
if (fs::exists(path)){
|
||||||
lua_pushinteger(L, fs::file_size(path));
|
lua_pushinteger(L, fs::file_size(path));
|
||||||
} else {
|
} else {
|
||||||
@@ -83,8 +84,7 @@ int l_file_length(lua_State* L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int l_file_mkdir(lua_State* L) {
|
int l_file_mkdir(lua_State* L) {
|
||||||
auto paths = scripting::engine->getPaths();
|
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||||
fs::path path = paths->resolve(lua_tostring(L, 1));
|
|
||||||
lua_pushboolean(L, fs::create_directory(path));
|
lua_pushboolean(L, fs::create_directory(path));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user