diff --git a/BetterGenshinImpact/Core/Script/Dependence/KeyMouseScript.cs b/BetterGenshinImpact/Core/Script/Dependence/KeyMouseScript.cs new file mode 100644 index 00000000..90dd124b --- /dev/null +++ b/BetterGenshinImpact/Core/Script/Dependence/KeyMouseScript.cs @@ -0,0 +1,18 @@ +using BetterGenshinImpact.Core.Recorder; +using System.Threading.Tasks; + +namespace BetterGenshinImpact.Core.Script.Dependence; + +public class KeyMouseScript(string rootPath) +{ + public async Task Run(string json) + { + await KeyMouseMacroPlayer.PlayMacro(json, CancellationContext.Instance.Cts.Token); + } + + public async Task RunFile(string path) + { + var json = await new LimitedFile(rootPath).ReadText(path); + await KeyMouseMacroPlayer.PlayMacro(json, CancellationContext.Instance.Cts.Token); + } +} diff --git a/BetterGenshinImpact/Core/Script/Dependence/LimitedFile.cs b/BetterGenshinImpact/Core/Script/Dependence/LimitedFile.cs index 473daaa7..2c64ad74 100644 --- a/BetterGenshinImpact/Core/Script/Dependence/LimitedFile.cs +++ b/BetterGenshinImpact/Core/Script/Dependence/LimitedFile.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Threading.Tasks; +using BetterGenshinImpact.Core.Script.Utils; namespace BetterGenshinImpact.Core.Script.Dependence; @@ -11,18 +12,7 @@ public class LimitedFile(string rootPath) /// private string NormalizePath(string path) { - // convert to full path relative to root - path = path.Replace('\\', '/'); - var fullPath = Path.GetFullPath(Path.Combine(rootPath, path)); - - // if root is locked, make sure didn't attempt to exit it - if (!fullPath.StartsWith(rootPath)) - { - throw new ArgumentException($"Path '{path}' is not allowed, because its outside the caged root folder!"); - } - - // return full path - return fullPath; + return ScriptUtils.NormalizePath(rootPath, path); } /// diff --git a/BetterGenshinImpact/Core/Script/EngineExtend.cs b/BetterGenshinImpact/Core/Script/EngineExtend.cs index 6f6264d6..5edb6511 100644 --- a/BetterGenshinImpact/Core/Script/EngineExtend.cs +++ b/BetterGenshinImpact/Core/Script/EngineExtend.cs @@ -1,6 +1,5 @@ using BetterGenshinImpact.Core.Script.Dependence; using Microsoft.ClearScript; -using System.Collections.Generic; using System.Threading.Tasks; namespace BetterGenshinImpact.Core.Script; @@ -12,6 +11,7 @@ public class EngineExtend // engine.AddHostObject("xHost", new ExtendedHostFunctions()); // 有越权的安全风险 // 添加我的自定义实例化对象 + engine.AddHostObject("keyMouseScript", new KeyMouseScript(workDir)); engine.AddHostObject("genshin", new Dependence.Genshin()); engine.AddHostObject("log", new Log()); engine.AddHostObject("file", new LimitedFile(workDir)); // 限制文件访问 diff --git a/BetterGenshinImpact/Core/Script/Utils/ScriptUtils.cs b/BetterGenshinImpact/Core/Script/Utils/ScriptUtils.cs new file mode 100644 index 00000000..b5a05d01 --- /dev/null +++ b/BetterGenshinImpact/Core/Script/Utils/ScriptUtils.cs @@ -0,0 +1,26 @@ +using System; +using System.IO; + +namespace BetterGenshinImpact.Core.Script.Utils; + +public class ScriptUtils +{ + /// + /// Normalize and validate a path. + /// + public static string NormalizePath(string root, string path) + { + // convert to full path relative to root + path = path.Replace('\\', '/'); + var fullPath = Path.GetFullPath(Path.Combine(root, path)); + + // if root is locked, make sure didn't attempt to exit it + if (!fullPath.StartsWith(root)) + { + throw new ArgumentException($"Path '{path}' is not allowed, because its outside the caged root folder!"); + } + + // return full path + return fullPath; + } +} diff --git a/BetterGenshinImpact/Script/AutoCrystalfly/main.js b/BetterGenshinImpact/Script/AutoCrystalfly/main.js index cb849b85..994bd2db 100644 --- a/BetterGenshinImpact/Script/AutoCrystalfly/main.js +++ b/BetterGenshinImpact/Script/AutoCrystalfly/main.js @@ -4,6 +4,6 @@ log.info(' {name}', 'TP'); await genshin.tp(3452.310059,2290.465088); log.warn('TP'); - //await sleep(1000); - //await runKeyMouseScript('1.json'); + await sleep(1000); + await keyMouseScript.runFile('1.json'); })(); \ No newline at end of file diff --git a/BetterGenshinImpact/Script/AutoCrystalfly/manifest.json b/BetterGenshinImpact/Script/AutoCrystalfly/manifest.json index a11ee145..ed01a205 100644 --- a/BetterGenshinImpact/Script/AutoCrystalfly/manifest.json +++ b/BetterGenshinImpact/Script/AutoCrystalfly/manifest.json @@ -1,9 +1,9 @@ { "manifest_version": 1, "id": "bgi_crystalfly", - "name": "Զɼ", + "name": "自动采集晶蝶", "version": "1.0", - "description": "BetterGIԴűԶɼ봫͵Ͻľ", + "description": "BetterGI自带脚本,自动采集离传送点较近的晶蝶。", "authors": [ { "name": "huiyadanli", diff --git a/BetterGenshinImpact/ViewModel/Pages/ScriptControlViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/ScriptControlViewModel.cs index 7ca72681..6865a8c3 100644 --- a/BetterGenshinImpact/ViewModel/Pages/ScriptControlViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/ScriptControlViewModel.cs @@ -1,7 +1,6 @@ -using BetterGenshinImpact.Model; +using BetterGenshinImpact.Core.Script.Group; using CommunityToolkit.Mvvm.ComponentModel; using System.Collections.ObjectModel; -using BetterGenshinImpact.Core.Script.Group; using Wpf.Ui.Controls; namespace BetterGenshinImpact.ViewModel.Pages;