minor refactor

This commit is contained in:
MihailRis 2025-11-04 11:53:48 +03:00
parent 90f7fce2b1
commit ca6096c397
5 changed files with 29 additions and 14 deletions

View File

@ -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

View File

@ -186,6 +186,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})
setfenv(chunk, script_env)
return start_coroutine(chunk, path)
end
gui_util = require "core:internal/gui_util"

View File

@ -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()
);

View File

@ -151,9 +151,10 @@ std::unique_ptr<IClientProjectScript> scripting::load_client_project_script(
return std::make_unique<LuaProjectScript>(L, std::move(env));
}
std::unique_ptr<Process> scripting::start_coroutine(const io::path& script) {
static std::unique_ptr<Process> start_lua_coroutine(
const io::path& script, const std::string& method
) {
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());
@ -167,6 +168,14 @@ std::unique_ptr<Process> scripting::start_coroutine(const io::path& script) {
return nullptr;
}
std::unique_ptr<Process> scripting::start_coroutine(const io::path& script) {
return start_lua_coroutine(script, "__vc_start_coroutine");
}
std::unique_ptr<Process> scripting::start_app_script(const io::path& script) {
return start_lua_coroutine(script, "__vc_start_app_script");
}
[[nodiscard]] scriptenv scripting::get_root_environment() {
return std::make_shared<int>(0);
}

View File

@ -77,6 +77,7 @@ namespace scripting {
);
std::unique_ptr<Process> start_coroutine(const io::path& script);
std::unique_ptr<Process> start_app_script(const io::path& script);
void on_world_load(LevelController* controller);
void on_world_tick(int tps);