private void AddEventToPlugin(CQPlugin plugin, PluginEvent pluginEvent) { var eventType = pluginEvent.type; var fieldInfo = CQPlugin.GetEvent(eventType); if (fieldInfo == null) { throw new MissingFieldException("未找到事件 " + eventType); } fieldInfo.SetValue( plugin, GetProcAs(plugin.dllModule, pluginEvent.function, fieldInfo.FieldType)); }
private CQPlugin LoadPluginInner(string pluginPath) { var dllPath = pluginPath + "app.dll"; var jsonPath = pluginPath + "app.json"; if (!File.Exists(dllPath)) { throw new ArgumentException("未找到文件 " + dllPath); } if (!File.Exists(jsonPath)) { throw new ArgumentException("未找到文件 " + jsonPath); } var plugin = new CQPlugin { dllModule = LoadLibrary(dllPath), appDir = pluginPath }; //加载Dll plugin.appInfo = GetProcAs <CQEvent.AppInfoDelegate>(plugin.dllModule, "AppInfo"); plugin.initialize = GetProcAs <CQEvent.InitializeDelegate>(plugin.dllModule, "Initialize"); //获取插件基本信息 var appInfo = GetString(plugin.appInfo()); var infoArray = appInfo.Split(','); if (infoArray.Length != 2) { throw new ArgumentException("获取到非法appInfo: " + appInfo + "不符合 \"apiVersion.appId\" 要求"); } if (!int.TryParse(infoArray[0], out plugin.apiVersion)) { throw new ArgumentException("获取到非法appInfo: " + infoArray[0] + "不是一个有效的int整数"); } plugin.appId = infoArray[1]; //解析Json获得插件详细信息 var jsonFile = File.OpenRead(jsonPath); var jsonFileReader = new StreamReader(jsonFile); var jsonStr = jsonFileReader.ReadToEnd(); jsonStr = jsonStr.Replace("\"event\"", "\"eventList\""); plugin.info = JsonConvert.DeserializeObject <PluginInfo>(jsonStr); //判断Api版本 if (plugin.info.apiver != plugin.apiVersion) { throw new ArgumentException("Json中定义的ApiVersion为 " + plugin.apiVersion + " 但是AppInfo当中的ApiVersion为" + plugin.apiVersion); } //获取事件信息 foreach (var pluginEvent in plugin.info.eventList) { AddEventToPlugin(plugin, pluginEvent); } //插件加载完成 return(plugin); }