[HarmonyPatch(typeof(Bill), nameof(Bill.PawnAllowedToStartAnew))] public static class RimWorld__Bill__PawnAllowedToStartAnew { internal static IEnumerable Transpiler(IEnumerable instructions, ILGenerator il) { var info1 = AccessTools.DeclaredField(typeof(Pawn), nameof(Pawn.skills)); var label = il.DefineLabel(); var once = true; foreach(var code in instructions) { if(code.opcode == OpCodes.Ldloc_0 && once) yield return code.WithLabels(label); else yield return code; if(code.LoadsField(info1)) { yield return new CodeInstruction(OpCodes.Ldnull); yield return new CodeInstruction(OpCodes.Beq, label); yield return new CodeInstruction(OpCodes.Ldarg_1); yield return new CodeInstruction(OpCodes.Ldfld, info1); } } } } [StaticConstructorOnStartup] internal static class Verse__AI__Toils_Recip__FinishRecipeAndStartStoringProduct { static Verse__AI__Toils_Recip__FinishRecipeAndStartStoringProduct() { var m = AccessTools.Method(AccessTools.TypeByName("Verse.AI.Toils_Recipe+<>c__DisplayClass3_0"), "b__0"); SMHarmonyBase.Harmony.Patch(m, transpiler: new HarmonyMethod(typeof(Verse__AI__Toils_Recip__FinishRecipeAndStartStoringProduct), nameof(Transpiler))); } internal static IEnumerable Transpiler(IEnumerable instructions, ILGenerator il) { var info1 = AccessTools.DeclaredField(typeof(Pawn), nameof(Pawn.skills)); var info2 = AccessTools.DeclaredMethod(typeof(SkillRecord), nameof(SkillRecord.Learn)); var label = il.DefineLabel(); var list = instructions.ToList(); for(int i = 0; i < list.Count; i++) { var code = list[i]; if(i - 1 > -1 && list[i - 1].Calls(info2)) yield return code.WithLabels(label); else yield return code; if(code.LoadsField(info1)) { yield return new CodeInstruction(OpCodes.Ldnull); yield return new CodeInstruction(OpCodes.Beq, label); yield return new CodeInstruction(OpCodes.Ldloc_0); yield return new CodeInstruction(OpCodes.Ldfld, info1); } } } }