Skip to content

Commit

Permalink
Patch Part Tooltip resources, also patch PrintSI so that it doesn't p…
Browse files Browse the repository at this point in the history
…rint trailing zeroes.
  • Loading branch information
NathanKell committed Oct 23, 2023
1 parent e64253a commit a06c3bf
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 8 deletions.
21 changes: 21 additions & 0 deletions Source/Harmony/KSPUtil.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
2 changes: 1 addition & 1 deletion Source/Harmony/ModuleEngines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Source/Harmony/ModuleRCS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
52 changes: 52 additions & 0 deletions Source/Harmony/PartLoader.cs
Original file line number Diff line number Diff line change
@@ -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 = "<b>" + resourceInfo.displayName + ": </b>" + 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));
}
}
}
2 changes: 2 additions & 0 deletions Source/RealismOverhaul.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
<Compile Include="DebugTools\DebugDrawer.cs" />
<Compile Include="DebugTools\DrawTools.cs" />
<Compile Include="Harmony\ModuleEngines.cs" />
<Compile Include="Harmony\KSPUtil.cs" />
<Compile Include="Harmony\PartLoader.cs" />
<Compile Include="Harmony\ResourceItem.cs" />
<Compile Include="Harmony\ModuleRCS.cs" />
<Compile Include="Harmony\ModuleResource.cs" />
Expand Down
33 changes: 27 additions & 6 deletions Source/ResourceUnitInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 += "- <b>" + title + ": </b>" + KSPUtil.PrintSI(rate, unitRate, sigFigs, longPrefix) + massRate + "\n";
else
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit a06c3bf

Please sign in to comment.