Skip to content

Commit 255a5c2

Browse files
committed
Update TankComponentPatches.cs
1 parent 965304f commit 255a5c2

1 file changed

Lines changed: 46 additions & 28 deletions

File tree

src/Patches/Hooks/TankComponentPatches.cs

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,49 @@ public static IEnumerable<CodeInstruction> TankComponent_GameTick_Transpiler(IEn
2828
if (cargoTraffic.TryInsertItemAtHead(this.belt0, this.fluidId, (byte) currentOutputStack, (byte) inc2 * currentOutputStack))
2929
{
3030
this.fluidCount -= currentOutputStack;
31-
this.fluidInc -= inc2;
31+
this.fluidInc -= inc2 * currentOutputStack;
3232
}
3333
*/
3434

3535
LocalBuilder local = ilGenerator.DeclareLocal(typeof(int));
3636
local.SetLocalSymInfo("currentOutputStack");
3737
var localIndex = local.LocalIndex;
3838

39-
matcher.Start().InsertAndAdvance(new CodeInstruction(OpCodes.Ldarg_0),
40-
new CodeInstruction(OpCodes.Call,
41-
AccessTools.Method(typeof(TankComponentPatches), nameof(TankComponent_GameTick_Insert_Method))),
42-
new CodeInstruction(OpCodes.Stloc_S, localIndex));
43-
4439
while (true)
4540
{
4641
/*
47-
48-
IL_01e4: ldarg.0 // this
49-
IL_01e5: ldfld int32 TankComponent::fluidInc
50-
IL_01ea: ldarg.0 // this
51-
IL_01eb: ldfld int32 TankComponent::fluidCount
52-
IL_01f0: div
53-
IL_01f1: br.s IL_01f4
54-
IL_01f3: ldc.i4.0
55-
IL_01f4: stloc.s inc3
42+
IL_04ae: ldarg.0 // this
43+
IL_04af: ldfld int32 TankComponent::fluidInc
44+
IL_04b4: brfalse.s IL_04c5
45+
46+
IL_04b6: ldarg.0 // this
47+
IL_04b7: ldfld int32 TankComponent::fluidInc
48+
IL_04bc: ldarg.0 // this
49+
IL_04bd: ldfld int32 TankComponent::fluidCount
50+
IL_04c2: div
51+
IL_04c3: br.s IL_04c6
52+
IL_04c5: ldc.i4.0
53+
IL_04c6: stloc.s inc5
54+
55+
// [302 7 - 302 94]
56+
IL_04c8: ldloc.2 // cargoTraffic
57+
IL_04c9: ldarg.0 // this
58+
IL_04ca: ldfld int32 TankComponent::belt3
59+
IL_04cf: ldarg.0 // this
60+
IL_04d0: ldfld int32 TankComponent::fluidId
61+
IL_04d5: ldc.i4.1
62+
IL_04d6: ldloc.s inc5
63+
IL_04d8: conv.u1
64+
IL_04d9: callvirt instance bool CargoTraffic::TryInsertItemAtHead(int32, int32, unsigned int8, unsigned int8)
65+
IL_04de: brfalse IL_05df
66+
67+
// [304 7 - 304 24]
68+
IL_04e3: ldarg.0 // this
69+
IL_04e4: ldarg.0 // this
70+
IL_04e5: ldfld int32 TankComponent::fluidCount
71+
IL_04ea: ldc.i4.1
72+
IL_04eb: sub
73+
IL_04ec: stfld int32 TankComponent::fluidCount
5674
*/
5775

5876
matcher.MatchForward(false, new CodeMatch(OpCodes.Ldarg_0),
@@ -61,24 +79,25 @@ public static IEnumerable<CodeInstruction> TankComponent_GameTick_Transpiler(IEn
6179

6280
if (matcher.IsInvalid) break;
6381

64-
// tankComponent.fluidInc *= currentOutputStack;
82+
matcher.InsertAndAdvance(new CodeInstruction(OpCodes.Ldarg_0),
83+
new CodeInstruction(OpCodes.Call,
84+
AccessTools.Method(typeof(TankComponentPatches), nameof(TankComponent_GameTick_Insert_Method))),
85+
new CodeInstruction(OpCodes.Stloc_S, localIndex));
86+
87+
// tankComponent.fluidInc * currentOutputStack;
6588
matcher.InsertAndAdvance(new CodeInstruction(OpCodes.Ldloc_S, localIndex), new CodeInstruction(OpCodes.Mul));
6689

67-
matcher.Advance(3);
68-
}
90+
// stack = (byte) currentOutputStack;
91+
matcher.MatchForward(true, new CodeMatch(OpCodes.Ldarg_0),
92+
new CodeMatch(OpCodes.Ldfld, AccessTools.Field(typeof(TankComponent), nameof(TankComponent.fluidId))),
93+
new CodeMatch(OpCodes.Ldc_I4_1));
6994

70-
matcher.Start();
95+
matcher.SetAndAdvance(OpCodes.Ldloc_S, localIndex).InsertAndAdvance(new CodeInstruction(OpCodes.Conv_U1));
7196

72-
// replace all Ldc_I4_1 to currentOutputStack
73-
while (true)
74-
{
75-
matcher.MatchForward(false, new CodeMatch(OpCodes.Ldc_I4_1));
76-
77-
if (matcher.IsInvalid) break;
97+
// this.fluidCount -= currentOutputStack;
98+
matcher.MatchForward(false, new CodeMatch(OpCodes.Ldc_I4_1), new CodeMatch(OpCodes.Sub));
7899

79100
matcher.SetAndAdvance(OpCodes.Ldloc_S, localIndex);
80-
81-
matcher.Advance(1);
82101
}
83102

84103
return matcher.InstructionEnumeration();
@@ -87,7 +106,6 @@ public static IEnumerable<CodeInstruction> TankComponent_GameTick_Transpiler(IEn
87106
public static int TankComponent_GameTick_Insert_Method(ref TankComponent component)
88107
{
89108
int componentFluidCount = component.fluidCount;
90-
91109
int historyStationPilerLevel = GameMain.history.stationPilerLevel;
92110

93111
return componentFluidCount < historyStationPilerLevel ? componentFluidCount : historyStationPilerLevel;

0 commit comments

Comments
 (0)