diff --git a/BetterGenshinImpact/App.xaml.cs b/BetterGenshinImpact/App.xaml.cs index fcb4df7c..f89baf57 100644 --- a/BetterGenshinImpact/App.xaml.cs +++ b/BetterGenshinImpact/App.xaml.cs @@ -282,6 +282,12 @@ public partial class App : Application { try { + // 忽略V8引擎释放后pending的Task回调抛出的异常 + if (IsV8EngineReleasedException(e.Exception)) + { + return; + } + HandleException(e.Exception); } catch (Exception ex) @@ -294,6 +300,21 @@ public partial class App : Application } } + private static bool IsV8EngineReleasedException(Exception? ex) + { + while (ex != null) + { + if (ex.Message?.Contains("V8 object has been released") == true) + { + return true; + } + + ex = ex.InnerException; + } + + return false; + } + //非UI线程未捕获异常处理事件(例如自己创建的一个子线程) private static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) { diff --git a/BetterGenshinImpact/Core/Script/Project/ScriptProject.cs b/BetterGenshinImpact/Core/Script/Project/ScriptProject.cs index c37b78e8..c32b6297 100644 --- a/BetterGenshinImpact/Core/Script/Project/ScriptProject.cs +++ b/BetterGenshinImpact/Core/Script/Project/ScriptProject.cs @@ -10,8 +10,10 @@ using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using BetterGenshinImpact.Core.Script.Dependence; +using BetterGenshinImpact.GameTask.Common; using BetterGenshinImpact.View; using Microsoft.ClearScript.JavaScript; +using Microsoft.Extensions.Logging; namespace BetterGenshinImpact.Core.Script.Project; @@ -32,6 +34,7 @@ public class ScriptProject { throw new DirectoryNotFoundException("脚本文件夹不存在:" + ProjectPath); } + ManifestFile = Path.GetFullPath(Path.Combine(ProjectPath, "manifest.json")); if (!File.Exists(ManifestFile)) { @@ -49,6 +52,7 @@ public class ScriptProject { return null; } + var stackPanel = new StackPanel { Margin = new Thickness(0, 0, 20, 0) // 给右侧滚动条留出位置 @@ -75,7 +79,7 @@ public class ScriptProject private IScriptEngine BuildScriptEngine(PathingPartyConfig? partyConfig) { V8ScriptEngine engine = new V8ScriptEngine(V8ScriptEngineFlags.UseCaseInsensitiveMemberBinding | V8ScriptEngineFlags.EnableTaskPromiseConversion); - + // packages 依赖和资源重载 var loader = new PackageDocumentLoader(ProjectPath); engine.DocumentSettings.Loader = loader; @@ -93,14 +97,14 @@ public class ScriptProject return engine; } - public async Task ExecuteAsync(dynamic? context = null, PathingPartyConfig? partyConfig=null) + public async Task ExecuteAsync(dynamic? context = null, PathingPartyConfig? partyConfig = null) { // 默认值 GlobalMethod.SetGameMetrics(1920, 1080); // 加载代码 var code = await LoadCode(); var engine = BuildScriptEngine(partyConfig); - + // 使用自定义加载器解析脚本文件 var loader = (PackageDocumentLoader)engine.DocumentSettings.Loader; @@ -109,10 +113,11 @@ public class ScriptProject // 写入配置的内容 engine.AddHostObject("settings", context); } + try { - bool useModule = Manifest.Library.Length != 0 || - code.Contains("import ", StringComparison.Ordinal) || + bool useModule = Manifest.Library.Length != 0 || + code.Contains("import ", StringComparison.Ordinal) || code.Contains("export ", StringComparison.Ordinal); if (useModule) @@ -140,7 +145,16 @@ public class ScriptProject } finally { - HtmlMaskWindow.CloseAll(); + // 终止代码执行 + try + { + engine.Interrupt(); + } + catch (Exception e) + { + TaskControl.Logger.LogError(e, "中断脚本执行异常:" + e.Message); + } + engine.Dispose(); } } @@ -155,4 +169,4 @@ public class ScriptProject return code; } -} +} \ No newline at end of file