#include "lua_commons.h" #include "api_lua.h" #include "../scripting.h" #include "../../../engine.h" #include "../../../files/files.h" #include "../../../files/engine_paths.h" #include #include namespace fs = std::filesystem; static 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()); abort(); // unreachable } } static int l_file_resolve(lua_State* L) { fs::path path = resolve_path(L, lua_tostring(L, 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)); if (fs::is_regular_file(path)) { lua_pushstring(L, files::read_string(path).c_str()); return 1; } return luaL_error(L, "file does not exists '%s'", path.u8string().c_str()); } 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); files::write_string(path, text); return 1; } static int l_file_exists(lua_State* L) { fs::path path = resolve_path(L, lua_tostring(L, 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)); 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)); 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)); if (fs::exists(path)){ lua_pushinteger(L, fs::file_size(path)); } else { lua_pushinteger(L, -1); } return 1; } static int l_file_mkdir(lua_State* L) { fs::path path = resolve_path(L, lua_tostring(L, 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)); 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)); if (fs::is_regular_file(path)) { size_t length = (size_t) fs::file_size(path); ubyte* bytes = files::read_bytes(path, length); lua_createtable(L, length, 0); int newTable = lua_gettop(L); for(size_t i = 0; i < length; i++) { lua_pushnumber(L, bytes[i]); lua_rawseti(L, newTable, i+1); } return 1; } return luaL_error(L, "file does not exists '%s'", path.u8string().c_str()); } static int l_file_write_bytes(lua_State* L) { int pathIndex = 1; if(!lua_isstring(L, pathIndex)) { return luaL_error(L, "string expected"); } fs::path path = resolve_path(L, lua_tostring(L, pathIndex)); std::vector bytes; size_t len = lua_objlen(L, 2); int bytesIndex = -1; if(!lua_istable(L, bytesIndex)) { return luaL_error(L, "table expected"); } else { lua_pushnil(L); bytesIndex--; size_t i = 1; while(lua_next(L, bytesIndex) != 0) { if(i >= len) { break; } const int byte = lua_tointeger(L, -1); if(byte < 0 || byte > 255) { return luaL_error(L, "byte '%i' at index '%i' is less than 0 or greater than 255", byte, i); } bytes.push_back(byte); lua_pop(L, 1); i++; } lua_pushboolean(L, files::write_bytes(path, &bytes[0], len)); return 1; } } const luaL_Reg filelib [] = { {"resolve", lua_wrap_errors}, {"read", lua_wrap_errors}, {"write", lua_wrap_errors}, {"exists", lua_wrap_errors}, {"isfile", lua_wrap_errors}, {"isdir", lua_wrap_errors}, {"length", lua_wrap_errors}, {"mkdir", lua_wrap_errors}, {"mkdirs", lua_wrap_errors}, {"read_bytes", lua_wrap_errors}, {"write_bytes", lua_wrap_errors}, {NULL, NULL} };