Example #1
0
        private void PrepareType()
        {
            if (!RunMethod <HarmonyPrepare, bool>(true))
            {
                return;
            }
            var enumerable = RunMethod <HarmonyTargetMethods, IEnumerable <MethodBase> >(null);

            if (enumerable != null)
            {
                originals = enumerable.ToList();
            }
            else if (Attribute.GetCustomAttribute(container, typeof(HarmonyPatchAll)) != null)
            {
                var originalType = containerAttributes.originalType;
                originals.AddRange(AccessTools.GetDeclaredConstructors(originalType).Cast <MethodBase>());
                originals.AddRange(AccessTools.GetDeclaredMethods(originalType).Cast <MethodBase>());
            }
            else
            {
                var methodBase = GetOriginalMethod() ?? RunMethod <HarmonyTargetMethod, MethodBase>(null);
                if (methodBase == null)
                {
                    throw new ArgumentException("No target method specified for class " + container.FullName);
                }
                originals.Add(methodBase);
            }

            PatchTools.GetPatches(container, out prefix.method, out postfix.method, out transpiler.method);
            if (prefix.method != null)
            {
                if (!prefix.method.IsStatic)
                {
                    throw new ArgumentException("Patch method " + prefix.method.FullDescription() + " must be static");
                }
                var harmonyMethods = prefix.method.GetHarmonyMethods();
                containerAttributes.Merge(HarmonyMethod.Merge(harmonyMethods)).CopyTo(prefix);
            }

            if (postfix.method != null)
            {
                if (!postfix.method.IsStatic)
                {
                    throw new ArgumentException("Patch method " + postfix.method.FullDescription() + " must be static");
                }
                var harmonyMethods2 = postfix.method.GetHarmonyMethods();
                containerAttributes.Merge(HarmonyMethod.Merge(harmonyMethods2)).CopyTo(postfix);
            }

            if (transpiler.method != null)
            {
                if (!transpiler.method.IsStatic)
                {
                    throw new ArgumentException("Patch method " + transpiler.method.FullDescription() +
                                                " must be static");
                }
                var harmonyMethods3 = transpiler.method.GetHarmonyMethods();
                containerAttributes.Merge(HarmonyMethod.Merge(harmonyMethods3)).CopyTo(transpiler);
            }
        }
Example #2
0
        void ProcessType()
        {
            original = GetOriginalMethod();

            var patchable = CallPrepare();

            if (patchable)
            {
                if (original == null)
                {
                    original = RunMethod <HarmonyTargetMethod, MethodBase>(null);
                }
                if (original == null)
                {
                    throw new ArgumentException("No target method specified for class " + container.FullName);
                }

                PatchTools.GetPatches(container, original, out prefix.method, out postfix.method, out transpiler.method);

                if (prefix.method != null)
                {
                    if (prefix.method.IsStatic == false)
                    {
                        throw new ArgumentException("Patch method " + prefix.method.Name + " in " + prefix.method.DeclaringType + " must be static");
                    }

                    var prefixAttributes = prefix.method.GetHarmonyMethods();
                    containerAttributes.Merge(HarmonyMethod.Merge(prefixAttributes)).CopyTo(prefix);
                }

                if (postfix.method != null)
                {
                    if (postfix.method.IsStatic == false)
                    {
                        throw new ArgumentException("Patch method " + postfix.method.Name + " in " + postfix.method.DeclaringType + " must be static");
                    }

                    var postfixAttributes = postfix.method.GetHarmonyMethods();
                    containerAttributes.Merge(HarmonyMethod.Merge(postfixAttributes)).CopyTo(postfix);
                }

                if (transpiler.method != null)
                {
                    if (transpiler.method.IsStatic == false)
                    {
                        throw new ArgumentException("Patch method " + transpiler.method.Name + " in " + transpiler.method.DeclaringType + " must be static");
                    }

                    var infixAttributes = transpiler.method.GetHarmonyMethods();
                    containerAttributes.Merge(HarmonyMethod.Merge(infixAttributes)).CopyTo(transpiler);
                }
            }
        }
Example #3
0
        void ProcessType()
        {
            original = GetOriginalMethod();

            var patchable = CallPrepare();

            if (patchable)
            {
                if (original == null)
                {
                    original = RunMethod <HarmonyTargetMethod, MethodBase>(null);
                }
                if (original == null)
                {
                    throw new ArgumentException("No target method specified for class " + container.FullName);
                }

                PatchTools.GetPatches(container, original, out prefix.method, out postfix.method, out infix.method);

                if (prefix.method != null)
                {
                    var prefixAttributes = prefix.method.GetHarmonyMethods();
                    containerAttributes.Merge(HarmonyMethod.Merge(prefixAttributes)).CopyTo(prefix);
                }

                if (postfix.method != null)
                {
                    var postfixAttributes = postfix.method.GetHarmonyMethods();
                    containerAttributes.Merge(HarmonyMethod.Merge(postfixAttributes)).CopyTo(postfix);
                }

                if (infix.method != null)
                {
                    var infixAttributes = infix.method.GetHarmonyMethods();
                    containerAttributes.Merge(HarmonyMethod.Merge(infixAttributes)).CopyTo(infix);
                }
            }
        }
