From 9d1660270272180cd71147d6ddd53d1382643d2e Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 02 十一月 2026 18:00:14 +0800
Subject: [PATCH] merge: 并入远程 Initial commit 作为历史根节点

---
 HMI.Base/PluginLoader.cs |  225 +++++++++++++++++++++++++++++++------------------------
 1 files changed, 126 insertions(+), 99 deletions(-)

diff --git a/HMI.Base/PluginLoader.cs b/HMI.Base/PluginLoader.cs
index fc082bd..81e1f84 100644
--- a/HMI.Base/PluginLoader.cs
+++ b/HMI.Base/PluginLoader.cs
@@ -4,33 +4,38 @@
 using log4net;
 using System;
 using System.Collections.Generic;
-using System.Data;
 using System.IO;
 using System.Reflection;
 using System.Threading;
-using System.Threading.Tasks;
 using System.Windows;
-using System.Windows.Media.Imaging;
 using System.Windows.Threading;
 
 namespace HMI.Base
 {
     public class PluginLoader
     {
+        private static ILog log = LogManager.GetLogger("PluginLoader");
         private static string PluginFolder = PathHelper.PluginFolder();
 
-        public bool Completed { get; private set; } = false;
         public SqliteHelper sqliteHelper;
         public List<PluginInfo> Menus { get; private set; }
         public List<PluginInfo> Plugins { get; private set; }
         public static PluginLoader Instance { get; private set; }
 
-        public event EventHandler<string> LoadMessageAppend;
-        public event EventHandler Loaded;
+        public event EventHandler<LoadMsg> LoadMessageAppend;
 
         private PluginLoader()
         {
             sqliteHelper = new SqliteHelper(PathHelper.DatabasePath);
+        }
+
+        public static void Reset()
+        {
+            if (Instance != null)
+            {
+                Instance = null;
+            }
+            GC.Collect();
         }
 
         public static PluginLoader GetLoader()
@@ -42,32 +47,61 @@
             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);
-                Loaded?.Invoke(this, null);
+                sqliteHelper.Close();
+                LoadMessageAppend?.Invoke(this, new LoadMsg { Type = LoadMsgType.FINISHED });
             }
+            pluginContainer.DoCloseEvent += PluginContainer_DoCloseEvent;
         }
 
