| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <windows.h> |
| #include <bcrypt.h> |
| #include <psapi.h> |
| #include <tlhelp32.h> |
| #include <strsafe.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <stdint.h> |
|
|
| |
| static const uint64_t PREFERRED_BASE = 0x140000000ULL; |
| static const uint64_t CAVE_VMA = 0x14081DE66ULL; |
| static const uint64_t SITE1_MOVSD_VMA = 0x14081D83AULL; |
| static const uint64_t SITE2_MOVSD_VMA = 0x14081D943ULL; |
| static const uint8_t SITE1_ORIG[4] = { 0x26, 0x3F, 0x5A, 0x01 }; |
| static const uint8_t SITE1_NEW[4] = { 0x24, 0x06, 0x00, 0x00 }; |
| static const uint8_t SITE2_ORIG[4] = { 0x1D, 0x3E, 0x5A, 0x01 }; |
| static const uint8_t SITE2_NEW[4] = { 0x1B, 0x05, 0x00, 0x00 }; |
|
|
| static const wchar_t* EU4_EXE_W = L"C:\\Program Files (x86)\\Steam\\steamapps\\common\\Europa Universalis IV\\eu4.exe"; |
| static const char* EU4_EXE = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\Europa Universalis IV\\eu4.exe"; |
|
|
| |
| static int json_get_double(const char* path, const char* key, double* out) { |
| FILE* fp = fopen(path, "rb"); |
| if (!fp) return -1; |
| fseek(fp, 0, SEEK_END); long sz = ftell(fp); fseek(fp, 0, SEEK_SET); |
| char* buf = (char*)malloc(sz + 1); if (!buf) { fclose(fp); return -1; } |
| fread(buf, 1, sz, fp); buf[sz] = '\0'; fclose(fp); |
| char needle[64]; snprintf(needle, sizeof(needle), "\"%s\"", key); |
| char* p = strstr(buf, needle); |
| if (!p) { free(buf); return -2; } |
| p = strchr(p, ':'); if (!p) { free(buf); return -2; } |
| p++; while (*p==' '||*p=='\t'||*p=='\n'||*p=='\r') p++; |
| *out = strtod(p, NULL); free(buf); return 0; |
| } |
|
|
| static int sha256_file(const wchar_t* path, uint8_t out[32]) { |
| BCRYPT_ALG_HANDLE hAlg=NULL; BCRYPT_HASH_HANDLE hHash=NULL; |
| PUCHAR hObj=NULL; HANDLE hF=INVALID_HANDLE_VALUE; int rc=-1; |
| if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&hAlg,BCRYPT_SHA256_ALGORITHM,NULL,0)))return -1; |
| DWORD sz=0,rs=0; BCryptGetProperty(hAlg,BCRYPT_OBJECT_LENGTH,(PUCHAR)&sz,sizeof(sz),&rs,0); |
| hObj=(PUCHAR)HeapAlloc(GetProcessHeap(),0,sz); if(!hObj)goto d; |
| if(!BCRYPT_SUCCESS(BCryptCreateHash(hAlg,&hHash,hObj,sz,NULL,0,0)))goto d; |
| hF=CreateFileW(path,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL); |
| if(hF==INVALID_HANDLE_VALUE)goto d; |
| {BYTE b[1<<16];DWORD n; while(ReadFile(hF,b,sizeof(b),&n,NULL)&&n>0) |
| if(!BCRYPT_SUCCESS(BCryptHashData(hHash,b,n,0)))goto d;} |
| if(!BCRYPT_SUCCESS(BCryptFinishHash(hHash,out,32,0)))goto d; rc=0; |
| d: if(hF!=INVALID_HANDLE_VALUE)CloseHandle(hF); if(hHash)BCryptDestroyHash(hHash); |
| if(hObj)HeapFree(GetProcessHeap(),0,hObj); if(hAlg)BCryptCloseAlgorithmProvider(hAlg,0); |
| return rc; |
| } |
| static void to_hex(const uint8_t h[32], char o[65]) { |
| static const char*H="0123456789abcdef"; |
| for(int i=0;i<32;i++){o[i*2]=H[h[i]>>4];o[i*2+1]=H[h[i]&0xf];} o[64]='\0'; |
| } |
|
|
| static DWORD find_pid(const wchar_t* name) { |
| HANDLE s=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); if(s==INVALID_HANDLE_VALUE)return 0; |
| PROCESSENTRY32W pe; memset(&pe,0,sizeof(pe)); pe.dwSize=sizeof(pe); DWORD pid=0; |
| if(Process32FirstW(s,&pe)) do{ if(_wcsicmp(pe.szExeFile,name)==0){pid=pe.th32ProcessID;break;} }while(Process32NextW(s,&pe)); |
| CloseHandle(s); return pid; |
| } |
| static uint64_t mod_base(HANDLE hP) { |
| HMODULE m[16]; DWORD need=0; |
| if(!EnumProcessModules(hP,m,sizeof(m),&need))return 0; |
| MODULEINFO mi; memset(&mi,0,sizeof(mi)); |
| GetModuleInformation(hP,m[0],&mi,sizeof(mi)); |
| return (uint64_t)(uintptr_t)mi.lpBaseOfDll; |
| } |
| static BOOL rd(HANDLE hP,uint64_t vma,void*dst,size_t n){ |
| SIZE_T g=0; return ReadProcessMemory(hP,(LPCVOID)(uintptr_t)vma,dst,n,&g)&&g==n; |
| } |
| static BOOL wr(HANDLE hP,uint64_t vma,const void*src,size_t n){ |
| DWORD old=0; if(!VirtualProtectEx(hP,(LPVOID)(uintptr_t)vma,n,PAGE_EXECUTE_READWRITE,&old))return FALSE; |
| SIZE_T w=0; BOOL ok=WriteProcessMemory(hP,(LPVOID)(uintptr_t)vma,src,n,&w)&&w==n; |
| VirtualProtectEx(hP,(LPVOID)(uintptr_t)vma,n,old,&old); return ok; |
| } |
|
|
| |
| static void path_next_to_exe(char* dst, size_t dstsz, const char* fname) { |
| GetModuleFileNameA(NULL, dst, (DWORD)dstsz); |
| char* p = strrchr(dst, '\\'); if (p) *(p+1)='\0'; else dst[0]='\0'; |
| StringCchCatA(dst, dstsz, fname); |
| } |
|
|
| static void write_state(const char* path, const char* sha, |
| const uint8_t co[8], const uint8_t s1[4], const uint8_t s2[4], |
| double interval) { |
| FILE* f=fopen(path,"w"); if(!f)return; |
| fprintf(f,"{\n \"eu4_sha256\":\"%s\",\n \"tip_interval_seconds\":%.6f,\n",sha,interval); |
| fprintf(f," \"cave_original\":[%d",co[0]); for(int i=1;i<8;i++)fprintf(f,",%d",co[i]); fprintf(f,"],\n"); |
| fprintf(f," \"site1_original\":[%d",s1[0]); for(int i=1;i<4;i++)fprintf(f,",%d",s1[i]); fprintf(f,"],\n"); |
| fprintf(f," \"site2_original\":[%d",s2[0]); for(int i=1;i<4;i++)fprintf(f,",%d",s2[i]); fprintf(f,"]\n}\n"); |
| fclose(f); |
| } |
|
|
| |
| static int do_apply(void) { |
| char cpath[MAX_PATH], spath[MAX_PATH]; |
| path_next_to_exe(cpath, sizeof(cpath), "config.json"); |
| path_next_to_exe(spath, sizeof(spath), "state.json"); |
|
|
| double interval = 0.0; |
| int rc = json_get_double(cpath, "tip_interval_seconds", &interval); |
| if (rc != 0 || interval <= 0.0) { |
| fprintf(stderr, "error: tip_interval_seconds missing/invalid in config.json (need > 0)\n"); |
| return 2; |
| } |
| fprintf(stderr, "config: tip_interval_seconds = %.6f\n", interval); |
|
|
| DWORD pid = find_pid(L"eu4.exe"); |
| if (!pid) { fprintf(stderr, "error: eu4.exe not running. Launch EU4 first.\n"); return 3; } |
| fprintf(stderr, "eu4.exe PID = %lu\n", pid); |
|
|
| uint8_t hash[32]; |
| if (sha256_file(EU4_EXE_W, hash) != 0) { fprintf(stderr, "error: sha256 failed\n"); return 4; } |
| char hex[65]; to_hex(hash, hex); |
| fprintf(stderr, "eu4.exe sha256: %s\n", hex); |
|
|
| HANDLE hP = OpenProcess(PROCESS_VM_READ|PROCESS_VM_WRITE|PROCESS_VM_OPERATION|PROCESS_QUERY_INFORMATION, FALSE, pid); |
| if (!hP) { fprintf(stderr, "OpenProcess failed: %lu\n", GetLastError()); return 5; } |
|
|
| uint64_t base = mod_base(hP); |
| int64_t delta = (int64_t)(base - PREFERRED_BASE); |
| fprintf(stderr, "actual base 0x%llx, ASLR delta %+lld\n", (unsigned long long)base, (long long)delta); |
|
|
| uint64_t cave = CAVE_VMA + delta; |
| uint64_t s1 = SITE1_MOVSD_VMA + delta; |
| uint64_t s2 = SITE2_MOVSD_VMA + delta; |
|
|
| |
| uint8_t co[8], s1o[4], s2o[4]; |
| if (!rd(hP, cave, co, 8)) { fprintf(stderr,"read cave failed: %lu\n",GetLastError()); CloseHandle(hP); return 6; } |
| if (!rd(hP, s1 + 4, s1o, 4)) { fprintf(stderr,"read site1 failed\n"); CloseHandle(hP); return 6; } |
| if (!rd(hP, s2 + 4, s2o, 4)) { fprintf(stderr,"read site2 failed\n"); CloseHandle(hP); return 6; } |
|
|
| |
| if (memcmp(s1o, SITE1_ORIG, 4) != 0) { |
| fprintf(stderr, "WARN: site1 bytes != expected. got %02x %02x %02x %02x\n", s1o[0],s1o[1],s1o[2],s1o[3]); |
| fprintf(stderr, " eu4.exe may be a different version. Aborting.\n"); |
| CloseHandle(hP); return 7; |
| } |
| if (memcmp(s2o, SITE2_ORIG, 4) != 0) { |
| fprintf(stderr, "WARN: site2 bytes != expected. got %02x %02x %02x %02x\n", s2o[0],s2o[1],s2o[2],s2o[3]); |
| CloseHandle(hP); return 7; |
| } |
|
|
| |
| double iv = interval; |
| if (!wr(hP, cave, &iv, 8)) { fprintf(stderr,"write cave failed\n"); CloseHandle(hP); return 8; } |
| fprintf(stderr, "wrote %.6f to code cave @ 0x%llx\n", interval, (unsigned long long)cave); |
|
|
| |
| if (!wr(hP, s1 + 4, SITE1_NEW, 4)) { fprintf(stderr,"write site1 failed\n"); CloseHandle(hP); return 8; } |
| fprintf(stderr, "patched site1 disp32 @ 0x%llx\n", (unsigned long long)(s1+4)); |
|
|
| |
| if (!wr(hP, s2 + 4, SITE2_NEW, 4)) { fprintf(stderr,"write site2 failed\n"); CloseHandle(hP); return 8; } |
| fprintf(stderr, "patched site2 disp32 @ 0x%llx\n", (unsigned long long)(s2+4)); |
|
|
| write_state(spath, hex, co, s1o, s2o, interval); |
| fprintf(stderr, "OK: loading-screen cycle interval -> %.1f s. state saved to state.json\n", interval); |
| CloseHandle(hP); |
| return 0; |
| } |
|
|
| static int do_revert(void) { |
| char spath[MAX_PATH]; path_next_to_exe(spath, sizeof(spath), "state.json"); |
| DWORD pid = find_pid(L"eu4.exe"); |
| if (!pid) { fprintf(stderr, "eu4.exe not running\n"); return 3; } |
| HANDLE hP = OpenProcess(PROCESS_VM_READ|PROCESS_VM_WRITE|PROCESS_VM_OPERATION|PROCESS_QUERY_INFORMATION, FALSE, pid); |
| if (!hP) { fprintf(stderr, "OpenProcess failed\n"); return 5; } |
| uint64_t base = mod_base(hP); |
| int64_t delta = (int64_t)(base - PREFERRED_BASE); |
| uint64_t cave = CAVE_VMA + delta, s1 = SITE1_MOVSD_VMA + delta, s2 = SITE2_MOVSD_VMA + delta; |
|
|
| |
| FILE* f = fopen(spath, "r"); if (!f) { fprintf(stderr, "no state.json\n"); CloseHandle(hP); return 9; } |
| char sb[4096]; size_t n = fread(sb, 1, sizeof(sb)-1, f); sb[n]='\0'; fclose(f); |
| |
| uint8_t co[8], s1o[4], s2o[4]; |
| char* p1 = strstr(sb, "cave_original"); char* b1 = strchr(p1, '['); |
| char* p2 = strstr(sb, "site1_original"); char* b2 = strchr(p2, '['); |
| char* p3 = strstr(sb, "site2_original"); char* b3 = strchr(p3, '['); |
| for (int i = 0; i < 8; i++) co[i] = (uint8_t)strtol(b1 + (i? strchr(b1,',')+1 - b1 : 1), &b1, 10); |
| for (int i = 0; i < 4; i++) s1o[i] = (uint8_t)strtol(b2 + (i? strchr(b2,',')+1 - b2 : 1), &b2, 10); |
| for (int i = 0; i < 4; i++) s2o[i] = (uint8_t)strtol(b3 + (i? strchr(b3,',')+1 - b3 : 1), &b3, 10); |
|
|
| wr(hP, cave, co, 8); |
| wr(hP, s1 + 4, s1o, 4); |
| wr(hP, s2 + 4, s2o, 4); |
| fprintf(stderr, "reverted: restored original bytes at all 3 sites\n"); |
| CloseHandle(hP); |
| return 0; |
| } |
|
|
| static int do_status(void) { |
| char spath[MAX_PATH]; path_next_to_exe(spath, sizeof(spath), "state.json"); |
| FILE* f = fopen(spath, "r"); if (!f) { fprintf(stderr, "no state.json (not patched?)\n"); return 0; } |
| char line[256]; |
| while (fgets(line, sizeof(line), f)) printf(" %s", line); |
| fclose(f); |
| return 0; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| static int do_launch(void) { |
| char cpath[MAX_PATH]; path_next_to_exe(cpath, sizeof(cpath), "config.json"); |
| char lpath[MAX_PATH]; path_next_to_exe(lpath, sizeof(lpath), "launch.log"); |
| FILE* lf = fopen(lpath, "w"); |
| #define LOG(...) do { fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); \ |
| if (lf) { fprintf(lf, __VA_ARGS__); fprintf(lf, "\n"); fflush(lf); } } while(0) |
| double interval = 0.0; |
| if (json_get_double(cpath, "tip_interval_seconds", &interval) != 0 || interval <= 0.0) { |
| LOG("error: tip_interval_seconds missing/invalid in config.json"); |
| if (lf) fclose(lf); |
| return 2; |
| } |
| LOG("config: tip_interval_seconds = %.6f", interval); |
|
|
| STARTUPINFOA si; memset(&si, 0, sizeof(si)); si.cb = sizeof(si); |
| PROCESS_INFORMATION pi; memset(&pi, 0, sizeof(pi)); |
| char cmdline[1024]; snprintf(cmdline, sizeof(cmdline), "\"%s\"", EU4_EXE); |
| if (!CreateProcessA(EU4_EXE, cmdline, NULL, NULL, FALSE, |
| CREATE_SUSPENDED, NULL, |
| "C:\\Program Files (x86)\\Steam\\steamapps\\common\\Europa Universalis IV", |
| &si, &pi)) { |
| LOG("CreateProcess failed: %lu", GetLastError()); |
| if (lf) fclose(lf); |
| return 10; |
| } |
| LOG("launched eu4.exe PID %lu (suspended)", pi.dwProcessId); |
|
|
| HANDLE hP = pi.hProcess; |
|
|
| |
| Sleep(50); |
|
|
| |
| uint64_t base = mod_base(hP); |
| LOG("EnumProcessModules base = 0x%llx", (unsigned long long)base); |
| if (base == 0) { |
| typedef LONG (WINAPI *pNtQIP)(HANDLE, ULONG, PVOID, ULONG, PULONG); |
| HMODULE ntdll = GetModuleHandleW(L"ntdll.dll"); |
| pNtQIP NtQIP = (pNtQIP)GetProcAddress(ntdll, "NtQueryInformationProcess"); |
| LOG("NtQIP ptr = %p", (void*)NtQIP); |
| if (NtQIP) { |
| |
| unsigned char pbi[48]; memset(pbi, 0, sizeof(pbi)); ULONG ret = 0; |
| LONG st = NtQIP(hP, 0, pbi, sizeof(pbi), &ret); |
| LOG("NtQIP status = %ld, returned %lu bytes", st, ret); |
| uint64_t peb = *(uint64_t*)(pbi + 8); |
| LOG("PEB = 0x%llx", (unsigned long long)peb); |
| if (peb) { |
| uint64_t imgbase = 0; SIZE_T g = 0; |
| BOOL ok = ReadProcessMemory(hP, (LPCVOID)(peb + 0x10), &imgbase, 8, &g); |
| LOG("PEB ImageBaseAddress read: ok=%d g=%llu imgbase=0x%llx", ok, (unsigned long long)g, (unsigned long long)imgbase); |
| base = imgbase; |
| } |
| } |
| } |
| if (base == 0) { |
| LOG("error: cannot resolve eu4.exe base. killing process."); |
| TerminateProcess(hP, 0); CloseHandle(hP); CloseHandle(pi.hThread); |
| if (lf) fclose(lf); |
| return 11; |
| } |
| int64_t delta = (int64_t)(base - PREFERRED_BASE); |
| LOG("actual base 0x%llx, delta %+lld", (unsigned long long)base, (long long)delta); |
|
|
| uint64_t cave = CAVE_VMA + delta, s1 = SITE1_MOVSD_VMA + delta, s2 = SITE2_MOVSD_VMA + delta; |
| LOG("cave=0x%llx site1=0x%llx site2=0x%llx", |
| (unsigned long long)cave, (unsigned long long)s1, (unsigned long long)s2); |
|
|
| |
| uint8_t chk1[4], chk2[4]; |
| BOOL r1 = rd(hP, s1+4, chk1, 4); |
| BOOL r2 = rd(hP, s2+4, chk2, 4); |
| LOG("read site1: ok=%d bytes=%02x%02x%02x%02x", r1, chk1[0],chk1[1],chk1[2],chk1[3]); |
| LOG("read site2: ok=%d bytes=%02x%02x%02x%02x", r2, chk2[0],chk2[1],chk2[2],chk2[3]); |
| if (!r1 || memcmp(chk1, SITE1_ORIG, 4) != 0) { |
| LOG("error: site1 mismatch (eu4.exe version?). killing."); |
| TerminateProcess(hP, 0); CloseHandle(hP); CloseHandle(pi.hThread); |
| if (lf) fclose(lf); |
| return 12; |
| } |
| if (!r2 || memcmp(chk2, SITE2_ORIG, 4) != 0) { |
| LOG("error: site2 mismatch (eu4.exe version?). killing."); |
| TerminateProcess(hP, 0); CloseHandle(hP); CloseHandle(pi.hThread); |
| if (lf) fclose(lf); |
| return 12; |
| } |
|
|
| |
| double iv = interval; |
| BOOL w0 = wr(hP, cave, &iv, 8); |
| LOG("write cave (8 bytes double): ok=%d err=%lu", w0, GetLastError()); |
| |
| BOOL w1 = wr(hP, s1+4, SITE1_NEW, 4); |
| BOOL w2 = wr(hP, s2+4, SITE2_NEW, 4); |
| LOG("write site1 disp32: ok=%d err=%lu", w1, GetLastError()); |
| LOG("write site2 disp32: ok=%d err=%lu", w2, GetLastError()); |
| if (!w0 || !w1 || !w2) { |
| LOG("error: a write failed. killing."); |
| TerminateProcess(hP, 0); CloseHandle(hP); CloseHandle(pi.hThread); |
| if (lf) fclose(lf); |
| return 13; |
| } |
|
|
| |
| uint8_t co[8]; memset(co, 0xCC, 8); |
| uint8_t hash[32]; char hex[65] = {0}; |
| if (sha256_file(EU4_EXE_W, hash) == 0) to_hex(hash, hex); |
| char spath[MAX_PATH]; path_next_to_exe(spath, sizeof(spath), "state.json"); |
| write_state(spath, hex, co, SITE1_ORIG, SITE2_ORIG, interval); |
| LOG("state saved."); |
|
|
| |
| DWORD prev = ResumeThread(pi.hThread); |
| LOG("ResumeThread: prev suspend count = %lu", prev); |
| CloseHandle(pi.hThread); |
| CloseHandle(hP); |
| LOG("OK: eu4.exe running with loading-tip interval = %.1f s", interval); |
| if (lf) fclose(lf); |
| return 0; |
| } |
|
|
| int main(int argc, char** argv) { |
| const char* mode = (argc >= 2) ? argv[1] : "--launch"; |
| if (strcmp(mode, "--launch") == 0) return do_launch(); |
| if (strcmp(mode, "--apply") == 0) return do_apply(); |
| if (strcmp(mode, "--revert") == 0) return do_revert(); |
| if (strcmp(mode, "--status") == 0) return do_status(); |
| fprintf(stderr, "Usage: loading_tip_patcher.exe [--launch|--apply|--revert|--status]\n"); |
| fprintf(stderr, " --launch (default): CREATE_SUSPENDED eu4.exe, patch, resume\n"); |
| return 1; |
| } |
|
|