add Game::SpawnAsync#3109
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a cross-platform Game::SpawnAsync helper to launch external processes (notably the bot) without using fork() on POSIX, aiming to be safer in multithreaded environments. It also adds new UTF-8 string conversion helpers used by the new spawn path.
Changes:
- Added
Game::SpawnAsync(exePath, args)usingCreateProcessW(Windows) andposix_spawn(POSIX). - Updated the bot start flow (
BUTTON_BOT_START) to build argument vectors and launch viaGame::SpawnAsync. - Added
BufferIO::{EncodeUTF8String, DecodeUTF8String}overloads forstd::wstring/std::string.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| gframe/menu_handler.cpp | Switch bot process launching to Game::SpawnAsync with argument vector construction. |
| gframe/gframe.cpp | Adds SIGCHLD handling on non-Windows to avoid zombie processes from spawned children. |
| gframe/game.h | Declares Game::SpawnAsync. |
| gframe/game.cpp | Implements Game::SpawnAsync using CreateProcessW / posix_spawn. |
| gframe/bufferio.h | Adds std::string/std::wstring UTF-8 encode/decode convenience helpers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+126
to
+139
| static std::string EncodeUTF8String(const std::wstring& wstr) { | ||
| if (wstr.empty()) | ||
| return std::string(); | ||
| std::mbstate_t state{}; | ||
| const wchar_t* src = wstr.c_str(); | ||
| size_t len = std::wcsrtombs(nullptr, &src, 0, &state); | ||
| if (len == static_cast<size_t>(-1)) | ||
| return std::string(); | ||
| std::string result(len, '\0'); | ||
| state = std::mbstate_t{}; | ||
| src = wstr.c_str(); | ||
| std::wcsrtombs(&result[0], &src, len, &state); | ||
| return result; | ||
| } |
Comment on lines
+153
to
+166
| static std::wstring DecodeUTF8String(const std::string& str) { | ||
| if (str.empty()) | ||
| return std::wstring(); | ||
| std::mbstate_t state{}; | ||
| const char* src = str.c_str(); | ||
| size_t len = std::mbsrtowcs(nullptr, &src, 0, &state); | ||
| if (len == static_cast<size_t>(-1)) | ||
| return std::wstring(); | ||
| std::wstring result(len, L'\0'); | ||
| state = std::mbstate_t{}; | ||
| src = str.c_str(); | ||
| std::mbsrtowcs(&result[0], &src, len, &state); | ||
| return result; | ||
| } |
Comment on lines
+2412
to
+2415
| std::wstring cmdLine = L"\"" + exePath + L"\""; | ||
| for (const auto& arg : args) { | ||
| cmdLine += L" \"" + arg + L"\""; | ||
| } |
Comment on lines
+2436
to
+2442
| std::string exePathUTF8 = BufferIO::EncodeUTF8String(exePath); | ||
|
|
||
| std::vector<std::string> utf8Args; | ||
| utf8Args.emplace_back(exePathUTF8); | ||
| for (const auto& arg : args) { | ||
| utf8Args.push_back(BufferIO::EncodeUTF8String(arg)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Game::SpawnAsyncfor launching processes.BUTTON_BOT_STARTsection inMenuHandlerto useGame::SpawnAsync.posix_spawninstead offork, it is more friendly to multithreaded environments and eliminates the need toforkbefore starting the server and other threads.std::string EncodeUTF8String(const std::wstring& wstr).