From a06c3bfae4708e994078a6cd97de1d047b89f7c0 Mon Sep 17 00:00:00 2001 From: NathanKell Date: Mon, 23 Oct 2023 01:22:12 -0700 Subject: [PATCH] Patch Part Tooltip resources, also patch PrintSI so that it doesn't print trailing zeroes. --- Source/Harmony/KSPUtil.cs | 21 +++++++++++++ Source/Harmony/ModuleEngines.cs | 2 +- Source/Harmony/ModuleRCS.cs | 2 +- Source/Harmony/PartLoader.cs | 52 +++++++++++++++++++++++++++++++++ Source/RealismOverhaul.csproj | 2 ++ Source/ResourceUnitInfo.cs | 33 +++++++++++++++++---- 6 files changed, 104 insertions(+), 8 deletions(-) create mode 100644 Source/Harmony/KSPUtil.cs create mode 100644 Source/Harmony/PartLoader.cs diff --git a/Source/Harmony/KSPUtil.cs b/Source/Harmony/KSPUtil.cs new file mode 100644 index 0000000000..74d838f560 --- /dev/null +++ b/Source/Harmony/KSPUtil.cs @@ -0,0 +1,21 @@ +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(KSPUtil))] + internal class PatchKSPUtil + { + [HarmonyPrefix] + [HarmonyPatch("PrintSI")] + internal static bool Prefix_PrintSI(double amount, string unitName, int sigFigs, bool longPrefix, ref string __result) + { + __result = ResourceUnits.PrintSI(amount, unitName, sigFigs, longPrefix); + return false; + } + } +} diff --git a/Source/Harmony/ModuleEngines.cs b/Source/Harmony/ModuleEngines.cs index 834013e178..6bda61589c 100644 --- a/Source/Harmony/ModuleEngines.cs +++ b/Source/Harmony/ModuleEngines.cs @@ -19,7 +19,7 @@ private static string GetInfo(ModuleEngines mod) output += ResourceUnits.PrintRate(flow, propellant.id, true, null, propellant, true); } if (massFlow > 0d) - output += Localizer.Format("#autoLOC_900654") + " " + Localizer.Format("#autoLOC_6001048", ResourceUnits.PrintMassRate(massFlow)) + "\n"; + output += Localizer.Format("#autoLOC_900654") + " " + Localizer.Format("#autoLOC_6001048", ResourceUnits.PrintMass(massFlow)) + "\n"; output += Localizer.Format("#autoLOC_220759", (mod.ignitionThreshold * 100f).ToString("0.#")); if (!mod.allowShutdown) diff --git a/Source/Harmony/ModuleRCS.cs b/Source/Harmony/ModuleRCS.cs index cbf559d735..38a716b148 100644 --- a/Source/Harmony/ModuleRCS.cs +++ b/Source/Harmony/ModuleRCS.cs @@ -19,7 +19,7 @@ private static string GetInfo(ModuleRCS mod) output += ResourceUnits.PrintRate(flow, propellant.id, false, null, propellant, true); } if (massFlow > 0d) - output += Localizer.Format("#autoLOC_900654") + " " + Localizer.Format("#autoLOC_6001048", ResourceUnits.PrintMassRate(massFlow)) + "\n"; + output += Localizer.Format("#autoLOC_900654") + " " + Localizer.Format("#autoLOC_6001048", ResourceUnits.PrintMass(massFlow)) + "\n"; if (!mod.moduleIsEnabled) { diff --git a/Source/Harmony/PartLoader.cs b/Source/Harmony/PartLoader.cs new file mode 100644 index 0000000000..c4aac4bb4d --- /dev/null +++ b/Source/Harmony/PartLoader.cs @@ -0,0 +1,52 @@ +using HarmonyLib; +using KSP.Localization; +using System.Collections.Generic; + +namespace RealismOverhaul.Harmony +{ + [HarmonyPatch(typeof(PartLoader))] + internal class PatchPartLoader + { + [HarmonyPostfix] + [HarmonyPatch("CompilePartInfo")] + internal static void Postfix_CompilePartInfo(AvailablePart newPartInfo, Part part) + { + // better to fix in place, but we'll just recreate. + newPartInfo.resourceInfos.Clear(); + + string rInfoStr = string.Empty; + foreach(var partResource in part.Resources) + { + int resID = partResource.info.id; + AvailablePart.ResourceInfo resourceInfo = new AvailablePart.ResourceInfo(); + resourceInfo.resourceName = partResource.resourceName; + resourceInfo.displayName = partResource.info.displayName.LocalizeRemoveGender(); + resourceInfo.info = Localizer.Format("#autoLOC_166269", ResourceUnits.PrintAmount(partResource.amount, resID, 6, "F1")); + if (partResource.amount != partResource.maxAmount) + resourceInfo.info += " " + Localizer.Format("#autoLOC_6004042", ResourceUnits.PrintAmount(partResource.maxAmount, resID, 6, "F1")); + double tons = partResource.amount * (double)partResource.info.density; + if (tons > 0d) + resourceInfo.info += Localizer.Format("#autoLOC_166270", ResourceUnits.PrintMass(tons)); + if (partResource.info.unitCost > 0f) + resourceInfo.info += Localizer.Format("#autoLOC_166271", (partResource.amount * (double)partResource.info.unitCost).ToString("F2")); + if (partResource.maxAmount > 0.0) + resourceInfo.primaryInfo = "" + resourceInfo.displayName + ": " + ResourceUnits.PrintAmount(partResource.maxAmount, resID, 6, "F1"); + + if (!string.IsNullOrEmpty(resourceInfo.info)) + { + newPartInfo.resourceInfos.Add(resourceInfo); + if (rInfoStr != string.Empty) + rInfoStr += "\n"; + + rInfoStr += resourceInfo.info; + } + } + if (part.Resources.Count > 0) + { + rInfoStr = rInfoStr + "\nDry Mass: " + part.mass; + } + newPartInfo.resourceInfo = rInfoStr; + newPartInfo.resourceInfos.Sort((AvailablePart.ResourceInfo rp1, AvailablePart.ResourceInfo rp2) => rp1.resourceName.CompareTo(rp2.resourceName)); + } + } +} diff --git a/Source/RealismOverhaul.csproj b/Source/RealismOverhaul.csproj index f8aa1180b0..f4bc9e159b 100644 --- a/Source/RealismOverhaul.csproj +++ b/Source/RealismOverhaul.csproj @@ -38,6 +38,8 @@ + + diff --git a/Source/ResourceUnitInfo.cs b/Source/ResourceUnitInfo.cs index 2dce098560..c7d0cb00bc 100644 --- a/Source/ResourceUnitInfo.cs +++ b/Source/ResourceUnitInfo.cs @@ -92,9 +92,9 @@ public static ResourceUnitInfo GetResourceUnitInfo(int id) return info; } - public static string PrintMassRate(double massFlow) + public static string PrintMass(double mass) { - return massFlow < 1d ? KSPUtil.PrintSI(massFlow * 1000d * 1000d, "g") : KSPUtil.PrintSI(massFlow, "t"); + return mass < 1d ? KSPUtil.PrintSI(mass * 1000d * 1000d, "g") : KSPUtil.PrintSI(mass, "t"); } public static string PrintRatePerSecBare(double rate, int resID, int sigFigs = 3, string precision = "G2", bool longPrefix = false) @@ -159,23 +159,23 @@ public static string PrintRate(double rate, int resID, bool showFlowMode, Module { if (Math.Abs(rate) > 0.1d) { - string massRate = density * rate > 0d ? " - " + PrintMassRate(rate * density) : string.Empty; + string massRate = density * rate > 0d ? " - " + PrintMass(rate * density) : string.Empty; output += Localizer.Format("#autoLOC_244197", title, rate.ToString("0.0") + unitRate + massRate); } else if (Math.Abs(rate) > (0.1d / 60d)) { - string massRate = density * rate > 0d ? " - " + PrintMassRate(rate * density * 60d) : string.Empty; + string massRate = density * rate > 0d ? " - " + PrintMass(rate * density * 60d) : string.Empty; output += Localizer.Format("#autoLOC_244201", title, (rate * 60d).ToString("0.0") + unitRate + massRate); } else { - string massRate = density * rate > 0d ? " - " + PrintMassRate(rate * density * 3600d) : string.Empty; + string massRate = density * rate > 0d ? " - " + PrintMass(rate * density * 3600d) : string.Empty; output += Localizer.Format("#autoLOC_6002411", title, (rate * 3600d).ToString("0.0") + unitRate + massRate); } } else { - string massRate = density * rate > 0d ? " - " + Localizer.Format("#autoLOC_6001048", PrintMassRate(rate * density)) : string.Empty; + string massRate = density * rate > 0d ? " - " + Localizer.Format("#autoLOC_6001048", PrintMass(rate * density)) : string.Empty; if (useSI) output += "- " + title + ": " + KSPUtil.PrintSI(rate, unitRate, sigFigs, longPrefix) + massRate + "\n"; else @@ -234,5 +234,26 @@ public static string PrintSIAmount(double rate, ResourceUnitInfo rui, int sigFig { return PrintSIAmount(rate * rui.MultiplierToUnit, rui.AmountUnit, sigFigs, longPrefix); } + + public static string PrintSI(double amount, string unitName, int sigFigs = 3, bool longPrefix = false) + { + if (amount == 0d || double.IsInfinity(amount) || double.IsNaN(amount)) + return Localizer.Format("<<1>><<2>>", amount.ToString(), unitName); + + int exp = (int)Math.Floor(Math.Log10(Math.Abs(amount))); + int prefix = ((exp >= 0) ? (exp / 3) : ((exp - 2) / 3)); + int digits = exp - prefix * 3 + 1; + int decimals = sigFigs - digits; + int index = Math.Max(0, Math.Min(prefix + KSPUtil.unitIndex, KSPUtil.prefixMults.Length - 1)); + string prefixString = (longPrefix ? KSPUtil.longSIprefixes[index] : KSPUtil.shortSIprefixes[index]); + amount /= KSPUtil.prefixMults[index]; + if (decimals < 0) + { + double mult = KSPUtil.digitsScale[-decimals]; + amount = Math.Round(amount / mult) * mult; + decimals = 0; + } + return Localizer.Format("<<1>><<2>><<3>>", amount.ToString(decimals == 0 ? "F0" : "0." + new string('#', decimals)), prefixString, unitName); + } } }