wenxiangchao
2026-11-02 fe2aa4880d5c9fd88088ad0ed81ebaca05629f5f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using HMI.Core.Api;
using HMI.Core.Entity;
using HMI.Core.Util;
using log4net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
 
namespace HMI.Base
{
    public class PluginLoader
    {
        private static ILog log = LogManager.GetLogger("PluginLoader");
        private static string PluginFolder = PathHelper.PluginFolder();
 
        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<LoadMsg> LoadMessageAppend;
 
        private PluginLoader()
        {
            sqliteHelper = new SqliteHelper(PathHelper.DatabasePath);
        }
 
        public static void Reset()
        {
            if (Instance != null)
            {
                Instance = null;
            }
            GC.Collect();
        }
 
        public static PluginLoader GetLoader()
        {
            if (Instance == null)
            {
                Instance = new PluginLoader();
            }
            return Instance;
        }
 
        private IPluginContainer pluginContainer;
 
        public void LoadMenuAndPlugin(IPluginContainer container, Dispatcher dispatcher)
        {
            pluginContainer = container;
            if (Menus == null)
            {
                Plugins = new List<PluginInfo>();
                Menus = LoadTreeMenu(dispatcher);
                sqliteHelper.Close();
                LoadMessageAppend?.Invoke(this, new LoadMsg { Type = LoadMsgType.FINISHED });
            }
            pluginContainer.DoCloseEvent += PluginContainer_DoCloseEvent;
        }
 
        private void PluginContainer_DoCloseEvent(object sender, EventArgs e)
        {
            Reset();
        }
 
        private void LoadChildrenMenu()
        {
            foreach (var menu in Plugins)
            {
                CountdownEvent c = new CountdownEvent(1);
                Thread.Sleep(50);
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    try
                    {
                        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))
                            {
                                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)
                                {
                                    fo.SetBaseFolder(configFolder);
                                }
                                menu.SettingControl = SettingInstance;
                            }
                        }
                        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)
                    {
                        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
                    {
                        c.Signal();
                    }
                }));
                c.Wait();
            }
        }
 
        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)
        {
            List<PluginInfo> menus = new List<PluginInfo>();
            Dictionary<string, PluginInfo> cache = new Dictionary<string, PluginInfo>();
            try
            {
                List<PermissionData> allPermissions = UserInfo.User?.AllPermissions;
                if (allPermissions != null)
                {
                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        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)
                        {
                            if (data.Type == "Permission") continue;
                            var menu = new PluginInfo
                            {
                                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))
                            {
                                menus.Add(menu);
                            }
                            else
                            {
                                cache[menu.ParentCode].Children.Add(menu);
                            }
                            Plugins.Add(menu);
                        }
                    }));
                    LoadChildrenMenu();
                }
            }
            catch (Exception ex)
            {
                //LoadMessageAppend?.Invoke(this, ex.Message);
            }
            return menus;
        }
    }
}