alielie
2025-03-04 2b1a6417285597bc58ef28aa3f01aeee2532cd19
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.IO;
using System.Reflection;
using System.Windows.Media.Imaging;
using HMI.Core.Api;
using HMI.Core.Util;
using log4net;
 
namespace HMI.Base
{
    public class PluginLoader
    {
        private static string PluginFolder = PathHelper.PluginFolder();
 
        public bool Completed { get; private set; } = false;
        public SqliteHelper sqliteHelper;
        public ObservableCollection<PluginInfo> Menus { get; private set; }
        public ObservableCollection<PluginInfo> Plugins { get; private set; }
 
        public static PluginLoader Instance { get; private set; }
 
        public event EventHandler<string> LoadMessageAppend;
 
        private PluginLoader()
        {
            sqliteHelper = new SqliteHelper(PathHelper.DatabasePath);
        }
 
        public static PluginLoader GetLoader()
        {
            if (Instance == null)
            {
                Instance = new PluginLoader();
            }
            return Instance;
        }
 
        public void LoadMenuAndPlugin()
        {
            if (Menus == null)
            {
                Plugins = new ObservableCollection<PluginInfo>();
                Menus = LoadTreeMenu();
            }
        }
 
        private void LoadChildrenMenu(PluginInfo menu)
        {
            LoadMessageAppend?.Invoke(this, $"正在加载{menu.Name}菜单...");
            try
            {
                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;
                    }
                }
            }
            catch (Exception ex)
            {
                LoadMessageAppend?.Invoke(this, $"{ex.Message} {ex.StackTrace}");
            }
        }
 
        public ObservableCollection<PluginInfo> LoadTreeMenu()
        {
            ObservableCollection<PluginInfo> data = new ObservableCollection<PluginInfo>();
            Dictionary<string, PluginInfo> cache = new Dictionary<string, PluginInfo>();
            try
            {
                LoadMessageAppend?.Invoke(this, "开始加载菜单...");
                DataTable dt = sqliteHelper.GetDataTable(@"SELECT * FROM menu ORDER BY level,id;");
                if (dt != null)
                {
                    LoadMessageAppend?.Invoke(this, $"共有{dt.Rows.Count}条菜单待加载...");
                    foreach (DataRow dr in dt.Rows)
                    {
                        string path = Path.Combine(PathHelper.IconFolder, dr["iconPath"].ToString());
                        var menu = new PluginInfo
                        {
                            Code = dr["code"].ToString(),
                            Name = dr["name"].ToString(),
                            ParentCode = dr["parentCode"].ToString(),
                            DllPath = dr["dllPath"].ToString(),
                            ClassName = dr["className"].ToString(),
                            Icon = new BitmapImage(new Uri(Path.Combine(PathHelper.IconFolder, dr["iconPath"].ToString())))
                        };
                        cache[menu.Code] = menu;
                        if (!cache.ContainsKey(menu.ParentCode))
                        {
                            data.Add(menu);
                        }
                        else
                        {
                            cache[menu.ParentCode].Children.Add(menu);
                        }
                        Plugins.Add(menu);
                        LoadChildrenMenu(menu);
                    }
                }
            }
            catch (Exception ex)
            {
                LoadMessageAppend?.Invoke(this, ex.Message);
            }
            return data;
        }
 
    }
}