-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathPatch_JobSystem.cs
More file actions
174 lines (158 loc) · 5.5 KB
/
Copy pathPatch_JobSystem.cs
File metadata and controls
174 lines (158 loc) · 5.5 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using HarmonyLib;
using RimWorld;
using SmashTools.Patching;
using Verse;
using Verse.AI;
using OpCodes = System.Reflection.Emit.OpCodes;
namespace Vehicles;
internal class Patch_JobSystem : IPatchCategory
{
private static bool startingErrorRecoverJob;
PatchSequence IPatchCategory.PatchAt => PatchSequence.Async;
void IPatchCategory.PatchMethods()
{
HarmonyPatcher.Patch(
original: AccessTools.Method(typeof(JobUtility),
nameof(JobUtility.TryStartErrorRecoverJob)),
prefix: new HarmonyMethod(typeof(Patch_JobSystem),
nameof(VehicleErrorRecoverJob)));
HarmonyPatcher.Patch(original: AccessTools.Method(typeof(JobGiver_Wander), "TryGiveJob"),
prefix: new HarmonyMethod(typeof(Patch_JobSystem),
nameof(VehiclesDontWander)));
HarmonyPatcher.Patch(
original: AccessTools.Method(typeof(Pawn_JobTracker),
nameof(Pawn_JobTracker.CheckForJobOverride)), prefix: null, postfix: null,
transpiler: new HarmonyMethod(typeof(Patch_JobSystem),
nameof(NoOverrideDamageTakenTranspiler)));
HarmonyPatcher.Patch(
original: AccessTools
.Property(typeof(JobDriver_PrepareCaravan_GatherItems), "Transferables")
.GetGetMethod(nonPublic: true),
prefix: new HarmonyMethod(typeof(Patch_JobSystem),
nameof(TransferablesVehicle)));
HarmonyPatcher.Patch(
original: AccessTools.Method(typeof(ThingRequest), nameof(ThingRequest.Accepts)),
prefix: null,
postfix: new HarmonyMethod(typeof(Patch_JobSystem),
nameof(AcceptsVehicleRefuelable)));
}
/// <summary>
/// Intercept Error Recover handler of no job, and assign idling for vehicle
/// </summary>
/// <param name="pawn"></param>
/// <param name="message"></param>
/// <param name="exception"></param>
/// <param name="concreteDriver"></param>
public static bool VehicleErrorRecoverJob(Pawn pawn, string message, Exception exception = null,
JobDriver concreteDriver = null)
{
if (pawn is VehiclePawn)
{
if (exception != null)
{
message += $"\n{exception}";
}
Log.Error(message);
if (pawn.jobs != null)
{
if (pawn.jobs.curJob != null)
{
pawn.jobs.EndCurrentJob(JobCondition.Errored, false);
}
if (startingErrorRecoverJob)
{
Log.Error(
$"An error occurred while starting an error recover job. We have to stop now to avoid infinite recursion. This means that the vehicle is now jobless which can cause further bugs. vehicle={pawn}");
return false;
}
startingErrorRecoverJob = true;
try
{
if (pawn.jobs.jobQueue.Count > 0)
{
Job job = pawn.jobs.jobQueue.Dequeue().job;
pawn.jobs.StartJob(job, JobCondition.Succeeded);
}
else
{
pawn.jobs.StartJob(JobMaker.MakeJob(JobDefOf_Vehicles.IdleVehicle, -1),
JobCondition.Incompletable);
}
startingErrorRecoverJob = false;
}
catch
{
Log.Error(
$"An error occurred when trying to recover the job for {pawn}. Unable to assign idle recovery job.");
}
finally
{
startingErrorRecoverJob = false;
}
}
return false;
}
return true;
}
public static bool VehiclesDontWander(Pawn pawn, ref Job __result)
{
if (pawn is VehiclePawn)
{
if (__result is not null)
{
JobMaker.ReturnToPool(__result);
}
__result = JobMaker.MakeJob(JobDefOf_Vehicles.IdleVehicle);
return false;
}
return true;
}
public static IEnumerable<CodeInstruction> NoOverrideDamageTakenTranspiler(
IEnumerable<CodeInstruction> instructions, ILGenerator ilg)
{
List<CodeInstruction> instructionList = instructions.ToList();
for (int i = 0; i < instructionList.Count; i++)
{
CodeInstruction instruction = instructionList[i];
if (instruction.opcode == OpCodes.Stloc_1)
{
yield return instruction; //STLOC.1
instruction = instructionList[++i];
Label label = ilg.DefineLabel();
Label retlabel = ilg.DefineLabel();
yield return new CodeInstruction(opcode: OpCodes.Ldloc_0);
yield return new CodeInstruction(opcode: OpCodes.Brfalse, retlabel);
yield return new CodeInstruction(opcode: OpCodes.Ldloca_S, operand: 1);
yield return new CodeInstruction(opcode: OpCodes.Call,
operand: AccessTools.Property(typeof(ThinkResult), nameof(ThinkResult.IsValid))
.GetGetMethod());
yield return new CodeInstruction(opcode: OpCodes.Brtrue, label);
yield return new CodeInstruction(opcode: OpCodes.Ret)
{ labels = new List<Label> { retlabel } };
instruction.labels.Add(label);
}
yield return instruction;
}
}
public static bool TransferablesVehicle(JobDriver_PrepareCaravan_GatherItems __instance,
ref List<TransferableOneWay> __result)
{
if (__instance.job.lord.LordJob is LordJob_FormAndSendVehicles)
{
__result = ((LordJob_FormAndSendVehicles)__instance.job.lord.LordJob).transferables;
return false;
}
return true;
}
public static void AcceptsVehicleRefuelable(Thing t, ref bool __result, ThingRequest __instance)
{
if (t is VehiclePawn vehicle && __instance.group == ThingRequestGroup.Refuelable)
{
__result = vehicle.CompFueledTravel != null;
}
}
}