From e059b9395f8b4be6b2ddf99b661fbc474e956988 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 16 Apr 2024 22:23:46 +0300 Subject: [PATCH] gui.template test --- res/layouts/templates/pack.xml | 15 +++++++++++++++ res/scripts/stdlib.lua | 13 +++++++++++++ src/logic/scripting/lua/libfile.cpp | 22 ++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 res/layouts/templates/pack.xml diff --git a/res/layouts/templates/pack.xml b/res/layouts/templates/pack.xml new file mode 100644 index 00000000..c5e56d46 --- /dev/null +++ b/res/layouts/templates/pack.xml @@ -0,0 +1,15 @@ + + + + + + + diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 942f3c15..cbbf5762 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -201,6 +201,19 @@ function time.post_runnable(runnable) table.insert(__post_runnables, runnable) end +function gui.template(name, params) + local text = file.read(file.find("layouts/templates/"..name..".xml")) + for k,v in pairs(params) do + text = text:gsub("(%%{"..k.."})", tostring(v)) + end + text = text:gsub("if%s*=%s*'%%{%w+}'", "if=''") + text = text:gsub("if%s*=%s*\"%%{%w+}\"", "if=\"\"") + -- remove unsolved properties: attr='%{var}' + text = text:gsub("%w+%s*=%s*'%%{%w+}'%s?", "") + text = text:gsub("%w+%s*=%s*\"%%{%w+}\"%s?", "") + return text +end + -- Deprecated functions block_index = block.index block_name = block.name diff --git a/src/logic/scripting/lua/libfile.cpp b/src/logic/scripting/lua/libfile.cpp index be166db9..96839aa1 100644 --- a/src/logic/scripting/lua/libfile.cpp +++ b/src/logic/scripting/lua/libfile.cpp @@ -19,6 +19,27 @@ static fs::path resolve_path(lua_State* L, const std::string& path) { } } +// TODO: move to ResPaths +static int l_file_find(lua_State* L) { + std::string path = lua_tostring(L, 1); + + auto& packs = scripting::engine->getContentPacks(); + for (int i = packs.size()-1; i >= 0; i--) { + auto& pack = packs[i]; + if (fs::exists(pack.folder / fs::path(path))) { + lua_pushstring(L, (pack.id+":"+path).c_str()); + return 1; + } + } + auto resDir = scripting::engine->getResPaths()->getMainRoot(); + if (fs::exists(resDir / fs::path(path))) { + lua_pushstring(L, ("core:"+path).c_str()); + return 1; + } + luaL_error(L, "file not found %q", path.c_str()); + return 0; +} + 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()); @@ -145,6 +166,7 @@ static int l_file_write_bytes(lua_State* L) { const luaL_Reg filelib [] = { {"resolve", lua_wrap_errors}, + {"find", lua_wrap_errors}, {"read", lua_wrap_errors}, {"write", lua_wrap_errors}, {"exists", lua_wrap_errors},