-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
194 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
|
||
namespace AM.CAI5000Patch; | ||
|
||
[HotSwapAll] | ||
[UsedImplicitly] | ||
public class PatchCore : Mod | ||
{ | ||
|
57 changes: 57 additions & 0 deletions
57
Source/CAI5000Patch/ThingComp_CombatAI_CompTickRare_Transpiler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using CombatAI.Comps; | ||
using HarmonyLib; | ||
using JetBrains.Annotations; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection.Emit; | ||
using System.Runtime.CompilerServices; | ||
using Verse; | ||
|
||
namespace AM.CAI5000Patch; | ||
|
||
[UsedImplicitly] | ||
[HarmonyPatch(typeof(ThingComp_CombatAI), nameof(ThingComp_CombatAI.CompTickRare))] | ||
public static class ThingComp_CombatAI_CompTickRare_Transpiler | ||
{ | ||
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
var spawnedGetter = AccessTools.PropertyGetter(typeof(Thing), nameof(Thing.Spawned)); | ||
if (spawnedGetter == null) | ||
throw new System.Exception("ThingComp_CombatAI.IsDeadOrDowned method not found."); | ||
|
||
var list = instructions.ToList(); | ||
bool found = false; | ||
|
||
for (int i = 1; i < list.Count; i++) | ||
{ | ||
var ins = list[i]; | ||
|
||
if (ins.Calls(spawnedGetter)) | ||
{ | ||
found = true; | ||
list[i] = MakeReplacement(); | ||
break; | ||
} | ||
} | ||
|
||
if (!found) | ||
Core.Error("Failed to find reference instruction for CompTickRare transpiler! CAI compatibility will be broken."); | ||
|
||
return list.AsEnumerable(); | ||
} | ||
|
||
private static CodeInstruction MakeReplacement() | ||
{ | ||
var detour = AccessTools.Method(typeof(ThingComp_CombatAI_CompTickRare_Transpiler), nameof(ShouldKeepGoing)); | ||
if (detour == null) | ||
throw new System.Exception("ShouldKeepGoing method not found."); | ||
|
||
return new CodeInstruction(OpCodes.Call, detour); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
public static bool ShouldKeepGoing(Pawn pawn) | ||
{ | ||
return pawn.Spawned && pawn.TryGetAnimator() == null; | ||
} | ||
} |
Oops, something went wrong.