file library error handling update
This commit is contained in:
parent
eca050123d
commit
b7887fa1ca
@ -1,5 +1,6 @@
|
||||
#include "lua_commons.hpp"
|
||||
#include "api_lua.hpp"
|
||||
#include "LuaState.hpp"
|
||||
#include "../scripting.hpp"
|
||||
#include "../../../engine.hpp"
|
||||
#include "../../../files/files.hpp"
|
||||
@ -11,14 +12,20 @@
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace scripting {
|
||||
extern lua::LuaState* state;
|
||||
}
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
static fs::path resolve_path(lua_State*, const std::string& path) {
|
||||
return scripting::engine->getPaths()->resolve(path);
|
||||
return engine->getPaths()->resolve(path);
|
||||
}
|
||||
|
||||
static int l_file_find(lua_State* L) {
|
||||
std::string path = lua_tostring(L, 1);
|
||||
std::string path = state->requireString(1);
|
||||
try {
|
||||
lua_pushstring(L, scripting::engine->getResPaths()->findRaw(path).c_str());
|
||||
lua_pushstring(L, engine->getResPaths()->findRaw(path).c_str());
|
||||
return 1;
|
||||
} catch (const std::runtime_error& err) {
|
||||
return 0;
|
||||
@ -26,13 +33,13 @@ static int l_file_find(lua_State* L) {
|
||||
}
|
||||
|
||||
static int l_file_resolve(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
fs::path path = resolve_path(L, state->requireString(1));
|
||||
lua_pushstring(L, path.u8string().c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_file_read(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
fs::path path = resolve_path(L, state->requireString(1));
|
||||
if (fs::is_regular_file(path)) {
|
||||
lua_pushstring(L, files::read_string(path).c_str());
|
||||
return 1;
|
||||
@ -41,14 +48,14 @@ static int l_file_read(lua_State* L) {
|
||||
}
|
||||
|
||||
static int l_file_write(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
const char* text = lua_tostring(L, 2);
|
||||
fs::path path = resolve_path(L, state->requireString(1));
|
||||
auto text = state->requireString(2);
|
||||
files::write_string(path, text);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_file_remove(lua_State* L) {
|
||||
std::string rawpath = lua_tostring(L, 1);
|
||||
std::string rawpath = state->requireString(1);
|
||||
fs::path path = resolve_path(L, rawpath);
|
||||
auto entryPoint = rawpath.substr(0, rawpath.find(':'));
|
||||
if (entryPoint != "world") {
|
||||
@ -59,7 +66,7 @@ static int l_file_remove(lua_State* L) {
|
||||
}
|
||||
|
||||
static int l_file_remove_tree(lua_State* L) {
|
||||
std::string rawpath = lua_tostring(L, 1);
|
||||
std::string rawpath = state->requireString(1);
|
||||
fs::path path = resolve_path(L, rawpath);
|
||||
auto entryPoint = rawpath.substr(0, rawpath.find(':'));
|
||||
if (entryPoint != "world") {
|
||||
@ -70,25 +77,25 @@ static int l_file_remove_tree(lua_State* L) {
|
||||
}
|
||||
|
||||
static int l_file_exists(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
fs::path path = resolve_path(L, state->requireString(1));
|
||||
lua_pushboolean(L, fs::exists(path));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_file_isfile(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
fs::path path = resolve_path(L, state->requireString(1));
|
||||
lua_pushboolean(L, fs::is_regular_file(path));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_file_isdir(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
fs::path path = resolve_path(L, state->requireString(1));
|
||||
lua_pushboolean(L, fs::is_directory(path));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_file_length(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
fs::path path = resolve_path(L, state->requireString(1));
|
||||
if (fs::exists(path)){
|
||||
lua_pushinteger(L, fs::file_size(path));
|
||||
} else {
|
||||
@ -98,19 +105,19 @@ static int l_file_length(lua_State* L) {
|
||||
}
|
||||
|
||||
static int l_file_mkdir(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
fs::path path = resolve_path(L, state->requireString(1));
|
||||
lua_pushboolean(L, fs::create_directory(path));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_file_mkdirs(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
fs::path path = resolve_path(L, state->requireString(1));
|
||||
lua_pushboolean(L, fs::create_directories(path));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_file_read_bytes(lua_State* L) {
|
||||
fs::path path = resolve_path(L, lua_tostring(L, 1));
|
||||
fs::path path = resolve_path(L, state->requireString(1));
|
||||
if (fs::is_regular_file(path)) {
|
||||
size_t length = static_cast<size_t>(fs::file_size(path));
|
||||
|
||||
@ -152,7 +159,7 @@ static int l_file_write_bytes(lua_State* L) {
|
||||
throw std::runtime_error("string expected");
|
||||
}
|
||||
|
||||
fs::path path = resolve_path(L, lua_tostring(L, pathIndex));
|
||||
fs::path path = resolve_path(L, state->requireString(pathIndex));
|
||||
|
||||
std::vector<ubyte> bytes;
|
||||
|
||||
@ -167,7 +174,7 @@ static int l_file_write_bytes(lua_State* L) {
|
||||
}
|
||||
|
||||
static int l_file_list_all_res(lua_State* L, const std::string& path) {
|
||||
auto files = scripting::engine->getResPaths()->listdirRaw(path);
|
||||
auto files = engine->getResPaths()->listdirRaw(path);
|
||||
lua_createtable(L, files.size(), 0);
|
||||
for (size_t i = 0; i < files.size(); i++) {
|
||||
lua_pushstring(L, files[i].c_str());
|
||||
@ -177,7 +184,7 @@ static int l_file_list_all_res(lua_State* L, const std::string& path) {
|
||||
}
|
||||
|
||||
static int l_file_list(lua_State* L) {
|
||||
std::string dirname = lua_tostring(L, 1);
|
||||
std::string dirname = state->requireString(1);
|
||||
if (dirname.find(':') == std::string::npos) {
|
||||
return l_file_list_all_res(L, dirname);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user