1+ using System . Collections . Generic ;
2+ using System . Reflection . Emit ;
3+ using HarmonyLib ;
4+ using ProjectRimFactory . Drones ;
5+ using Verse ;
6+
7+ namespace ProjectRimFactory . Common . HarmonyPatches ;
8+
9+ [ HarmonyPatch ( typeof ( Pawn ) , "get_IsColonyMech" ) ]
10+ // ReSharper disable once InconsistentNaming
11+ // Required for Drone Skills to function property
12+ // get_IsColonyMech checks if a Pawn is Mechanoid and controlled by the Play but NOT if it is a Biotech Mech
13+ // BUT it is used as IF that where the case. requiring this patch
14+ public class Patch_Pawn_get_IsColonyMech
15+ {
16+
17+ private static bool foundReturnFalsePointer ;
18+ private static object returnFalsePointer ;
19+ private static int referencePointCnt ;
20+ private static bool foundReferencePoint ;
21+
22+ // ReSharper disable once UnusedMember.Local
23+ static IEnumerable < CodeInstruction > Transpiler ( IEnumerable < CodeInstruction > instructions )
24+ {
25+
26+ foreach ( var instruction in instructions )
27+ {
28+ // Get the Return False Pointer
29+ if ( ! foundReturnFalsePointer && instruction . opcode == OpCodes . Brfalse_S )
30+ {
31+ foundReturnFalsePointer = true ;
32+ returnFalsePointer = instruction . operand ;
33+ }
34+
35+ // Find the get_MentalStateDef() Check (we want to be inserted right after that)
36+ if ( ! foundReferencePoint && foundReturnFalsePointer &&
37+ instruction . operand ? . ToString ( ) == "Verse.MentalStateDef get_MentalStateDef()" )
38+ {
39+ foundReferencePoint = true ;
40+ }
41+
42+ if ( foundReferencePoint ) referencePointCnt ++ ;
43+
44+ if ( foundReferencePoint && referencePointCnt == 3 )
45+ {
46+ // now we want to insert a check if the pawn is Pawn_Drone
47+ // For maximum performance fully inlined IL
48+ // 5 additional instructions
49+ yield return new CodeInstruction ( OpCodes . Ldarg_0 ) ;
50+ yield return new CodeInstruction ( OpCodes . Isinst , typeof ( Pawn_Drone ) ) ;
51+ yield return new CodeInstruction ( OpCodes . Ldnull ) ;
52+ yield return new CodeInstruction ( OpCodes . Cgt_Un ) ;
53+
54+ yield return new CodeInstruction ( OpCodes . Brtrue_S , returnFalsePointer ) ;
55+
56+ }
57+
58+
59+
60+ yield return instruction ;
61+ }
62+ }
63+ }
0 commit comments