From 31ea1983017fc8fd395f4675ce3d9a16702fe43e Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 15 Nov 2025 16:57:24 +0300 Subject: [PATCH] add app.start_debug_instance --- src/engine/EnginePaths.cpp | 8 +++++++ src/engine/EnginePaths.hpp | 3 +++ src/logic/scripting/lua/libs/libapp.cpp | 30 ++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/engine/EnginePaths.cpp b/src/engine/EnginePaths.cpp index 8a786140..2d84317d 100644 --- a/src/engine/EnginePaths.cpp +++ b/src/engine/EnginePaths.cpp @@ -71,6 +71,14 @@ EnginePaths::EnginePaths(CoreParameters& params) io::create_subdevice("config", "user", "config"); } +std::filesystem::path EnginePaths::getResourcesFolder() const { + return resourcesFolder; +} + +std::filesystem::path EnginePaths::getUserFilesFolder() const { + return userFilesFolder; +} + io::path EnginePaths::getNewScreenshotFile(const std::string& ext) const { auto folder = SCREENSHOTS_FOLDER; if (!io::is_directory(folder)) { diff --git a/src/engine/EnginePaths.hpp b/src/engine/EnginePaths.hpp index 41d02236..4296a40b 100644 --- a/src/engine/EnginePaths.hpp +++ b/src/engine/EnginePaths.hpp @@ -48,6 +48,9 @@ public: EnginePaths(CoreParameters& params); + std::filesystem::path getResourcesFolder() const; + std::filesystem::path getUserFilesFolder() const; + io::path getWorldFolderByName(const std::string& name); io::path getWorldsFolder() const; diff --git a/src/logic/scripting/lua/libs/libapp.cpp b/src/logic/scripting/lua/libs/libapp.cpp index 4bd38f4b..7c8fab71 100644 --- a/src/logic/scripting/lua/libs/libapp.cpp +++ b/src/logic/scripting/lua/libs/libapp.cpp @@ -1,6 +1,34 @@ #include "api_lua.hpp" +#include "logic/scripting/scripting.hpp" +#include "engine/Engine.hpp" +#include "engine/EnginePaths.hpp" +#include "network/Network.hpp" +#include "util/platform.hpp" + +using namespace scripting; + +static int l_start_debug_instance(lua::State* L) { + int port = lua::tointeger(L, 1); + if (port == 0) { + port = engine->getNetwork().findFreePort(); + if (port == -1) { + throw std::runtime_error("could not find free port"); + } + } + const auto& paths = engine->getPaths(); + + std::vector args { + "--res", paths.getResourcesFolder().u8string(), + "--dir", paths.getUserFilesFolder().u8string(), + "--dbg-server", "tcp:" + std::to_string(port), + }; + platform::new_engine_instance(std::move(args)); + return lua::pushinteger(L, port); +} + const luaL_Reg applib[] = { - // see libcore.cpp an stdlib.lua + {"start_debug_instance", lua::wrap}, + // for other functions see libcore.cpp and stdlib.lua {nullptr, nullptr} };