private static void LoadExtend() { if (!Directory.Exists(ExtendedDir)) { Directory.CreateDirectory(ExtendedDir); } foreach (var dir in Directory.GetDirectories(ExtendedDir)) { string metaFile = Path.Combine(dir, "metadata.json"); if (!File.Exists(metaFile)) { Logger.Error(dir + "内未包含metadata.json"); continue; } Logger.Info("已发现 " + new DirectoryInfo(dir).Name); string json = ConcurrentFile.ReadAllText(metaFile); ExtendMeta extendMeta = JsonConvert.DeserializeObject <ExtendMeta>(json); ExtendPlugin extendPlugin = new ExtendPlugin() { Program = extendMeta.Program, File = new FileInfo(Path.Combine(dir, extendMeta.File)).FullName, Name = extendMeta.Name, Author = extendMeta.Author, Major = extendMeta.Major, Minor = extendMeta.Minor, Patch = extendMeta.Patch, State = extendMeta.State, Helps = extendMeta.Help, Commands = extendMeta.Command, }; if (extendPlugin.Commands.Length != 0) { foreach (var item in extendPlugin.Commands) { CommandMap.TryAdd(item, extendPlugin.GetType()); CommandMapStatic.TryAdd(item, extendPlugin); } Logger.Origin($"命令 \"{extendMeta.Name}\" ({string.Join(',', extendPlugin.Commands)}) 已经加载完毕。"); } else { Logger.Warn($"\"{extendMeta.Name}\"尚未设置命令,因此无法被用户激活。"); Logger.Origin($"命令 \"{extendMeta.Name}\" 已经加载完毕。"); } } }
private static void InsertPlugin(Type type, string[] args) { try { Plugin plugin = Activator.CreateInstance(type) as Plugin; if (plugin.PluginType != PluginType.Command) { InsertPlugin(plugin, args); } else { CommandPlugin cmdPlugin = (CommandPlugin)plugin; cmdPlugin.Initialize(args); string str = ""; if (cmdPlugin.Commands != null) { str = "("; foreach (var cmd in cmdPlugin.Commands) { //CommandMap.TryAdd(cmd, (CommandApp)plugin); CommandMap.TryAdd(cmd, type); CommandMapStatic.TryAdd(cmd, cmdPlugin); str += cmd + ","; } str = str.TrimEnd(',') + ") "; } Logger.Origin($"命令 \"{plugin.Name}\" {str}已经加载完毕。"); } } catch (Exception ex) { Logger.Exception(ex.InnerException ?? ex); Logger.Error($"加载插件{type.Name}失败。"); } }