From e64253a7ec67103e2d8b90a53832bc61d9587c18 Mon Sep 17 00:00:00 2001 From: NathanKell Date: Mon, 23 Oct 2023 00:09:28 -0700 Subject: [PATCH] Patch resource panel. Use units, use SI, flip negative sign, account for warp --- Source/Harmony/ResourceItem.cs | 51 ++++++++++++++++++++++++++++++++++ Source/RealismOverhaul.csproj | 1 + Source/ResourceUnitInfo.cs | 28 +++++++++++++++++-- 3 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 Source/Harmony/ResourceItem.cs diff --git a/Source/Harmony/ResourceItem.cs b/Source/Harmony/ResourceItem.cs new file mode 100644 index 0000000000..ea306cb8a1 --- /dev/null +++ b/Source/Harmony/ResourceItem.cs @@ -0,0 +1,51 @@ +using HarmonyLib; +using KSP.Localization; +using KSP.UI.Screens; +using System.Reflection.Emit; +using System.Reflection; +using System.Collections.Generic; + +namespace RealismOverhaul.Harmony +{ + [HarmonyPatch(typeof(ResourceItem))] + internal class PatchResourceItem + { + [HarmonyTranspiler] + [HarmonyPatch("FixedUpdate")] + internal static IEnumerable Transpiler_FixedUpdate(IEnumerable instructions) + { + List code = new List(instructions); + for (int i = 1; i < code.Count; ++i) + { + if (code[i].opcode == OpCodes.Call && (MethodInfo)code[i].operand == AccessTools.Method(typeof(UnityEngine.Time), "get_fixedDeltaTime")) + { + code[i].operand = AccessTools.Method(typeof(TimeWarp), "get_fixedDeltaTime"); + break; // let the smoothing still occur in realtime + } + } + + return code; + } + + [HarmonyPrefix] + [HarmonyPatch("Update")] + internal static bool Prefix_Update(ResourceItem __instance) + { + int resID = __instance.resourceID; + + __instance.resourceBar.value = (float)__instance.resourceValue; + + __instance.amountText.text = "" + ResourceUnits.PrintAmount(__instance.vesselResourceCurrent, resID, 5, __instance.vesselResourceCurrent > 100 ? "F0" : "F2") + ""; + __instance.maxText.text = "" + ResourceUnits.PrintAmount(__instance.vesselResourceTotal, resID, 5, __instance.vesselResourceCurrent > 100 ? "F0" : "F2") + ""; + + if (__instance.deltaSmoothed == 0f) + __instance.deltaText.text = "(0)"; + else if (__instance.deltaSmoothed > 0f) + __instance.deltaText.text = "(-" + ResourceUnits.PrintRatePerSecBare(-__instance.deltaSmoothed, resID, 3, "F2") + ")"; + else + __instance.deltaText.text = "(" + ResourceUnits.PrintRatePerSecBare(-__instance.deltaSmoothed, resID, 3, "F2") + ")"; + + return false; + } + } +} diff --git a/Source/RealismOverhaul.csproj b/Source/RealismOverhaul.csproj index f4245c6e8c..f8aa1180b0 100644 --- a/Source/RealismOverhaul.csproj +++ b/Source/RealismOverhaul.csproj @@ -38,6 +38,7 @@ + diff --git a/Source/ResourceUnitInfo.cs b/Source/ResourceUnitInfo.cs index e3a855052f..2dce098560 100644 --- a/Source/ResourceUnitInfo.cs +++ b/Source/ResourceUnitInfo.cs @@ -96,6 +96,29 @@ public static string PrintMassRate(double massFlow) { return massFlow < 1d ? KSPUtil.PrintSI(massFlow * 1000d * 1000d, "g") : KSPUtil.PrintSI(massFlow, "t"); } + + public static string PrintRatePerSecBare(double rate, int resID, int sigFigs = 3, string precision = "G2", bool longPrefix = false) + { + string unitRate = string.Empty; + bool useSI = false; + if (GetResourceUnitInfo(resID) is ResourceUnitInfo rui) + { + unitRate = rui.RateUnit; + rate *= rui.MultiplierToUnit; + if (rui.UseHuman) + { + useSI = false; + } + else + { + useSI = true; + } + } + if (useSI) + return KSPUtil.PrintSI(rate, unitRate, sigFigs, longPrefix); + else + return rate.ToString(precision) + unitRate; + } public static string PrintRate(double rate, int resID, bool showFlowMode, ModuleResource res = null, Propellant p = null, bool showMass = false, int sigFigs = 3, bool longPrefix = false) { @@ -168,13 +191,12 @@ public static string PrintRate(double rate, int resID, bool showFlowMode, Module return output; } - public static string PrintAmount(double amount, int resID, int sigFigs = 3, string precision = "G2", bool longPrefix = false) + public static string PrintAmount(double amount, int resID, int sigFigs = 3, string precision = "G2", bool preventSI = false, bool longPrefix = false) { string unit = string.Empty; if (GetResourceUnitInfo(resID) is ResourceUnitInfo rui) { - - if (rui.UseHuman) + if (rui.UseHuman || preventSI) unit = rui.AmountUnit; else return PrintSIAmount(amount, rui, sigFigs, longPrefix);