@@ -200,81 +200,38 @@ void HandleSpecialLaunchOptions()
200200// ////////////////////////////////////////////////////////
201201void HandleDuplicateLaunching ()
202202{
203- LPSTR lpCmdLine = GetCommandLine ();
203+ LPWSTR szCommandLine = GetCommandLineW ();
204204
205- int iRecheckTimeLimit = 2000 ;
206- while (!CreateSingleInstanceMutex ())
207- {
208- if (strcmp (lpCmdLine, " " ) != 0 )
209- {
210- HWND hwMTAWindow = FindWindow (NULL , " MTA: San Andreas" );
205+ if (!szCommandLine[0 ])
206+ return ;
207+
208+ HWND gameWindow = FindWindowA (nullptr , " MTA: San Andreas" );
211209#ifdef MTA_DEBUG
212- if (hwMTAWindow == NULL )
213- hwMTAWindow = FindWindow ( NULL , " MTA: San Andreas [DEBUG]" );
210+ if (gameWindow == nullptr )
211+ gameWindow = FindWindowA ( nullptr , " MTA: San Andreas [DEBUG]" );
214212#endif
215- if (hwMTAWindow != NULL )
216- {
217- LPWSTR szCommandLine = GetCommandLineW ();
218- int numArgs;
219- LPWSTR* aCommandLineArgs = CommandLineToArgvW (szCommandLine, &numArgs);
220- for (int i = 1 ; i < numArgs; ++i)
221- {
222- if (WStringX (aCommandLineArgs[i]).BeginsWith (L" mtasa://" ))
223- {
224- WString wideConnectInfo = aCommandLineArgs[i];
225- SString strConnectInfo = ToUTF8 (wideConnectInfo);
226213
227- COPYDATASTRUCT cdStruct;
228- cdStruct.cbData = strConnectInfo.length () + 1 ;
229- cdStruct.lpData = const_cast <char *>(strConnectInfo.c_str ());
230- cdStruct.dwData = URI_CONNECT;
214+ if (gameWindow == nullptr )
215+ return ;
231216
232- SendMessage (hwMTAWindow, WM_COPYDATA, NULL , (LPARAM)&cdStruct);
233- break ;
234- }
235- }
236- }
237- else
238- {
239- if (iRecheckTimeLimit > 0 )
240- {
241- // Sleep a little bit and check the mutex again
242- Sleep (500 );
243- iRecheckTimeLimit -= 500 ;
244- continue ;
245- }
246- SString strMessage;
247- strMessage +=
248- _ (" Trouble restarting MTA:SA\n\n "
249- " If the problem persists, open Task Manager and\n "
250- " stop the 'gta_sa.exe' and 'Multi Theft Auto.exe' processes\n\n\n "
251- " Try to launch MTA:SA again?" );
252- if (MessageBoxUTF8 (0 , strMessage, _ (" Error" ) + _E (" CL04" ), MB_ICONWARNING | MB_YESNO | MB_TOPMOST) ==
253- IDYES) // Trouble restarting MTA:SA
254- {
255- TerminateGTAIfRunning ();
256- TerminateOtherMTAIfRunning ();
257- ShellExecuteNonBlocking (" open" , PathJoin (GetMTASAPath (), MTA_EXE_NAME), lpCmdLine);
258- }
259- return ExitProcess (EXIT_ERROR);
260- }
261- }
262- else
217+ int numArgs;
218+ LPWSTR* aCommandLineArgs = CommandLineToArgvW (szCommandLine, &numArgs);
219+
220+ for (int i = 1 ; i < numArgs; ++i)
221+ {
222+ if (WStringX (aCommandLineArgs[i]).BeginsWith (L" mtasa://" ))
263223 {
264- if (!IsGTARunning () && !IsOtherMTARunning ())
265- {
266- MessageBoxUTF8 (0 , _ (" Another instance of MTA is already running.\n\n If this problem persists, please restart your computer" ),
267- _ (" Error" ) + _E (" CL05" ), MB_ICONERROR | MB_TOPMOST);
268- }
269- else if (MessageBoxUTF8 (0 , _ (" Another instance of MTA is already running.\n\n Do you want to terminate it?" ), _ (" Error" ) + _E (" CL06" ),
270- MB_ICONQUESTION | MB_YESNO | MB_TOPMOST) == IDYES)
271- {
272- TerminateGTAIfRunning ();
273- TerminateOtherMTAIfRunning ();
274- ShellExecuteNonBlocking (" open" , PathJoin (GetMTASAPath (), MTA_EXE_NAME), lpCmdLine);
275- }
224+ WString wideConnectInfo = aCommandLineArgs[i];
225+ SString strConnectInfo = ToUTF8 (wideConnectInfo);
226+
227+ COPYDATASTRUCT cdStruct;
228+ cdStruct.cbData = strConnectInfo.length () + 1 ;
229+ cdStruct.lpData = const_cast <char *>(strConnectInfo.c_str ());
230+ cdStruct.dwData = URI_CONNECT;
231+
232+ SendMessage (gameWindow, WM_COPYDATA, NULL , (LPARAM)&cdStruct);
233+ return ExitProcess (EXIT_ERROR);
276234 }
277- return ExitProcess (EXIT_ERROR);
278235 }
279236}
280237
@@ -480,8 +437,6 @@ void HandleCustomStartMessage()
480437// ////////////////////////////////////////////////////////
481438void PreLaunchWatchDogs ()
482439{
483- assert (!CreateSingleInstanceMutex ());
484-
485440 //
486441 // "L0" is opened before the launch sequence and is closed if MTA shutsdown with no error
487442 // "L1" is opened before the launch sequence and is closed if GTA is succesfully started
@@ -587,35 +542,6 @@ void PostRunWatchDogs(int iReturnCode)
587542 }
588543}
589544
590- // ////////////////////////////////////////////////////////
591- //
592- // HandleIfGTAIsAlreadyRunning
593- //
594- // Check for and maybe stop a running GTA process
595- //
596- // ////////////////////////////////////////////////////////
597- void HandleIfGTAIsAlreadyRunning ()
598- {
599- if (IsGTARunning ())
600- {
601- if (MessageBoxUTF8 (
602- 0 , _ (" An instance of GTA: San Andreas is already running. It needs to be terminated before MTA:SA can be started. Do you want to do that now?" ),
603- _ (" Information" ) + _E (" CL10" ), MB_YESNO | MB_ICONQUESTION | MB_TOPMOST) == IDYES)
604- {
605- TerminateOtherMTAIfRunning ();
606- TerminateGTAIfRunning ();
607- if (IsGTARunning ())
608- {
609- MessageBoxUTF8 (0 , _ (" Unable to terminate GTA: San Andreas. If the problem persists, please restart your computer." ),
610- _ (" Information" ) + _E (" CL11" ), MB_OK | MB_ICONERROR | MB_TOPMOST);
611- return ExitProcess (EXIT_ERROR);
612- }
613- }
614- else
615- return ExitProcess (EXIT_OK);
616- }
617- }
618-
619545// ////////////////////////////////////////////////////////
620546//
621547// ValidateGTAPath
@@ -844,7 +770,6 @@ void CheckDataFiles()
844770 {
845771 SString strMessage (_ (" Main file has an incorrect name (%s)" ), *GetLaunchFilename ());
846772 int iResponse = MessageBoxUTF8 (NULL , strMessage, _ (" Error" ) + _E (" CL33" ), MB_RETRYCANCEL | MB_ICONERROR | MB_TOPMOST);
847- ReleaseSingleInstanceMutex ();
848773 if (iResponse == IDRETRY)
849774 ShellExecuteNonBlocking (" open" , PathJoin (strMTASAPath, MTA_EXE_NAME));
850775 return ExitProcess (EXIT_ERROR);
@@ -1272,7 +1197,6 @@ int LaunchGame(SString strCmdLine)
12721197 //
12731198 // Cleanup and exit.
12741199 CloseHandle (piLoadee.hProcess );
1275- ReleaseSingleInstanceMutex ();
12761200
12771201 // Success, maybe
12781202 return dwExitCode;
0 commit comments