diff --git a/src/engine/LocalizedString.cs b/src/engine/LocalizedString.cs index cc16047ba14..90617a51c81 100644 --- a/src/engine/LocalizedString.cs +++ b/src/engine/LocalizedString.cs @@ -48,8 +48,17 @@ public string ToString(string? format, IFormatProvider? formatProvider) return format ?? TranslationServer.Translate(translationKey); } - return string.Format(formatProvider ?? CultureInfo.CurrentCulture, - format ?? TranslationServer.Translate(translationKey), formatStringArgs); + try + { + return string.Format(formatProvider ?? CultureInfo.CurrentCulture, + format ?? TranslationServer.Translate(translationKey), formatStringArgs); + } + catch (FormatException e) + { + GD.PrintErr("Invalid translation format in string ", translationKey, " for current language, exception: ", + e); + return TranslationServer.Translate(translationKey); + } } public override bool Equals(object? obj) diff --git a/src/gui_common/CustomRichTextLabel.cs b/src/gui_common/CustomRichTextLabel.cs index f8520e12115..c36b45eb428 100644 --- a/src/gui_common/CustomRichTextLabel.cs +++ b/src/gui_common/CustomRichTextLabel.cs @@ -89,17 +89,13 @@ public override void _Draw() } /// - /// Parses ExtendedBbcode for any custom Thrive tags and applying the final result - /// into this RichTextLabel's bbcode text. + /// The actual method parsing our extended bbcode, . This is a separate method + /// to be able to easily catch any exceptions this causes. /// - private void ParseCustomTags() + /// The extended bbcode string + /// Parsed bbcode string in standard format + private static string ParseCustomTagsString(string extendedBbcode) { - if (extendedBbcode == null) - { - BbcodeText = null; - return; - } - var result = new StringBuilder(extendedBbcode.Length); var currentTagBlock = new StringBuilder(50); @@ -197,12 +193,20 @@ private void ParseCustomTags() // Is a closing tag if (bbcodeNamespace.StartsWith("/", StringComparison.InvariantCulture)) { + if (tagStack.Count < 1) + { + // We have a closing tag with no opening tag seen + result.Append($"[{tagBlock}]"); + isIteratingTag = false; + continue; + } + var chunks = tagStack.Peek(); var bbcode = chunks[0]; // Closing tag doesn't match opening tag or vice versa, aborting parsing - if (tagStack.Count == 0 || bbcode != splitTagBlock[0]) + if (bbcode != splitTagBlock[0]) { result.Append($"[{tagBlock}]"); isIteratingTag = false; @@ -248,8 +252,8 @@ private void ParseCustomTags() } } - // Apply the final string into this RichTextLabel's bbcode text - BbcodeText = result.ToString(); + // Return the final string which will be used as this RichTextLabel's bbcode text + return result.ToString(); } /// @@ -258,7 +262,7 @@ private void ParseCustomTags() /// The string enclosed by the custom tags /// Custom Thrive bbcode-styled tags /// Attributes specifying additional functionalities to the bbcode. - private string BuildTemplateForTag(string input, ThriveBbCode bbcode, List attributes) + private static string BuildTemplateForTag(string input, ThriveBbCode bbcode, List attributes) { // Defaults to input so if something fails output returns unchanged var output = input; @@ -384,6 +388,32 @@ private string BuildTemplateForTag(string input, ThriveBbCode bbcode, List + /// Parses ExtendedBbcode for any custom Thrive tags and applying the final result + /// into this RichTextLabel's bbcode text. + /// + private void ParseCustomTags() + { + if (extendedBbcode == null) + { + BbcodeText = null; + return; + } + + try + { + // Parse our custom tags into standard tags and display that text + BbcodeText = ParseCustomTagsString(extendedBbcode); + } + catch (Exception e) + { + GD.PrintErr("Failed to parse bbcode string due to exception: ", e); + + // Just display the raw markup for now + BbcodeText = extendedBbcode; + } + } + private void OnInputsRemapped(object sender, EventArgs args) { ParseCustomTags(); diff --git a/src/microbe_stage/editor/PatchMapEditorComponent.cs b/src/microbe_stage/editor/PatchMapEditorComponent.cs index aab00f1f47c..784ba2c5075 100644 --- a/src/microbe_stage/editor/PatchMapEditorComponent.cs +++ b/src/microbe_stage/editor/PatchMapEditorComponent.cs @@ -384,9 +384,7 @@ private void UpdatePatchDetails(Patch patch) patch.BiomeTemplate.Name); // {0}-{1}m below sea level - patchDepth.Text = string.Format(CultureInfo.CurrentCulture, - TranslationServer.Translate("BELOW_SEA_LEVEL"), - patch.Depth[0], patch.Depth[1]); + patchDepth.Text = new LocalizedString("BELOW_SEA_LEVEL", patch.Depth[0], patch.Depth[1]).ToString(); patchPlayerHere.Visible = Editor.CurrentPatch == patch; var percentageFormat = TranslationServer.Translate("PERCENTAGE_VALUE");