diff --git a/src/logic/scripting/lua/libs/libapp.cpp b/src/logic/scripting/lua/libs/libapp.cpp index 09ea33ce3..39acd97c0 100644 --- a/src/logic/scripting/lua/libs/libapp.cpp +++ b/src/logic/scripting/lua/libs/libapp.cpp @@ -362,7 +362,7 @@ static int l_start_debug_instance(lua::State* L) { args.emplace_back(io::resolve(std::string(projectPath)).string()); } - platform::new_engine_instance(std::move(args)); + platform::new_engine_instance(std::move(args), ""); return lua::pushinteger(L, port); } diff --git a/src/util/platform.cpp b/src/util/platform.cpp index f9ecf070d..f113641e5 100644 --- a/src/util/platform.cpp +++ b/src/util/platform.cpp @@ -215,7 +215,9 @@ std::filesystem::path platform::get_executable_path() { #endif } -void platform::new_engine_instance(const std::vector& args) { +void platform::new_engine_instance( + const std::vector& args, std::filesystem::path outputFile +) { auto executable = get_executable_path(); #ifdef _WIN32 @@ -250,6 +252,24 @@ void platform::new_engine_instance(const std::vector& args) { auto commandString = toWString(ss.str()); STARTUPINFOW si = { sizeof(si) }; + if (!outputFile.empty()) { + si.dwFlags |= STARTF_USESTDHANDLES; + si.hStdOutput = CreateFileW( + toWString(outputFile.u8string()).data(), + GENERIC_WRITE, + FILE_SHARE_WRITE | FILE_SHARE_READ, + nullptr, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + nullptr + ); + if (si.hStdOutput == INVALID_HANDLE_VALUE) { + throw std::runtime_error( + "CreateFileW failed with code: " + + std::to_string(GetLastError()) + ); + } + } PROCESS_INFORMATION pi = { 0 }; DWORD flags = CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS; // | CREATE_NO_WINDOW; @@ -280,7 +300,11 @@ void platform::new_engine_instance(const std::vector& args) { for (int i = 0; i < args.size(); i++) { ss << " " << util::quote(args[i]); } - ss << " >/dev/null &"; + if (outputFile.empty()) { + ss << " >/dev/null &"; + } else { + ss << " >" << util::quote(outputFile.u8string()) << " &"; + } auto command = ss.str(); logger.info() << command; @@ -302,4 +326,4 @@ bool platform::stdin_has_data() { fds.events = POLLIN; return poll(&fds, 1, 0) == 1; #endif -} \ No newline at end of file +} diff --git a/src/util/platform.hpp b/src/util/platform.hpp index 620178fc1..54c56a0c5 100644 --- a/src/util/platform.hpp +++ b/src/util/platform.hpp @@ -18,7 +18,9 @@ namespace platform { /// @brief Get current process running executable path std::filesystem::path get_executable_path(); /// @brief Run a separate engine instance with specified arguments - void new_engine_instance(const std::vector& args); + void new_engine_instance( + const std::vector& args, std::filesystem::path outputFile + ); /// @brief Open URL in web browser bool open_url(const std::string& url); /// @brief Check if stdin has input