Skip to content

Commit

Permalink
bfme2: startup crash fix
Browse files Browse the repository at this point in the history
missing options.ini causes ctd on modern systems, create if not exist
  • Loading branch information
anzz1 authored Oct 5, 2023
1 parent 4120eea commit 31997c3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
27 changes: 27 additions & 0 deletions include/game_bfme2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
9 changes: 5 additions & 4 deletions include/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <wininet.h>
#include <iphlpapi.h>
#include <shellapi.h>
#include <shlobj.h>

#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "iphlpapi.lib")
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 31997c3

Please sign in to comment.