TopLevelExceptionFilter::TopLevelExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER lpNewExceptionFilter) : pOldExceptionFilter(NULL) { InitPrimaryConfig(); if ( lpNewExceptionFilter ) pOldExceptionFilter = SetUnhandledExceptionFilter(lpNewExceptionFilter); InitializeSymFunctions(); }
TopLevelExceptionFilter::TopLevelExceptionFilter() : pOldExceptionFilter(NULL) { InitPrimaryConfig(); InitializeSymFunctions(); }
//------------------------------------------------- DLL MAIN ------------------------------------------------- BOOL APIENTRY DllMain(HMODULE, DWORD ul_reason_for_call, LPVOID) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: { #ifdef _DEBUG _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); #endif // Retrieve the initial configuration stuff if not already InitPrimaryConfig(); // Get revision/build automatically char szDllPath[MAX_PATH]; DWORD dwDesiredRevision = 0; std::string aicfg = LoadConfigString("ai", BUILD_DEBUG ? "ai_dbg" : "ai", "_NULL"); strncpy(szDllPath, aicfg.c_str(), MAX_PATH); if ( aicfg == "_NULL" ) { BWAPIError("Could not find %s under ai in \"%s\" for revision identification.", BUILD_DEBUG ? "ai_dbg" : "ai", szConfigPath); } else { DWORD dwDesiredBuild = 0; // 0 = undefined, 1 = release, 2 = debug // Tokenize and retrieve correct path for the instance number char *pszDll = strtok(szDllPath, ","); for ( unsigned int i = 0; i < gdwProcNum-1; ++i ) { char *pszNext = strtok(NULL, ","); if ( !pszNext ) break; pszDll = pszNext; } // Retrieve revision info if it exists char *pszLoadRevCheck = strchr(pszDll, ':'); if ( pszLoadRevCheck ) { pszLoadRevCheck[0] = 0; ++pszLoadRevCheck; sscanf(pszLoadRevCheck, "%u", &dwDesiredRevision); } // Remove spaces while ( isspace(pszDll[0]) ) ++pszDll; // Open File HANDLE hFile = NULL; if ( !SFileOpenFileEx(NULL, pszDll, SFILE_FROM_ABSOLUTE, &hFile) || !hFile) { BWAPIError("Could not load module \"%s\" for revision identification.", pszDll); } else { // Obtain file size DWORD dwFileSize = SFileGetFileSize(hFile, 0); // Allocate memory char *pbBuffer = (char*)SMAlloc(dwFileSize); if ( !pbBuffer ) { BWAPIError("Unable to allocate enough memory for module \"%s\" for revision identification.", pszDll); } else { // Read file DWORD dwBytesRead = 0; SFileReadFile(hFile, pbBuffer, dwFileSize, &dwBytesRead, 0); for ( u32 i = 0; i < dwBytesRead && (dwDesiredRevision == 0 || dwDesiredBuild == 0); ++i ) { if ( dwDesiredRevision == 0 && memcmp(&pbBuffer[i], "XBWAPIXREVISIONXSTAMPX", 22) == 0 ) { i += 22; sscanf(&pbBuffer[i], "%u", &dwDesiredRevision); i += 5; } // if REVISION if ( memcmp(&pbBuffer[i], "XBWAPIXBUILDXSTAMPX", 19) == 0 ) { i += 19; if ( strcmp(&pbBuffer[i], "DEBUG") == 0 ) { dwDesiredBuild = 2; i += 6; } else if ( strcmp(&pbBuffer[i], "RELEASE") == 0 ) { dwDesiredBuild = 1; i += 8; } } // if BUILD } // for (iterate file) // Free memory and close file SMFree(pbBuffer); SFileCloseFile(hFile); } // buffer was allocated } // file was opened /* Do revision checking */ if ( dwDesiredRevision > 0 && dwDesiredRevision != SVN_REV ) { // revision that ai_dll_# for multiple instances was introduced if ( gdwProcNum && dwDesiredRevision < 2753 && showWarn ) { char err[512]; sprintf(err, "Revision %u is not compatible with multiple instances.\nExpecting revision 2753 (BWAPI Beta 3.1) or greater. If you proceed, the older revision of BWAPI will attempt to load its module from ai_dll instead of the multi-instance specification. Do you want to continue anyway?", dwDesiredRevision); BWAPIError("%s", err); if ( MessageBox(NULL, err, "Error", MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON1 | MB_TASKMODAL) == IDNO ) return TRUE; } // revision is old if ( dwDesiredBuild == 0 ) dwDesiredBuild = BUILD_DEBUG + 1; char szRevModule[MAX_PATH]; sprintf_s(szRevModule, MAX_PATH, "%sbwapi-data\\revisions\\%u%s.dll", szInstallPath, dwDesiredRevision, dwDesiredBuild == 2 ? "d" : ""); HMODULE hLib = LoadLibrary(szRevModule); if ( hLib ) { if ( showWarn ) { char msg[MAX_PATH+32]; char szLoadedName[MAX_PATH]; GetModuleFileName(hLib, szLoadedName, MAX_PATH); sprintf_s(msg, MAX_PATH+32, "Loaded \"%s\" instead.", szLoadedName); MessageBox(NULL, msg, "Success", MB_OK | MB_ICONINFO); } return TRUE; } if ( showWarn ) { char err[512]; sprintf(err, "Couldn't find revision module \"%s\" of which the AI DLL was compiled for. Do you want to try using the current revision instead?", szRevModule); BWAPIError("%s", err); if ( MessageBox(NULL, err, "Error", MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON1 | MB_TASKMODAL) == IDNO ) return TRUE; } } // specified rev is not this one else if ( dwDesiredBuild && BUILD_DEBUG + 1 != dwDesiredBuild ) { char envBuffer[MAX_PATH]; if ( !GetEnvironmentVariable("ChaosDir", envBuffer, MAX_PATH) ) if ( !GetCurrentDirectory(MAX_PATH, envBuffer) && showWarn ) BWAPIError("Could not find ChaosDir or current directory for build identification."); SStrNCat(envBuffer, "\\BWAPI", MAX_PATH); if ( dwDesiredBuild == 2 ) SStrNCat(envBuffer, "d", MAX_PATH); SStrNCat(envBuffer, ".dll", MAX_PATH); HMODULE hLib = LoadLibrary(envBuffer); if ( hLib ) { if ( showWarn ) { char msg[MAX_PATH+32]; sprintf_s(msg, MAX_PATH+32, "Loaded \"%s\" instead.", envBuffer); MessageBox(NULL, msg, "Success", MB_OK | MB_ICONINFO); } return TRUE; } if ( showWarn ) { char err[512]; sprintf(err, "Couldn't find build module \"%s\" of which the AI DLL was compiled for. Do you want to try using the current build instead?", envBuffer); BWAPIError("%s", err); if ( MessageBox(NULL, err, "Error", MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON1 | MB_TASKMODAL) == IDNO ) return TRUE; } return TRUE; } } // module str was found // Do version checking CheckVersion(); // Load the auto-menu config BWAPI::BroodwarImpl.loadAutoMenuData(); // Apply all hacks and patches to the game ApplyCodePatches(); // Initialize BWAPI BWAPI::BWAPI_init(); // Create our thread that persistently applies hacks CreateThread(NULL, 0, &PersistentPatch, NULL, 0, NULL); return TRUE; } } return TRUE; }