由于修改了参数配置的实现,Debug Console 中加载配置参数会出现问题,因此需要修改相应的逻辑实现。

实现思路

在 onload () 事件中(执行 onload 的时候所有插件都已经完成初始化 init (),所以会在对应的 Attributes 中包含需要保存的参数),读取主程序和各个插件的 Attributes,然后找出 bool 类型的变量,将其加入到开关字典 switches<string,object > 中,并且根据读取到的变量列表 l_MainWnd、l_cfg、l_plugin 来生成 checkbox 控件,最后定义其点击事件,在事件中将对应键值与 checkbox 的 Checked 属性进行绑定,最终实现点击选择框修改对应的变量。

该思路的核心部分在于读入插件参数并创建控件的过程,其实现如下:

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
//根据反射获取属性字典 动态添加开关控件
private void CreateContorls(Form f)
{
//开关集合 <键名,对应的字典>
switches = new Dictionary<string, object>();

cBoxs = new List<CheckBox>();

//value为bool类型的key列表
List<string> l_MainWnd = new List<string>();
List<string> l_cfg = new List<string>();

object cfg = Toolkits.GetValue(MainWnd, "cfg");

Dictionary<string, object> MainParam = (Dictionary<string, object>)Toolkits.getObj(MainWnd, "Attributes");
Dictionary<string, object> cfgParam = (Dictionary<string, object>)Toolkits.getObj(cfg, "Parameters");

//获取插件中的参数
foreach (IPlugin plugin in plugins)
{
if (plugin.Attributes != null && !plugin.Equals(this))
{
List<string> l_plugin = new List<string>();
GetBooleanValues(plugin.Attributes, ref l_plugin);
...;
}
}

//获取主程序中的参数
GetBooleanValues(MainParam, ref l_MainWnd);
GetBooleanValues(cfgParam, ref l_cfg);
foreach (string key in l_MainWnd)
{
...;
}

foreach (string key in l_cfg)
{
...;
}
...;

}

OnLoad 中的对应流程 (其他无关部分省略):

1
2
3
4
5
6
7
8
9
10
11
12
13
OnLoad()
{
//创建窗口,绑定事件打开DebugConsole窗口,设置窗口
...;
logo.Click += new EventHandler((object sender, EventArgs ea) =>
{
...;
//创建控件
CreateContorls(form);
...;
});

}

最终结果

image-20221120152138968

image-20221120152223075

左侧的控件就是在这个过程中自动装填生成的。