Beispiel #1
0
        public static bool Initialize(bool ignoreSystem = true)
        {
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

            if (Initialized == false || assemblies.Length > Assemblies.Count)
            {
                foreach (Assembly A in assemblies)
                {
                    if (!Assemblies.Contains(A))
                    {
                        Assemblies.Add(A);

                        foreach (Type T in A.GetTypes())
                        {
                            string typeName = T.FullName.Replace("+", ".");

                            // Skip: System types when it's need //
                            if (ignoreSystem && typeName.StartsWith("Mono."))
                            {
                                continue;
                            }
                            if (ignoreSystem && typeName.StartsWith("System."))
                            {
                                continue;
                            }
                            if (ignoreSystem && typeName.StartsWith("Microsoft."))
                            {
                                continue;
                            }

                            // Skip: Invoker(self), Generic(example<T>), Attr(CompilerGenerated) //
                            if (T == typeof(Invoker) || T.FullName[0] == '<')
                            {
                                continue;
                            }

                            // Skip types with "Compiler Generated" attrubute //
                            if (T.GetCustomAttributes(typeof(CompilerGeneratedAttribute), true).Length > 0)
                            {
                                continue;
                            }

                            if (T.IsGenericType)
                            {
                                int argsIndex = typeName.IndexOf('`');
                                if (argsIndex == -1)
                                {
                                    continue;
                                }

                                typeName  = typeName.Substring(0, argsIndex);
                                typeName += "<" + GetParameters(T.GetGenericArguments()) + ">";
                            }

                            // Add type to list of all types
                            if (!Types.ContainsKey(typeName))
                            {
                                Types.Add(typeName, T);
                            }

                            // Get Methods of Type //
                            foreach (MethodInfo info in T.GetMethods(AllBindingFlags))
                            {
                                // Skip methods of properties (get, set) //
                                if (info.Name.StartsWith("get_") || info.Name.StartsWith("set_"))
                                {
                                    continue;
                                }
                                // Skip when contains generic parameters of methods of operator //
                                if (info.ContainsGenericParameters || info.Name.StartsWith("op_"))
                                {
                                    continue;
                                }
                                // Skip methods with "Compiler Generated" attrubute //
                                if (info.GetCustomAttributes(typeof(CompilerGeneratedAttribute), true).Length > 0)
                                {
                                    continue;
                                }

                                // Apply SetBase attribute for target class//
                                object[] attributeHooks = info.GetCustomAttributes(typeof(Hook), true);
                                if (attributeHooks != null && attributeHooks.Length > 0)
                                {
                                    Hook attributeHook = (attributeHooks[0] as Hook);
                                    Hooks[attributeHook.Name] = info;
                                }

                                // Register method with inject & params attribute //
                                object[] attributeInject = info.GetCustomAttributes(typeof(Patch), true);
                                object[] attributeParams = info.GetCustomAttributes(typeof(Params), true);
                                if (attributeInject != null && attributeInject.Length > 0 && attributeParams != null && attributeParams.Length > 0)
                                {
                                    Patch  invokerPatch  = (attributeInject[0] as Patch);
                                    Params invokerParams = (attributeParams[0] as Params);
                                    Patches.Create(invokerPatch, info, invokerParams);
                                }
                            }
                        }
                    }
                }
            }

            return(Assemblies.Count > 0 && Types.Count > 0);
        }