-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathThoughtFixCountParts.cs
81 lines (65 loc) · 3.24 KB
/
ThoughtFixCountParts.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using HarmonyLib;
using RimWorld;
using Verse;
namespace MSE2.HarmonyPatches
{
[HarmonyPatch( typeof( HediffUtility ) )]
[HarmonyPatch( nameof( HediffUtility.CountAddedAndImplantedParts ) )]
internal static class ThoughtFixCountParts
{
// to fix thoughts and shit, only count limbs once
// makes the function into this
/*
public static int CountAddedAndImplantedParts(this HediffSet hs)
{
int num = 0;
List<Hediff> hediffs = hs.hediffs;
for ( int i = 0; i < hediffs.Count; i++ )
{
if ( hediffs[i].def.countsAsAddedPartOrImplant && NullOrAncestorHasNoAddedParts(hs, hediffs[i].Part ) )
{
num++;
}
}
return num;
}
*/
[HarmonyTranspiler]
internal static IEnumerable<CodeInstruction> Transpiler ( IEnumerable<CodeInstruction> instructions )
{
List<CodeInstruction> instrList = instructions.ToList();
int brfalseIndex = instrList.FindIndex( i => i.opcode == OpCodes.Brfalse_S );
if ( brfalseIndex < 0 ) throw new ApplicationException( "Could not find branch false operation" );
CodeInstruction brfalse = instrList[brfalseIndex];
instrList.InsertRange( brfalseIndex + 1, InstructionsToInsert( brfalse.operand ) );
return instrList;
}
// code for: && !hs.AncestorHasDirectlyAddedParts( hediffs[i].Part )
private static IEnumerable<CodeInstruction> InstructionsToInsert ( object branchTarget )
{
yield return new CodeInstruction( OpCodes.Ldarg_0 );
yield return new CodeInstruction( OpCodes.Ldloc_1 );
yield return new CodeInstruction( OpCodes.Ldloc_2 );
System.Reflection.MethodInfo listGetter = typeof( List<Hediff> ).GetMethod( "get_Item" );
if ( listGetter == null ) throw new ApplicationException( "Could not find list getter" );
yield return new CodeInstruction( OpCodes.Callvirt, listGetter );
System.Reflection.MethodInfo partGetter = typeof( Hediff ).GetMethod( "get_Part" );
if ( partGetter == null ) throw new ApplicationException( "Could not find hediff part getter" );
yield return new CodeInstruction( OpCodes.Callvirt, partGetter );
System.Reflection.MethodInfo ancHasPart = typeof( ThoughtFixCountParts ).GetMethod( nameof( NullOrAncestorHasNoAddedParts ), System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static );
if ( ancHasPart == null ) throw new ApplicationException( "Could not find ThoughtFixCountParts.NullOrAncestorHasNoAddedParts" );
yield return new CodeInstruction( OpCodes.Call, ancHasPart );
yield return new CodeInstruction( OpCodes.Brfalse, branchTarget );
}
private static bool NullOrAncestorHasNoAddedParts(HediffSet hediffSet, BodyPartRecord bodyPartRecord)
{
return bodyPartRecord == null || !hediffSet.AncestorHasDirectlyAddedParts( bodyPartRecord );
}
}
}