Skip to content

Commit

Permalink
Fix missing tooltips localisation error
Browse files Browse the repository at this point in the history
  • Loading branch information
Xerren09 committed Jan 18, 2025
1 parent cce09e1 commit 830679c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 4 deletions.
8 changes: 7 additions & 1 deletion Patches/LocalisationPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ private static bool GetTootipData(Item __instance, ref IEnumerable<IHaveUIData>
{
return true;
}
// Parse glyphs into the default tooltips if they weren't localised, without overwriting them on the item (fallback if the item was not localised)
string key = __instance.name.Trim().Replace(" ", "") + ShopLocalisation.TooltipsSuffix;
// Attempt to load previously defined tooltips, in case the current locale is not provided (fallback).
IEnumerable<string> locTooltips = __instance.Tooltips.Select(ikt => ikt.m_text);
var ret = new List<ItemKeyTooltip>();
if (ShopLocalisation.TryGetLocaleString(key, out var localeStr))
Expand All @@ -50,9 +50,15 @@ private static bool GetTootipData(Item __instance, ref IEnumerable<IHaveUIData>
}
foreach (var tip in locTooltips)
{
if (string.IsNullOrEmpty(tip))
{
continue;
}
string parsedTip = ParseTip(__instance, tip, out IMKbPromptProvider provider, out List<ControllerGlyphs.GlyphType> glyphType);
ret.Add(new ItemKeyTooltip(parsedTip, provider, glyphType));
}
// Set localised tooltips back as a failsafe
__instance.Tooltips = ret;
__result = ret;
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ Note that when localising item tooltips the key must be the item's name, suffixe
| SelfieGlyph | R (Default) |
| ZoomGlyph | Scroll wheel |

> [!IMPORTANT]
> If you don't want to add localisation ( :( ), use the `SetDefaultTooltips` extension method on your `Item` to set default tooltips.
> If you set tooltips in the editor, they won't work: this is a bug on Unity's end, not this mod. (those tooltips are serialised to null when you save them, even if they look right in the inspector)
> Setting a default is recommended in any case, but especially if you don't or only partially provide localisation.
## Compatibility

Expand Down
2 changes: 1 addition & 1 deletion ShopApiPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ShopApiPlugin
{
public const string MOD_GUID = "xerren.cwshopapi";
public const string MOD_NAME = "ShopAPI";
public const string MOD_VER = "1.0.0";
public const string MOD_VER = "1.0.1";

#if STEAM
static ShopApiPlugin()
Expand Down
18 changes: 17 additions & 1 deletion ShopLocalisation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine;
using ShopAPI.Patches;
using UnityEngine;
using UnityEngine.Localization.Settings;

namespace ContentWarningShop.Localisation
Expand Down Expand Up @@ -121,5 +122,20 @@ public static bool TryGetLocaleString(string key, out string res)
res = str;
return found;
}

/// <summary>
/// Sets default tooltip string on the item. This will be used if you don't otherwise provide localisation for your item.
/// </summary>
/// <param name="item"></param>
/// <param name="tooltipString">A ";" separated string of item tooltips. See <see href="https://github.com/Xerren09/ContentWarningShopAPI#localisation"/> for more.</param>
public static void SetDefaultTooltips(this Item item, string tooltipString)
{
var tooltips = tooltipString.Split(';');
item.Tooltips = new();
foreach (var tooltip in tooltips)
{
item.Tooltips.Add(new ItemKeyTooltip(tooltip, null, null));
}
}
}
}
5 changes: 5 additions & 0 deletions publish/thunderstore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
All notable changes will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.0.1
- Fix `GetTootipData` patch exception when null tooltips are set on the Item, and no localised strings were provided.
- This is unity's fault. Tooltips should be serialised correctly on Item resources, but they aren't loaded, so the count is correct but the text is null.
- Added `SetDefaultTooltips` extension method to set fallback tooltips.

## 1.0.0
- Initial Release
4 changes: 4 additions & 0 deletions publish/thunderstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ Note that when localising item tooltips the key must be the item's name, suffixe
| SelfieGlyph | R (Default) |
| ZoomGlyph | Scroll wheel |

> IMPORTANT:
> If you don't want to add localisation ( :( ), use the `SetDefaultTooltips` extension method on your `Item` to set default tooltips.
> If you set tooltips in the editor, they won't work: this is a bug on Unity's end, not this mod. (those tooltips are serialised to null when you save them, even if they look right in the inspector)
> Setting a default is recommended in any case, but especially if you don't or only partially provide localisation.
## Compatibility

Expand Down
2 changes: 1 addition & 1 deletion publish/thunderstore/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ShopAPI",
"version_number": "1.0.0",
"version_number": "1.0.1",
"website_url": "https://github.com/Xerren09/ContentWarningShopAPI",
"description": "Exposes an easy-to-use API to add custom items to the in-game shop.",
"dependencies": [
Expand Down

0 comments on commit 830679c

Please sign in to comment.