Example #1
0
        public static ReplaceMethod Read(MethodDefinition method)
        {
            var attr = method.CustomAttributes.FirstOrDefault(a => a.AttributeType.Name == nameof(ReplaceMethod));

            if (attr == null)
            {
                return(null);
            }
            var           targetType = (string)attr.ConstructorArguments[0].Value;
            ReplaceMethod val        = new ReplaceMethod(targetType);

            return(val);
        }
Example #2
0
        private static void PatchAssembly(string file)
        {
            var asmSelf   = AssemblyDefinition.ReadAssembly(Assembly.GetExecutingAssembly().Location);
            var patchType = asmSelf.MainModule.GetType(nameof(OvercookedControlsPatcher) + "." + nameof(PatchSource));

            var asmTarget = AssemblyDefinition.ReadAssembly(file, new ReaderParameters()
            {
                ReadWrite = true
            });

            foreach (var patchField in patchType.Fields)
            {
                var addFieldMeta = AddField.Read(patchField);

                if (addFieldMeta != null)
                {
                    var targetType = asmTarget.MainModule.GetType(addFieldMeta.targetType);
                    PatchTools.AddField(targetType, patchField);
                }
            }

            foreach (var patchMethod in patchType.Methods)
            {
                var addMethodMeta     = AddMethod.Read(patchMethod);
                var replaceMethodMeta = ReplaceMethod.Read(patchMethod);

                if (addMethodMeta != null)
                {
                    var targetType = asmTarget.MainModule.GetType(addMethodMeta.targetType);
                    if (!targetType.Methods.Any(f =>
                                                f.Name == patchMethod.Name && f.Parameters.Count == patchMethod.Parameters.Count))
                    {
                        PatchTools.CopyMethod(targetType, patchMethod);
                    }
                }
                else if (replaceMethodMeta != null)
                {
                    var targetType = asmTarget.MainModule.GetType(replaceMethodMeta.targetType);

                    // Not exactly right, but good enough for now.
                    MethodDefinition targetMethod = targetType.Methods.First(m =>
                                                                             m.Name == patchMethod.Name && patchMethod.Parameters.Count == m.Parameters.Count);

                    PatchTools.ReplaceMethod(targetMethod, patchMethod);
                }
            }

            asmTarget.Write();
        }