예제 #1
0
        public static int InjectHooks(Dictionary <string, List <AttributeData> > attributes, AssemblyDefinition assemblyDefinition)
        {
            if (!attributes.ContainsKey(nameof(AddMethodAttribute)))
            {
                return(0);
            }
            var injectedCorrectly = 0;

            ConsoleHelper.WriteNewline();
            foreach (var hook in attributes[nameof(AddMethodAttribute)])
            {
                // Improve: Add check for multiple args here
                var typeDefinition = Cecil.ConvertStringToClass(hook.Attribute.ConstructorArguments[0].Value.ToString(), assemblyDefinition);
                if (typeDefinition == null)
                {
                    continue;
                }
                var methodName = hook.Attribute.ConstructorArguments[1].Value.ToString();
                if (Cecil.MethodExists(typeDefinition, methodName))
                {
                    ConsoleHelper.WriteError($"Method \"{methodName}\" Already exists in type {typeDefinition.Name}.");
                    continue;
                }
                if (Cecil.InjectNewMethod(typeDefinition, assemblyDefinition, methodName, hook))
                {
                    ++injectedCorrectly;
                }
            }
            return(injectedCorrectly);
        }
예제 #2
0
        public static int InjectHooks(Dictionary <string, List <HookData> > attributes, AssemblyDefinition assembly, Dictionary <string, TypeDefinition> typeDefinitions)
        {
            if (!attributes.ContainsKey(nameof(AddMethodHook)))
            {
                return(0);
            }

            int injectedCorrectly = 0;

            foreach (HookData hook in attributes[nameof(AddMethodHook)])
            {
                TypeDefinition typeDef = Cecil.ConvertStringToClass(hook.Attribute.ConstructorArguments[0].Value.ToString(), assembly, typeDefinitions);

                if (typeDef == null)
                {
                    continue;
                }

                string methodName = hook.Attribute.ConstructorArguments[1].Value.ToString();

                if (Cecil.MethodExists(typeDef, methodName))
                {
                    string desc = string.Format("Method {0} already exists in type {1}", methodName, typeDef.FullName);
                    Externs.MessageBox("Method already exists!", desc);
                }
                else if (Cecil.InjectNewMethod(typeDef, assembly, typeDefinitions, methodName, hook))
                {
                    ++injectedCorrectly;
                }
            }

            return(injectedCorrectly);
        }