From 4cac1b533e133fdc8b3def90a0f998820f51907a Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期三, 26 三月 2025 17:57:59 +0800
Subject: [PATCH] 优化UI,新增日志自动删除;

---
 HMI.Base/PluginLoader.cs |  110 +++++++++++++++++++------------------------------------
 1 files changed, 38 insertions(+), 72 deletions(-)

diff --git a/HMI.Base/PluginLoader.cs b/HMI.Base/PluginLoader.cs
index eef52ba..e7be956 100644
--- a/HMI.Base/PluginLoader.cs
+++ b/HMI.Base/PluginLoader.cs
@@ -44,6 +44,14 @@
             sqliteHelper = new SqliteHelper(PathHelper.DatabasePath);
         }
 
+        public static void Reset()
+        {
+            if (Instance != null)
+            {
+                Instance = null;
+            }
+        }
+
         public static PluginLoader GetLoader()
         {
             if (Instance == null)
@@ -53,79 +61,29 @@
             return Instance;
         }
 
-        public void LoadMenuAndPlugin(Dispatcher dispatcher)
+        private IPluginContainer pluginContainer;
+        public void LoadMenuAndPlugin(IPluginContainer container, Dispatcher dispatcher)
         {
+            pluginContainer = container;
             if (Menus == null)
             {
                 Plugins = new List<PluginInfo>();
                 Menus = LoadTreeMenu(dispatcher);
+                sqliteHelper.Close();
                 dispatcher.Invoke(() =>
                 {
                     LoadMessageAppend?.Invoke(this, new LoadMsg { Type = LoadMsgType.FINISHED });
                 });
             }
         }
-
-        private void LoadChildrenMenu(PluginInfo menu, Dispatcher dispatcher)
-        {
-            LoadMessageAppend?.Invoke(this, new LoadMsg
-            {
-                Type = LoadMsgType.LOADING,
-                Message = $"姝e湪鍔犺浇{menu.Name}鑿滃崟..."
-            });
-            try
-            {
-                Application.Current.Dispatcher.Invoke(() =>
-                {
-                    if (!string.IsNullOrEmpty(menu.DllPath))
-                    {
-                        string dllPath = Path.Combine(PluginFolder, menu.DllPath);
-                        Assembly ass = Assembly.LoadFrom(dllPath);
-                        Type ClassType = ass.GetType(menu.ClassName);
-                        object Instance = Activator.CreateInstance(ClassType);
-                        menu.Content = Instance;
-                        if (Instance is IPlugin plugin)
-                        {
-                            plugin.SetLogger(LogManager.GetLogger(menu.Code));
-                            string configFolder = Path.Combine(PathHelper.ConfigFolder, menu.Code);
-                            if (!Directory.Exists(configFolder))
-                            {
-                                Directory.CreateDirectory(configFolder);
-                            }
-                            plugin.SetBaseFolder(configFolder);
-                            plugin.InitFinish();
-                        }
-                        Type SettingType = ass.GetType($"{menu.ClassName}_Setting");
-                        if (SettingType != null)
-                        {
-                            object SettingInstance = Activator.CreateInstance(SettingType);
-                            menu.SettingControl = SettingInstance;
-                        }
-                    }
-                });
-                Thread.Sleep(100);
-            }
-            catch (Exception ex)
-            {
-                LoadMessageAppend?.Invoke(this,
-                     new LoadMsg
-                     {
-                         Type = LoadMsgType.LOADING,
-                         Result = false,
-                         Message = $"{ex.Message} {ex.StackTrace}"
-                     });
-            }
-        }
-
         private void LoadChildrenMenu()
         {
             foreach (var menu in Plugins)
             {
                 CountdownEvent c = new CountdownEvent(1);
-                Thread.Sleep(100);
+                Thread.Sleep(80);
                 Application.Current.Dispatcher.Invoke(new Action(() =>
                 {
-
                     try
                     {
                         LoadMessageAppend?.Invoke(this,
@@ -139,29 +97,36 @@
                         {
                             string dllPath = Path.Combine(PluginFolder, menu.DllPath);
                             Assembly ass = Assembly.LoadFrom(dllPath);
+                            var tss = ass.GetTypes();
                             Type ClassType = ass.GetType(menu.ClassName);
                             object Instance = Activator.CreateInstance(ClassType);
                             menu.Content = Instance;
                             if (Instance is IPlugin plugin)
                             {
-                                plugin.SetLogger(LogManager.GetLogger(menu.Code));
-                                string configFolder = Path.Combine(PathHelper.ConfigFolder, menu.Code);
-                                if (!Directory.Exists(configFolder))
+                            }
+                            else
+                            {
+                                plugin = new PluginPackage(Instance);
+                            }
+                            plugin.SetLogger(LogManager.GetLogger(menu.Code));
+                            string configFolder = Path.Combine(PathHelper.ConfigFolder, menu.Code);
+                            if (!Directory.Exists(configFolder))
+                            {
+                                Directory.CreateDirectory(configFolder);
+                            }
+                            plugin.SetBaseFolder(configFolder);
+                            plugin.InitFinish();
+                            pluginContainer.OnCloseEvent += plugin.OnClose;
+                            pluginContainer.DoCloseEvent += plugin.DoClose;
+                            Type SettingType = ass.GetType($"{menu.ClassName}_Setting");
+                            if (SettingType != null)
+                            {
+                                object SettingInstance = Activator.CreateInstance(SettingType);
+                                if (SettingInstance is IFolderOwner fo)
                                 {
-                                    Directory.CreateDirectory(configFolder);
+                                    fo.SetBaseFolder(configFolder);
                                 }
-                                plugin.SetBaseFolder(configFolder);
-                                plugin.InitFinish();
-                                Type SettingType = ass.GetType($"{menu.ClassName}_Setting");
-                                if (SettingType != null)
-                                {
-                                    object SettingInstance = Activator.CreateInstance(SettingType);
-                                    if (SettingInstance is IFolderOwner fo)
-                                    {
-                                        fo.SetBaseFolder(configFolder);
-                                    }
-                                    menu.SettingControl = SettingInstance;
-                                }
+                                menu.SettingControl = SettingInstance;
                             }
                         }
                         LoadMessageAppend?.Invoke(this,
@@ -202,7 +167,7 @@
                 string sql = $@"SELECT m.* FROM menu AS m INNER JOIN role_menu_rel AS rrel ON m.code = rrel.menuCode
 INNER JOIN role AS r ON rrel.roleCode = r.code
 INNER JOIN user_role_rel AS urel ON r.code = urel.roleCode
-AND urel.username = '{user.Username}' ORDER BY m.level, m.id;";
+AND urel.username = '{user.Username}' ORDER BY m.level,m.sort, m.id;";
 
                 DataTable dt = sqliteHelper.GetDataTable(sql);
                 if (dt != null)
@@ -224,6 +189,7 @@
                             {
                                 Code = dr["code"].ToString(),
                                 Name = dr["name"].ToString(),
+                                Type = dr["type"].ToString(),
                                 ParentCode = dr["parentCode"].ToString(),
                                 DllPath = dr["dllPath"].ToString(),
                                 ClassName = dr["className"].ToString(),

--
Gitblit v1.9.1