#define WIN32_LEAN_AND_MEAN #include #include #include #ifndef ULTRAPARA_VER #define ULTRAPARA_VER "0.0.0" #endif typedef void *(*fn_root)(void); typedef void *(*fn_attach)(void *); typedef void (*fn_detach)(void *); typedef void *(*fn_image_loaded)(const char *); typedef void *(*fn_open_data)(char *, unsigned, int, int *); typedef void *(*fn_load_from)(void *, const char *, int *, int); typedef void *(*fn_get_image)(void *); typedef void *(*fn_class)(void *, const char *, const char *); typedef void *(*fn_method)(void *, const char *, int); typedef void *(*fn_invoke)(void *, void *, void **, void **); static HMODULE self; static wchar_t logPath[MAX_PATH + 64]; static void Log(const char *fmt, ...) { if (!logPath[0]) return; char line[512]; SYSTEMTIME t; GetLocalTime(&t); int n = _snprintf_s(line, sizeof(line), _TRUNCATE, "%04d-%02d-%02d %02d:%02d:%02d ", t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond); if (n < 0) n = 0; va_list a; va_start(a, fmt); int m = _vsnprintf_s(line + n, sizeof(line) - n - 2, _TRUNCATE, fmt, a); va_end(a); if (m < 0) m = (int)strlen(line + n); n += m; line[n++] = '\r'; line[n++] = '\n'; HANDLE f = CreateFileW(logPath, FILE_APPEND_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (f == INVALID_HANDLE_VALUE) return; DWORD w; WriteFile(f, line, (DWORD)n, &w, NULL); CloseHandle(f); } static void SetupLog(void) { wchar_t p[MAX_PATH + 64]; DWORD n = GetModuleFileNameW(self, p, MAX_PATH); if (n == 0 || n >= MAX_PATH) return; wchar_t *cut = p; for (wchar_t *c = p; *c; c++) if (*c == L'\\' || *c == L'/') cut = c + 1; *cut = 0; lstrcatW(p, L"UltraParadise"); CreateDirectoryW(p, NULL); lstrcatW(p, L"\\UltraPara.log"); HANDLE f = CreateFileW(p, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (f != INVALID_HANDLE_VALUE) CloseHandle(f); lstrcpyW(logPath, p); } static int UnderWine(void) { HMODULE nt = GetModuleHandleW(L"ntdll.dll"); return nt && GetProcAddress(nt, "wine_get_version") != NULL; } static DWORD WINAPI Work(LPVOID unused) { int wine = UnderWine(); char ver[16]; lstrcpyA(ver, ULTRAPARA_VER); Log("UltraPara %s started in process %lu (%s)", ver, GetCurrentProcessId(), wine ? "Linux/Proton" : "Windows"); SetEnvironmentVariableA("ULTRAPARA_VERSION", ver); HMODULE mono = NULL; for (int i = 0; i < 6000 && !mono; i++) { mono = GetModuleHandleW(L"mono-2.0-bdwgc.dll"); if (!mono) mono = GetModuleHandleW(L"mono.dll"); if (!mono) Sleep(50); } if (!mono) { Log("FAILED: the game's Mono runtime never loaded"); return 0; } fn_root root = (fn_root)GetProcAddress(mono, "mono_get_root_domain"); fn_attach attach = (fn_attach)GetProcAddress(mono, "mono_thread_attach"); fn_detach detach = (fn_detach)GetProcAddress(mono, "mono_thread_detach"); fn_image_loaded loaded = (fn_image_loaded)GetProcAddress(mono, "mono_image_loaded"); fn_open_data open = (fn_open_data)GetProcAddress(mono, "mono_image_open_from_data"); fn_load_from load = (fn_load_from)GetProcAddress(mono, "mono_assembly_load_from_full"); fn_get_image image = (fn_get_image)GetProcAddress(mono, "mono_assembly_get_image"); fn_class cls = (fn_class)GetProcAddress(mono, "mono_class_from_name"); fn_method method = (fn_method)GetProcAddress(mono, "mono_class_get_method_from_name"); fn_invoke invoke = (fn_invoke)GetProcAddress(mono, "mono_runtime_invoke"); if (!root || !attach || !open || !load || !image || !cls || !method || !invoke) { Log("FAILED: the game's Mono runtime is missing functions"); return 0; } void *domain = NULL; int waited = 0; for (; waited < 6000; waited++) { domain = root(); if (domain && (!loaded || loaded("Assembly-CSharp"))) break; Sleep(50); } if (!domain) { Log("FAILED: the game's Mono runtime never started"); return 0; } Sleep(500); HRSRC r = FindResourceW(self, MAKEINTRESOURCEW(wine ? 102 : 101), MAKEINTRESOURCEW(10)); HGLOBAL g = r ? LoadResource(self, r) : NULL; DWORD size = r ? SizeofResource(self, r) : 0; char *data = g ? (char *)LockResource(g) : NULL; if (!data || size == 0) { Log("FAILED: the trainer code inside UltraPara.dll is missing"); return 0; } void *thread = attach(domain); int status = 0; void *img = open(data, size, 1, &status); void *as = img ? load(img, "UltraParadise.Payload", &status, 0) : NULL; void *im = as ? image(as) : NULL; void *c = im ? cls(im, "UltraParadise", "Loader") : NULL; void *m = c ? method(c, "Init", 0) : NULL; void *exc = NULL; if (m) invoke(m, NULL, NULL, &exc); if (detach && thread) detach(thread); if (!img) Log("FAILED: couldn't read the trainer code (status %d)", status); else if (!as) Log("FAILED: couldn't load the trainer code (status %d)", status); else if (!m) Log("FAILED: couldn't find the trainer's start"); else if (exc) Log("FAILED: the trainer code crashed while starting"); else Log("LOADED: trainer code %s is running in the game", wine ? "(Linux)" : "(Windows)"); return 0; } BOOL WINAPI DllMain(HINSTANCE h, DWORD why, LPVOID r) { if (why == DLL_PROCESS_ATTACH) { self = h; DisableThreadLibraryCalls(h); SetupLog(); HANDLE t = CreateThread(NULL, 0, Work, NULL, 0, NULL); if (t) CloseHandle(t); } return TRUE; }