alielie
2025-03-17 c401a2f2fd1a509cdeb33e850bb5c2827074af48
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 = $"正在加载{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,
@@ -152,6 +110,8 @@
                                }
                                plugin.SetBaseFolder(configFolder);
                                plugin.InitFinish();
                                pluginContainer.OnCloseEvent += plugin.OnClose;
                                pluginContainer.DoCloseEvent += plugin.DoClose;
                                Type SettingType = ass.GetType($"{menu.ClassName}_Setting");
                                if (SettingType != null)
                                {