this is insane but sure

This commit is contained in:
2025-12-18 00:36:19 -05:00
parent 7e1b84e83c
commit df7dfde1d6
2 changed files with 93 additions and 17 deletions

View File

@@ -1,11 +1,17 @@
#include "render.h"
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <filesystem>
#include <format>
#include <fstream>
#include <string>
#if _WIN32
#include <windows.h>
#endif
#include "log.h"
#include "process_.h"
#include "sdl.h"
@@ -109,28 +115,55 @@ namespace anm2ed
command += " 2>&1";
#if _WIN32
auto escape_double_quotes_for_cmd = [](const std::string& input)
auto logCommand =
std::string("& ") + command + " | Tee-Object -FilePath " + string::quote(loggerPathString) + " -Append";
logger.command(logCommand);
auto utf8_to_wstring = [](const std::string& value)
{
std::string escaped{};
escaped.reserve(input.size() * 2);
for (char character : input)
{
if (character == '"')
escaped += "\"\"";
else
escaped += character;
}
return escaped;
if (value.empty()) return std::wstring{};
auto length = MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, nullptr, 0);
if (length <= 0) return std::wstring{};
std::wstring wide(static_cast<std::size_t>(length - 1), L'\0');
MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, wide.data(), length);
return wide;
};
auto powershellScript =
std::string("& { & ") + command + " | Tee-Object -FilePath " + string::quote(loggerPathString) + " -Append }";
command = "powershell -Command \"" + escape_double_quotes_for_cmd(powershellScript) + "\"";
auto base64_encode_utf16 = [](const std::wstring& input)
{
const auto* bytes = reinterpret_cast<const unsigned char*>(input.c_str());
auto length = input.size() * sizeof(wchar_t);
static constexpr char alphabet[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string encoded{};
encoded.reserve(((length + 2) / 3) * 4);
for (std::size_t i = 0; i < length;)
{
auto remaining = length - i;
auto octetA = bytes[i++];
auto octetB = remaining > 1 ? bytes[i++] : 0;
auto octetC = remaining > 2 ? bytes[i++] : 0;
auto triple = (static_cast<uint32_t>(octetA) << 16) | (static_cast<uint32_t>(octetB) << 8) |
static_cast<uint32_t>(octetC);
encoded.push_back(alphabet[(triple >> 18) & 0x3F]);
encoded.push_back(alphabet[(triple >> 12) & 0x3F]);
encoded.push_back(remaining > 1 ? alphabet[(triple >> 6) & 0x3F] : '=');
encoded.push_back(remaining > 2 ? alphabet[triple & 0x3F] : '=');
}
return encoded;
};
auto script = std::wstring(L"& { ") + utf8_to_wstring(logCommand) + L" }";
auto encodedCommand = base64_encode_utf16(script);
command = "powershell -NoProfile -EncodedCommand " + string::quote(encodedCommand);
#else
command += " | tee -a " + string::quote(loggerPathString);
#endif
logger.command(command);
#endif
Process process(command.c_str(), "w");