Skip to content

Commit 6177af3

Browse files
authored
Merge pull request #540 from immutable/fix/windows-deeplink-profiles
[ID-3892] fix: handle windows deeplink when build profile executable name diffe…
2 parents 2c2a32e + 3005fbf commit 6177af3

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

src/Packages/Passport/Runtime/Scripts/Private/Helpers/WindowsDeepLink.cs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
using System;
33
using System.IO;
44
using System.Runtime.InteropServices;
5+
using System.Diagnostics;
6+
#if UNITY_EDITOR
7+
using UnityEditor;
8+
#endif
59
using UnityEditor;
610
using UnityEngine;
711
using Immutable.Passport.Core.Logging;
@@ -173,7 +177,7 @@ private static void CreateCommandScript(string protocolName)
173177
// Store deeplink URI in registry
174178
$"REG ADD \"HKCU\\Software\\Classes\\{protocolName}\" /v \"{REGISTRY_DEEP_LINK_NAME}\" /t REG_SZ /d %1 /f >nul 2>&1",
175179
// 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",
177181
"if %ERRORLEVEL%==0 (",
178182
// Bring existing game window to foreground
179183
" powershell -NoProfile -ExecutionPolicy Bypass -Command ^",
@@ -264,11 +268,45 @@ private static void RegisterProtocol(string protocolName)
264268
RegCloseKey(commandKey);
265269
RegCloseKey(hKey);
266270
}
267-
271+
268272
private static string GetGameExecutablePath(string suffix)
269273
{
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("/", "\\");
272310
}
273311

274312
private void OnApplicationFocus(bool hasFocus)

0 commit comments

Comments
 (0)