Skip to content

Commit

Permalink
Added transpiler for CAI5000
Browse files Browse the repository at this point in the history
  • Loading branch information
Epicguru committed Jun 19, 2023
1 parent fe35b35 commit 09ac4ce
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 69 deletions.
94 changes: 47 additions & 47 deletions Source/Animations/UserSettings/Layouts/default-2022.dwlt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ MonoBehaviour:
m_MinSize: {x: 300, y: 100}
m_MaxSize: {x: 24288, y: 16192}
vertical: 0
controlID: 88
controlID: 46
--- !u!114 &3
MonoBehaviour:
m_ObjectHideFlags: 52
Expand Down Expand Up @@ -92,8 +92,8 @@ MonoBehaviour:
serializedVersion: 2
x: 0
y: 0
width: 482
height: 638.6667
width: 481.33334
height: 829.3333
m_MinSize: {x: 201, y: 221}
m_MaxSize: {x: 4001, y: 4021}
m_ActualView: {fileID: 16}
Expand All @@ -117,9 +117,9 @@ MonoBehaviour:
m_Position:
serializedVersion: 2
x: 0
y: 638.6667
y: 829.3333
width: 1954.6666
height: 660.6667
height: 470.00006
m_MinSize: {x: 101, y: 121}
m_MaxSize: {x: 4001, y: 4021}
m_ActualView: {fileID: 13}
Expand Down Expand Up @@ -224,7 +224,7 @@ MonoBehaviour:
m_MinSize: {x: 200, y: 100}
m_MaxSize: {x: 16192, y: 16192}
vertical: 1
controlID: 19
controlID: 47
--- !u!114 &10
MonoBehaviour:
m_ObjectHideFlags: 52
Expand All @@ -245,11 +245,11 @@ MonoBehaviour:
x: 0
y: 0
width: 1954.6666
height: 638.6667
height: 829.3333
m_MinSize: {x: 200, y: 50}
m_MaxSize: {x: 16192, y: 8096}
vertical: 0
controlID: 20
controlID: 48
--- !u!114 &11
MonoBehaviour:
m_ObjectHideFlags: 52
Expand All @@ -265,10 +265,10 @@ MonoBehaviour:
m_Children: []
m_Position:
serializedVersion: 2
x: 482
x: 481.33334
y: 0
width: 1472.6666
height: 638.6667
width: 1473.3333
height: 829.3333
m_MinSize: {x: 202, y: 221}
m_MaxSize: {x: 4002, y: 4021}
m_ActualView: {fileID: 17}
Expand All @@ -277,7 +277,7 @@ MonoBehaviour:
- {fileID: 18}
- {fileID: 12}
m_Selected: 0
m_LastSelected: 2
m_LastSelected: 1
--- !u!114 &12
MonoBehaviour:
m_ObjectHideFlags: 52
Expand Down Expand Up @@ -402,9 +402,9 @@ MonoBehaviour:
m_Pos:
serializedVersion: 2
x: 0
y: 711.3334
y: 902
width: 1953.6666
height: 639.6667
height: 449.00006
m_SerializedDataModeController:
m_DataMode: 0
m_PreferredDataMode: 0
Expand All @@ -416,7 +416,7 @@ MonoBehaviour:
m_SaveData: []
m_LockTracker:
m_IsLocked: 0
m_LastSelectedObjectID: 19786
m_LastSelectedObjectID: 19034
--- !u!114 &14
MonoBehaviour:
m_ObjectHideFlags: 52
Expand All @@ -438,9 +438,9 @@ MonoBehaviour:
m_Pos:
serializedVersion: 2
x: 0
y: 711.3334
y: 902
width: 1953.6666
height: 639.6667
height: 449.00006
m_SerializedDataModeController:
m_DataMode: 0
m_PreferredDataMode: 0
Expand All @@ -463,23 +463,23 @@ MonoBehaviour:
m_SkipHidden: 0
m_SearchArea: 1
m_Folders:
- Assets/Animation
- Assets/Events
m_Globs: []
m_OriginalText:
m_ImportLogFlags: 0
m_ViewMode: 1
m_StartGridSize: 16
m_LastFolders:
- Assets/Animation
- Assets/Events
m_LastFoldersGridSize: 16
m_LastProjectPath: D:\Programs\Steam\steamapps\common\RimWorld\Mods\AdvancedAnimationMod\Source\Animations
m_LockTracker:
m_IsLocked: 0
m_FolderTreeState:
scrollPos: {x: 0, y: 0}
m_SelectedIDs: c04e0000
m_LastClickedID: 20160
m_ExpandedIDs: 00000000324e0000c84e000000ca9a3b
m_SelectedIDs: b24e0000
m_LastClickedID: 20146
m_ExpandedIDs: 00000000544e000000ca9a3b
m_RenameOverlay:
m_UserAcceptedRename: 0
m_Name:
Expand Down Expand Up @@ -507,7 +507,7 @@ MonoBehaviour:
scrollPos: {x: 0, y: 0}
m_SelectedIDs:
m_LastClickedID: 0
m_ExpandedIDs: 00000000324e0000
m_ExpandedIDs: 00000000544e0000
m_RenameOverlay:
m_UserAcceptedRename: 0
m_Name:
Expand All @@ -532,8 +532,8 @@ MonoBehaviour:
m_Icon: {fileID: 0}
m_ResourceFile:
m_ListAreaState:
m_SelectedInstanceIDs: 4a4d0000
m_LastClickedInstanceID: 19786
m_SelectedInstanceIDs: 62fbaeff
m_LastClickedInstanceID: -5309598
m_HadKeyboardFocusLastEvent: 1
m_ExpandedInstanceIDs: c6230000
m_RenameOverlay:
Expand All @@ -559,7 +559,7 @@ MonoBehaviour:
m_Icon: {fileID: 0}
m_ResourceFile:
m_NewAssetIndexInList: -1
m_ScrollPosition: {x: 0, y: 0}
m_ScrollPosition: {x: 0, y: 6.9999695}
m_GridSize: 16
m_SkipHiddenPackages: 0
m_DirectoriesAreaWidth: 207
Expand Down Expand Up @@ -631,8 +631,8 @@ MonoBehaviour:
serializedVersion: 2
x: 0
y: 72.66667
width: 481
height: 617.6667
width: 480.33334
height: 808.3333
m_SerializedDataModeController:
m_DataMode: 0
m_PreferredDataMode: 0
Expand All @@ -645,9 +645,9 @@ MonoBehaviour:
m_SceneHierarchy:
m_TreeViewState:
scrollPos: {x: 0, y: 0}
m_SelectedIDs: 4a4d0000
m_LastClickedID: 19786
m_ExpandedIDs: 2afbffff584b00004c4c00004a4d0000
m_SelectedIDs: 5a4a0000
m_LastClickedID: 19034
m_ExpandedIDs: 2afbffffe4490000e8490000ec490000024a0000124a00005a4a00009a4a0000c04a0000da4a00000a4b00000e4b0000344b0000444b00005e4b0000a04b0000ba4b0000be4b0000c24b0000c64b0000d64b0000104c0000264c00006c4c0000764c00008a4c00008e4c0000924c0000964c0000a04c0000aa4c0000c04c0000c84c0000244d0000384d00003c4d00004c4d00007a4d00008a4d0000944d0000984d00009c4d0000a64d0000
m_RenameOverlay:
m_UserAcceptedRename: 0
m_Name:
Expand Down Expand Up @@ -691,10 +691,10 @@ MonoBehaviour:
m_Tooltip:
m_Pos:
serializedVersion: 2
x: 482
x: 481.33334
y: 72.66667
width: 1470.6666
height: 617.6667
width: 1471.3333
height: 808.3333
m_SerializedDataModeController:
m_DataMode: 0
m_PreferredDataMode: 0
Expand Down Expand Up @@ -987,9 +987,9 @@ MonoBehaviour:
m_PlayAudio: 0
m_AudioPlay: 0
m_Position:
m_Target: {x: 0.8257154, y: -1.8396626, z: 0.31329566}
m_Target: {x: -0.12817527, y: 0.20179069, z: 0.04685791}
speed: 2
m_Value: {x: 0.8257154, y: -1.8396626, z: 0.31329566}
m_Value: {x: -0.12817527, y: 0.20179069, z: 0.04685791}
m_RenderMode: 0
m_CameraMode:
drawMode: 0
Expand Down Expand Up @@ -1039,9 +1039,9 @@ MonoBehaviour:
speed: 2
m_Value: {x: -0.7071068, y: 0, z: -0, w: -0.7071068}
m_Size:
m_Target: 1.4144411
m_Target: 5.2517457
speed: 2
m_Value: 1.4144411
m_Value: 5.0255938
m_Ortho:
m_Target: 1
speed: 2
Expand Down Expand Up @@ -1089,7 +1089,7 @@ MonoBehaviour:
x: 482
y: 72.66667
width: 1470.6666
height: 857
height: 808.3333
m_SerializedDataModeController:
m_DataMode: 0
m_PreferredDataMode: 0
Expand All @@ -1105,7 +1105,7 @@ MonoBehaviour:
m_ShowGizmos: 0
m_TargetDisplay: 0
m_ClearColor: {r: 0, g: 0, b: 0, a: 0}
m_TargetSize: {x: 2206, y: 1254}
m_TargetSize: {x: 2206, y: 1181}
m_TextureFilterMode: 0
m_TextureHideFlags: 61
m_RenderIMGUI: 1
Expand All @@ -1127,8 +1127,8 @@ MonoBehaviour:
vZoomLockedByDefault: 0
m_HBaseRangeMin: -735.3334
m_HBaseRangeMax: 735.3334
m_VBaseRangeMin: -418
m_VBaseRangeMax: 418
m_VBaseRangeMin: -393.6667
m_VBaseRangeMax: 393.6667
m_HAllowExceedBaseRangeMin: 1
m_HAllowExceedBaseRangeMax: 1
m_VAllowExceedBaseRangeMin: 1
Expand All @@ -1147,22 +1147,22 @@ MonoBehaviour:
x: 0
y: 21
width: 1470.6666
height: 836
height: 787.3333
m_Scale: {x: 0.99999994, y: 0.99999994}
m_Translation: {x: 735.3333, y: 418}
m_Translation: {x: 735.3333, y: 393.66666}
m_MarginLeft: 0
m_MarginRight: 0
m_MarginTop: 0
m_MarginBottom: 0
m_LastShownAreaInsideMargins:
serializedVersion: 2
x: -735.3334
y: -418.00003
y: -393.6667
width: 1470.6667
height: 836.00006
height: 787.3334
m_MinimalGUI: 1
m_defaultScale: 0.99999994
m_LastWindowPixelSize: {x: 2206, y: 1285.5}
m_LastWindowPixelSize: {x: 2206, y: 1212.5}
m_ClearInEditMode: 1
m_NoCameraWarning: 1
m_LowResolutionForAspectRatios: 00000000000000000000
Expand Down
2 changes: 1 addition & 1 deletion Source/CAI5000Patch/AntiRetreatPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class AntiRetreatPatch
{
public static bool Prefix(Pawn pawn, ref Job __result)
{
if (pawn.TryGetAnimator() == null)
if (pawn != null && pawn.TryGetAnimator() == null)
return true;

__result = null;
Expand Down
22 changes: 12 additions & 10 deletions Source/CAI5000Patch/CAI5000Patch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
<DebugType>none</DebugType>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\ThingGenerator\AnimationMod.csproj">
<CopyLocal>False</CopyLocal>
<Private>False</Private>
<ExcludeAssets>all</ExcludeAssets>
</ProjectReference>
<PackageReference Include="Lib.Harmony" Version="2.2.2">
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
</ItemGroup>

<!-- _____ 1.4 _____ -->
<ItemGroup Condition="'$(Configuration)'=='v1.4'">
<PackageReference Include="Krafs.Rimworld.Ref">
Expand All @@ -28,16 +39,7 @@
<ExcludeAssets>runtime</ExcludeAssets>
</Reference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Lib.Harmony" Version="2.2.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ThingGenerator\AnimationMod.csproj">
<CopyLocal>False</CopyLocal>
<Private>False</Private>
<ExcludeAssets>all</ExcludeAssets>
</ProjectReference>
</ItemGroup>


<!-- Output -->
<PropertyGroup Condition="'$(Configuration)'=='v1.4'">
Expand Down
1 change: 1 addition & 0 deletions Source/CAI5000Patch/PatchCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace AM.CAI5000Patch;

[HotSwapAll]
[UsedImplicitly]
public class PatchCore : Mod
{
Expand Down
57 changes: 57 additions & 0 deletions Source/CAI5000Patch/ThingComp_CombatAI_CompTickRare_Transpiler.cs
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;
}
}
Loading

0 comments on commit 09ac4ce

Please sign in to comment.