lua: file library
This commit is contained in:
parent
74483dd385
commit
04a9c2045e
@ -31,8 +31,7 @@ end
|
|||||||
-- nocache - ignore cached script, load anyway
|
-- nocache - ignore cached script, load anyway
|
||||||
function load_script(path, nocache)
|
function load_script(path, nocache)
|
||||||
local packname, filename = parse_path(path)
|
local packname, filename = parse_path(path)
|
||||||
local packpath = pack.get_folder(packname)
|
local fullpath = file.resolve(path);
|
||||||
local fullpath = packpath..filename
|
|
||||||
|
|
||||||
-- __cached_scripts used in condition because cached result may be nil
|
-- __cached_scripts used in condition because cached result may be nil
|
||||||
if not nocache and __cached_scripts[fullpath] ~= nil then
|
if not nocache and __cached_scripts[fullpath] ~= nil then
|
||||||
|
|||||||
@ -162,6 +162,8 @@ void Engine::loadContent() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
assets->extend(*new_assets.get());
|
assets->extend(*new_assets.get());
|
||||||
|
|
||||||
|
paths->setContentPacks(&contentPacks);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::loadWorldContent(const fs::path& folder) {
|
void Engine::loadWorldContent(const fs::path& folder) {
|
||||||
|
|||||||
@ -6,8 +6,6 @@
|
|||||||
|
|
||||||
#define SCREENSHOTS_FOLDER "screenshots"
|
#define SCREENSHOTS_FOLDER "screenshots"
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
|
||||||
|
|
||||||
fs::path EnginePaths::getUserfiles() const {
|
fs::path EnginePaths::getUserfiles() const {
|
||||||
return userfiles;
|
return userfiles;
|
||||||
}
|
}
|
||||||
@ -55,6 +53,36 @@ void EnginePaths::setResources(fs::path folder) {
|
|||||||
this->resources = folder;
|
this->resources = folder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void EnginePaths::setContentPacks(std::vector<ContentPack>* contentPacks) {
|
||||||
|
this->contentPacks = contentPacks;
|
||||||
|
}
|
||||||
|
|
||||||
|
fs::path EnginePaths::resolve(std::string path) {
|
||||||
|
size_t separator = path.find(':');
|
||||||
|
if (separator == std::string::npos) {
|
||||||
|
return fs::path(path);
|
||||||
|
}
|
||||||
|
std::string prefix = path.substr(0, separator);
|
||||||
|
std::string filename = path.substr(separator+1);
|
||||||
|
|
||||||
|
if (prefix == "res" || prefix == "core") {
|
||||||
|
return resources/fs::path(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (prefix == "user") {
|
||||||
|
return userfiles/fs::path(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contentPacks) {
|
||||||
|
for (auto& pack : *contentPacks) {
|
||||||
|
if (pack.id == prefix) {
|
||||||
|
return pack.folder/fs::path(filename);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fs::path("./"+filename);
|
||||||
|
}
|
||||||
|
|
||||||
ResPaths::ResPaths(fs::path mainRoot, std::vector<fs::path> roots)
|
ResPaths::ResPaths(fs::path mainRoot, std::vector<fs::path> roots)
|
||||||
: mainRoot(mainRoot), roots(roots) {
|
: mainRoot(mainRoot), roots(roots) {
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,30 +5,38 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
class EnginePaths {
|
#include "../content/ContentPack.h"
|
||||||
std::filesystem::path userfiles {"."};
|
|
||||||
std::filesystem::path resources {"res"};
|
|
||||||
public:
|
|
||||||
std::filesystem::path getUserfiles() const;
|
|
||||||
std::filesystem::path getResources() const;
|
|
||||||
|
|
||||||
std::filesystem::path getScreenshotFile(std::string ext);
|
namespace fs = std::filesystem;
|
||||||
std::filesystem::path getWorldsFolder();
|
|
||||||
|
class EnginePaths {
|
||||||
|
fs::path userfiles {"."};
|
||||||
|
fs::path resources {"res"};
|
||||||
|
std::vector<ContentPack>* contentPacks = nullptr;
|
||||||
|
public:
|
||||||
|
fs::path getUserfiles() const;
|
||||||
|
fs::path getResources() const;
|
||||||
|
|
||||||
|
fs::path getScreenshotFile(std::string ext);
|
||||||
|
fs::path getWorldsFolder();
|
||||||
bool isWorldNameUsed(std::string name);
|
bool isWorldNameUsed(std::string name);
|
||||||
|
|
||||||
void setUserfiles(std::filesystem::path folder);
|
void setUserfiles(fs::path folder);
|
||||||
void setResources(std::filesystem::path folder);
|
void setResources(fs::path folder);
|
||||||
|
void setContentPacks(std::vector<ContentPack>* contentPacks);
|
||||||
|
|
||||||
|
fs::path resolve(std::string path);
|
||||||
};
|
};
|
||||||
|
|
||||||
class ResPaths {
|
class ResPaths {
|
||||||
std::filesystem::path mainRoot;
|
fs::path mainRoot;
|
||||||
std::vector<std::filesystem::path> roots;
|
std::vector<fs::path> roots;
|
||||||
public:
|
public:
|
||||||
ResPaths(std::filesystem::path mainRoot,
|
ResPaths(fs::path mainRoot,
|
||||||
std::vector<std::filesystem::path> roots);
|
std::vector<fs::path> roots);
|
||||||
|
|
||||||
std::filesystem::path find(const std::string& filename) const;
|
fs::path find(const std::string& filename) const;
|
||||||
std::vector<std::filesystem::path> listdir(const std::string& folder) const;
|
std::vector<fs::path> listdir(const std::string& folder) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // FILES_ENGINE_PATHS_H_
|
#endif // FILES_ENGINE_PATHS_H_
|
||||||
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
#include "../../files/files.h"
|
||||||
#include "../../physics/Hitbox.h"
|
#include "../../physics/Hitbox.h"
|
||||||
#include "../../objects/Player.h"
|
#include "../../objects/Player.h"
|
||||||
#include "../../world/Level.h"
|
#include "../../world/Level.h"
|
||||||
@ -29,6 +30,75 @@ inline void luaL_openlib(lua_State* L, const char* name, const luaL_Reg* libfunc
|
|||||||
lua_setglobal(L, name);
|
lua_setglobal(L, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* == file library == */
|
||||||
|
static int l_file_resolve(lua_State* L) {
|
||||||
|
std::string path = lua_tostring(L, 1);
|
||||||
|
fs::path resolved = scripting::engine->getPaths()->resolve(path);
|
||||||
|
lua_pushstring(L, resolved.u8string().c_str());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int l_file_read(lua_State* L) {
|
||||||
|
auto paths = scripting::engine->getPaths();
|
||||||
|
fs::path path = paths->resolve(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) {
|
||||||
|
auto paths = scripting::engine->getPaths();
|
||||||
|
fs::path path = paths->resolve(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) {
|
||||||
|
auto paths = scripting::engine->getPaths();
|
||||||
|
fs::path path = paths->resolve(lua_tostring(L, 1));
|
||||||
|
lua_pushboolean(L, fs::exists(path));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int l_file_isfile(lua_State* L) {
|
||||||
|
auto paths = scripting::engine->getPaths();
|
||||||
|
fs::path path = paths->resolve(lua_tostring(L, 1));
|
||||||
|
lua_pushboolean(L, fs::is_regular_file(path));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int l_file_isdir(lua_State* L) {
|
||||||
|
auto paths = scripting::engine->getPaths();
|
||||||
|
fs::path path = paths->resolve(lua_tostring(L, 1));
|
||||||
|
lua_pushboolean(L, fs::is_directory(path));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int l_file_length(lua_State* L) {
|
||||||
|
auto paths = scripting::engine->getPaths();
|
||||||
|
fs::path path = paths->resolve(lua_tostring(L, 1));
|
||||||
|
if (fs::exists(path)){
|
||||||
|
lua_pushinteger(L, fs::file_size(path));
|
||||||
|
} else {
|
||||||
|
lua_pushinteger(L, -1);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const luaL_Reg filelib [] = {
|
||||||
|
{"resolve", l_file_resolve},
|
||||||
|
{"read", l_file_read},
|
||||||
|
{"file", l_file_write},
|
||||||
|
{"exists", l_file_exists},
|
||||||
|
{"isfile", l_file_isfile},
|
||||||
|
{"isdir", l_file_isdir},
|
||||||
|
{"length", l_file_length},
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
/* == time library == */
|
/* == time library == */
|
||||||
static int l_time_uptime(lua_State* L) {
|
static int l_time_uptime(lua_State* L) {
|
||||||
lua_pushnumber(L, Window::time());
|
lua_pushnumber(L, Window::time());
|
||||||
@ -313,6 +383,7 @@ void apilua::create_funcs(lua_State* L) {
|
|||||||
luaL_openlib(L, "world", worldlib, 0);
|
luaL_openlib(L, "world", worldlib, 0);
|
||||||
luaL_openlib(L, "player", playerlib, 0);
|
luaL_openlib(L, "player", playerlib, 0);
|
||||||
luaL_openlib(L, "time", timelib, 0);
|
luaL_openlib(L, "time", timelib, 0);
|
||||||
|
luaL_openlib(L, "file", filelib, 0);
|
||||||
|
|
||||||
lua_addfunc(L, l_block_index, "block_index");
|
lua_addfunc(L, l_block_index, "block_index");
|
||||||
lua_addfunc(L, l_block_name, "block_name");
|
lua_addfunc(L, l_block_name, "block_name");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user