Ejemplo n.º 1
0
        private void AddPlugin(string dllPath)
        {
            // Create a new assembly from the plugin file we're adding..
            Assembly pluginAssembly = Assembly.LoadFrom(dllPath);

            var guid = AssemblyUtils.Guid(pluginAssembly);

            foreach (var plugin in _repository.PluginsByType)
            {
                if (plugin.AssemblyInfo.Guid == guid)
                {
                    return; // avoid loading duplicated plugin.dll file
                }
            }

            var machineName = Path.GetFileNameWithoutExtension(dllPath) ?? pluginAssembly.GetName().Name ?? "";

            machineName = Regex.Replace(machineName, "Plugin$", "", RegexOptions.IgnoreCase);
            var configFileName = machineName + ".config.json";
            var configFilePath = Path.Combine(_directoryLocator.PluginConfigDir, machineName, configFileName);

            var disabledGuids = _preferenceManager.Preferences.Plugins.DisabledPluginGuids;

            // Next we'll loop through all the Types found in the assembly
            foreach (Type pluginType in pluginAssembly.GetTypes().Where(IsValidPlugin))
            {
                // Create a new instance and store the instance in the collection for later use
                // We could change this later on to not load an instance.. we have 2 options
                // 1- Make one instance, and use it whenever we need it.. it's always there
                // 2- Don't make an instance, and instead make an instance whenever we use it, then close it
                // For now we'll just make an instance of all the plugins
                var newPlugin = (IPlugin)_kernel.Get(pluginType);

                // TODO: Store this in preferences file
                newPlugin.Enabled = !disabledGuids.Contains(guid);

                var assemblyInfo = new PluginAssemblyInfo(dllPath,
                                                          AssemblyUtils.GetAssemblyVersion(pluginAssembly),
                                                          AssemblyUtils.GetLinkerTimestamp(pluginAssembly),
                                                          guid,
                                                          configFilePath);

                // Initialize the plugin
                newPlugin.LoadPlugin(_repository, assemblyInfo);

                // Add the new plugin to our collection here
                _repository.Add(newPlugin);
            }
        }
Ejemplo n.º 2
0
        public async Task <Plugin> Add(Plugin plugin, IFormFile file)
        {
            plugin.FileName = file.FileName;

            var plugins = await _pluginRepository.All();

            if (plugins.Any(p => p.Name == plugin.Name || p.FileName == plugin.FileName))
            {
                throw new Exception("The plugin name and filename must be unique.");
            }

            var uploadedPlugin = await _pluginRepository.Add(plugin);

            var filePath = Path.Combine(PluginPath, plugin.FileName);

            await using var stream = File.Create(filePath);
            await file.CopyToAsync(stream);

            return(uploadedPlugin);
        }