From 31997c3cac4f007d07cd395d26002478271cf251 Mon Sep 17 00:00:00 2001 From: anzz1 Date: Thu, 5 Oct 2023 10:43:36 +0300 Subject: [PATCH] bfme2: startup crash fix missing options.ini causes ctd on modern systems, create if not exist --- include/game_bfme2.h | 27 +++++++++++++++++++++++++++ include/global.h | 9 +++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/include/game_bfme2.h b/include/game_bfme2.h index 046fc6f..350ed9a 100644 --- a/include/game_bfme2.h +++ b/include/game_bfme2.h @@ -6,6 +6,15 @@ #include "include/global.h" #include "iathook/iathook.h" +static const char* bfme2_options_ini = + "AudioLOD = High\r\n" \ + "FlashTutorial = 0\r\n" \ + "HasSeenLogoMovies = yes\r\n" \ + "IdealStaticGameLOD = High\r\n" \ + "Resolution = 1024 768\r\n" \ + "StaticGameLOD = High\r\n" \ + "TimesInGame = 6\r\n"; + LPHOSTENT __stdcall hk_gethostbyname(const char* name); LPHOSTENT __stdcall bfme2_hk_gethostbyname(const char* name) { @@ -22,8 +31,26 @@ __forceinline static void bfme2_hook_gs() { HOOK_FUNC(0, gethostbyname, bfme2_hk_gethostbyname, "wsock32.dll", 0, TRUE); // OFT missing } +// missing options.ini causes crash on startup +__forceinline static void bfme2_create_options() { + char path[MAX_PATH+40]; + HANDLE hFile = 0; + DWORD dw = 0; + if (SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, path) >= 0) { + __strcat(path, "\\My Battle for Middle-earth(tm) II Files"); + CreateDirectoryA(path, NULL); + __strcat(path, "\\options.ini"); + hFile = CreateFileA(path, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); + if (hFile && hFile != INVALID_HANDLE_VALUE) { + WriteFile(hFile, bfme2_options_ini, 150, &dw, NULL); + CloseHandle(hFile); + } + } +} + __noinline static void patch_bfme2() { bfme2_hook_gs(); + bfme2_create_options(); } #endif // __GAME_BFME2_H diff --git a/include/global.h b/include/global.h index 941ceca..2227c0c 100644 --- a/include/global.h +++ b/include/global.h @@ -17,6 +17,7 @@ #include #include #include +#include #pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "iphlpapi.lib") @@ -98,10 +99,10 @@ __forceinline static char* __strncpy(char* dst, const char* src, unsigned int le dst[i] = 0; return dst; } -//__forceinline static void __strcat(char* dst, const char* src) { -// while (*dst) dst++; -// __strcpy(dst, src); -//} +__forceinline static void __strcat(char* dst, const char* src) { + while (*dst) dst++; + __strcpy(dst, src); +} // s2 should be in lowercase __forceinline static char* __stristr(const char* s1, const char* s2) { unsigned int i;