Skip to content

Commit

Permalink
Enhance power charger UI window to show count of other charging players
Browse files Browse the repository at this point in the history
  • Loading branch information
starfi5h committed Jan 4, 2024
1 parent cd8516a commit 1ea91a8
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions NebulaPatcher/Patches/Transpilers/UIPowerNodeWindow_Transpiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using NebulaModel.Logger;
using NebulaWorld;

#endregion

namespace NebulaPatcher.Patches.Transpilers;

[HarmonyPatch(typeof(UIPowerNodeWindow))]
internal class UIPowerNodeWindow_Transpiler
{
[HarmonyTranspiler]
[HarmonyPatch(nameof(UIPowerNodeWindow._OnUpdate))]
public static IEnumerable<CodeInstruction> OnUpdate_Transpiler(IEnumerable<CodeInstruction> instructions)
{
var codeInstructions = instructions as CodeInstruction[] ?? instructions.ToArray();
try
{

// from: if (powerNodeComponent.requiredEnergy > powerNodeComponent.idleEnergyPerTick && this.player.mecha.energyChanges[2] > 0.0)
// to: if (powerNodeComponent.requiredEnergy > powerNodeComponent.idleEnergyPerTick)
var codeMatcher = new CodeMatcher(codeInstructions)
.MatchForward(false,
new CodeMatch(OpCodes.Ldarg_0),
new CodeMatch(OpCodes.Ldfld),
new CodeMatch(i => i.opcode == OpCodes.Callvirt && ((MethodInfo)i.operand).Name == "get_mecha"),
new CodeMatch(i => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "energyChanges")
)
.RemoveInstructions(8);

// from: this.chargeStateValueText.text = "正在充电".Translate();
// to: this.chargeStateValueText.text = ChargeStateText("正在充电".Translate());
codeMatcher.MatchForward(false,
new CodeMatch(i => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "chargeStateValueText"))
.MatchForward(false,
new CodeMatch(i => i.opcode == OpCodes.Callvirt && ((MethodInfo)i.operand).Name == "set_text")
)
.Insert(
new CodeInstruction(OpCodes.Ldarg_0),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(UIPowerNodeWindow_Transpiler), nameof(ChargeStateText)))
);

return codeMatcher.InstructionEnumeration();
}
catch (Exception e)
{
Log.Warn("UIPowerNodeWindow._OnUpdate Transpiler failed. Charger UI will be unchanged.");
Log.Warn(e);
return codeInstructions;
}
}

private static string ChargeStateText(string oringalValue, UIPowerNodeWindow powerNodeWindow)
{
if (!Multiplayer.IsActive)
{
return oringalValue;
}
var hashId = ((long)powerNodeWindow.factory.planetId << 32) | (long)powerNodeWindow.nodeId;
Multiplayer.Session.PowerTowers.RemoteChargerHashIds.TryGetValue(hashId, out var count);
return oringalValue + '[' + count + ']';
}
}

0 comments on commit 1ea91a8

Please sign in to comment.