Ejemplo n.º 1
0
        private void LoadExtensions()
        {
            var load = new List <Assembly>();

            // Create a dependency-ordered list of extensions.
            var loading = new List <Assembly>();

            foreach (
                var assembly in
                GetType()
                .Assembly.GetReferencedAssemblies()
                .Select(Assembly.Load)
                .Concat(new[] { GetType().Assembly })
                .Distinct()
                .Where(a => a.GetCustomAttributes <SampSharpExtensionAttribute>().Any()))
            {
                AddExtensionToLoadList(assembly, load, loading);
            }

            // Load extensions according to dependency list.
            foreach (var assembly in load)
            {
                var attributes = assembly.GetCustomAttributes <SampSharpExtensionAttribute>();

                foreach (var extensionType in attributes.Select(attribute => attribute.Type))
                {
                    if (extensionType == null)
                    {
                        continue;
                    }
                    if (!typeof(IExtension).IsAssignableFrom(extensionType))
                    {
                        FrameworkLog.WriteLine(FrameworkMessageLevel.Warning,
                                               "The extension from {0} could not be loaded. The specified extension type does not inherit from IExtension.",
                                               assembly);
                        continue;
                    }
                    if (extensionType.Assembly != assembly)
                    {
                        FrameworkLog.WriteLine(FrameworkMessageLevel.Warning,
                                               "The extension from {0} could not be loaded. The specified extension type is not part of the assembly.",
                                               assembly);
                        continue;
                    }
                    if (_extensions.Any(e => e.GetType() == extensionType))
                    {
                        FrameworkLog.WriteLine(FrameworkMessageLevel.Warning,
                                               "The extension from {0} could not be loaded. The specified extension type was already loaded.",
                                               assembly);
                        continue;
                    }

                    // Register the extension to the plugin.
                    var extension = (IExtension)Activator.CreateInstance(extensionType);
                    Extension.Register(extension);
                    _extensions.Add(extension);
                }
            }
        }
Ejemplo n.º 2
0
        private void AutoloadPoolTypes()
        {
            var types = new List <Type>();

            foreach (var poolType in new[] { typeof(BaseMode), GetType() }.Concat(_extensions.Select(e => e.GetType()))
                     .Select(t => t.Assembly)
                     .Distinct()
                     .SelectMany(a => a.GetTypes())
                     .Where(t => t.IsClass && t.GetCustomAttribute <PooledTypeAttribute>() != null)
                     .Distinct())
            {
                // If poolType or subclass of poolType is already in types, continue.
                if (types.Any(t => t == poolType || poolType.IsAssignableFrom(t)))
                {
                    FrameworkLog.WriteLine(FrameworkMessageLevel.Debug,
                                           $"Pool of type {poolType} is not autoloaded because a subclass of it will already be loaded.");
                    continue;
                }

                // Remove all types in types where type is supertype of poolType.
                foreach (var t in types.Where(t => t.IsAssignableFrom(poolType)).ToArray())
                {
                    FrameworkLog.WriteLine(FrameworkMessageLevel.Debug,
                                           $"No longer autoloading type {poolType} because a subclass of it is going to be loaded.");
                    types.Remove(t);
                }

                FrameworkLog.WriteLine(FrameworkMessageLevel.Debug, $"Autoloading pool of type {poolType}.");
                types.Add(poolType);
            }

            var poolTypes = new[]
            {
                typeof(IdentifiedPool <>),
                typeof(IdentifiedOwnedPool <,>)
            };

            foreach (var type in types)
            {
                var pool = type;
                do
                {
                    pool = pool.BaseType;
                } while (pool != null && (!pool.IsGenericType || !poolTypes.Contains(pool.GetGenericTypeDefinition())));

                if (pool == null)
                {
                    FrameworkLog.WriteLine(FrameworkMessageLevel.Debug, $"Skipped autoloading pool of type {type} because it's not a subtype of a pool.");
                    continue;
                }

                pool.GetMethod("Register", new[] { typeof(Type) }).Invoke(null, new[] { type });
            }
        }
Ejemplo n.º 3
0
 public void AutoloadControllersForAssembly(Assembly assembly)
 {
     foreach (var type in assembly.GetExportedTypes()
              .Where(t => t.IsClass &&
                     typeof(IController).IsAssignableFrom(t) &&
                     t.GetCustomAttribute <ControllerAttribute>() != null))
     {
         FrameworkLog.WriteLine(FrameworkMessageLevel.Debug, $"Autoloading type {type}...");
         _controllers.Override(Activator.CreateInstance(type) as IController);
     }
 }
Ejemplo n.º 4
0
        protected BaseMode(bool redirectConsole)
        {
            if (redirectConsole)
            {
                Console.SetOut(new ServerLogWriter());
            }

            if (FrameworkConfiguration.MessageLevel == FrameworkMessageLevel.Debug)
            {
                var type        = Type.GetType("Mono.Runtime");
                var displayName = type?.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static);

                if (displayName != null)
                {
                    FrameworkLog.WriteLine(FrameworkMessageLevel.Debug, "Detected mono version: {0}",
                                           displayName.Invoke(null, null));
                }
            }

            Instance = this;
        }