|
2 | 2 | using System; |
3 | 3 | using System.IO; |
4 | 4 | using System.Runtime.InteropServices; |
| 5 | +using System.Diagnostics; |
| 6 | +#if UNITY_EDITOR |
| 7 | +using UnityEditor; |
| 8 | +#endif |
5 | 9 | using UnityEditor; |
6 | 10 | using UnityEngine; |
7 | 11 | using Immutable.Passport.Core.Logging; |
@@ -173,7 +177,7 @@ private static void CreateCommandScript(string protocolName) |
173 | 177 | // Store deeplink URI in registry |
174 | 178 | $"REG ADD \"HKCU\\Software\\Classes\\{protocolName}\" /v \"{REGISTRY_DEEP_LINK_NAME}\" /t REG_SZ /d %1 /f >nul 2>&1", |
175 | 179 | // Check if game is already running |
176 | | - $"tasklist /FI \"IMAGENAME eq {gameExeName}\" 2>NUL | find /I \"{gameExeName}\" >NUL", |
| 180 | + $"tasklist /FI \"IMAGENAME eq {gameExeName}\" >nul 2>&1", |
177 | 181 | "if %ERRORLEVEL%==0 (", |
178 | 182 | // Bring existing game window to foreground |
179 | 183 | " powershell -NoProfile -ExecutionPolicy Bypass -Command ^", |
@@ -264,11 +268,45 @@ private static void RegisterProtocol(string protocolName) |
264 | 268 | RegCloseKey(commandKey); |
265 | 269 | RegCloseKey(hKey); |
266 | 270 | } |
267 | | - |
| 271 | + |
268 | 272 | private static string GetGameExecutablePath(string suffix) |
269 | 273 | { |
270 | | - var exeName = Application.productName + suffix; |
271 | | - return Path.Combine(Application.persistentDataPath, exeName).Replace("/", "\\"); |
| 274 | +#if !UNITY_EDITOR_WIN |
| 275 | + if (suffix == ".exe") |
| 276 | + { |
| 277 | + // Get the path of the currently running executable |
| 278 | + try |
| 279 | + { |
| 280 | + var process = System.Diagnostics.Process.GetCurrentProcess(); |
| 281 | + if (process?.MainModule?.FileName != null) |
| 282 | + { |
| 283 | + return process.MainModule.FileName.Replace("/", "\\"); |
| 284 | + } |
| 285 | + } |
| 286 | + catch (System.ComponentModel.Win32Exception ex) |
| 287 | + { |
| 288 | + PassportLogger.Warn($"Failed to get process MainModule: {ex.Message}. Using fallback method."); |
| 289 | + } |
| 290 | + catch (System.InvalidOperationException ex) |
| 291 | + { |
| 292 | + PassportLogger.Warn($"Process inaccessible: {ex.Message}. Using fallback method."); |
| 293 | + } |
| 294 | + |
| 295 | + // Fallback: Use command line args |
| 296 | + var args = System.Environment.GetCommandLineArgs(); |
| 297 | + if (args.Length > 0 && !string.IsNullOrEmpty(args[0])) |
| 298 | + { |
| 299 | + var exePath = Path.GetFullPath(args[0]); |
| 300 | + if (File.Exists(exePath)) |
| 301 | + { |
| 302 | + return exePath.Replace("/", "\\"); |
| 303 | + } |
| 304 | + } |
| 305 | + } |
| 306 | +#endif |
| 307 | + // For the editor, or for .cmd files in a build, use persistentDataPath as it's a writable location |
| 308 | + var fileName = Application.productName + suffix; |
| 309 | + return Path.Combine(Application.persistentDataPath, fileName).Replace("/", "\\"); |
272 | 310 | } |
273 | 311 |
|
274 | 312 | private void OnApplicationFocus(bool hasFocus) |
|
0 commit comments