From b1e14a294444e4fde541b6d56291635d7f381206 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 2 Aug 2026 16:20:33 +0300 Subject: [PATCH] add app.terminate(handle) --- src/constants.hpp | 1 + src/logic/scripting/lua/libs/libapp.cpp | 33 ++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/constants.hpp b/src/constants.hpp index 0396f59a6..5bacfe078 100644 --- a/src/constants.hpp +++ b/src/constants.hpp @@ -16,6 +16,7 @@ inline constexpr bool ENGINE_DEBUG_BUILD = true; inline const std::string ENGINE_VERSION_STRING = "0.32"; +inline constexpr uint MAX_SUBPROCESSES = 1; inline constexpr uint MAX_SUBPROCESS_DEPTH = 2; /// @brief world regions format version diff --git a/src/logic/scripting/lua/libs/libapp.cpp b/src/logic/scripting/lua/libs/libapp.cpp index e3d41d8c7..e86b6d88e 100644 --- a/src/logic/scripting/lua/libs/libapp.cpp +++ b/src/logic/scripting/lua/libs/libapp.cpp @@ -20,10 +20,12 @@ #include "window/Window.hpp" #include "world/Level.hpp" +#include + using namespace scripting; namespace { - static std::unique_ptr sub_instance = nullptr; + static std::array, MAX_SUBPROCESSES> processes; } /// @brief Check if content is loaded @@ -405,12 +407,36 @@ static int l_start_background_instance(lua::State* L) { args.emplace_back("--project"); args.emplace_back(io::resolve(engine->getProject().path).u8string()); - ::sub_instance = platform::new_engine_instance( + int handle = -1; + for (int i = 0; i < ::processes.size(); i++) { + if (!::processes[i] || !::processes[i]->isActive()) { + handle = i; + break; + } + } + if (handle == -1) { + throw std::runtime_error("sub-processes limit exceeded"); + } + ::processes[handle] = platform::new_engine_instance( std::move(args), outputPath.empty() ? "" : io::resolve(outputPath), true ); - return 0; + return lua::pushinteger(L, handle); +} + +static int l_terminate(lua::State* L) { + int handle = lua::tointeger(L, 1); + if (handle < 0 || handle >= ::processes.size()) { + throw std::runtime_error("invalid process handle"); + } + auto& process = ::processes[handle]; + if (process == nullptr) { + return lua::pushboolean(L, false); + } + bool active = process->isActive(); + process.reset(); + return lua::pushboolean(L, active); } const luaL_Reg applib[] = { @@ -446,5 +472,6 @@ const luaL_Reg applib[] = { {"create_memory_device", lua::wrap}, {"start_debug_instance", lua::wrap}, {"start_background_instance", lua::wrap}, + {"terminate", lua::wrap}, {nullptr, nullptr} };