2022-11-30 12:05:14 -05:00
|
|
|
#include <Geode/loader/IPC.hpp>
|
2022-11-22 10:21:00 -05:00
|
|
|
#include <Geode/loader/Log.hpp>
|
2022-12-14 06:50:46 -05:00
|
|
|
#include <loader/ModImpl.hpp>
|
2022-12-04 11:39:40 -05:00
|
|
|
#include <iostream>
|
2022-12-14 06:50:46 -05:00
|
|
|
#include <loader/LoaderImpl.hpp>
|
2023-01-24 03:28:43 -05:00
|
|
|
#include <Geode/utils/string.hpp>
|
2022-11-22 10:21:00 -05:00
|
|
|
|
2023-03-10 14:33:24 -05:00
|
|
|
using namespace geode::prelude;
|
2022-11-22 10:21:00 -05:00
|
|
|
|
|
|
|
#ifdef GEODE_IS_WINDOWS
|
|
|
|
|
2023-01-24 03:28:43 -05:00
|
|
|
#include <Psapi.h>
|
|
|
|
|
2022-11-23 08:53:38 -05:00
|
|
|
static constexpr auto IPC_BUFFER_SIZE = 512;
|
|
|
|
|
2022-12-10 11:30:14 -05:00
|
|
|
void Loader::Impl::platformMessageBox(char const* title, std::string const& info) {
|
2022-11-22 10:21:00 -05:00
|
|
|
MessageBoxA(nullptr, info.c_str(), title, MB_ICONERROR);
|
|
|
|
}
|
|
|
|
|
2023-05-06 12:32:34 -04:00
|
|
|
bool hasAnsiColorSupport = false;
|
|
|
|
|
2023-04-10 05:49:59 -04:00
|
|
|
void Loader::Impl::logConsoleMessageWithSeverity(std::string const& msg, Severity severity) {
|
|
|
|
if (m_platformConsoleOpen) {
|
2023-05-06 12:32:34 -04:00
|
|
|
if (hasAnsiColorSupport) {
|
|
|
|
int color = 0;
|
|
|
|
switch (severity) {
|
|
|
|
case Severity::Debug: color = 243; break;
|
|
|
|
case Severity::Info: color = 33; break;
|
|
|
|
case Severity::Warning: color = 229; break;
|
|
|
|
case Severity::Error: color = 9; break;
|
|
|
|
default: color = 7; break;
|
|
|
|
}
|
|
|
|
auto const colorStr = fmt::format("\x1b[38;5;{}m", color);
|
|
|
|
auto const newMsg = fmt::format("{}{}\x1b[0m{}", colorStr, msg.substr(0, 8), msg.substr(8));
|
|
|
|
|
|
|
|
std::cout << newMsg << "\n" << std::flush;
|
|
|
|
} else {
|
|
|
|
std::cout << msg << "\n" << std::flush;
|
|
|
|
}
|
2023-04-10 05:49:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-10 11:30:14 -05:00
|
|
|
void Loader::Impl::openPlatformConsole() {
|
2022-11-22 10:21:00 -05:00
|
|
|
if (m_platformConsoleOpen) return;
|
|
|
|
if (AllocConsole() == 0) return;
|
|
|
|
SetConsoleCP(CP_UTF8);
|
|
|
|
// redirect console output
|
|
|
|
freopen_s(reinterpret_cast<FILE**>(stdout), "CONOUT$", "w", stdout);
|
|
|
|
freopen_s(reinterpret_cast<FILE**>(stdin), "CONIN$", "r", stdin);
|
|
|
|
|
2023-05-06 12:32:34 -04:00
|
|
|
// Set output mode to handle ansi color sequences
|
|
|
|
auto handleStdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
|
|
|
|
DWORD consoleMode = 0;
|
|
|
|
if (GetConsoleMode(handleStdout, &consoleMode)) {
|
|
|
|
consoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
|
|
|
if (SetConsoleMode(handleStdout, consoleMode)) {
|
|
|
|
hasAnsiColorSupport = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 10:21:00 -05:00
|
|
|
m_platformConsoleOpen = true;
|
|
|
|
|
2022-12-12 14:32:44 -05:00
|
|
|
for (auto const& log : log::Logger::list()) {
|
2023-05-06 12:32:34 -04:00
|
|
|
this->logConsoleMessageWithSeverity(log->toString(true), log->getSeverity());
|
2022-11-22 10:21:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-10 11:30:14 -05:00
|
|
|
void Loader::Impl::closePlatformConsole() {
|
2022-11-22 10:21:00 -05:00
|
|
|
if (!m_platformConsoleOpen) return;
|
|
|
|
|
|
|
|
fclose(stdin);
|
|
|
|
fclose(stdout);
|
|
|
|
FreeConsole();
|
|
|
|
|
|
|
|
m_platformConsoleOpen = false;
|
|
|
|
}
|
|
|
|
|
2022-11-23 08:53:38 -05:00
|
|
|
void ipcPipeThread(HANDLE pipe) {
|
|
|
|
char buffer[IPC_BUFFER_SIZE * sizeof(TCHAR)];
|
|
|
|
DWORD read;
|
|
|
|
|
|
|
|
std::optional<std::string> replyID = std::nullopt;
|
|
|
|
|
|
|
|
// log::debug("Waiting for I/O");
|
|
|
|
if (ReadFile(pipe, buffer, sizeof(buffer) - 1, &read, nullptr)) {
|
|
|
|
buffer[read] = '\0';
|
|
|
|
|
2023-01-27 18:25:19 -05:00
|
|
|
std::string reply = LoaderImpl::get()->processRawIPC((void*)pipe, buffer).dump();
|
2022-11-30 11:51:38 -05:00
|
|
|
|
|
|
|
DWORD written;
|
|
|
|
WriteFile(pipe, reply.c_str(), reply.size(), &written, nullptr);
|
|
|
|
}
|
2022-11-23 08:53:38 -05:00
|
|
|
// log::debug("Connection done");
|
|
|
|
|
|
|
|
FlushFileBuffers(pipe);
|
|
|
|
DisconnectNamedPipe(pipe);
|
|
|
|
CloseHandle(pipe);
|
|
|
|
|
|
|
|
// log::debug("Disconnected pipe");
|
2022-11-22 10:21:00 -05:00
|
|
|
}
|
|
|
|
|
2022-12-10 11:30:14 -05:00
|
|
|
void Loader::Impl::setupIPC() {
|
2023-08-09 11:37:26 -04:00
|
|
|
std::thread ipcThread([]() {
|
2022-11-29 17:48:06 -05:00
|
|
|
while (true) {
|
2022-11-23 08:53:38 -05:00
|
|
|
auto pipe = CreateNamedPipeA(
|
|
|
|
IPC_PIPE_NAME,
|
|
|
|
PIPE_ACCESS_DUPLEX,
|
|
|
|
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
|
|
|
|
PIPE_UNLIMITED_INSTANCES,
|
|
|
|
IPC_BUFFER_SIZE,
|
|
|
|
IPC_BUFFER_SIZE,
|
|
|
|
NMPWAIT_USE_DEFAULT_WAIT,
|
|
|
|
nullptr
|
|
|
|
);
|
|
|
|
if (pipe == INVALID_HANDLE_VALUE) {
|
2022-12-04 11:39:40 -05:00
|
|
|
// todo: Rn this quits IPC, but we might wanna change that later
|
|
|
|
// to just continue trying. however, I'm assuming that if
|
|
|
|
// CreateNamedPipeA fails, then it will probably fail again if
|
|
|
|
// you try right after, so changing the break; to continue; might
|
2022-11-23 08:53:38 -05:00
|
|
|
// just result in the console getting filled with error messages
|
|
|
|
log::warn("Unable to create pipe, quitting IPC");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// log::debug("Waiting for pipe connections");
|
2022-11-22 10:21:00 -05:00
|
|
|
if (ConnectNamedPipe(pipe, nullptr)) {
|
2022-11-23 08:53:38 -05:00
|
|
|
// log::debug("Got connection, creating thread");
|
2023-08-09 11:37:26 -04:00
|
|
|
std::thread pipeThread(&ipcPipeThread, pipe);
|
2023-09-10 20:18:24 -04:00
|
|
|
// SetThreadDescription(pipeThread.native_handle(), L"Geode IPC Pipe");
|
2023-08-09 11:37:26 -04:00
|
|
|
pipeThread.detach();
|
2022-12-04 11:39:40 -05:00
|
|
|
}
|
|
|
|
else {
|
2022-11-23 08:53:38 -05:00
|
|
|
// log::debug("No connection, cleaning pipe");
|
|
|
|
CloseHandle(pipe);
|
2022-11-22 10:21:00 -05:00
|
|
|
}
|
|
|
|
}
|
2023-08-09 11:37:26 -04:00
|
|
|
});
|
2023-09-10 20:18:24 -04:00
|
|
|
// SetThreadDescription(ipcThread.native_handle(), L"Geode Main IPC");
|
2023-08-09 11:37:26 -04:00
|
|
|
ipcThread.detach();
|
2022-11-23 08:53:38 -05:00
|
|
|
|
2022-12-04 11:39:40 -05:00
|
|
|
log::debug("IPC set up");
|
2022-11-22 10:21:00 -05:00
|
|
|
}
|
|
|
|
|
2023-01-24 03:28:43 -05:00
|
|
|
bool Loader::Impl::userTriedToLoadDLLs() const {
|
|
|
|
static std::unordered_set<std::string> KNOWN_MOD_DLLS {
|
|
|
|
"betteredit-v4.0.5.dll",
|
|
|
|
"betteredit-v4.0.5-min.dll",
|
|
|
|
"betteredit-v4.0.3.dll",
|
|
|
|
"betteredit.dll",
|
|
|
|
"gdshare-v0.3.4.dll",
|
|
|
|
"gdshare.dll",
|
|
|
|
"hackpro.dll",
|
|
|
|
"hackproldr.dll",
|
|
|
|
"quickldr.dll",
|
|
|
|
"minhook.x32.dll",
|
|
|
|
"iconsave.dll",
|
|
|
|
"menuanim.dll",
|
|
|
|
"volumecontrol.dll",
|
|
|
|
"customsplash.dll",
|
|
|
|
"scrollanyinput-v1.1.dll",
|
|
|
|
"alttabfix-v1.0.dll",
|
|
|
|
"sceneswitcher-v1.1.dll",
|
|
|
|
"gdantialiasing.dll",
|
|
|
|
"textureldr.dll",
|
|
|
|
"run-info.dll",
|
|
|
|
};
|
|
|
|
|
|
|
|
bool triedToLoadDLLs = false;
|
|
|
|
|
|
|
|
// Check for .DLLs in mods dir
|
2023-08-02 10:54:17 -04:00
|
|
|
if (auto files = file::readDirectory(dirs::getModsDir(), true)) {
|
2023-01-24 03:28:43 -05:00
|
|
|
for (auto& file : files.unwrap()) {
|
|
|
|
if (file.extension() == ".dll") {
|
|
|
|
triedToLoadDLLs = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check all loaded DLLs in the process
|
|
|
|
std::array<HMODULE, 1024> mods;
|
|
|
|
DWORD needed;
|
|
|
|
auto process = GetCurrentProcess();
|
|
|
|
|
|
|
|
if (EnumProcessModules(process, mods.data(), mods.size(), &needed)) {
|
|
|
|
for (auto i = 0; i < (needed / sizeof(HMODULE)); i++) {
|
|
|
|
std::array<char, MAX_PATH> modName;
|
|
|
|
if (GetModuleFileNameExA(process, mods[i], modName.data(), modName.size())) {
|
|
|
|
if (KNOWN_MOD_DLLS.count(string::trim(string::toLower(
|
|
|
|
ghc::filesystem::path(modName.data()).filename().string()
|
|
|
|
)))) {
|
|
|
|
triedToLoadDLLs = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return triedToLoadDLLs;
|
|
|
|
}
|
|
|
|
|
2022-11-22 10:21:00 -05:00
|
|
|
#endif
|