Пример #1
0
        internal static void LoadModularTypes()
        {
            modularTypes = new List <Type>();
            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                modularTypes.AddRange(assembly.GetTypes().Where(type => type.IsSubclassOf(typeof(CNMBodyComponent))));
                // typeof(ModularCommNetVessel).IsAssignableFrom(type) && !type.IsAbstract).ToList());
            }

            foreach (Type type in modularTypes)
            {
                MethodInfo[] methodsInType = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                foreach (MethodInfo method in methodsInType)
                {
                    if (method.DeclaringType != type)
                    {
                        continue;
                    }
                    methodTypes.Add(method, type);

                    if (!methodsSequence.ContainsKey(method.Name))
                    {
                        methodsSequence.Add(method.Name, new SequenceList <MethodInfo>());
                    }

                    if (Attribute.IsDefined(method, typeof(CNMAttrSequence)))
                    {
                        CNMAttrSequence attr = Attribute.GetCustomAttribute(method, typeof(CNMAttrSequence)) as CNMAttrSequence;
                        methodsSequence[method.Name].Add(attr.when, method);
                    }
                    else
                    {
                        methodsSequence[method.Name].Add(CNMAttrSequence.options.LATE, method);
                    }
                }
            }
            methodsLoaded = true;
        }
        internal static void Initiate()
        {
            if (networkTypes == null)
            {
                networkTypes = new List <Type>();
            }
            else
            {
                networkTypes.Clear();
            }
            methodTypes.Clear();
            methodsSequence.Clear();
            andOrList.Clear();

            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                networkTypes.AddRange(assembly.GetTypes().Where(type => typeof(CommNetwork).IsAssignableFrom(type) && !type.IsAbstract).ToList());
            }

            foreach (Type type in networkTypes)
            {
                log.info("Found a CommNetwork type: " + type.Name);
                if (type == typeof(CommNetwork) || type == typeof(CommNetManagerNetwork))
                {
                    log.info("Skipping type " + type.Name);
                    continue;
                }

                MethodInfo[] methodsInType = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);

                foreach (MethodInfo method in methodsInType)
                {
                    // If it's not declared in that type, we don't care.
                    // i.e. If it comes from a base class, it's already taken care of.

                    if (method.DeclaringType != type)
                    {
                        continue;
                    }

                    methodTypes.Add(method, type);

                    if (Attribute.IsDefined(method, typeof(CNMAttrAndOr)))
                    {
                        andOrList.Add(method, (Attribute.GetCustomAttribute(method, typeof(CNMAttrAndOr)) as CNMAttrAndOr).andOr);
                    }
                    else
                    {
                        // Maybe not necessary
                        andOrList.Add(method, CNMAttrAndOr.options.AND);
                    }

                    if (!methodsSequence.ContainsKey(method.Name))
                    {
                        methodsSequence.Add(method.Name, new SequenceList <MethodInfo>());
                    }

                    if (Attribute.IsDefined(method, typeof(CNMAttrSequence)))
                    {
                        CNMAttrSequence attr = Attribute.GetCustomAttribute(method, typeof(CNMAttrSequence)) as CNMAttrSequence;
                        methodsSequence[method.Name].Add(attr.when, method);
                    }
                    else
                    {
                        // Maybe not necessary
                        if (andOrList.ContainsKey(method) && andOrList[method] == CNMAttrAndOr.options.OR)
                        {
                            methodsSequence[method.Name].Add(CNMAttrSequence.options.EARLY, method);
                        }
                        else
                        {
                            methodsSequence[method.Name].Add(CNMAttrSequence.options.LATE, method);
                        }
                    }
                }
            }

            // Sorting methods:
            // TODO:

            methodsLoaded = true;
        }