Esempio n. 1
0
        public async Task <ICollection <Assembly> > LoadPluginAssembliesAsync(IPluginAssembliesSource source)
        {
            var providerAssemblies = await source.LoadPluginAssembliesAsync();

            foreach (var providerAssembly in providerAssemblies.ToList())
            {
                var pluginMetadata = providerAssembly.GetCustomAttribute <PluginMetadataAttribute>();
                if (pluginMetadata == null)
                {
                    m_Logger.LogWarning($"No plugin metadata attribute found in assembly: {providerAssembly}; skipping loading of this assembly as plugin");
                    providerAssemblies.Remove(providerAssembly);
                    continue;
                }

                if (!providerAssembly.FindTypes <IOpenModPlugin>(false).Any())
                {
                    m_Logger.LogWarning($"No {nameof(IOpenModPlugin)} implementation found in assembly: {providerAssembly}; skipping loading of this assembly as plugin");
                    providerAssemblies.Remove(providerAssembly);
                    continue;
                }
            }

            m_LoadedPluginAssemblies.AddRange(providerAssemblies.Select(d => new WeakReference(d)));
            return(providerAssemblies);
        }
Esempio n. 2
0
        public async Task <ICollection <Assembly> > LoadPluginAssembliesAsync(IPluginAssembliesSource source)
        {
            var providerAssemblies = await source.LoadPluginAssembliesAsync();

            foreach (var providerAssembly in providerAssemblies.ToList())
            {
                var pluginMetadata = providerAssembly.GetCustomAttribute <PluginMetadataAttribute>();
                if (pluginMetadata == null)
                {
                    m_Logger.LogWarning($"No plugin metadata attribute found in assembly: {providerAssembly}; skipping loading of this assembly as plugin");
                    providerAssemblies.Remove(providerAssembly);
                    continue;
                }

                ICollection <Type> types;
                try
                {
                    types = providerAssembly.GetTypes();
                }
                catch (ReflectionTypeLoadException ex)
                {
                    m_Logger.LogTrace(ex, $"Failed to load some types from plugin \"{pluginMetadata.Id}\"");
                    if (ex.LoaderExceptions != null && ex.LoaderExceptions.Length > 0)
                    {
                        foreach (var loaderException in ex.LoaderExceptions)
                        {
                            m_Logger.LogTrace(loaderException, "Loader Exception: ");
                        }
                    }

                    types = ex.Types.Where(d => d != null).ToArray();
                }

                if (!types.Any(d => typeof(IOpenModPlugin).IsAssignableFrom(d) && !d.IsAbstract && d.IsClass))
                {
                    m_Logger.LogWarning($"No {nameof(IOpenModPlugin)} implementation found in assembly: {providerAssembly}; skipping loading of this assembly as plugin");
                    providerAssemblies.Remove(providerAssembly);
                    continue;
                }
            }

            m_LoadedPluginAssemblies.AddRange(providerAssemblies.Select(d => new WeakReference(d)));
            return(providerAssemblies);
        }
Esempio n. 3
0
        public async Task <ICollection <Assembly> > LoadPluginAssembliesAsync(IPluginAssembliesSource source)
        {
            var providerAssemblies = await source.LoadPluginAssembliesAsync();

            var assembliesWithMissingDependencies = new List <Assembly>();

            foreach (var providerAssembly in providerAssemblies.ToList())
            {
                var pluginMetadata = providerAssembly.GetCustomAttribute <PluginMetadataAttribute>();
                if (pluginMetadata == null)
                {
                    m_Logger.LogWarning($"No plugin metadata attribute found in assembly: {providerAssembly}; skipping loading of this assembly as plugin");
                    providerAssemblies.Remove(providerAssembly);
                    continue;
                }

                ICollection <Type> types;
                try
                {
                    types = providerAssembly.GetTypes();
                }
                catch (ReflectionTypeLoadException ex)
                {
                    //Remove Assembly from loading because required dependencies are missing.
                    providerAssemblies.Remove(providerAssembly);

                    var missingAssemblies          = CheckRequiredDependencies(ex.LoaderExceptions);
                    var installMissingDependencies = Configuration != null &&
                                                     Configuration.GetSection("nuget:tryAutoInstallMissingDependencies")
                                                     .Get <bool>();   //todo fix Configuration always null

                    if (!installMissingDependencies)
                    {
                        m_Logger.LogWarning($"Couldn't load plugin from {providerAssembly}: Failed to resolve required dependencies: {string.Join(", ", missingAssemblies.Keys)}", Color.DarkRed);
                        continue;
                    }

                    var installWithSuccess = await TryInstallRequiredDependenciesAsync(providerAssembly, missingAssemblies);

                    if (installWithSuccess)
                    {
                        assembliesWithMissingDependencies.Add(providerAssembly);
                    }

                    continue;
                }

                if (types.Any(d => typeof(IOpenModPlugin).IsAssignableFrom(d) && !d.IsAbstract && d.IsClass))
                {
                    continue;
                }

                m_Logger.LogWarning($"No {nameof(IOpenModPlugin)} implementation found in assembly: {providerAssembly}; skipping loading of this assembly as plugin");
                providerAssemblies.Remove(providerAssembly);
            }

            //Lovely mono :(

            /*if (assembliesWithMissingDependencies.Count != 0)
             * {
             *  m_Logger.LogWarning($"Some plugins can not load until server restart: {string.Join(", ", assembliesWithMissingDependencies)}", Color.DarkRed);
             * }*/

            m_LoadedPluginAssemblies.AddRange(providerAssemblies.Select(d => new WeakReference(d)));
            return(providerAssemblies);
        }