From e8b8a405f238c5576fa093766c62039c0b20148c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=89=E9=B8=AD=E8=9B=8B?= Date: Fri, 19 Sep 2025 00:24:18 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85km+shell?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Core/Script/Group/ScriptGroupProject.cs | 2 +- .../Pages/Component/GearTaskViewModel.cs | 3 +- .../Pages/GearTaskListPageViewModel.cs | 60 +++++++++++++++++++ .../Pages/KeyMouseRecordPageViewModel.cs | 8 +-- .../ViewModel/Pages/ScriptControlViewModel.cs | 4 +- 5 files changed, 69 insertions(+), 8 deletions(-) diff --git a/BetterGenshinImpact/Core/Script/Group/ScriptGroupProject.cs b/BetterGenshinImpact/Core/Script/Group/ScriptGroupProject.cs index 4d55a5fb..1525b8cf 100644 --- a/BetterGenshinImpact/Core/Script/Group/ScriptGroupProject.cs +++ b/BetterGenshinImpact/Core/Script/Group/ScriptGroupProject.cs @@ -188,7 +188,7 @@ public partial class ScriptGroupProject : ObservableObject else if (Type == "KeyMouse") { // 加载并执行 - var json = await File.ReadAllTextAsync(Global.Absolute(@$"User\KeyMouseScript\{Name}")); + var json = await File.ReadAllTextAsync(Path.Combine(KeyMouseRecordPageViewModel.ScriptPath, Name)); await KeyMouseMacroPlayer.PlayMacro(json, CancellationContext.Instance.Cts.Token, false); } else if (Type == "Pathing") diff --git a/BetterGenshinImpact/ViewModel/Pages/Component/GearTaskViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/Component/GearTaskViewModel.cs index f69961f3..95da5118 100644 --- a/BetterGenshinImpact/ViewModel/Pages/Component/GearTaskViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/Component/GearTaskViewModel.cs @@ -51,9 +51,10 @@ public partial class GearTaskViewModel : ObservableObject /// /// 任务参数,存储为JSON字符串 + /// shell 脚本存储 shell 命令 /// [ObservableProperty] - private string _parameters = "{}"; + private string _parameters = ""; /// /// 创建时间 diff --git a/BetterGenshinImpact/ViewModel/Pages/GearTaskListPageViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/GearTaskListPageViewModel.cs index 0b307916..1e8cba26 100644 --- a/BetterGenshinImpact/ViewModel/Pages/GearTaskListPageViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/GearTaskListPageViewModel.cs @@ -4,11 +4,15 @@ using CommunityToolkit.Mvvm.Input; using Microsoft.Extensions.Logging; using System.Linq; using System; +using System.Collections.Generic; using BetterGenshinImpact.ViewModel.Pages.Component; using BetterGenshinImpact.Service; using System.Threading.Tasks; using System.Collections.Specialized; +using System.IO; using System.Windows; +using System.Windows.Controls; +using BetterGenshinImpact.Core.Config; using BetterGenshinImpact.View.Windows; using BetterGenshinImpact.View.Windows.GearTask; using BetterGenshinImpact.ViewModel.Windows; @@ -380,6 +384,52 @@ public partial class GearTaskListPageViewModel : ViewModel return; // 用户取消了操作 } } + else if (taskType == "Shell") + { + var list = LoadAllKmScripts(); + var folder = KeyMouseRecordPageViewModel.ScriptPath; + var combobox = new ComboBox + { + VerticalAlignment = VerticalAlignment.Top + }; + + foreach (var fileInfo in list) + { + // 计算相对于 KeyMouseScript 文件夹的相对路径 + var relativePath = Path.GetRelativePath(folder, fileInfo.FullName); + combobox.Items.Add(relativePath); + } + + var str = PromptDialog.Prompt("请选择需要添加的键鼠脚本", "请选择需要添加的键鼠脚本", combobox); + if (!string.IsNullOrEmpty(str)) + { + newTask = new GearTaskViewModel(str) + { + TaskType = "Javascript", + Path = @$"{{kmUserFolder}}\{str}\" + }; + } + else + { + return; + } + } + else if (taskType == "Shell") + { + var str = PromptDialog.Prompt("执行 shell 操作存在极大风险!请勿输入你看不懂的指令!以免引发安全隐患并损坏系统!\n执行 shell 的时候,游戏可能会失去焦点", "请输入需要执行的shell"); + if (!string.IsNullOrEmpty(str)) + { + newTask = new GearTaskViewModel(str) + { + TaskType = "Shell", + Parameters = str + }; + } + else + { + return; + } + } else { // 其他类型使用原有的对话框 @@ -410,6 +460,16 @@ public partial class GearTaskListPageViewModel : ViewModel // 自动保存到文件 await _storageService.SaveTaskDefinitionAsync(SelectedTaskDefinition); } + + private List LoadAllKmScripts() + { + var folder = KeyMouseRecordPageViewModel.ScriptPath; + // 获取所有脚本项目 + var files = Directory.GetFiles(folder, "*.*", + SearchOption.AllDirectories); + + return files.Select(file => new FileInfo(file)).ToList(); + } /// /// 添加任务组 diff --git a/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs index 23b9ea27..68ed57eb 100644 --- a/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs @@ -24,7 +24,7 @@ namespace BetterGenshinImpact.ViewModel.Pages; public partial class KeyMouseRecordPageViewModel : ViewModel { private readonly ILogger _logger = App.GetLogger(); - private readonly string scriptPath = Global.Absolute(@"User\KeyMouseScript"); + public static readonly string ScriptPath = Global.Absolute(@"User\KeyMouseScript"); [ObservableProperty] private ObservableCollection _scriptItems = []; @@ -45,7 +45,7 @@ public partial class KeyMouseRecordPageViewModel : ViewModel private void InitScriptListViewData() { ScriptItems.Clear(); - var fileInfos = LoadScriptFiles(scriptPath) + var fileInfos = LoadScriptFiles(ScriptPath) .OrderByDescending(f => f.CreationTime) .ToList(); foreach (var f in fileInfos) @@ -102,7 +102,7 @@ public partial class KeyMouseRecordPageViewModel : ViewModel { var macro = GlobalKeyMouseRecord.Instance.StopRecord(); // Genshin Copilot Macro - File.WriteAllText(Path.Combine(scriptPath, $"BetterGI_GCM_{DateTime.Now:yyyyMMddHHmmssffff}.json"), macro); + File.WriteAllText(Path.Combine(ScriptPath, $"BetterGI_GCM_{DateTime.Now:yyyyMMddHHmmssffff}.json"), macro); // 刷新ListView InitScriptListViewData(); IsRecording = false; @@ -140,7 +140,7 @@ public partial class KeyMouseRecordPageViewModel : ViewModel [RelayCommand] public void OnOpenScriptFolder() { - Process.Start("explorer.exe", scriptPath); + Process.Start("explorer.exe", ScriptPath); } [RelayCommand] diff --git a/BetterGenshinImpact/ViewModel/Pages/ScriptControlViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/ScriptControlViewModel.cs index e2d9aed7..c496248b 100644 --- a/BetterGenshinImpact/ViewModel/Pages/ScriptControlViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/ScriptControlViewModel.cs @@ -1558,7 +1558,7 @@ public partial class ScriptControlViewModel : ViewModel private List LoadAllKmScripts() { - var folder = Global.Absolute(@"User\KeyMouseScript"); + var folder = KeyMouseRecordPageViewModel.ScriptPath; // 获取所有脚本项目 var files = Directory.GetFiles(folder, "*.*", SearchOption.AllDirectories); @@ -1742,7 +1742,7 @@ public partial class ScriptControlViewModel : ViewModel path = Path.Combine(Global.ScriptPath(), item.FolderName); break; case "KeyMouse": - path = Global.Absolute(@"User\KeyMouseScript"); + path = KeyMouseRecordPageViewModel.ScriptPath; break; case "Pathing": path = Path.Combine(MapPathingViewModel.PathJsonPath, item.FolderName);