Skip to content

Commit

Permalink
Merge pull request #6 from legodude17/master
Browse files Browse the repository at this point in the history
Fix transpilers for 1.3
  • Loading branch information
juanosarg authored Jul 8, 2021
2 parents 175d213 + b5c6be8 commit 30a0c83
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 96 deletions.
Binary file modified 1.3/Assemblies/VanillaPowerExpanded.dll
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,27 @@ namespace GasNetwork
public class CompGasTrader_Buffer : CompGasTrader
{
private static readonly StringBuilder sb = new StringBuilder();
private bool bufferOff;
private float desired;
private float stored;
private bool bufferOff;
private float desired;
private float stored;

public new CompProperties_GasTrader_Buffer Props => props as CompProperties_GasTrader_Buffer;

public bool HasFuel => stored > 0;
public float Fuel => stored;
public bool HasFuel => stored > 0;
public float Fuel => stored;

public virtual Vector2 BarSize => new Vector2(1f, .16f);

public override void PostSpawnSetup(bool respawningAfterLoad)
{
base.PostSpawnSetup(respawningAfterLoad);
if (!respawningAfterLoad)
{
desired = Props.maxBuffer;
}
if (!respawningAfterLoad) desired = Props.maxBuffer;
}

public void ConsumeFuel(float amount)
{
stored -= amount;
if (stored < 0)
{
stored = 0;
}
if (stored < 0) stored = 0;
}

public override void CompTick()
Expand All @@ -52,22 +46,15 @@ public override void CompTick()
if (GasOn)
{
stored += GasConsumptionPerTick;
if (stored > Props.maxBuffer)
{
stored = Props.maxBuffer;
}
if (stored > Props.maxBuffer) stored = Props.maxBuffer;
}

if (parent.IsHashIntervalTick(GenTicks.TicksPerRealSecond))
{
if (bufferOff || stored >= desired)
{
GasConsumption = 0;
}
else
{
GasConsumption = Props.gasConsumption;
}
}
}

Expand All @@ -77,23 +64,16 @@ public override string CompInspectStringExtra()
sb.AppendLine(base.CompInspectStringExtra());
sb.AppendLine(I18n.Stored(stored, Mathf.RoundToInt(Props.maxBuffer)));
if (bufferOff)
{
sb.AppendLine(I18n.BufferOff);
}
else if (Math.Abs(desired - Props.maxBuffer) > Mathf.Epsilon)
{
sb.AppendLine(I18n.DesiredBuffer(desired, Props.maxBuffer));
}

return sb.ToString().Trim();
}

public override IEnumerable<Gizmo> CompGetGizmosExtra()
{
foreach (var gizmo in base.CompGetGizmosExtra())
{
yield return gizmo;
}
foreach (var gizmo in base.CompGetGizmosExtra()) yield return gizmo;

// TODO: Add buffer toggle gizmo.
// TODO: Add buffer size gizmo.
Expand All @@ -113,13 +93,13 @@ public override void PostDraw()
base.PostDraw();
var barRequest = new GenDraw.FillableBarRequest
{
center = parent.DrawPos + Vector3.up * .2f + Vector3.forward * .25f,
size = BarSize,
center = parent.DrawPos + Vector3.up * .2f + Vector3.forward * .25f,
size = BarSize,
fillPercent = stored / Props.maxBuffer,
filledMat = Resources.GasBarFilledMaterial,
filledMat = Resources.GasBarFilledMaterial,
unfilledMat = Resources.GasBarUnfilledMaterial,
margin = .1f,
rotation = parent.Rotation.Rotated(RotationDirection.Clockwise)
margin = .1f,
rotation = parent.Rotation.Rotated(RotationDirection.Clockwise)
};
GenDraw.DrawFillableBar(barRequest);
}
Expand All @@ -131,9 +111,9 @@ public class Command_Buffer : Command

public class CompProperties_GasTrader_Buffer : CompProperties_GasTrader
{
public float maxBuffer = 125;
public bool showBufferSlider = true;
public bool showBufferToggle = true;
public float maxBuffer = 125;
public bool showBufferSlider = true;
public bool showBufferToggle = true;
}

