From 68b588ee7b879c609adb7ef62479f6aad34aa329 Mon Sep 17 00:00:00 2001 From: CJKmkp <2564608840@qq.com> Date: Sat, 21 Mar 2026 16:49:37 +0800 Subject: [PATCH] =?UTF-8?q?improve:=E5=A4=96=E9=83=A8=E7=82=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ink Canvas/Helpers/ExternalCallerLauncher.cs | 81 +++++++++++++++++++ .../MainWindow_cs/MW_FloatingBarIcons.cs | 17 ++-- .../Windows/NewStyleRollCallWindow.xaml.cs | 17 ++-- Ink Canvas/Windows/RandWindow.xaml.cs | 17 ++-- 4 files changed, 105 insertions(+), 27 deletions(-) create mode 100644 Ink Canvas/Helpers/ExternalCallerLauncher.cs diff --git a/Ink Canvas/Helpers/ExternalCallerLauncher.cs b/Ink Canvas/Helpers/ExternalCallerLauncher.cs new file mode 100644 index 00000000..41b4defe --- /dev/null +++ b/Ink Canvas/Helpers/ExternalCallerLauncher.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; + +namespace Ink_Canvas.Helpers +{ + public static class ExternalCallerLauncher + { + private static readonly string[] ClassIslandProtocols = + { + "classisland://plugins/IslandCaller/Simple/1", + "classisland://plugins/IslandCaller/Simple", + "classisland://plugins/IslandCaller/Run" + }; + + public static string[] GetProtocolsByType(int externalCallerType) + { + switch (externalCallerType) + { + case 0: + return ClassIslandProtocols; + case 1: + return new[] + { + "secrandom://roll_call/quick_draw", + "secrandom://direct_extraction" + }; + case 2: + return new[] { "namepicker://" }; + default: + return ClassIslandProtocols; + } + } + + public static string[] GetProtocolsByName(string externalCallerName) + { + switch (externalCallerName) + { + case "ClassIsland": + return ClassIslandProtocols; + case "SecRandom": + return new[] + { + "secrandom://roll_call/quick_draw", + "secrandom://direct_extraction" + }; + case "NamePicker": + return new[] { "namepicker://" }; + default: + return ClassIslandProtocols; + } + } + + public static bool TryLaunch(IEnumerable protocols, out Exception lastException) + { + lastException = null; + if (protocols == null) return false; + + foreach (var protocol in protocols) + { + if (string.IsNullOrWhiteSpace(protocol)) continue; + + try + { + Process.Start(new ProcessStartInfo + { + FileName = protocol, + UseShellExecute = true + }); + return true; + } + catch (Exception ex) + { + lastException = ex; + } + } + + return false; + } + } +} diff --git a/Ink Canvas/MainWindow_cs/MW_FloatingBarIcons.cs b/Ink Canvas/MainWindow_cs/MW_FloatingBarIcons.cs index 3744d19f..e3c1f7e2 100644 --- a/Ink Canvas/MainWindow_cs/MW_FloatingBarIcons.cs +++ b/Ink Canvas/MainWindow_cs/MW_FloatingBarIcons.cs @@ -1365,28 +1365,27 @@ namespace Ink_Canvas { try { - string protocol = ""; + string[] protocols; switch (Settings.RandSettings.ExternalCallerType) { case 0: // ClassIsland点名 - protocol = "classisland://plugins/IslandCaller/Simple/1"; + protocols = ExternalCallerLauncher.GetProtocolsByType(0); break; case 1: // SecRandom点名 - protocol = "secrandom://direct_extraction"; + protocols = ExternalCallerLauncher.GetProtocolsByType(1); break; case 2: // NamePicker点名 - protocol = "namepicker://"; + protocols = ExternalCallerLauncher.GetProtocolsByType(2); break; default: - protocol = "classisland://plugins/IslandCaller/Simple/1"; + protocols = ExternalCallerLauncher.GetProtocolsByType(0); break; } - Process.Start(new ProcessStartInfo + if (!ExternalCallerLauncher.TryLaunch(protocols, out Exception lastException)) { - FileName = protocol, - UseShellExecute = true - }); + throw lastException ?? new InvalidOperationException("external caller protocols are unavailable"); + } } catch (Exception ex) { diff --git a/Ink Canvas/Windows/NewStyleRollCallWindow.xaml.cs b/Ink Canvas/Windows/NewStyleRollCallWindow.xaml.cs index ae442f2b..16813087 100644 --- a/Ink Canvas/Windows/NewStyleRollCallWindow.xaml.cs +++ b/Ink Canvas/Windows/NewStyleRollCallWindow.xaml.cs @@ -1523,28 +1523,27 @@ namespace Ink_Canvas try { - string protocol = ""; + string[] protocols; switch (selectedExternalCaller) { case "ClassIsland": - protocol = "classisland://plugins/IslandCaller/Simple/1"; + protocols = ExternalCallerLauncher.GetProtocolsByName("ClassIsland"); break; case "SecRandom": - protocol = "secrandom://direct_extraction"; + protocols = ExternalCallerLauncher.GetProtocolsByName("SecRandom"); break; case "NamePicker": - protocol = "namepicker://"; + protocols = ExternalCallerLauncher.GetProtocolsByName("NamePicker"); break; default: - protocol = "classisland://plugins/IslandCaller/Simple/1"; + protocols = ExternalCallerLauncher.GetProtocolsByName("ClassIsland"); break; } - System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo + if (!ExternalCallerLauncher.TryLaunch(protocols, out Exception lastException)) { - FileName = protocol, - UseShellExecute = true - }); + throw lastException ?? new InvalidOperationException("external caller protocols are unavailable"); + } UpdateStatusDisplay($"已启动外部点名: {selectedExternalCaller}"); } diff --git a/Ink Canvas/Windows/RandWindow.xaml.cs b/Ink Canvas/Windows/RandWindow.xaml.cs index e0839716..5137f835 100644 --- a/Ink Canvas/Windows/RandWindow.xaml.cs +++ b/Ink Canvas/Windows/RandWindow.xaml.cs @@ -428,28 +428,27 @@ namespace Ink_Canvas try { - string protocol = ""; + string[] protocols; switch (ComboBoxCallerType.SelectedIndex) { case 0: // ClassIsland点名 - protocol = "classisland://plugins/IslandCaller/Simple/1"; + protocols = ExternalCallerLauncher.GetProtocolsByType(0); break; case 1: // SecRandom点名 - protocol = "secrandom://direct_extraction"; + protocols = ExternalCallerLauncher.GetProtocolsByType(1); break; case 2: // NamePicker点名 - protocol = "namepicker://"; + protocols = ExternalCallerLauncher.GetProtocolsByType(2); break; default: - protocol = "classisland://plugins/IslandCaller/Simple/1"; + protocols = ExternalCallerLauncher.GetProtocolsByType(0); break; } - Process.Start(new ProcessStartInfo + if (!ExternalCallerLauncher.TryLaunch(protocols, out Exception lastException)) { - FileName = protocol, - UseShellExecute = true - }); + throw lastException ?? new InvalidOperationException("external caller protocols are unavailable"); + } } catch (Exception ex) {