コード例 #1
0
        public static void AddPlugin(FileSystem fileSystem)
        {
            var pluginFiles = fileSystem.GetFiles();

            ResourceManager.AddFileSystem(fileSystem, pluginFiles);
            if (!pluginFiles.Contains(PluginInfoFile))
            {
                Logger.Error($"Plugin \"{fileSystem.Name}\" does not have an info file and was ignored!");
                return;
            }

            var pluginInfoFileData = fileSystem.ReadFile(PluginInfoFile);
            var pluginInfo         = JsonConvert.DeserializeObject <PluginInfo>(Encoding.UTF8.GetString(pluginInfoFileData));

            if (LoadedPlugins.Contains(pluginInfo.PluginName))
            {
                return;
            }
            LoadedPlugins.Add(pluginInfo.PluginName);

            if (pluginInfo.PluginDlls == null)
            {
                return;
            }
            foreach (var dllPath in pluginInfo.PluginDlls)
            {
                if (!pluginFiles.Contains(dllPath))
                {
                    Logger.Error($"Plugin dll \"{dllPath}\" from \"{pluginInfo.PluginName}\" not found!");
                    continue;
                }

                try
                {
                    var dllData  = fileSystem.ReadFile(dllPath);
                    var assembly = Assembly.Load(dllData);
                    foreach (var type in assembly.GetTypes().Where(t => typeof(IPlugin).IsAssignableFrom(t)))
                    {
                        var attributes = type.GetCustomAttributes(typeof(PluginAttribute), false);
                        if (attributes.Length != 1)
                        {
                            continue;
                        }

                        var plugin        = (IPlugin)Activator.CreateInstance(type);
                        var attribute     = (PluginAttribute)attributes[0];
                        var pluginContext = new PluginContext(attribute, plugin);
                        PluginDlls.Add(attribute.Id, pluginContext);

                        Logger.Info($"Plugin dll \"{attribute.Id}\" added");
                    }
                }
                catch (ReflectionTypeLoadException ex)
                {
                    Logger.Error($"There is a problem with plugin dll \"{dllPath}\" in \"{pluginInfo.PluginName}\":");
                    Logger.Exception(ex.LoaderExceptions[0]);
                }
                catch (Exception ex)
                {
                    Logger.Error($"Error loading plugin dll:\"{dllPath}\" in \"{pluginInfo.PluginName}\"");
                    Logger.Exception(ex);
                }
            }
        }