Skip to content

Commit

Permalink
Avoid wastefully updating the Details panel if it is not even open, r…
Browse files Browse the repository at this point in the history
…educing lag spikes caused by Food Supply Tooltips. Also fixes a rare crash when killing some critters.

Update the French translation of Pip Plant Overlay, credit to user Pholith.
  • Loading branch information
peterhaneve committed Jan 25, 2025
1 parent e528d94 commit c262c84
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion FoodTooltip/FoodTooltip.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyTitle>Food Supply Tooltip</AssemblyTitle>
<FileVersion>3.7.0.0</FileVersion>
<FileVersion>3.8.0.0</FileVersion>
<RootNamespace>PeterHan.FoodTooltip</RootNamespace>
<Description>Shows tooltips on plants and critters for how much food they can supply per cycle in their current states.</Description>
<AssemblyVersion>3.2.0.0</AssemblyVersion>
Expand Down
4 changes: 2 additions & 2 deletions FoodTooltip/FoodTooltipPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static class CreatureCalorieMonitor_Def_GetDescriptors_Patch {
/// Applied after GetDescriptors runs.
/// </summary>
internal static void Postfix(GameObject obj, List<Descriptor> __result) {
if (__result != null && obj != null)
if (__result != null)
FoodTooltipUtils.AddCritterDescriptors(obj, __result);
}
}
Expand Down Expand Up @@ -140,7 +140,7 @@ public static class SimpleInfoScreen_OnDeselectTarget_Patch {
/// <summary>
/// Applied after OnDeselectTarget runs.
/// </summary>
internal static void Postfix(SimpleInfoScreen __instance, GameObject target) {
internal static void Prefix(SimpleInfoScreen __instance, GameObject target) {
if (__instance != null && __instance.TryGetComponent(
out InfoScreenRefresher rf))
rf.OnDeselectTarget(target);
Expand Down
23 changes: 14 additions & 9 deletions FoodTooltip/FoodTooltipUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,18 @@ internal static class FoodTooltipUtils {
/// <param name="crop">The plant to query.</param>
/// <param name="descriptors">The location where the descriptors should be placed.</param>
internal static void AddCropDescriptors(Crop crop, IList<Descriptor> descriptors) {
var db = Db.Get();
var cropVal = crop.cropVal;
GameObject go = crop.gameObject;
Klei.AI.Attribute yieldAmount = Db.Get().PlantAttributes.YieldAmount;
float preModifiedAttributeValue = go.GetComponent<Modifiers>().GetPreModifiedAttributeValue(yieldAmount);
var maturity = Db.Get().Amounts.Maturity.Lookup(crop.gameObject);
if (maturity != null)
CreateDescriptors(TagManager.Create(cropVal.cropId), descriptors,
maturity.GetDelta() * Constants.SECONDS_PER_CYCLE * cropVal.numProduced
* preModifiedAttributeValue / maturity.GetMax(), FoodDescriptorTexts.PLANTS);
if (crop != null && crop.TryGetComponent(out Modifiers modifiers)) {
float preModifiedAttributeValue = modifiers.GetPreModifiedAttributeValue(
db.PlantAttributes.YieldAmount);
var maturity = Db.Get().Amounts.Maturity.Lookup(crop);
if (maturity != null)
CreateDescriptors(TagManager.Create(cropVal.cropId), descriptors,
maturity.GetDelta() * Constants.SECONDS_PER_CYCLE * cropVal.
numProduced * preModifiedAttributeValue / maturity.GetMax(),
FoodDescriptorTexts.PLANTS);
}
}

/// <summary>
Expand Down Expand Up @@ -86,8 +89,10 @@ internal static void AddCritterDescriptors(GameObject critter,
/// <param name="drops">The tag IDs of the dropped items.</param>
/// <param name="result">The location where the dropped items will be stored.</param>
private static void CollectDrops(string[] drops, IDictionary<string, int> result) {
int n = drops.Length;
result.Clear();
foreach (string drop in drops) {
for (int i = 0; i < n; i++) {
string drop = drops[i];
if (!result.TryGetValue(drop, out int qty))
qty = 0;
result[drop] = qty + 1;
Expand Down
7 changes: 5 additions & 2 deletions FoodTooltip/InfoScreenRefresher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ internal sealed class InfoScreenRefresher : KMonoBehaviour {
/// </summary>
/// <param name="data">The effect that was added or removed.</param>
private void EffectRefresh(object data) {
var di = DetailsScreen.Instance;
// Effect IDs are hard coded in HappinessMonitor and Tinkerable
if (data is Effect effect && EFFECTS.Contains(effect.Id) && infoScreen != null)
if (data is Effect effect && EFFECTS.Contains(effect.Id) && infoScreen != null &&
di != null && di.isActiveAndEnabled && di.target != null)
infoScreen.RefreshInfoScreen(true);
}

Expand Down Expand Up @@ -84,7 +86,8 @@ internal void OnSelectTarget(GameObject target) {
/// Refreshes the information panel. The argument is always null when reached.
/// </summary>
private void RefreshInfoPanel(object _) {
if (infoScreen != null)
var di = DetailsScreen.Instance;
if (infoScreen != null && di != null && di.isActiveAndEnabled && di.target != null)
infoScreen.RefreshInfoScreen(true);
}
}
Expand Down
8 changes: 4 additions & 4 deletions PipPlantOverlay/translations/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: \n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Last-Translator: Pholith\n"
"Language-Team: \n"
"Language: fr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Application: Oxygen Not IncludedPOT Version: 2.0\n"
"X-Generator: Poedit 3.4.1\n"
"X-Generator: Poedit 3.5\n"

#. PeterHan.PipPlantOverlay.PipPlantOverlayStrings.INPUT_BINDINGS.ROOT.PIPPLANT
msgctxt ""
Expand All @@ -30,7 +30,7 @@ msgctxt ""
"PeterHan.PipPlantOverlay.PipPlantOverlayStrings.UI.OVERLAYS.PIPPLANTING."
"CANPLANT"
msgid "Can plant here"
msgstr "Ne peut pas planter ici"
msgstr "Peut planter ici"

#. PeterHan.PipPlantOverlay.PipPlantOverlayStrings.UI.OVERLAYS.PIPPLANTING.DESCRIPTION
msgctxt ""
Expand Down Expand Up @@ -119,7 +119,7 @@ msgctxt ""
"PeterHan.PipPlantOverlay.PipPlantOverlayStrings.UI.OVERLAYS.PIPPLANTING."
"TOOLTIPS.HARDNESS"
msgid "Natural tile hardness is above {0:D}!"
msgstr "La dureté naturelle des carreaux est supérieure à {0:D} !"
msgstr "La dureté naturelle des dalles est supérieure à {0:D} !"

#. PeterHan.PipPlantOverlay.PipPlantOverlayStrings.UI.OVERLAYS.PIPPLANTING.TOOLTIPS.PLANTCOUNT
msgctxt ""
Expand Down

0 comments on commit c262c84

Please sign in to comment.