CString GetAppPath() {//获取应用程序根目录 TCHAR modulePath[MAX_PATH]; GetModuleFileName(NULL, modulePath, MAX_PATH); CString strModulePath(modulePath); strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\'))); return strModulePath; }
CModule::CModule(CString strName) { // Remove any illegal characters from the module name SharedUtility::RemoveIllegalCharacters(strName); // Get the module path string CString strModulePath(SharedUtility::GetAbsolutePath("multiplayer/modules/%s", strName.Get())); // Replace '/' with '\\' strModulePath.Substitute("/", "\\"); // Create the libray instance m_pLibrary = new CLibrary(); // Is the library instance invalid? if(!m_pLibrary) return; // Did the module fail to load? if(!m_pLibrary->Load(strModulePath.Get())) { // Delete the library instance SAFE_DELETE(m_pLibrary); return; } // Assign module name m_strName = strName; // Get the module function pointers m_moduleFunctions.pfnSetupFunctions = (SetupFunctions_t)m_pLibrary->GetProcedureAddress("SetupFunctions"); m_moduleFunctions.pfnSetupInterfaces = (SetupInterfaces_t)m_pLibrary->GetProcedureAddress("SetupInterfaces"); m_moduleFunctions.pfnSetupNewInterfaces = (SetupNewInterfaces_t)m_pLibrary->GetProcedureAddress("SetupNewInterfaces"); m_moduleFunctions.pfnInitialiseModule = (InitialiseModule_t)m_pLibrary->GetProcedureAddress("InitModule"); m_moduleFunctions.pfnPulse = (Pulse_t)m_pLibrary->GetProcedureAddress("Pulse"); // Are the pointers invalid? if(!IsValid()) { // Delete the library instance SAFE_DELETE(m_pLibrary); return; } // Setup the functions m_moduleFunctions.pfnSetupFunctions(FunctionContainer); // Setup the pointers InterfacesContainer[0] = (void *)g_pCore->GetNetworkManager(); InterfacesContainer[1] = (void *)g_pCore->GetGame()->GetPlayerManager(); InterfacesContainer[2] = (void *)g_pCore->GetGame()->GetVehicleManager(); // Setup the container functions with the module m_moduleFunctions.pfnSetupInterfaces(InterfacesContainer); }
void GetApplicationResourcesPath(Path& path) { TCHAR* pModuleFilename = new TCHAR[MAX_PATH + 1]; GetModuleFileName(NULL, pModuleFilename, MAX_PATH); TCHAR* pLastSep = _tcsrchr(pModuleFilename, TEXT('\\')); String strModulePath(pModuleFilename, pLastSep); delete[] pModuleFilename; path = Path(strModulePath + TEXT("\\res")); }
CString CommonStrMethod::GetModuleDir(void) { WCHAR wszModulePath[MAX_PATH] = {0}; GetModuleFileName(NULL, wszModulePath, MAX_PATH); CString strModulePath(wszModulePath); int nPos = strModulePath.ReverseFind(_T('\\')); if(nPos < 0) { return CString(); } else { return strModulePath.Left(nPos+1); } }
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { #ifndef _DEBUG // Is there a debugger present? if ( IsDebuggerPresent () ) { // Exit ExitProcess ( -1 ); } #endif // Create the gui instance pGUI = new CGUI; #ifndef _DEBUG // Create the updater instance /*pUpdater = new CUpdate; // Check for updates pUpdater->CheckForUpdates();*/ #endif // bool bFoundCustomDirectory = false; char szInstallDirectory[ MAX_PATH ]; // Try get the custom directory if( !SharedUtility::ReadRegistryString( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Mafia 2 Multiplayer", "GameDir", NULL, szInstallDirectory, sizeof(szInstallDirectory) ) ) { // Ask them to find their own directory if( ShowMessageBox( "Failed to find Mafia II install directory. Do you want to select it now?", (MB_ICONEXCLAMATION | MB_YESNO) ) == IDYES ) { // Construct the browse info BROWSEINFO browseInfo = {0}; browseInfo.lpszTitle = "Select your Mafia II directory"; ITEMIDLIST * pItemIdList = SHBrowseForFolder( &browseInfo ); // Did they finish looking for a folder? if( pItemIdList != NULL ) { // Get the name of the selected folder if( SHGetPathFromIDList( pItemIdList, szInstallDirectory ) ) bFoundCustomDirectory = true; // Was any memory used? IMalloc * pIMalloc = NULL; if( SUCCEEDED( SHGetMalloc( &pIMalloc ) ) ) { // Free the memory pIMalloc->Free( pItemIdList ); // Release the malloc pIMalloc->Release(); } } // Did they not find the registry? if( !bFoundCustomDirectory ) { ShowMessageBox( "Failed to find Mafia II install directory. Can't launch "MOD_NAME"." ); return 1; } } } // Get the launch path string String strLaunchPath( "%s\\pc", szInstallDirectory ); // Get the full path to Mafia2.exe String strApplicationPath( "%s\\Mafia2.exe", strLaunchPath.Get() ); // Does Mafia2.exe not exist? if( !SharedUtility::Exists( strApplicationPath.Get() ) ) { ShowMessageBox( "Failed to find Mafia2.exe. Can't launch "MOD_NAME"." ); return 1; } // If we have a custom directory, save it! if( bFoundCustomDirectory ) SharedUtility::WriteRegistryString( HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Mafia 2 Multiplayer", "GameDir", szInstallDirectory, sizeof(szInstallDirectory) ); // Get the full path to m2mp.dll String strModulePath( "%s\\%s", SharedUtility::GetAppPath(), CORE_MODULE ); // Does m2mp.dll not exist? if( !SharedUtility::Exists( strModulePath.Get() ) ) { ShowMessageBox( "Failed to find "CORE_MODULE". Can't launch "MOD_NAME"." ); return 1; } // Terminate Mafia II process if it's already running? if( SharedUtility::IsProcessRunning( "Mafia2.exe" ) ) SharedUtility::_TerminateProcess( "Mafia2.exe" ); // Create the startup info struct STARTUPINFO siStartupInfo; PROCESS_INFORMATION piProcessInfo; memset( &siStartupInfo, 0, sizeof(siStartupInfo) ); memset( &piProcessInfo, 0, sizeof(piProcessInfo) ); siStartupInfo.cb = sizeof(siStartupInfo); // Create the Mafia II process if( !CreateProcess( strApplicationPath.Get(), NULL, NULL, NULL, TRUE, CREATE_SUSPENDED, NULL, SharedUtility::GetAppPath(), &siStartupInfo, &piProcessInfo ) ) { ShowMessageBox( "Failed to start Mafia2.exe. Can't launch "MOD_NAME"." ); return 1; } // Inject m2mp.dll into Mafia2.exe int iReturn = SharedUtility::InjectLibraryIntoProcess( piProcessInfo.hProcess, strModulePath.Get() ); // Did m2mp.dll fail to inject? if( iReturn > 0 ) { // Terminate Mafia2.exe TerminateProcess( piProcessInfo.hProcess, 0 ); // Generate the error string String strError( "Unknown Error. Can't launch "MOD_NAME"." ); // Find the cause of the error if( iReturn == 1 ) strError = "Failed to write library path into remote process. Can't launch "MOD_NAME"."; else if( iReturn == 2 ) strError = "Failed to create remote thread in remote process. Can't launch "MOD_NAME"."; else if( iReturn == 2 ) strError = "Failed to open the remote process. Can't launch "MOD_NAME"."; // Show the error message ShowMessageBox( strError.Get() ); return 1; } // Resume Mafia2.exe thread ResumeThread( piProcessInfo.hThread ); return 0; }
CModule::CModule(const char * szName) { String strModuleName(szName); SharedUtility::RemoveIllegalCharacters(strModuleName); String strModulePath(SharedUtility::GetAbsolutePath("modules/%s", strModuleName.Get())); #ifdef WIN32 // In windows replace all '/' with '\\' or the module will not load strModulePath.Substitute("/", "\\"); #endif m_pLibrary = new CLibrary(); if(!m_pLibrary) return; if(!m_pLibrary->Load(strModulePath.Get())) { delete m_pLibrary; m_pLibrary = NULL; return; } m_ModuleFunctions.pfnSetupFunctions = (SetupFunctions_t)m_pLibrary->GetProcedureAddress("SetupFunctions"); m_ModuleFunctions.pfnSetupInterfaces = (SetupInterfaces_t)m_pLibrary->GetProcedureAddress("SetupInterfaces"); m_ModuleFunctions.pfnSetupNewInterfaces = (SetupNewInterfaces_t)m_pLibrary->GetProcedureAddress("SetupNewInterfaces"); m_ModuleFunctions.pfnInitModule = (InitModule_t)m_pLibrary->GetProcedureAddress("InitModule"); m_ModuleFunctions.pfnScriptLoad = (ScriptLoad_t)m_pLibrary->GetProcedureAddress("ScriptLoad"); m_ModuleFunctions.pfnScriptUnload = (ScriptUnload_t)m_pLibrary->GetProcedureAddress("ScriptUnload"); m_ModuleFunctions.pfnPulse = (Pulse_t)m_pLibrary->GetProcedureAddress("Pulse"); if(!IsValid()) { delete m_pLibrary; m_pLibrary = NULL; return; } // Setup the functions m_ModuleFunctions.pfnSetupFunctions(FunctionContainer); // Setup the pointers InterfacesContainer[0] = (void*)g_pNetworkManager; InterfacesContainer[1] = (void*)g_pPlayerManager; InterfacesContainer[2] = (void*)g_pVehicleManager; InterfacesContainer[3] = (void*)g_pObjectManager; InterfacesContainer[4] = (void*)g_pBlipManager; InterfacesContainer[5] = (void*)g_pActorManager; InterfacesContainer[6] = (void*)g_pPickupManager; InterfacesContainer[7] = (void*)g_pCheckpointManager; InterfacesContainer[8] = (void*)NULL; // model manager InterfacesContainer[9] = (void*)g_pScriptingManager; InterfacesContainer[10] = (void*)g_pModuleManager; InterfacesContainer[11] = (void*)g_pNetworkManager->GetNetServer(); InterfacesContainer[12] = (void*)NULL; // FIXME InterfacesContainer[13] = (void*)g_pTime; InterfacesContainer[14] = (void*)g_pTrafficLights; InterfacesContainer[15] = (void*)g_pEvents; InterfacesContainer[16] = new CSquirrelArgumentManager(); // Setup new interfaces NewInterfaceContainer[0] = (void*)g_pActorModuleNatives; NewInterfaceContainer[1] = (void*)g_pBlipModuleNatives; NewInterfaceContainer[2] = (void*)g_pCheckpointModuleNatives; NewInterfaceContainer[3] = (void*)g_pObjectModuleNatives; NewInterfaceContainer[4] = (void*)g_pPickupModuleNatives; NewInterfaceContainer[5] = (void*)g_pPlayerModuleNatives; NewInterfaceContainer[6] = (void*)g_pServerModuleNatives; NewInterfaceContainer[7] = (void*)g_pVehicleModuleNatives; NewInterfaceContainer[8] = (void*)g_pScriptModuleNatives; NewInterfaceContainer[9] = (void*)g_pAreaModuleNatives; NewInterfaceContainer[10] = (void*)g_pHashModuleNatives; NewInterfaceContainer[11] = (void*)g_pWorldModuleNatives; // Send it if(m_ModuleFunctions.pfnSetupInterfaces) m_ModuleFunctions.pfnSetupInterfaces(InterfacesContainer); if(m_ModuleFunctions.pfnSetupNewInterfaces) m_ModuleFunctions.pfnSetupNewInterfaces(NewInterfaceContainer); char szModuleName[64]; strcpy(szModuleName, szName); if(m_ModuleFunctions.pfnInitModule(szModuleName)) CLogFile::Printf("%s module loaded", szModuleName); }
CModule::CModule( String strName ) { // Store the module name m_strName = strName; // Reset library m_pLibrary = NULL; // Remove any illegal characters from the module name SharedUtility::RemoveIllegalCharacters( strName ); // Get the module path string String strModulePath( "modules\\%s%s", strName.Get(), LIB_EXTENSION ); // Does the module not exists? if( !SharedUtility::Exists( strModulePath.Get() ) ) { CLogFile::Printf( "Failed to load module '%s%s'. (File doesn't exist)", strName.Get(), LIB_EXTENSION ); return; } // Create the libray instance m_pLibrary = new CLibrary(); // Is the library instance invalid? if( !m_pLibrary ) return; // Did the module fail to load? if( !m_pLibrary->Load( strModulePath.Get() ) ) { CLogFile::Printf( "Failed to load module '%s%s'. (Failed to load)", strName.Get(), LIB_EXTENSION ); // Delete the library instance SAFE_DELETE( m_pLibrary ); return; } // Get the module function pointers m_moduleFunctions.pfnSetupSquirrel = (SetupSquirrel_t)m_pLibrary->GetProcedureAddress( "SetupSquirrel" ); m_moduleFunctions.pfnSetupCore = (SetupCore_t)m_pLibrary->GetProcedureAddress( "SetupCore" ); m_moduleFunctions.pfnInitialiseModule = (InitialiseModule_t)m_pLibrary->GetProcedureAddress( "ModuleInitialise" ); m_moduleFunctions.pfnScriptLoad = (ScriptLoad_t)m_pLibrary->GetProcedureAddress( "OnScriptLoad" ); m_moduleFunctions.pfnScriptUnload = (ScriptUnload_t)m_pLibrary->GetProcedureAddress( "OnScriptUnload" ); m_moduleFunctions.pfnPulse = (Pulse_t)m_pLibrary->GetProcedureAddress( "Pulse" ); // Are the pointers invalid? if( !IsValid() ) { CLogFile::Printf( "Failed to load module '%s%s'. (Unable to get module procedures)", strName.Get(), LIB_EXTENSION ); // Delete the library instance SAFE_DELETE( m_pLibrary ); return; } // Setup the functions m_moduleFunctions.pfnSetupSquirrel( SquirrelInterfaceContainer ); // Setup the core interface with the module m_moduleFunctions.pfnSetupCore( (void *)pCore ); // Initialise the module if( m_moduleFunctions.pfnInitialiseModule() ) CLogFile::Printf( "Loaded module '%s%s'.", strName.Get(), LIB_EXTENSION ); }