Example #1
0
        public static void RedirectConstructorFromBase(CecilContext stardewContext, Type asmType, Type[] test, string type, string method, Type[] parameters)
        {
            var types       = test.Select(n => stardewContext.GetTypeReference(n)).ToArray();
            var typeDef     = stardewContext.GetTypeDefinition(asmType.Namespace + "." + asmType.Name);
            var typeDefBase = stardewContext.GetTypeDefinition(asmType.BaseType.Namespace + "." + asmType.BaseType.Name);

            var newConstructorReference = stardewContext.GetConstructorReference(typeDef);
            var oldConstructorReference = stardewContext.GetConstructorReference(typeDefBase);

            var concreteFromConstructor = MakeHostInstanceGeneric(oldConstructorReference, types);
            var concreteToConstructor   = MakeHostInstanceGeneric(newConstructorReference, types);

            ILProcessor ilProcessor = stardewContext.GetMethodIlProcessor(type, method);

            var instructions = ilProcessor.Body.Instructions.Where(n => n.OpCode == OpCodes.Newobj && n.Operand is MethodReference &&
                                                                   ((MethodReference)n.Operand).FullName == concreteFromConstructor.FullName).ToList();

            foreach (var instruction in instructions)
            {
                ilProcessor.Replace(instruction, ilProcessor.Create(OpCodes.Newobj, concreteToConstructor));
            }
        }