[HarmonyPatch(typeof(CompLaunchable), nameof(CompLaunchable.FuelingPortSourceHasAnyFuel), MethodType.Getter)]
Expand All @@ -151,15 +131,9 @@ public static bool Prefix(ref bool __result, CompLaunchable __instance)
var fuelingPort = __instance.FuelingPortSource;
if (fuelingPort != null)
{
if (fuelingPort.TryGetComp<CompRefuelable>(out var refuelable))
{
__result = refuelable.HasFuel;
}

if (fuelingPort.TryGetComp<CompGasTrader_Buffer>(out var buffer))
{
__result |= buffer.HasFuel;
}
if (fuelingPort.TryGetComp<CompRefuelable>(out var refuelable)) __result = refuelable.HasFuel;

if (fuelingPort.TryGetComp<CompGasTrader_Buffer>(out var buffer)) __result |= buffer.HasFuel;
}
}

Expand All @@ -178,13 +152,8 @@ public static bool Prefix(ref float __result, CompLaunchable __instance)
{
var fuelingPort = __instance.FuelingPortSource;
if (fuelingPort.TryGetComp<CompRefuelable>(out var refuelable))
{
__result = refuelable.Fuel;
}
else if (fuelingPort.TryGetComp<CompGasTrader_Buffer>(out var buffer))
{
__result = buffer.Fuel;
}
else if (fuelingPort.TryGetComp<CompGasTrader_Buffer>(out var buffer)) __result = buffer.Fuel;
}

// the vanilla method assumes that CompRefuelable exists.
Expand All @@ -196,7 +165,7 @@ public static bool Prefix(ref float __result, CompLaunchable __instance)
public static class CompLaunchable_TryLaunch
{
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> _instructions,
ILGenerator generator)
ILGenerator generator)
{
var tryGetComp_MI = AccessTools.Method(typeof(ThingCompUtility), nameof(ThingCompUtility.TryGetComp));
var tryGetComp_CompRefuelable_MI = tryGetComp_MI.MakeGenericMethod(typeof(CompRefuelable));
Expand All @@ -217,24 +186,29 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
{
// Add null check for vanilla CompRefuelable.ConsumeFuel call, because vanilla assumes compRefuelable exists on fuelingPortSource.
var compRefuelableIsNull = generator.DefineLabel();
var refuelableFinished = generator.DefineLabel();
yield return new CodeInstruction(OpCodes.Dup); // duplicate compRefuelable on the stack
yield return new CodeInstruction(OpCodes.Brfalse_S, compRefuelableIsNull); // break to beyond ConsumeFuel call if null
yield return instructions[i + 1]; // load fuel amount onto the stack
yield return instructions[i + 2]; // call CompRefuelable.ConsumeFuel
yield return new CodeInstruction(OpCodes.Br, refuelableFinished); // stack is OK
var refuelableFinished = generator.DefineLabel();
yield return new CodeInstruction(OpCodes.Dup); // duplicate compRefuelable on the stack
yield return
new CodeInstruction(OpCodes.Brfalse_S,
compRefuelableIsNull); // break to beyond ConsumeFuel call if null
yield return instructions[i + 1]; // load fuel amount onto the stack
yield return instructions[i + 2]; // call CompRefuelable.ConsumeFuel
yield return new CodeInstruction(OpCodes.Br, refuelableFinished); // stack is OK
yield return new CodeInstruction(OpCodes.Pop).WithLabels(compRefuelableIsNull);

// Add conditional call to CompGasTrader_Buffer.ConsumeFuel
var compBufferIsNull = generator.DefineLabel();
var bufferFinished = generator.DefineLabel();
yield return new CodeInstruction(OpCodes.Ldloc_S, 11).WithLabels(refuelableFinished); // (Building) FuelingPortSource
yield return new CodeInstruction(OpCodes.Call, tryGetComp_CompGasTrader_Buffer_MI); // compBuffer
var bufferFinished = generator.DefineLabel();
yield return
new CodeInstruction(OpCodes.Ldloc_S, 7)
.WithLabels(refuelableFinished); // (Building) FuelingPortSource
yield return new CodeInstruction(OpCodes.Call, tryGetComp_CompGasTrader_Buffer_MI); // compBuffer
yield return new CodeInstruction(OpCodes.Dup);
yield return new CodeInstruction(OpCodes.Brfalse_S, compBufferIsNull);
yield return new CodeInstruction(OpCodes.Ldloc_S, 4); // fuel amount
yield return new CodeInstruction(OpCodes.Callvirt, consumeFuel_CompGasTrader_Buffer_MI); // consume fuel
yield return new CodeInstruction(OpCodes.Br, bufferFinished);
yield return new CodeInstruction(OpCodes.Ldloc_S, 4); // fuel amount
yield return
new CodeInstruction(OpCodes.Callvirt, consumeFuel_CompGasTrader_Buffer_MI); // consume fuel
yield return new CodeInstruction(OpCodes.Br, bufferFinished);
yield return new CodeInstruction(OpCodes.Pop).WithLabels(compBufferIsNull);

// done!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,11 @@ public static partial class Helpers
public static bool GasCheck(ThingDef constructible, ThingDef target)
{
Log.Debug($"c: {constructible.defName}, t: {target.defName}");
if (!constructible.EverTransmitsGas())
{
return false;
}
if (!constructible.EverTransmitsGas()) return false;

if (target != DefOf.VPE_GasPipe && target != DefOf.VPE_GasPipeSub)
{
return false;
}
if (target != DefOf.VPE_GasPipe && target != DefOf.VPE_GasPipeSub) return false;

if (constructible == DefOf.VPE_GasPipe || constructible == DefOf.VPE_GasPipeSub)
{
return false;
}
if (constructible == DefOf.VPE_GasPipe || constructible == DefOf.VPE_GasPipeSub) return false;

return true;
}
Expand Down Expand Up @@ -63,24 +54,27 @@ public class GenConstruct_CanPlaceBlueprintOver
* IL_0183: brfalse.s IL_019C
*/
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> _instructions,
ILGenerator generator)
ILGenerator generator)
{
var instructions = _instructions.ToList();
for (var i = 0; i < instructions.Count; i++)
{
yield return instructions[i];

if (i > 4
&& instructions[i - 4].LoadsField(Helpers.PowerConduit_FI)
&& instructions[i - 3].Branches(out _)
&& instructions[i - 2].LoadsConstant(1L)
&& instructions[i - 1].opcode == OpCodes.Ret
&& instructions[i - 0].opcode == OpCodes.Ldarg_0)
&& instructions[i - 4].LoadsField(Helpers.PowerConduit_FI)
&& instructions[i - 3].Branches(out _)
&& instructions[i - 2].LoadsConstant(1L)
&& instructions[i - 1].opcode == OpCodes.Ret
&& instructions[i - 0].opcode == OpCodes.Ldarg_0)
{
yield return new CodeInstruction(OpCodes.Ldloc_3);
yield return new CodeInstruction(OpCodes.Ldloc_2);
yield return new CodeInstruction(OpCodes.Ldfld,
AccessTools.Field(AccessTools.Inner(typeof(GenConstruct), "<>c__DisplayClass16_0"),
"oldDefBuilt"));
yield return new CodeInstruction(OpCodes.Call, AccessTools.Method(
typeof(Helpers),
nameof(Helpers.GasCheck)));
typeof(Helpers),
nameof(Helpers.GasCheck)));

