cu-trainer/UltraPara/src/ultrapara.c
| 1 | #define WIN32_LEAN_AND_MEAN |
| 2 | #include <windows.h> |
| 3 | #include <stdio.h> |
| 4 | #include <stdarg.h> |
| 5 | |
| 6 | #ifndef ULTRAPARA_VER |
| 7 | #define ULTRAPARA_VER "0.0.0" |
| 8 | #endif |
| 9 | |
| 10 | typedef void *(*fn_root)(void); |
| 11 | typedef void *(*fn_attach)(void *); |
| 12 | typedef void (*fn_detach)(void *); |
| 13 | typedef void *(*fn_image_loaded)(const char *); |
| 14 | typedef void *(*fn_open_data)(char *, unsigned, int, int *); |
| 15 | typedef void *(*fn_load_from)(void *, const char *, int *, int); |
| 16 | typedef void *(*fn_get_image)(void *); |
| 17 | typedef void *(*fn_class)(void *, const char *, const char *); |
| 18 | typedef void *(*fn_method)(void *, const char *, int); |
| 19 | typedef void *(*fn_invoke)(void *, void *, void **, void **); |
| 20 | |
| 21 | static HMODULE self; |
| 22 | static wchar_t logPath[MAX_PATH + 64]; |
| 23 | |
| 24 | static void Log(const char *fmt, ...) |
| 25 | { |
| 26 | if (!logPath[0]) return; |
| 27 | char line[512]; |
| 28 | SYSTEMTIME t; |
| 29 | GetLocalTime(&t); |
| 30 | 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); |
| 31 | if (n < 0) n = 0; |
| 32 | va_list a; |
| 33 | va_start(a, fmt); |
| 34 | int m = _vsnprintf_s(line + n, sizeof(line) - n - 2, _TRUNCATE, fmt, a); |
| 35 | va_end(a); |
| 36 | if (m < 0) m = (int)strlen(line + n); |
| 37 | n += m; |
| 38 | line[n++] = '\r'; |
| 39 | line[n++] = '\n'; |
| 40 | HANDLE f = CreateFileW(logPath, FILE_APPEND_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 41 | if (f == INVALID_HANDLE_VALUE) return; |
| 42 | DWORD w; |
| 43 | WriteFile(f, line, (DWORD)n, &w, NULL); |
| 44 | CloseHandle(f); |
| 45 | } |
| 46 | |
| 47 | static void SetupLog(void) |
| 48 | { |
| 49 | wchar_t p[MAX_PATH + 64]; |
| 50 | DWORD n = GetModuleFileNameW(self, p, MAX_PATH); |
| 51 | if (n == 0 || n >= MAX_PATH) return; |
| 52 | wchar_t *cut = p; |
| 53 | for (wchar_t *c = p; *c; c++) if (*c == L'\\' || *c == L'/') cut = c + 1; |
| 54 | *cut = 0; |
| 55 | lstrcatW(p, L"UltraParadise"); |
| 56 | CreateDirectoryW(p, NULL); |
| 57 | lstrcatW(p, L"\\UltraPara.log"); |
| 58 | HANDLE f = CreateFileW(p, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 59 | if (f != INVALID_HANDLE_VALUE) CloseHandle(f); |
| 60 | lstrcpyW(logPath, p); |
| 61 | } |
| 62 | |
| 63 | static int UnderWine(void) |
| 64 | { |
| 65 | HMODULE nt = GetModuleHandleW(L"ntdll.dll"); |
| 66 | return nt && GetProcAddress(nt, "wine_get_version") != NULL; |
| 67 | } |
| 68 | |
| 69 | static DWORD WINAPI Work(LPVOID unused) |
| 70 | { |
| 71 | int wine = UnderWine(); |
| 72 | char ver[16]; |
| 73 | lstrcpyA(ver, ULTRAPARA_VER); |
| 74 | Log("UltraPara %s started in process %lu (%s)", ver, GetCurrentProcessId(), wine ? "Linux/Proton" : "Windows"); |
| 75 | SetEnvironmentVariableA("ULTRAPARA_VERSION", ver); |
| 76 | |
| 77 | HMODULE mono = NULL; |
| 78 | for (int i = 0; i < 6000 && !mono; i++) |
| 79 | { |
| 80 | mono = GetModuleHandleW(L"mono-2.0-bdwgc.dll"); |
| 81 | if (!mono) mono = GetModuleHandleW(L"mono.dll"); |
| 82 | if (!mono) Sleep(50); |
| 83 | } |
| 84 | if (!mono) { Log("FAILED: the game's Mono runtime never loaded"); return 0; } |
| 85 | |
| 86 | fn_root root = (fn_root)GetProcAddress(mono, "mono_get_root_domain"); |
| 87 | fn_attach attach = (fn_attach)GetProcAddress(mono, "mono_thread_attach"); |
| 88 | fn_detach detach = (fn_detach)GetProcAddress(mono, "mono_thread_detach"); |
| 89 | fn_image_loaded loaded = (fn_image_loaded)GetProcAddress(mono, "mono_image_loaded"); |
| 90 | fn_open_data open = (fn_open_data)GetProcAddress(mono, "mono_image_open_from_data"); |
| 91 | fn_load_from load = (fn_load_from)GetProcAddress(mono, "mono_assembly_load_from_full"); |
| 92 | fn_get_image image = (fn_get_image)GetProcAddress(mono, "mono_assembly_get_image"); |
| 93 | fn_class cls = (fn_class)GetProcAddress(mono, "mono_class_from_name"); |
| 94 | fn_method method = (fn_method)GetProcAddress(mono, "mono_class_get_method_from_name"); |
| 95 | fn_invoke invoke = (fn_invoke)GetProcAddress(mono, "mono_runtime_invoke"); |
| 96 | if (!root || !attach || !open || !load || !image || !cls || !method || !invoke) { Log("FAILED: the game's Mono runtime is missing functions"); return 0; } |
| 97 | |
| 98 | void *domain = NULL; |
| 99 | int waited = 0; |
| 100 | for (; waited < 6000; waited++) |
| 101 | { |
| 102 | domain = root(); |
| 103 | if (domain && (!loaded || loaded("Assembly-CSharp"))) break; |
| 104 | Sleep(50); |
| 105 | } |
| 106 | if (!domain) { Log("FAILED: the game's Mono runtime never started"); return 0; } |
| 107 | Sleep(500); |
| 108 | |
| 109 | HRSRC r = FindResourceW(self, MAKEINTRESOURCEW(wine ? 102 : 101), MAKEINTRESOURCEW(10)); |
| 110 | HGLOBAL g = r ? LoadResource(self, r) : NULL; |
| 111 | DWORD size = r ? SizeofResource(self, r) : 0; |
| 112 | char *data = g ? (char *)LockResource(g) : NULL; |
| 113 | if (!data || size == 0) { Log("FAILED: the trainer code inside UltraPara.dll is missing"); return 0; } |
| 114 | |
| 115 | void *thread = attach(domain); |
| 116 | int status = 0; |
| 117 | void *img = open(data, size, 1, &status); |
| 118 | void *as = img ? load(img, "UltraParadise.Payload", &status, 0) : NULL; |
| 119 | void *im = as ? image(as) : NULL; |
| 120 | void *c = im ? cls(im, "UltraParadise", "Loader") : NULL; |
| 121 | void *m = c ? method(c, "Init", 0) : NULL; |
| 122 | void *exc = NULL; |
| 123 | if (m) invoke(m, NULL, NULL, &exc); |
| 124 | if (detach && thread) detach(thread); |
| 125 | |
| 126 | if (!img) Log("FAILED: couldn't read the trainer code (status %d)", status); |
| 127 | else if (!as) Log("FAILED: couldn't load the trainer code (status %d)", status); |
| 128 | else if (!m) Log("FAILED: couldn't find the trainer's start"); |
| 129 | else if (exc) Log("FAILED: the trainer code crashed while starting"); |
| 130 | else Log("LOADED: trainer code %s is running in the game", wine ? "(Linux)" : "(Windows)"); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | BOOL WINAPI DllMain(HINSTANCE h, DWORD why, LPVOID r) |
| 135 | { |
| 136 | if (why == DLL_PROCESS_ATTACH) |
| 137 | { |
| 138 | self = h; |
| 139 | DisableThreadLibraryCalls(h); |
| 140 | SetupLog(); |
| 141 | HANDLE t = CreateThread(NULL, 0, Work, NULL, 0, NULL); |
| 142 | if (t) CloseHandle(t); |
| 143 | } |
| 144 | return TRUE; |
| 145 | } |