private static bool InitField(TypeDefinition type) { List <MethodDefinition> consturctors = type.GetConstructors().ToList(); if (consturctors.Count == 0) { RTPFLogger.LogCritical($"Can't find constructor for BTLight\n"); return(false); } foreach (MethodDefinition consturctor in consturctors) { ILProcessor ilProcessor = consturctor.Body.GetILProcessor(); Instruction ctorEnd = consturctor.Body.Instructions.Last(); ilProcessor.InsertBefore( ctorEnd , Instruction.Create(OpCodes.Ldarg_0)); ilProcessor.InsertBefore( ctorEnd , Instruction.Create(OpCodes.Ldarg_0)); ilProcessor.InsertBefore(ctorEnd, _getInstanceID); ilProcessor.InsertBefore( ctorEnd , Instruction.Create(OpCodes.Stfld, InstanceId)); } return(true); }
private static void InjectIL(TypeDefinition type) { const string targetMethod = "LateUpdate"; MethodDefinition method = type.GetMethods().FirstOrDefault(m => m.Name == targetMethod); if (method == null) { RTPFLogger.LogCritical($"Can't find method: {targetMethod}\n"); return; } ILProcessor ilProcessor = method.Body.GetILProcessor(); Instruction methodStart = method.Body.Instructions[0]; List <Instruction> newInstructions = CreateInstructions(ilProcessor, methodStart); newInstructions.Reverse(); foreach (Instruction instruction in newInstructions) { ilProcessor.InsertBefore(method.Body.Instructions[0], instruction); } }