Force Windows errors into English

This commit is contained in:
Cvolton 2024-01-27 03:10:14 +01:00
parent fc7728fbfb
commit 6ba656c2a1
No known key found for this signature in database
2 changed files with 16 additions and 4 deletions
loader/src
loader
platform/windows

View file

@ -657,7 +657,19 @@ Result<> Mod::Impl::unzipGeodeFile(ModMetadata metadata) {
std::error_code ec;
ghc::filesystem::remove_all(tempDir, ec);
if (ec) {
return Err("Unable to delete temp dir: " + ec.message());
auto message = ec.message();
#ifdef GEODE_IS_WINDOWS
// Force the error message into English
char* errorBuf = nullptr;
FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, ec.value(), MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPSTR)&errorBuf, 0, nullptr);
if (errorBuf) {
message = errorBuf;
LocalFree(errorBuf);
}
#endif
return Err("Unable to delete temp dir: " + message);
}
(void)utils::file::createDirectoryAll(tempDir);

View file

@ -53,16 +53,16 @@ std::string getLastWinError() {
char* errorBuf = nullptr;
auto result = FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&errorBuf, 0, nullptr);
nullptr, err, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPSTR)&errorBuf, 0, nullptr);
std::string msg;
if (result == 0 || !errorBuf) {
msg = fmt::format("Unknown ({})", err);
} else {
msg = std::string(errorBuf, errorBuf + result);
// the string sometimes includes a crlf, strip it
// the string sometimes includes a crlf, strip it, also remove unprintable chars
msg.erase(std::find_if(msg.rbegin(), msg.rend(), [](unsigned char ch) {
return ch != '\r' && ch != '\n';
return ch != '\r' && ch != '\n' && ch < 127;
}).base(), msg.end());
}