From b2b2c544c06874ce307bee780689b904f6fa5660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9C=A8=E6=A3=AE=20=C2=B7=20=E4=BD=9C=E9=9C=96?= <16236903+NMSAzulX@users.noreply.github.com> Date: Mon, 8 Jul 2024 23:58:01 +0800 Subject: [PATCH] =?UTF-8?q?update=20he=20=20=E4=BC=98=E5=8C=96=20winform?= =?UTF-8?q?=20=E7=83=AD=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HotExecutorGenerator.cs | 10 ++- .../Natasha.CSharp.HotExecutor/HEProxy.cs | 9 +- .../Component/Logger/HEFileLogger.cs | 2 +- .../BodyRewriter/WinformRewriter.cs | 88 +++++++++++++++---- .../ReCompile/HECompiler.cs | 1 - .../ReCompile/VSCSMainProjectWatcher.cs | 19 ++-- 6 files changed, 93 insertions(+), 36 deletions(-) diff --git a/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor.SG/HotExecutorGenerator.cs b/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor.SG/HotExecutorGenerator.cs index f63873ab..154466fc 100644 --- a/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor.SG/HotExecutorGenerator.cs +++ b/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor.SG/HotExecutorGenerator.cs @@ -18,10 +18,10 @@ void ISourceGenerator.Execute(GeneratorExecutionContext context) string coreScript = "HEProxy.SetProjectKind(HEProjectKind.Console);"; string winformAndwpfLoggerScript = @" -string debugFilePath = Path.Combine(VSCSharpProjectInfomation.HEOutputPath,""Debug.txt""); -if (Directory.Exists(VSCSharpProjectInfomation.HEOutputPath)) +string debugFilePath = Path.Combine(VSCSProjectInfoHelper.HEOutputPath,""Debug.txt""); +if (Directory.Exists(VSCSProjectInfoHelper.HEOutputPath)) { - var files = Directory.GetFiles(VSCSharpProjectInfomation.HEOutputPath); + var files = Directory.GetFiles(VSCSProjectInfoHelper.HEOutputPath); foreach (var file in files) { File.Delete(file); @@ -51,7 +51,7 @@ void ISourceGenerator.Execute(GeneratorExecutionContext context) HEProxy.SetProjectKind(HEProjectKind.Winform); HEProxy.ExcludeGlobalUsing(""System.Windows.Controls""); HEProxy.ExcludeGlobalUsing(""System.Windows""); -DelegateHelper.GetDelegate(""Remove"", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); +HEDelegateHelper.GetDelegate(""Remove"", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); "; } else @@ -75,6 +75,7 @@ void ISourceGenerator.Execute(GeneratorExecutionContext context) string proxyMethodContent = $@" //#if DEBUG using System.IO; +using System.Diagnostics; using System.Runtime.CompilerServices; using Natasha.CSharp.HotExecutor.Component; using Natasha.CSharp.Extension.HotExecutor; @@ -91,6 +92,7 @@ internal static void PreMain() {coreScript} HEProxy.SetCompileInitAction(()=>{{ + NatashaManagement.RegistDomainCreator(); NatashaManagement.Preheating((asmName, @namespace) => !string.IsNullOrWhiteSpace(@namespace) && (@namespace.StartsWith(""Microsoft.VisualBasic"")|| HEProxy.IsExcluded(@namespace)),true, true); diff --git a/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/HEProxy.cs b/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/HEProxy.cs index 70919e53..0d788c9b 100644 --- a/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/HEProxy.cs +++ b/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/HEProxy.cs @@ -12,6 +12,8 @@ public static class HEProxy { + public static bool IsRuntimeFirstRun = true; + public static object? ObjectInstance; public static Assembly CurrentAssembly = default!; private static string _argumentsMethodName = "ProxyMainArguments"; private static readonly ConcurrentDictionary _fileSyntaxTreeCache = new(); @@ -237,6 +239,7 @@ public static void Run() _cs0104UsingCache[file] = _cs0104TriviaPlugin.ExcludeUsings; //从默认Using缓存中排除 CS0104 root = UsingsHandler.Handle(root, _cs0104UsingCache); + ShowMessage(root.ToFullString()); return CSharpSyntaxTree.Create(root, _currentOptions, file, Encoding.UTF8); } if (root != reBuildRoot) @@ -342,6 +345,7 @@ private static async Task HotExecute() instance = Activator.CreateInstance(typeInfo); } + ShowMessage($"执行主入口回调方法...."); if (_asyncTriviaPlugin.IsAsync) { Task mainTask; @@ -377,9 +381,9 @@ private static async Task HotExecute() } - ShowMessage($"执行主入口回调方法...."); + ShowMessage($"执行入口回调方法...."); _endCallback?.Invoke(); - ShowMessage($"入口回调方法执行完毕."); + ShowMessage($"执行系列方法执行完毕."); } catch (Exception ex) @@ -456,6 +460,7 @@ private async static Task ReAnalysisFiles() public static void SetProjectKind(HEProjectKind kind) { + _proxyMethodPlugin.SetProjectKind(kind); _projectKind = kind; } diff --git a/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/ReCompile/Component/Logger/HEFileLogger.cs b/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/ReCompile/Component/Logger/HEFileLogger.cs index 9110861c..4a686a0a 100644 --- a/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/ReCompile/Component/Logger/HEFileLogger.cs +++ b/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/ReCompile/Component/Logger/HEFileLogger.cs @@ -33,9 +33,9 @@ public async Task WriteUtf8FileAsync(string msg) { await _writer.WriteLineAsync(msg); isWritten = true; - _fileLock.ReleaseLock(); await _writer.FlushAsync(); } + _fileLock.ReleaseLock(); return; } catch (Exception) diff --git a/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/ReCompile/Component/SyntaxRewriter/SyntaxPlugin/ProxyMainPlugin/BodyRewriter/WinformRewriter.cs b/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/ReCompile/Component/SyntaxRewriter/SyntaxPlugin/ProxyMainPlugin/BodyRewriter/WinformRewriter.cs index 2406ef8d..15bb15c3 100644 --- a/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/ReCompile/Component/SyntaxRewriter/SyntaxPlugin/ProxyMainPlugin/BodyRewriter/WinformRewriter.cs +++ b/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/ReCompile/Component/SyntaxRewriter/SyntaxPlugin/ProxyMainPlugin/BodyRewriter/WinformRewriter.cs @@ -50,7 +50,6 @@ internal static class WinformRewriter System.Threading.Tasks.Task.Run(() => {{ System.Windows.Forms.Application.ExitThread(); - while(!DiposeWindows()){{}}; var __heProxInstance = {runArgumentScript}; System.Windows.Forms.Form tempForm; if (__heProxInstance is System.Windows.Forms.Form) @@ -60,7 +59,9 @@ internal static class WinformRewriter else {{ tempForm = (System.Windows.Forms.Form)(typeof(System.Windows.Forms.ApplicationContext).GetProperty(""MainForm"")!.GetValue(__heProxInstance)!); - }} + }} + System.Diagnostics.Debug.WriteLine($""当前主窗体程序集为 {{tempForm.GetType().Assembly.FullName}}!""); + System.Diagnostics.Debug.WriteLine($""当前主窗体为 {{tempForm.Name}}!""); tempForm.FormClosed += (s, e) => {{ if (!HEProxy.IsHotCompiling) @@ -72,35 +73,88 @@ internal static class WinformRewriter }} }} }}; - + Action? removeHandler = null; + System.EventHandler handler = (s, e) => + {{ + while(!DiposeWindows(tempForm)){{}}; + removeHandler?.Invoke(); + + }}; + removeHandler = () => {{ + + tempForm.Shown -= handler; + + }}; + + tempForm.Shown += handler; try{{ + System.Diagnostics.Debug.WriteLine(tempForm.button4.Text); System.Windows.Forms.Application.Run(__heProxInstance); }}catch(System.Exception ex) {{ - HEProxy.ShowMessage(ex.Message); + System.Diagnostics.Debug.WriteLine(""启动出错"" + ex.Message); }} - - static bool DiposeWindows() + + static bool DiposeWindows(System.Windows.Forms.Form aliveForm) {{ - try{{ - for (int i = 0; i < System.Windows.Forms.Application.OpenForms.Count; i++) + System.Collections.Generic.HashSet forms = []; + System.Collections.Generic.HashSet removeForms = []; + if(HEProxy.ObjectInstance!=null) {{ - try{{ + forms.Add((System.Windows.Forms.Form)HEProxy.ObjectInstance); + }} + forms.Add(aliveForm); + System.Diagnostics.Debug.WriteLine(System.Windows.Forms.Application.OpenForms.Count); + + int i = 0; + do + {{ + try + {{ + var form = System.Windows.Forms.Application.OpenForms[i]; - if (form!=null) + if(forms.Contains(form)) {{ - HEProxy.ShowMessage($""当前将被注销的开放窗体 {{form.Name}}""); - form.Dispose(); - Natasha.CSharp.HotExecutor.Component.DelegateHelper.Execute.Invoke(System.Windows.Forms.Application.OpenForms, form); + i+=1; + continue; }} - }}catch{{ + + if (form != null && form != aliveForm) + {{ + + if(HEProxy.ObjectInstance==null){{ + HEProxy.ObjectInstance = form; + form.BeginInvoke(new Action(() => {{ + System.Diagnostics.Debug.WriteLine($""当前将被隐藏的开放窗体 {{form.Name}}!""); + form.Hide(); + removeForms.Add(form); + }})); + forms.Add(form); + }}else {{ + form.BeginInvoke(new Action(() => {{ + System.Diagnostics.Debug.WriteLine($""当前将被注销的开放窗体 {{form.Name}}!""); + form.Dispose(); + removeForms.Add(form); + }})); + forms.Add(form); + }} + }} + }} - }} - }}catch{{ - return false; + catch (System.Exception ex) + {{ + System.Diagnostics.Debug.WriteLine($""注销窗体出错 {{ex.Message}}!""); + return false; + }} + }} while (System.Windows.Forms.Application.OpenForms.Count > i); + + foreach(var removeForm in removeForms) + {{ + Natasha.CSharp.HotExecutor.Component.HEDelegateHelper.Execute.Invoke(System.Windows.Forms.Application.OpenForms, removeForm); }} + return true; }} }}); diff --git a/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/ReCompile/HECompiler.cs b/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/ReCompile/HECompiler.cs index 3ac4fb04..451b85f9 100644 --- a/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/ReCompile/HECompiler.cs +++ b/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/ReCompile/HECompiler.cs @@ -29,7 +29,6 @@ static HECompiler() CompilerBinderFlags.SuppressObsoleteChecks)); _builderCache .UseSmartMode() - .WithFileOutput() .WithoutSemanticCheck() .WithPreCompilationOptions() .WithoutPreCompilationReferences() diff --git a/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/ReCompile/VSCSMainProjectWatcher.cs b/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/ReCompile/VSCSMainProjectWatcher.cs index bd1a9505..c2fa88ed 100644 --- a/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/ReCompile/VSCSMainProjectWatcher.cs +++ b/src/Natasha.CSharp/Extension/HotExecutor/Natasha.CSharp.HotExecutor/ReCompile/VSCSMainProjectWatcher.cs @@ -98,6 +98,9 @@ public void DeployMonitor() _mainWatcher.Renamed += async (sender, e) => { +#if DEBUG + HEProxy.ShowMessage($"Renamed: {e.OldFullPath} -> {e.FullPath}"); +#endif if (PreFunction()) { return; @@ -106,13 +109,10 @@ public void DeployMonitor() { await ExecuteAfterFunction(); - }else if (e.OldFullPath.EndsWith(".cs")) + }else if (e.FullPath.EndsWith(".cs")) { - if (e.FullPath.EndsWith(".cs")) + if (e.OldFullPath.EndsWith(".cs")) { -#if DEBUG - HEProxy.ShowMessage($"Renamed: {e.OldFullPath} -> {e.FullPath}"); -#endif _compileLock.GetAndWaitLock(); if (VSCSProjectInfoHelper.CheckFileAvailiable(e.FullPath)) { @@ -125,15 +125,12 @@ public void DeployMonitor() _compileLock.ReleaseLock(); await ExecuteAfterFunction(); } - else if (e.FullPath.StartsWith(e.OldFullPath) && e.FullPath.EndsWith(".TMP")) + else { -#if DEBUG - HEProxy.ShowMessage($"Renamed: {e.OldFullPath} -> {e.FullPath}"); -#endif - if (VSCSProjectInfoHelper.CheckFileAvailiable(e.OldFullPath)) + if (VSCSProjectInfoHelper.CheckFileAvailiable(e.FullPath)) { _compileLock.GetAndWaitLock(); - ChangeFileAction(e.OldFullPath); + ChangeFileAction(e.FullPath); _compileLock.ReleaseLock(); await ExecuteAfterFunction(); }