diff --git a/doc/en/scripting/builtins/libapp.md b/doc/en/scripting/builtins/libapp.md index 69a472ee..45414ba1 100644 --- a/doc/en/scripting/builtins/libapp.md +++ b/doc/en/scripting/builtins/libapp.md @@ -7,6 +7,8 @@ The script/test name without the path and extension is available as `app.script` local filename = "script:"..app.script..".lua" ``` +Since the control script may not belong to any of the packs, it does not belongs to its own package and has its own global namespace in which all global functions and tables are available, as well as the `app` library. + ## Functions ```lua diff --git a/doc/ru/scripting/builtins/libapp.md b/doc/ru/scripting/builtins/libapp.md index 4e9493c4..d2ddec72 100644 --- a/doc/ru/scripting/builtins/libapp.md +++ b/doc/ru/scripting/builtins/libapp.md @@ -7,6 +7,8 @@ local filename = "script:"..app.script..".lua" ``` +Так как управляющий сценарий может не принадлежать ни одному из паков, он не относиться к своему паку и имеет собственное пространство имён, в котором доступны все глобальные функции и таблицы, а также библиотека `app`. + ## Функции ```lua diff --git a/res/layouts/pages/scripts.xml.lua b/res/layouts/pages/scripts.xml.lua index dad0d1ff..72c3246f 100644 --- a/res/layouts/pages/scripts.xml.lua +++ b/res/layouts/pages/scripts.xml.lua @@ -1,18 +1,10 @@ function run_script(path) - debug.log("starting application script "..path) - - local code = file.read(path) - local chunk, err = loadstring(code, path) - if chunk == nil then - error(err) - end - setfenv(chunk, setmetatable({app=__vc_app}, {__index=_G})) - start_coroutine(chunk, path) + __vc_start_app_script(path) end function refresh() document.list:clear() - + local allpacks = table.merge(pack.get_available(), pack.get_installed()) local infos = pack.get_info(allpacks) for _, name in ipairs(allpacks) do diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 19c17384..6e1280cd 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -114,6 +114,20 @@ events = require "core:internal/events" function pack.unload(prefix) events.remove_by_prefix(prefix) + __vc__pack_envs[prefix] = nil +end + +function __vc_start_app_script(path) + debug.log("starting application script "..path) + + local code = file.read(path) + local chunk, err = loadstring(code, path) + if chunk == nil then + error(err) + end + local script_env = setmetatable({app = app or __vc_app}, {__index=_G}) + chunk = setfenv(chunk, script_env) + return __vc_start_coroutine(chunk, path) end gui_util = require "core:internal/gui_util" diff --git a/src/engine/ServerMainloop.cpp b/src/engine/ServerMainloop.cpp index 2f446533..ab35ffbd 100644 --- a/src/engine/ServerMainloop.cpp +++ b/src/engine/ServerMainloop.cpp @@ -32,8 +32,7 @@ void ServerMainloop::run() { setLevel(std::move(level)); }); - logger.info() << "starting test " << coreParams.scriptFile.string(); - auto process = scripting::start_coroutine( + auto process = scripting::start_app_script( "script:" + coreParams.scriptFile.filename().u8string() ); diff --git a/src/logic/scripting/lua/libs/libgui.cpp b/src/logic/scripting/lua/libs/libgui.cpp index fc3732a1..588ab01b 100644 --- a/src/logic/scripting/lua/libs/libgui.cpp +++ b/src/logic/scripting/lua/libs/libgui.cpp @@ -19,6 +19,8 @@ #include "graphics/ui/gui_util.hpp" #include "graphics/ui/markdown.hpp" #include "graphics/core/Font.hpp" +#include "content/Content.hpp" +#include "content/ContentPack.hpp" #include "items/Inventories.hpp" #include "util/stringutil.hpp" #include "world/Level.hpp" @@ -1027,13 +1029,17 @@ static int l_gui_load_document(lua::State* L) { io::path filename = lua::require_string(L, 1); auto alias = lua::require_string(L, 2); auto args = lua::tovalue(L, 3); - + auto prefix = filename.entryPoint(); + + auto env = scripting::get_root_environment(); + if (content) { + if (auto runtime = content->getPackRuntime(prefix)) { + env = runtime->getEnvironment(); + } + } + auto documentPtr = UiDocument::read( - engine->getGUI(), - scripting::get_root_environment(), - alias, - filename, - filename.string() + engine->getGUI(), std::move(env), alias, filename, filename.string() ); auto document = documentPtr.get(); engine->getAssets()->store(std::move(documentPtr), alias); diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 6431d4ac..244975c3 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -150,12 +150,10 @@ std::unique_ptr scripting::load_client_project_script( return std::make_unique(L, std::move(env)); } -std::unique_ptr scripting::start_coroutine(const io::path& script) { +std::unique_ptr scripting::start_app_script(const io::path& script) { auto L = lua::get_main_state(); - auto method = "__vc_start_coroutine"; - if (lua::getglobal(L, method)) { - auto source = io::read_string(script); - lua::loadbuffer(L, 0, source, script.name()); + if (lua::getglobal(L, "__vc_start_app_script")) { + lua::pushstring(L, script.string()); if (lua::call(L, 1)) { int id = lua::tointeger(L, -1); lua::pop(L, 1); diff --git a/src/logic/scripting/scripting.hpp b/src/logic/scripting/scripting.hpp index 5a86b81d..d9c7e614 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -76,7 +76,7 @@ namespace scripting { const io::path& script ); - std::unique_ptr start_coroutine(const io::path& script); + std::unique_ptr start_app_script(const io::path& script); void on_world_load(LevelController* controller); void on_world_tick(int tps);