add platform::new_engine_instance

This commit is contained in:
MihailRis 2025-10-03 22:02:56 +03:00
parent b14bad5d24
commit 5a5bd5cb2e
2 changed files with 70 additions and 0 deletions

View File

@ -115,6 +115,73 @@ int platform::get_process_id() {
return getpid();
}
void platform::new_engine_instance(const std::vector<std::string>& args) {
auto executable = get_executable_path();
#ifdef _WIN32
std::stringstream ss;
for (int i = 0; i < args.size(); i++) {
ss << " " << util::quote(args[i]);
}
auto toWString = [](const std::string& src) {
if (src.empty())
return L"";
int size = MultiByteToWideChar(CP_UTF8, 0, src.c_str(), -1, nullptr, 0);
if (size == 0) {
throw std::runtime_error(
"MultiByteToWideChar failed with code: " +
std::to_string(GetLastError())
)
}
std::vector<wchar_t> buffer(size);
MultiByteToWideChar(CP_UTF8, 0, src.c_str(), -1, buffer.data(), size);
return std::wstring(buffer.data());
};
auto executableString = toWString(executable.u8string());
auto argsString = toWString(ss.str());
STARTUPINFOW si = { sizeof(si) };
PROCESS_INFORMATION pi = { 0 };
DWORD flags = CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS;
// | CREATE_NO_WINDOW;
BOOL success = CreateProcessW(
executable.u8string().c_str(),
argsString.c_str(),
nullptr,
nullptr,
FALSE,
flags,
nullptr,
"",
&si,
&pi
);
if (!success) {
throw std::runtime_error(
"starting an engine instance failed with code: " +
std::to_string(GetLastError())
);
}
#else
std::stringstream ss;
ss << executable;
for (int i = 0; i < args.size(); i++) {
ss << " " << util::quote(args[i]);
}
ss << " >/dev/null &";
auto command = ss.str();
if (int res = system(command.c_str())) {
throw std::runtime_error(
"starting an engine instance failed with code: " +
std::to_string(res)
);
}
#endif
}
bool platform::open_url(const std::string& url) {
if (url.empty()) return false;

View File

@ -1,6 +1,7 @@
#pragma once
#include <string>
#include <vector>
#include <filesystem>
namespace platform {
@ -16,6 +17,8 @@ namespace platform {
int get_process_id();
/// @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<std::string>& args);
/// @brief Open URL in web browser
bool open_url(const std::string& url);
}