Esempio n. 1
0
        /// <summary>
        /// Initializes the Covalence library
        /// </summary>
        internal void Initialize()
        {
            // Search for all provider types
            Type baseType = typeof(ICovalenceProvider);
            IEnumerable<Type> candidateSet = null;
            foreach (var ass in AppDomain.CurrentDomain.GetAssemblies())
            {
                Type[] assTypes = null;
                try
                {
                    assTypes = ass.GetTypes();
                }
                catch (ReflectionTypeLoadException rtlEx)
                {
                    assTypes = rtlEx.Types;
                }
                catch (TypeLoadException tlEx)
                {
                    logger.Write(LogType.Warning, "Covalence: Type {0} could not be loaded from assembly '{1}': {2}", tlEx.TypeName, ass.FullName, tlEx);
                }
                if (assTypes != null)
                {
                    if (candidateSet == null)
                        candidateSet = assTypes;
                    else
                        candidateSet = candidateSet.Concat(assTypes);
                }
            }
            if (candidateSet == null)
            {
                logger.Write(LogType.Warning, "No Covalence providers found, Covalence will not be functional for this session.");
                return;
            }
            List<Type> candidates = new List<Type>(
                candidateSet.Where((t) => t != null && t.IsClass && !t.IsAbstract && t.FindInterfaces((m, o) => m == baseType, null).Length == 1)
                );

            // Select a candidate
            Type selectedCandidate;
            if (candidates.Count == 0)
            {
                logger.Write(LogType.Warning, "No Covalence providers found, Covalence will not be functional for this session.");
                return;
            }
            else if (candidates.Count > 1)
            {
                selectedCandidate = candidates[0];
                StringBuilder sb = new StringBuilder();
                for (int i = 1; i < candidates.Count; i++)
                {
                    if (i > 1) sb.Append(',');
                    sb.Append(candidates[i].FullName);
                }
                logger.Write(LogType.Warning, "Multiple Covalence providers found! Using {0}. (Also found {1})", selectedCandidate, sb);
            }
            else
                selectedCandidate = candidates[0];

            // Create it
            try
            {
                provider = (ICovalenceProvider) Activator.CreateInstance(selectedCandidate);
            }
            catch (Exception ex)
            {
                logger.Write(LogType.Warning, "Got exception when instantiating Covalence provider, Covalence will not be functional for this session.");
                logger.Write(LogType.Warning, "{0}", ex);
                return;
            }

            // Create mediators
            Server = provider.CreateServer();
            Players = provider.CreatePlayerManager();
            cmdSystem = provider.CreateCommandSystemProvider();

            // Initialize other things
            commands = new Dictionary<string, RegisteredCommand>();

            // Log
            logger.Write(LogType.Info, "Using Covalence provider for game '{0}'", provider.GameName);
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes the Covalence library
        /// </summary>
        internal void Initialize()
        {
            // Search for all provider types
            var baseType = typeof(ICovalenceProvider);
            IEnumerable <Type> candidateSet = null;

            foreach (var ass in AppDomain.CurrentDomain.GetAssemblies())
            {
                Type[] assTypes = null;
                try
                {
                    assTypes = ass.GetTypes();
                }
                catch (ReflectionTypeLoadException rtlEx)
                {
                    assTypes = rtlEx.Types;
                }
                catch (TypeLoadException tlEx)
                {
                    logger.Write(LogType.Warning, "Covalence: Type {0} could not be loaded from assembly '{1}': {2}", tlEx.TypeName, ass.FullName, tlEx);
                }
                if (assTypes != null)
                {
                    candidateSet = candidateSet?.Concat(assTypes) ?? assTypes;
                }
            }
            if (candidateSet == null)
            {
                logger.Write(LogType.Warning, "No Covalence providers found, Covalence will not be functional for this session.");
                return;
            }
            var candidates = new List <Type>(
                candidateSet.Where(t => t != null && t.IsClass && !t.IsAbstract && t.FindInterfaces((m, o) => m == baseType, null).Length == 1)
                );

            // Select a candidate
            Type selectedCandidate;

            if (candidates.Count == 0)
            {
                logger.Write(LogType.Warning, "No Covalence providers found, Covalence will not be functional for this session.");
                return;
            }
            if (candidates.Count > 1)
            {
                selectedCandidate = candidates[0];
                var sb = new StringBuilder();
                for (var i = 1; i < candidates.Count; i++)
                {
                    if (i > 1)
                    {
                        sb.Append(',');
                    }
                    sb.Append(candidates[i].FullName);
                }
                logger.Write(LogType.Warning, "Multiple Covalence providers found! Using {0}. (Also found {1})", selectedCandidate, sb);
            }
            else
            {
                selectedCandidate = candidates[0];
            }

            // Create it
            try
            {
                provider = (ICovalenceProvider)Activator.CreateInstance(selectedCandidate);
            }
            catch (Exception ex)
            {
                logger.Write(LogType.Warning, "Got exception when instantiating Covalence provider, Covalence will not be functional for this session.");
                logger.Write(LogType.Warning, "{0}", ex);
                return;
            }

            // Create mediators
            Server    = provider.CreateServer();
            Players   = provider.CreatePlayerManager();
            cmdSystem = provider.CreateCommandSystemProvider();

            // Initialize other things
            commands = new Dictionary <string, RegisteredCommand>();

            // Log
            logger.Write(LogType.Info, "Using Covalence provider for game '{0}'", provider.GameName);
        }
        internal void Initialize()
        {
            Type               item;
            object             obj;
            TypeFilter         typeFilter2 = null;
            Type               type1       = typeof(ICovalenceProvider);
            IEnumerable <Type> types       = null;

            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            for (int i = 0; i < (int)assemblies.Length; i++)
            {
                Assembly assembly  = assemblies[i];
                Type[]   typeArray = null;
                try
                {
                    typeArray = assembly.GetTypes();
                }
                catch (ReflectionTypeLoadException reflectionTypeLoadException)
                {
                    typeArray = reflectionTypeLoadException.Types;
                }
                catch (TypeLoadException typeLoadException1)
                {
                    TypeLoadException typeLoadException = typeLoadException1;
                    this.logger.Write(LogType.Warning, "Covalence: Type {0} could not be loaded from assembly '{1}': {2}", new object[] { typeLoadException.TypeName, assembly.FullName, typeLoadException });
                }
                if (typeArray != null)
                {
                    if (types != null)
                    {
                        obj = types.Concat <Type>(typeArray);
                    }
                    else
                    {
                        obj = null;
                    }
                    if (obj == null)
                    {
                        obj = typeArray;
                    }
                    types = (IEnumerable <Type>)obj;
                }
            }
            if (types == null)
            {
                this.logger.Write(LogType.Warning, "Covalence not available yet for this game", Array.Empty <object>());
                return;
            }
            List <Type> types1 = new List <Type>(types.Where <Type>((Type t) => {
                if (!(t != null) || !t.IsClass || t.IsAbstract)
                {
                    return(false);
                }
                Type type = t;
                TypeFilter u003cu003e9_1 = typeFilter2;
                if (u003cu003e9_1 == null)
                {
                    TypeFilter typeFilter  = (Type m, object o) => m == type1;
                    TypeFilter typeFilter1 = typeFilter;
                    typeFilter2            = typeFilter;
                    u003cu003e9_1          = typeFilter1;
                }
                return((int)type.FindInterfaces(u003cu003e9_1, null).Length == 1);
            }));

            if (types1.Count == 0)
            {
                this.logger.Write(LogType.Warning, "Covalence not available yet for this game", Array.Empty <object>());
                return;
            }
            if (types1.Count <= 1)
            {
                item = types1[0];
            }
            else
            {
                item = types1[0];
                StringBuilder stringBuilder = new StringBuilder();
                for (int j = 1; j < types1.Count; j++)
                {
                    if (j > 1)
                    {
                        stringBuilder.Append(',');
                    }
                    stringBuilder.Append(types1[j].FullName);
                }
                this.logger.Write(LogType.Warning, "Multiple Covalence providers found! Using {0}. (Also found {1})", new object[] { item, stringBuilder });
            }
            try
            {
                this.provider = (ICovalenceProvider)Activator.CreateInstance(item);
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                this.logger.Write(LogType.Warning, "Got exception when instantiating Covalence provider, Covalence will not be functional for this session.", Array.Empty <object>());
                this.logger.Write(LogType.Warning, "{0}", new object[] { exception });
                return;
            }
            this.Server    = this.provider.CreateServer();
            this.Players   = this.provider.CreatePlayerManager();
            this.cmdSystem = this.provider.CreateCommandSystemProvider();
            this.logger.Write(LogType.Info, "Using Covalence provider for game '{0}'", new object[] { this.provider.GameName });
        }