-        private void LoadChildrenMenu(PluginInfo menu, Dispatcher dispatcher)
+        private void PluginContainer_DoCloseEvent(object sender, EventArgs e)
         {
-            LoadMessageAppend?.Invoke(this, $"姝e湪鍔犺浇{menu.Name}鑿滃崟...");
-            try
+            Reset();
+        }
+
+        private void LoadChildrenMenu()
+        {
+            foreach (var menu in Plugins)
             {
-                Application.Current.Dispatcher.Invoke(() =>
+                CountdownEvent c = new CountdownEvent(1);
+                Thread.Sleep(50);
+                Application.Current.Dispatcher.Invoke(new Action(() =>
                 {
-                    if (!string.IsNullOrEmpty(menu.DllPath))
+                    try
                     {
-                        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)
+                        var start = DateTime.Now;
+                        log.Info(this.lang("App.IsLoadingMenu", menu.Name));
+                        LoadMessageAppend?.Invoke(this,
+                            new LoadMsg
+                            {
+                                Type = LoadMsgType.LOADING,
+                                Message = this.lang("App.IsLoadingMenu", menu.Name)
+                            }
+                        );
+
+                        if (!string.IsNullOrEmpty(menu.DllPath))
                         {
+                            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)
+                            {
+                            }
+                            else
+                            {
+                                plugin = new PluginPackage(Instance);
+                            }
                             plugin.SetLogger(LogManager.GetLogger(menu.Code));
                             string configFolder = Path.Combine(PathHelper.ConfigFolder, menu.Code);
                             if (!Directory.Exists(configFolder))
@@ -76,69 +110,37 @@
                             }
                             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, $"{ex.Message} {ex.StackTrace}");
-            }
-        }
-
-        private void LoadChildrenMenu()
-        {
-            foreach (var menu in Plugins)
-            {
-                CountdownEvent c = new CountdownEvent(1);
-                Thread.Sleep(10);
-                Application.Current.Dispatcher.Invoke(new Action(() =>
-                {
-
-                    try
-                    {
-                        LoadMessageAppend?.Invoke(this, $"姝e湪鍔犺浇{menu.Name}鑿滃崟...");
-                        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)
+                            pluginContainer.OnCloseEvent += plugin.OnClose;
+                            pluginContainer.DoCloseEvent += plugin.DoClose;
+                            Type SettingType = ass.GetType($"{menu.ClassName}_Setting");
+                            if (SettingType != null)
                             {
-                                plugin.SetLogger(LogManager.GetLogger(menu.Code));
-                                string configFolder = Path.Combine(PathHelper.ConfigFolder, menu.Code);
-                                if (!Directory.Exists(configFolder))
+                                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, $"鍔犺浇瀹�");
+                        log.Info($"{this.lang("App.MenuLoadSuccess", menu.Name)} Cost: {(DateTime.Now - start).TotalMilliseconds}ms");
+                        LoadMessageAppend?.Invoke(this,
+                            new LoadMsg
+                            {
+                                Type = LoadMsgType.LOADING,
+                                Result = true,
+                                Message = this.lang("App.MenuLoadSuccess", menu.Name),
+                            });
                     }
                     catch (Exception ex)
                     {
-                        LoadMessageAppend?.Invoke(this, $"{ex.Message} {ex.StackTrace}");
+                        log.Error($"Load Plugin [{menu.Name}] Failed.", ex);
+                        LoadMessageAppend?.Invoke(this, new LoadMsg
+                        {
+                            Type = LoadMsgType.LOADING,
+                            Result = false,
+                            Message = this.lang("App.MenuLoadFailed", menu.Name, ex.Message, ex.StackTrace)
+                        });
                     }
                     finally
                     {
@@ -149,42 +151,68 @@
             }
         }
 
+        private PluginInfo ConvertPermission2PluginInfo(PermissionData data)
+        {
+            if (data.Type == "鏉冮檺") return null;
+            PluginInfo info = new PluginInfo
+            {
+                Code = data.Code,
+                Name = data.Name,
+                Type = data.Type,
+                ParentCode = data.ParentCode,
+                DllPath = data.DllPath,
+                ClassName = data.ClassName,
+                IconFont = data.IconFont
+                //Icon = new BitmapImage(new Uri(Path.Combine(PathHelper.IconFolder, dr["iconPath"].ToString())))
+            };
+            if (data.Children != null && data.Children.Count > 0)
+            {
+                foreach (var child in data.Children)
+                {
+                    var childInfo = ConvertPermission2PluginInfo(child);
+                    if (childInfo != null) info.Children.Add(childInfo);
+                }
+            }
+            return info;
+        }
+
         public List<PluginInfo> LoadTreeMenu(Dispatcher dispatcher)
         {
-            //LoadMessageAppend?.Invoke(this, "寮�濮嬪姞杞借彍鍗�...");
-            List<PluginInfo> data = new List<PluginInfo>();
+            List<PluginInfo> menus = new List<PluginInfo>();
             Dictionary<string, PluginInfo> cache = new Dictionary<string, PluginInfo>();
             try
             {
-                UserInfo user = UserInfo.User;
-                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;";
-
-                DataTable dt = sqliteHelper.GetDataTable(sql);
-                if (dt != null)
+                List<PermissionData> allPermissions = UserInfo.User?.AllPermissions;
+                if (allPermissions != null)
                 {
-                    //LoadMessageAppend?.Invoke(this, $"鍏辨湁{dt.Rows.Count}鏉¤彍鍗曞緟鍔犺浇...");
-                    Application.Current.Dispatcher.Invoke(() =>
+                    Application.Current.Dispatcher.Invoke(new Action(() =>
                     {
-                        foreach (DataRow dr in dt.Rows)
+                        log.Info(this.lang("App.MenuCount", allPermissions.Count));
+                        LoadMessageAppend?.Invoke(this,
+                          new LoadMsg
+                          {
+                              Type = LoadMsgType.INIT,
+                              NumData = allPermissions.Count,
+                              Message = this.lang("App.MenuCount", allPermissions.Count),
+                          });
+
+                        foreach (PermissionData data in allPermissions)
                         {
-                            string path = Path.Combine(PathHelper.IconFolder, dr["iconPath"].ToString());
+                            if (data.Type == "Permission") continue;
                             var menu = new PluginInfo
                             {
-                                Code = dr["code"].ToString(),
-                                Name = dr["name"].ToString(),
-                                ParentCode = dr["parentCode"].ToString(),
-                                DllPath = dr["dllPath"].ToString(),
-                                ClassName = dr["className"].ToString(),
-                                IconFont = dr["icon"].ToString()
-                                //Icon = new BitmapImage(new Uri(Path.Combine(PathHelper.IconFolder, dr["iconPath"].ToString())))
+                                Code = data.Code,
+                                Name = data.Name,
+                                Type = data.Type,
+                                ParentCode = data.ParentCode,
+                                DllPath = data.DllPath,
+                                ClassName = data.ClassName,
+                                IconFont = data.IconFont
                             };
                             cache[menu.Code] = menu;
                             if (!cache.ContainsKey(menu.ParentCode))
                             {
-                                data.Add(menu);
+                                menus.Add(menu);
                             }
                             else
                             {
@@ -192,7 +220,7 @@
                             }
                             Plugins.Add(menu);
                         }
-                    });
+                    }));
                     LoadChildrenMenu();
                 }
             }
@@ -200,8 +228,7 @@
             {
                 //LoadMessageAppend?.Invoke(this, ex.Message);
             }
-            return data;
+            return menus;
         }
-
     }
 }

--
Gitblit v1.9.1