var label = generator.DefineLabel();
yield return new CodeInstruction(OpCodes.Brfalse, label);
Expand Down Expand Up @@ -126,29 +120,29 @@ public class GenConstruct_BlocksConstruction
AccessTools.Field(typeof(Thing), nameof(Thing.def));

public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> _instructions,
ILGenerator generator)
ILGenerator generator)
{
var instructions = _instructions.ToList();
for (var i = 0; i < instructions.Count; i++)
{
yield return instructions[i];

if (i > 5
&& instructions[i - 5].LoadsField(Helpers.PowerConduit_FI)
&& instructions[i - 4].Branches(out var _dump)
&& instructions[i - 3].LoadsConstant(0L)
&& instructions[i - 2].opcode == OpCodes.Ret
&& instructions[i - 1].opcode == OpCodes.Ldarg_1
&& instructions[i - 0].LoadsField(def_FI))
&& instructions[i - 5].LoadsField(Helpers.PowerConduit_FI)
&& instructions[i - 4].Branches(out var _dump)
&& instructions[i - 3].LoadsConstant(0L)
&& instructions[i - 2].opcode == OpCodes.Ret
&& instructions[i - 1].opcode == OpCodes.Ldarg_1
&& instructions[i - 0].LoadsField(def_FI))

{
// push constructible.def
yield return new CodeInstruction(OpCodes.Ldloc_1);
// pop target.def & constructible.def, push bool
yield return new CodeInstruction(OpCodes.Call,
AccessTools.Method(
typeof(Helpers),
nameof(Helpers.GasCheck2)));
AccessTools.Method(
typeof(Helpers),
nameof(Helpers.GasCheck2)));

// on false, skip to next check.
// on true control passes to returning false.
Expand Down

0 comments on commit 30a0c83

Please sign in to comment.