@@ -137,13 +137,15 @@ private static void CreateCommandScript(string protocolName)
137137 " powershell -NoProfile -ExecutionPolicy Bypass -Command ^" ,
138138 " \" $ErrorActionPreference = 'Continue';\" ^" ,
139139 " \" $wshell = New-Object -ComObject wscript.shell;\" ^" ,
140- " \" Add-Content -Path \\ \" {logPath}\\ \" -Value ('[' + (Get-Date) + '] Attempting to activate process ID: ' + %%A);\" ^" ,
140+ $ " \" Add-Content -Path \\ \" { logPath } \\ \" -Value ('[' + (Get-Date) + '] Attempting to activate process ID: ' + %%A);\" ^",
141141 " \" Start-Sleep -Milliseconds 100;\" ^" ,
142142 " \" $result = $wshell.AppActivate(%%A);\" ^" ,
143- " \" Add-Content -Path \\ \" {logPath}\\ \" -Value ('[' + (Get-Date) + '] AppActivate result: ' + $result);\" ^" ,
144- " \" if (-not $result) { Add-Content -Path \\ \" {logPath}\\ \" -Value ('[' + (Get-Date) + '] Failed to activate window') }\" ^" ,
143+ $ " \" Add-Content -Path \\ \" { logPath } \\ \" -Value ('[' + (Get-Date) + '] AppActivate result: ' + $result);\" ^",
144+ $ " \" if (-not $result) {{ Add-Content -Path \\ \" { logPath } \\ \" -Value ('[' + (Get-Date) + '] Failed to activate window') } }\" ^",
145145 " >nul 2>&1" ,
146146 " if errorlevel 1 echo [%date% %time%] PowerShell error: %errorlevel% >> \" %LOG_PATH%\" " ,
147+ " echo [%date% %time%] Unity activated, self-deleting >> \" %LOG_PATH%\" " ,
148+ $ " del \" %~f0\" >nul 2>&1",
147149 " endlocal" ,
148150 " exit /b 0" ,
149151 " )" ,
@@ -155,7 +157,11 @@ private static void CreateCommandScript(string protocolName)
155157 "" ,
156158 // Start new Unity instance if none found
157159 $ "echo [%date% %time%] Starting new Unity instance >> \" %LOG_PATH%\" ",
158- $ "start \" \" \" { unityExe } \" -projectPath \" %PROJECT_PATH%\" >nul 2>&1"
160+ $ "start \" \" \" { unityExe } \" -projectPath \" %PROJECT_PATH%\" >nul 2>&1",
161+ "" ,
162+ // Self-delete the batch file when done
163+ "echo [%date% %time%] Script completed, self-deleting >> \" %LOG_PATH%\" " ,
164+ $ "del \" %~f0\" >nul 2>&1"
159165 } ;
160166
161167 File . WriteAllLines ( cmdPath , scriptLines ) ;
@@ -249,6 +255,7 @@ private static void RegisterProtocol(string protocolName)
249255
250256 // Set command to launch the script with the URI parameter
251257 var scriptLocation = GetGameExecutablePath ( ".cmd" ) ;
258+ //string command = $"cmd.exe /c \"\"{scriptLocation}\" \"%1\"\"";
252259 string command = $ "\" { scriptLocation } \" \" %1\" ";
253260 uint commandSize = ( uint ) ( ( command . Length + 1 ) * 2 ) ;
254261
@@ -357,18 +364,22 @@ private void HandleDeeplink()
357364 }
358365
359366 // Clean up command script
367+ // Note: Batch file will self-delete, no need to delete here
368+ // This prevents race condition where Unity deletes the file
369+ // while Windows is still trying to execute it
360370 var cmdPath = GetGameExecutablePath ( ".cmd" ) ;
361- if ( File . Exists ( cmdPath ) )
362- {
363- try
364- {
365- File . Delete ( cmdPath ) ;
366- }
367- catch ( Exception ex )
368- {
369- PassportLogger . Warn ( $ "Failed to delete script: { ex . Message } ") ;
370- }
371- }
371+ // Commented out to prevent race condition
372+ // if (File.Exists(cmdPath))
373+ // {
374+ // try
375+ // {
376+ // File.Delete(cmdPath);
377+ // }
378+ // catch (Exception ex)
379+ // {
380+ // PassportLogger.Warn($"Failed to delete script: {ex.Message}");
381+ // }
382+ // }
372383
373384 // Clean up instance
374385 Destroy ( gameObject ) ;
0 commit comments