From 6c568e6706cea0301c336c53aebc9e3d0f037ef6 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 1 Sep 2026 23:32:44 +0300 Subject: [PATCH] cleanup internal functions --- .../internal}/internal_events.lua | 6 ++- res/modules/internal/lifetime_events.lua | 45 ++++++++----------- res/scripts/stdlib.lua | 7 +-- src/engine/Engine.cpp | 2 +- src/logic/scripting/lua/lua_util.hpp | 6 +++ src/logic/scripting/scripting.cpp | 31 +++++-------- src/logic/scripting/scripting.hpp | 2 +- src/logic/scripting/scripting_hud.cpp | 8 +--- 8 files changed, 47 insertions(+), 60 deletions(-) rename res/{scripts => modules/internal}/internal_events.lua (97%) diff --git a/res/scripts/internal_events.lua b/res/modules/internal/internal_events.lua similarity index 97% rename from res/scripts/internal_events.lua rename to res/modules/internal/internal_events.lua index c71df5d61..b62758d4c 100644 --- a/res/scripts/internal_events.lua +++ b/res/modules/internal/internal_events.lua @@ -1,3 +1,5 @@ +local internals = __vc_internals + local updating_blocks = {} local present_queues = {} local REGISTER_BIT = 0x1 @@ -5,7 +7,7 @@ local UPDATING_BIT = 0x2 local PRESENT_BIT = 0x4 local REMOVED_BIT = 0x8 -block.__perform_ticks = function(delta) +function internals.tick_updating_blocks(delta) for id, entry in pairs(updating_blocks) do entry.timer = entry.timer + delta local steps = math.floor(entry.timer / entry.delta * #entry / 3) @@ -63,7 +65,7 @@ end local block_pull_register_events = block.__pull_register_events block.__pull_register_events = nil -block.__process_register_events = function() +function internals.process_block_register_events() local register_events = block_pull_register_events() if not register_events then return diff --git a/res/modules/internal/lifetime_events.lua b/res/modules/internal/lifetime_events.lua index 1d60d969d..ced28eb99 100644 --- a/res/modules/internal/lifetime_events.lua +++ b/res/modules/internal/lifetime_events.lua @@ -36,7 +36,7 @@ local function configure_SSAO() update_ssao_quality(app.get_setting("graphics.ssao")) end -local function __vc_on_hud_open() +function internals.on_hud_open() local _hud_is_content_access = hud._is_content_access local _hud_set_content_access = hud._set_content_access local _hud_set_debug_cheats = hud._set_debug_cheats @@ -131,7 +131,8 @@ local function ScheduleGroup() end local RULES_FILE = "world:rules.toml" -local function __vc_on_world_open() + +function internals.on_world_open() time.schedules.world = ScheduleGroup() if not file.exists(RULES_FILE) then @@ -143,15 +144,15 @@ local function __vc_on_world_open() end end -local function __vc_on_world_tick(tps) +function internals.on_world_tick(tps) time.schedules.world:tick(1.0 / tps) end -local function __vc_process_before_quit() - block.__process_register_events() +function internals.process_before_quit() + internals.process_block_register_events() end -local function __vc_on_world_save() +function internals.on_world_save() local rule_values = {} for name, rule in pairs(__rules.rules) do rule_values[name] = rule.value @@ -178,8 +179,6 @@ local __post_runnables = {} local fn_audio_reset_fetch_buffer = audio.__reset_fetch_buffer audio.__reset_fetch_buffer = nil -local __vc_update_coroutines = _G.__vc_update_coroutines - local function __process_post_runnables() if #__post_runnables > 0 then for _, func in ipairs(__post_runnables) do @@ -191,26 +190,30 @@ local function __process_post_runnables() __post_runnables = {} end - __vc_update_coroutines() + internals.update_coroutines() fn_audio_reset_fetch_buffer() debug.pull_events() network.__process_events() if not hud or not hud.is_paused() then - block.__process_register_events() - block.__perform_ticks(time.delta()) + internals.process_block_register_events() + internals.tick_updating_blocks(time.delta()) end end -local __vc__is_post_runnable = false +local is_post_runnable = false -function __vc__process_post_runnables() - __vc__is_post_runnable = true +function internals.process_post_runnables() + is_post_runnable = true local success, err = pcall(__process_post_runnables) if not success then debug.error("an error ocurred while processing post-runnables: ".. err) end - __vc__is_post_runnable = false + is_post_runnable = false +end + +function internals.is_post_runnable_context() + return is_post_runnable end function time.post_runnable(runnable) @@ -221,14 +224,4 @@ function internals.on_render() internals.on_animation_frame() end -return { - __vc_on_hud_open = __vc_on_hud_open, - __vc_on_world_open = __vc_on_world_open, - __vc_on_world_tick = __vc_on_world_tick, - __vc_process_before_quit = __vc_process_before_quit, - __vc_on_world_save = __vc_on_world_save, - __vc__process_post_runnables = __vc__process_post_runnables, - __vc_is_post_runnable_context = function() - return __vc__is_post_runnable - end -} +return {} diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index d2fdc59ab..069986e8e 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -1,4 +1,5 @@ local __app = __vc_app +local internals = __vc_internals local enable_experimental = __app.get_setting("debug.enable-experimental") ------------------------------------------------ @@ -45,7 +46,7 @@ local function complete_app_lib(app) app.tick = __app_tick local function call_in_app_script_co(func, ...) - if __vc_is_post_runnable_context() then + if internals.post_runnable_context() then func(...) return end @@ -208,7 +209,7 @@ function start_coroutine(chunk, name) __vc_named_coroutines[name] = co end -function __vc_update_coroutines() +function internals.update_coroutines() local dead = {} for name, co in pairs(__vc_named_coroutines) do local success, err = coroutine.resume(co) @@ -368,6 +369,7 @@ core.get_core_token = audio.input.__get_core_token require "core:internal/console" require "core:internal/deprecated" +require "core:internal/internal_events" ------------------------------------------ @@ -397,5 +399,4 @@ __vc_app = nil __vc_internals = nil __vc_lock_internal_modules() __vc_lock_internal_modules = nil -__vc_update_coroutines = nil __VC_SCRIPT_NAME = "" diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index f0e20f922..b3185f371 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -179,7 +179,7 @@ void Engine::initialize(CoreParameters coreParameters) { content = std::make_unique( *project, *paths, input.get(), [this]() { onContentLoad(); } ); - scripting::initialize(this); + scripting::initialize(*this); if (!isHeadless()) { gui->getMenu()->setPageLoader(scripting::create_page_loader()); diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index b1cd802bd..4ce6e2b51 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -636,6 +636,12 @@ namespace lua { int call(lua::State*, int argc, int nresults = -1); int call_nothrow(lua::State*, int argc, int nresults = 1); + inline void call_internal(lua::State* L, const std::string& name) { + if (lua::get_from_registry(L, lua::INTERNALS_TABLE, name, true)) { + lua::call_nothrow(L, 0, 0); + } + } + inline int eval( lua::State* L, int env, diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 8f3dc28eb..de3f61079 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -65,14 +65,13 @@ int scripting::load_script( return lua::execute(lua::get_main_state(), env, src, fileName); } -void scripting::initialize(Engine* engine) { - scripting::engine = engine; - scripting::content_control = &engine->getContentControl(); - lua::initialize(engine->getPaths(), engine->getCoreParameters()); +void scripting::initialize(Engine& engine) { + scripting::engine = &engine; + scripting::content_control = &engine.getContentControl(); + lua::initialize(engine.getPaths(), engine.getCoreParameters()); load_script(io::path("stdlib.lua"), true); load_script(io::path("classes.lua"), true); - load_script(io::path("internal_events.lua"), true); } class LuaCoroutine : public Process { @@ -244,9 +243,7 @@ std::unique_ptr scripting::start_app_script(const io::path& script) { void scripting::process_post_runnables() { auto L = lua::get_main_state(); - if (lua::getglobal(L, "__vc__process_post_runnables")) { - lua::call_nothrow(L, 0, 0); - } + lua::call_internal(L, "process_post_runnables"); } template @@ -321,9 +318,7 @@ void scripting::on_world_load(LevelController* controller) { scripting::controller = controller; auto L = lua::get_main_state(); - if (lua::getglobal(L, "__vc_on_world_open")) { - lua::call_nothrow(L, 0, 0); - } + lua::call_internal(L, "on_world_open"); for (auto& pack : content_control->getAllContentPacks()) { lua::emit_event(L, pack.id + ":.worldopen", [](auto L) { @@ -336,7 +331,7 @@ void scripting::on_world_load(LevelController* controller) { void scripting::on_world_tick(int tps) { auto L = lua::get_main_state(); - if (lua::getglobal(L, "__vc_on_world_tick")) { + if (lua::get_from_registry(L, lua::INTERNALS_TABLE, "on_world_tick", true)) { lua::pushinteger(L, tps); lua::call_nothrow(L, 1, 0); } @@ -350,16 +345,12 @@ void scripting::on_world_save() { for (auto& pack : content_control->getAllContentPacks()) { lua::emit_event(L, pack.id + ":.worldsave"); } - if (lua::getglobal(L, "__vc_on_world_save")) { - lua::call_nothrow(L, 0, 0); - } + lua::call_internal(L, "on_world_save"); } void scripting::process_before_quit() { auto L = lua::get_main_state(); - if (lua::getglobal(L, "__vc_process_before_quit")) { - lua::call_nothrow(L, 0, 0); - } + lua::call_internal(L, "process_before_quit"); } void scripting::on_world_quit() { @@ -367,9 +358,7 @@ void scripting::on_world_quit() { for (auto& pack : content_control->getAllContentPacks()) { lua::emit_event(L, pack.id + ":.worldquit"); } - if (lua::get_from_registry(L, lua::INTERNALS_TABLE, "on_world_quit", true)) { - lua::call_nothrow(L, 0, 0); - } + lua::call_internal(L, "on_world_quit"); scripting::level = nullptr; scripting::content = nullptr; scripting::indices = nullptr; diff --git a/src/logic/scripting/scripting.hpp b/src/logic/scripting/scripting.hpp index e774864ef..a80a6681b 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -48,7 +48,7 @@ namespace scripting { extern std::ostream* output_stream; extern std::ostream* error_stream; - void initialize(Engine* engine); + void initialize(Engine& engine); void on_content_load(Content* content); void on_content_reset(); diff --git a/src/logic/scripting/scripting_hud.cpp b/src/logic/scripting/scripting_hud.cpp index 075896710..dcd310e37 100644 --- a/src/logic/scripting/scripting_hud.cpp +++ b/src/logic/scripting/scripting_hud.cpp @@ -47,9 +47,7 @@ void scripting::on_frontend_init( load_script("hud_classes.lua"); - if (lua::getglobal(L, "__vc_on_hud_open")) { - lua::call_nothrow(L, 0, 0); - } + lua::call_internal(L, "on_hud_open"); for (auto& pack : content_control->getAllContentPacks()) { lua::emit_event( @@ -65,9 +63,7 @@ void scripting::on_frontend_init( void scripting::on_frontend_render() { auto L = lua::get_main_state(); - if (lua::get_from_registry(L, lua::INTERNALS_TABLE, "on_render", true)) { - lua::call_nothrow(L, 0, 0); - } + lua::call_internal(L, "on_render"); for (auto& pack : content_control->getAllContentPacks()) { lua::emit_event(L, pack.id + ":.hudrender", [](lua::State* L) { return 0;