Skip to content

Commit cc8299a

Browse files
committed
fix: handle windows deeplink when build profile executable name differs from product name
1 parent 2c2a32e commit cc8299a

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

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

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
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;
12+
using Debug = UnityEngine.Debug;
813

914
#nullable enable
1015
namespace Immutable.Passport.Helpers
@@ -173,7 +178,7 @@ private static void CreateCommandScript(string protocolName)
173178
// Store deeplink URI in registry
174179
$"REG ADD \"HKCU\\Software\\Classes\\{protocolName}\" /v \"{REGISTRY_DEEP_LINK_NAME}\" /t REG_SZ /d %1 /f >nul 2>&1",
175180
// Check if game is already running
176-
$"tasklist /FI \"IMAGENAME eq {gameExeName}\" 2>NUL | find /I \"{gameExeName}\" >NUL",
181+
$"tasklist /FI \"IMAGENAME eq {gameExeName}\" >nul 2>&1",
177182
"if %ERRORLEVEL%==0 (",
178183
// Bring existing game window to foreground
179184
" powershell -NoProfile -ExecutionPolicy Bypass -Command ^",
@@ -264,11 +269,45 @@ private static void RegisterProtocol(string protocolName)
264269
RegCloseKey(commandKey);
265270
RegCloseKey(hKey);
266271
}
267-
272+
268273
private static string GetGameExecutablePath(string suffix)
269274
{
270-
var exeName = Application.productName + suffix;
271-
return Path.Combine(Application.persistentDataPath, exeName).Replace("/", "\\");
275+
#if !UNITY_EDITOR_WIN
276+
if (suffix == ".exe")
277+
{
278+
// Get the path of the currently running executable
279+
try
280+
{
281+
var process = System.Diagnostics.Process.GetCurrentProcess();
282+
if (process?.MainModule?.FileName != null)
283+
{
284+
return process.MainModule.FileName.Replace("/", "\\");
285+
}
286+
}
287+
catch (System.ComponentModel.Win32Exception ex)
288+
{
289+
PassportLogger.Warn($"Failed to get process MainModule: {ex.Message}. Using fallback method.");
290+
}
291+
catch (System.InvalidOperationException ex)
292+
{
293+
PassportLogger.Warn($"Process inaccessible: {ex.Message}. Using fallback method.");
294+
}
295+
296+
// Fallback: Use command line args
297+
var args = System.Environment.GetCommandLineArgs();
298+
if (args.Length > 0 && !string.IsNullOrEmpty(args[0]))
299+
{
300+
var exePath = Path.GetFullPath(args[0]);
301+
if (File.Exists(exePath))
302+
{
303+
return exePath.Replace("/", "\\");
304+
}
305+
}
306+
}
307+
#endif
308+
// For the editor, or for .cmd files in a build, use persistentDataPath as it's a writable location
309+
var fileName = Application.productName + suffix;
310+
return Path.Combine(Application.persistentDataPath, fileName).Replace("/", "\\");
272311
}
273312

274313
private void OnApplicationFocus(bool hasFocus)

0 commit comments

Comments
 (0)