From 31481892e6fa3c543c10a9d32fc09a57c5159d1f Mon Sep 17 00:00:00 2001 From: Scighost Date: Thu, 26 Oct 2023 00:25:33 +0800 Subject: [PATCH] improve url protocol method --- src/Starward/Services/GameService.cs | 12 ++++++++++-- src/Starward/Services/UrlProtocolService.cs | 19 +++++++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/Starward/Services/GameService.cs b/src/Starward/Services/GameService.cs index 3b2c97915..fae00f7ae 100644 --- a/src/Starward/Services/GameService.cs +++ b/src/Starward/Services/GameService.cs @@ -240,7 +240,7 @@ 12 or 22 or (>= 32 and <= 36) => GameRegistry.MIHOYOSDK_ADL_PROD_OVERSEA_h115894 /// 启动游戏 /// /// - public Process? StartGame(GameBiz biz, bool ignoreRunningGame = false) + public Process? StartGame(GameBiz biz, bool ignoreRunningGame = false, string? installPath = null) { const int ERROR_CANCELLED = 0x000004C7; try @@ -253,7 +253,15 @@ 12 or 22 or (>= 32 and <= 36) => GameRegistry.MIHOYOSDK_ADL_PROD_OVERSEA_h115894 } } string? exe = null, arg = null, verb = null; - if (AppConfig.GetEnableThirdPartyTool(biz)) + if (Directory.Exists(installPath)) + { + var e = Path.Join(installPath, GetGameExeName(biz)); + if (File.Exists(e)) + { + exe = e; + } + } + if (string.IsNullOrWhiteSpace(exe) && AppConfig.GetEnableThirdPartyTool(biz)) { exe = AppConfig.GetThirdPartyToolPath(biz); if (File.Exists(exe)) diff --git a/src/Starward/Services/UrlProtocolService.cs b/src/Starward/Services/UrlProtocolService.cs index 816194e68..62fb7d583 100644 --- a/src/Starward/Services/UrlProtocolService.cs +++ b/src/Starward/Services/UrlProtocolService.cs @@ -55,7 +55,8 @@ public static async Task HandleUrlProtocolAsync(string url) } if (string.IsNullOrWhiteSpace(AppConfig.UserDataFolder)) { - throw new ArgumentNullException("UserDataFolder is null"); + log.LogWarning("UserDataFolder is null"); + return false; } if (uri.Host is "startgame") { @@ -63,6 +64,7 @@ public static async Task HandleUrlProtocolAsync(string url) { var kvs = HttpUtility.ParseQueryString(uri.Query); string? uidStr = kvs["uid"]; + string? installPath = kvs["install_path"]; var gameService = AppConfig.GetService(); if (int.TryParse(uidStr, out int uid)) { @@ -81,7 +83,7 @@ public static async Task HandleUrlProtocolAsync(string url) { log.LogWarning("Cannot parse the uid '{uid}'", uidStr); } - var p = gameService.StartGame(biz); + var p = gameService.StartGame(biz, false, installPath); if (p != null) { await AppConfig.GetService().StartProcessToLogAsync(biz); @@ -93,6 +95,19 @@ public static async Task HandleUrlProtocolAsync(string url) } return true; } + if (uri.Host is "playtime") + { + if (Enum.TryParse(uri.AbsolutePath.Trim('/'), out GameBiz biz)) + { + var kvs = HttpUtility.ParseQueryString(uri.Query); + await AppConfig.GetService().StartProcessToLogAsync(biz); + } + else + { + throw new ArgumentException($"Cannot parse the game biz '{uri.AbsolutePath.Trim('/')}'"); + } + return true; + } } } catch (Exception ex)