Example #4
0
        void PrepareType()
        {
            var mainPrepareResult = RunMethod <HarmonyPrepare, bool>(true);

            if (mainPrepareResult == false)
            {
                return;
            }

            var customOriginals = RunMethod <HarmonyTargetMethods, IEnumerable <MethodBase> >(null);

            if (customOriginals != null)
            {
                originals = customOriginals.ToList();
            }
            else
            {
                var isPatchAll = Attribute.GetCustomAttribute(container, typeof(HarmonyPatchAll)) != null;
                if (isPatchAll)
                {
                    var type = containerAttributes.declaringType;
                    originals.AddRange(AccessTools.GetDeclaredConstructors(type).Cast <MethodBase>());
                    originals.AddRange(AccessTools.GetDeclaredMethods(type).Cast <MethodBase>());
                }
                else
                {
                    var original = GetOriginalMethod();
                    if (original == null)
                    {
                        original = RunMethod <HarmonyTargetMethod, MethodBase>(null);
                    }
                    if (original != null)
                    {
                        originals.Add(original);
                    }
                    else
                    {
                        throw new ArgumentException("No target method specified for class " + container.FullName);
                    }
                }
            }

            PatchTools.GetPatches(container, out prefix.method, out postfix.method, out transpiler.method);

            if (prefix.method != null)
            {
                if (prefix.method.IsStatic == false)
                {
                    throw new ArgumentException("Patch method " + prefix.method.FullDescription() + " must be static");
                }

                var prefixAttributes = prefix.method.GetHarmonyMethods();
                containerAttributes.Merge(HarmonyMethod.Merge(prefixAttributes)).CopyTo(prefix);
            }

            if (postfix.method != null)
            {
                if (postfix.method.IsStatic == false)
                {
                    throw new ArgumentException("Patch method " + postfix.method.FullDescription() + " must be static");
                }

                var postfixAttributes = postfix.method.GetHarmonyMethods();
                containerAttributes.Merge(HarmonyMethod.Merge(postfixAttributes)).CopyTo(postfix);
            }

            if (transpiler.method != null)
            {
                if (transpiler.method.IsStatic == false)
                {
                    throw new ArgumentException("Patch method " + transpiler.method.FullDescription() + " must be static");
                }

                var infixAttributes = transpiler.method.GetHarmonyMethods();
                containerAttributes.Merge(HarmonyMethod.Merge(infixAttributes)).CopyTo(transpiler);
            }
        }
Example #5
0
        void PrepareType()
        {
            var mainPrepareResult = RunMethod <HarmonyPrepare, bool>(true);

            if (mainPrepareResult == false)
            {
                return;
            }

            var customOriginals = RunMethod <HarmonyTargetMethods, IEnumerable <MethodBase> >(null);

            if (customOriginals != null)
            {
                originals = customOriginals.ToList();
            }
            else
            {
                var originalMethodType = containerAttributes.methodType;

                // MethodType default is Normal
                if (containerAttributes.methodType == null)
                {
                    containerAttributes.methodType = MethodType.Normal;
                }

                var isPatchAll = Attribute.GetCustomAttribute(container, typeof(HarmonyPatchAll)) != null;
                if (isPatchAll)
                {
                    var type = containerAttributes.declaringType;
                    originals.AddRange(AccessTools.GetDeclaredConstructors(type).Cast <MethodBase>());
                    originals.AddRange(AccessTools.GetDeclaredMethods(type).Cast <MethodBase>());
                    var props = AccessTools.GetDeclaredProperties(type);
                    originals.AddRange(props.Select(prop => prop.GetGetMethod(true)).Where(method => method != null).Cast <MethodBase>());
                    originals.AddRange(props.Select(prop => prop.GetSetMethod(true)).Where(method => method != null).Cast <MethodBase>());
                }
                else
                {
                    var original = RunMethod <HarmonyTargetMethod, MethodBase>(null);

                    if (original == null)
                    {
                        original = GetOriginalMethod();
                    }

                    if (original == null)
                    {
                        var info = "(";
                        info += "declaringType=" + containerAttributes.declaringType + ", ";
                        info += "methodName =" + containerAttributes.methodName + ", ";
                        info += "methodType=" + originalMethodType + ", ";
                        info += "argumentTypes=" + containerAttributes.argumentTypes.Description();
                        info += ")";
                        throw new ArgumentException("No target method specified for class " + container.FullName + " " + info);
                    }

                    originals.Add(original);
                }
            }

            PatchTools.GetPatches(container, out prefix.method, out postfix.method, out transpiler.method);

            if (prefix.method != null)
            {
                if (prefix.method.IsStatic == false)
                {
                    throw new ArgumentException("Patch method " + prefix.method.FullDescription() + " must be static");
                }

                var prefixAttributes = prefix.method.GetHarmonyMethods();
                containerAttributes.Merge(HarmonyMethod.Merge(prefixAttributes)).CopyTo(prefix);
            }

            if (postfix.method != null)
            {
                if (postfix.method.IsStatic == false)
                {
                    throw new ArgumentException("Patch method " + postfix.method.FullDescription() + " must be static");
                }

                var postfixAttributes = postfix.method.GetHarmonyMethods();
                containerAttributes.Merge(HarmonyMethod.Merge(postfixAttributes)).CopyTo(postfix);
            }

            if (transpiler.method != null)
            {
                if (transpiler.method.IsStatic == false)
                {
                    throw new ArgumentException("Patch method " + transpiler.method.FullDescription() + " must be static");
                }

                var infixAttributes = transpiler.method.GetHarmonyMethods();
                containerAttributes.Merge(HarmonyMethod.Merge(infixAttributes)).CopyTo(transpiler);
            }
        }