Ejemplo n.º 1
0
 public PatchProcessor(HarmonyInstance instance, List <MethodBase> originals, HarmonyMethod prefix = null, HarmonyMethod postfix = null, HarmonyMethod transpiler = null)
 {
     this.instance   = instance;
     this.originals  = originals;
     this.prefix     = prefix ?? new HarmonyMethod(null);
     this.postfix    = postfix ?? new HarmonyMethod(null);
     this.transpiler = transpiler ?? new HarmonyMethod(null);
 }
Ejemplo n.º 2
0
        public void Patch(MethodBase original, HarmonyMethod prefix, HarmonyMethod postfix, HarmonyMethod infix = null)
        {
            var processor = new PatchProcessor(this, original, prefix, postfix, infix);

            processor.Patch();
        }
Ejemplo n.º 3
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>());
                }
                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);
            }
        }