some of the final code fixes, 3.3.1

This commit is contained in:
SillyStar-Github 2026-01-21 16:57:36 -05:00
parent a89ba825ee
commit b5a1c031af
8 changed files with 204 additions and 92 deletions

View file

@ -124,61 +124,6 @@ namespace PvZ_Fusion_Translator.AssetStore
return checkText; return checkText;
} }
public static string TranslateColorText(string originalText, bool isLog = false)
{
string first = ReplaceInsideColorTags(originalText);
string text = ReplaceOutsideColorTags(first);
string checkText;
#if DEBUG
Regex regex = new("\\p{IsCJKUnifiedIdeographs}+");
Match match = regex.Match(text);
if (match.Success)
{
// Log.LogDebug("Untranslated String: " + match.Value);
FileLoader.DumpUntranslatedStrings(text);
}
#endif
checkText = text;
return checkText;
}
public static string ReplaceOutsideColorTags(string input)
{
string pattern = @"(<color[^>]*>.*?</color>)";
var parts = Regex.Split(input, pattern);
for (int i = 0; i < parts.Length; i++)
{
if (i % 2 == 0)
{
parts[i] = DoTranslateText(parts[i], false);
if(Utils.CheckForUntranslatedText(parts[i]))
{
FileLoader.DumpUntranslatedStrings(parts[i]);
}
}
}
return string.Join("", parts);
}
public static string ReplaceInsideColorTags(string input)
{
string text = Regex.Replace(input, @"(<color=[^>]+>)(.*?)(</color>)", match =>
{
string translatedText = DoTranslateText(match.Groups[2].Value, false);
if (Utils.CheckForUntranslatedText(translatedText))
{
FileLoader.DumpUntranslatedStrings(translatedText);
}
return match.Groups[1].Value + DoTranslateText(match.Groups[2].Value, false) + match.Groups[3].Value;
});
return text;
}
public static string TranslateText(string originalText, string pattern, bool isLog = false) public static string TranslateText(string originalText, string pattern, bool isLog = false)
{ {
if (TestRegex(originalText, pattern) && translationStringRegex.ContainsKey(pattern)) if (TestRegex(originalText, pattern) && translationStringRegex.ContainsKey(pattern))
@ -266,12 +211,73 @@ namespace PvZ_Fusion_Translator.AssetStore
if (isLog) if (isLog)
Log.LogDebug($"Text '{originalText}' not translated"); Log.LogDebug($"Text '{originalText}' not translated");
if(Utils.CheckForUntranslatedText(originalText))
{
FileLoader.DumpUntranslatedStrings(originalText);
}
return originalText; return originalText;
} }
public static bool TestRegex(string originalText, string pattern) public static string TranslateColorText(string originalText, bool isLog = false)
{
string text = TranslateColorSegments(originalText);
string checkText;
#if DEBUG
Regex regex = new("\\p{IsCJKUnifiedIdeographs}+");
Match match = regex.Match(text);
if (match.Success)
{
// Log.LogDebug("Untranslated String: " + match.Value);
FileLoader.DumpUntranslatedStrings(text);
}
#endif
checkText = text;
return checkText;
}
public static string TranslateColorSegments(string input)
{ {
return Regex.IsMatch(originalText, pattern); string result = "";
string pattern = @"<color=[^>]+>[\s\S]*?<\/color>|(?:<(?!color=)|[^<])+";
string smallPattern = @"(<color=[^>]+>)(.*?)(</color>)";
MatchCollection matches = Regex.Matches(input, pattern);
for (int i = 0; i < matches.Count; i++)
{
Match match = matches[i];
if (Regex.Match(match.Value, smallPattern, RegexOptions.Singleline).Success)
{
result += TranslateColorSegment(match.Value);
}
else
{
result += DoTranslateText(match.Value);
}
}
return result;
}
public static string TranslateColorSegment(string input)
{
string result = input;
string pattern = @"(<color=[^>]+>)(.*?)(</color>)";
Match match = Regex.Match(input, pattern, RegexOptions.Singleline);
string translatedText = DoTranslateText(match.Groups[2].Value);
if(Utils.CheckForUntranslatedText(translatedText))
{
FileLoader.DumpUntranslatedStrings(translatedText);
}
result = match.Groups[1].Value + translatedText + match.Groups[3].Value;
return result;
}
public static bool TestRegex(string originalText, string pattern, bool singleLine = false)
{
RegexOptions options = (singleLine) ? RegexOptions.None : RegexOptions.Singleline;
return Regex.IsMatch(originalText, pattern, options);
} }
private static TextAlignmentOptions TextAnchorToTMPAlignment(TextAnchor anchor) private static TextAlignmentOptions TextAnchorToTMPAlignment(TextAnchor anchor)

View file

@ -4,7 +4,8 @@ using Il2CppTMPro;
using PvZ_Fusion_Translator.AssetStore; using PvZ_Fusion_Translator.AssetStore;
using PvZ_Fusion_Translator.Patches.Managers; using PvZ_Fusion_Translator.Patches.Managers;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using UnityEngine.TextCore.Text; using System.Xml.Linq;
using UnityEngine;
namespace PvZ_Fusion_Translator.Patches.BaseTextObjects namespace PvZ_Fusion_Translator.Patches.BaseTextObjects
{ {
@ -17,17 +18,45 @@ namespace PvZ_Fusion_Translator.Patches.BaseTextObjects
{ {
TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString());
foreach (TextMeshProUGUI txt in __instance.textMeshes) foreach (TextMeshProUGUI txt in __instance.GetComponentsInChildren<TextMeshProUGUI>())
{ {
string travelMatch = TravelMgr_Patch.MatchTravelBuff(txt.text); string travelMatch = TravelMgr_Patch.MatchTravelBuff(txt.text);
string originalText = "";
if (txt.gameObject.name.Contains("shadow"))
{
continue;
}
if (travelMatch != "") if (travelMatch != "")
{ {
txt.text = travelMatch; txt.text = travelMatch;
} }
else if(Regex.Match(txt.text, @"(<color[^>]*>.*?</color>)", RegexOptions.Singleline).Success)
{
txt.text = StringStore.TranslateColorText(txt.text, true);
if (txt.gameObject.name.Contains("main"))
{
originalText = txt.text;
Transform shadowText = txt.transform.parent.Find("Text_shadow");
if (shadowText != null)
{
shadowText.GetComponent<TextMeshProUGUI>().text = Utils.RemoveColorTags(originalText);
}
}
}
else else
{ {
txt.text = StringStore.TranslateText(txt.text, true); txt.text = StringStore.TranslateText(txt.text, true);
if (txt.gameObject.name.Contains("main"))
{
originalText = txt.text;
Transform shadowText = txt.transform.parent.Find("Text_shadow");
if (shadowText != null)
{
shadowText.GetComponent<TextMeshProUGUI>().text = Utils.RemoveColorTags(originalText);
}
}
} }
txt.font = fontAsset; txt.font = fontAsset;
} }

View file

@ -2,12 +2,79 @@
using Il2Cpp; using Il2Cpp;
using Il2CppTMPro; using Il2CppTMPro;
using PvZ_Fusion_Translator.AssetStore; using PvZ_Fusion_Translator.AssetStore;
using UnityEngine;
namespace PvZ_Fusion_Translator.Patches.GameObjects namespace PvZ_Fusion_Translator.Patches.GameObjects
{ {
[HarmonyPatch(typeof(AdvantureEvolutionMenu))] [HarmonyPatch(typeof(AdvantureEvolutionMenu))]
public static class AdvantureEvolutionMenu_Patch public static class AdvantureEvolutionMenu_Patch
{ {
[HarmonyPatch(nameof(AdvantureMenu.Awake))]
[HarmonyPostfix]
public static void Awake(AdvantureEvolutionMenu __instance)
{
foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>())
{
text.text = StringStore.TranslateColorText(text.text);
}
Transform baseWindow = __instance.transform.Find("BaseWindow");
Transform advancedWindow = __instance.transform.Find("AdvancedWindow");
if(baseWindow != null)
{
foreach(TextMeshProUGUI text in baseWindow.GetComponentsInChildren<TextMeshProUGUI>())
{
if(text.gameObject.name.Contains("Name"))
{
text.text = Utils.GetPlantNameFromAlmanac(__instance.thePlantType);
}
}
}
if (advancedWindow != null)
{
foreach (TextMeshProUGUI text in advancedWindow.GetComponentsInChildren<TextMeshProUGUI>())
{
if (text.gameObject.name.Contains("Name"))
{
text.text = Utils.GetPlantNameFromAlmanac(__instance.thePlantType);
}
}
}
}
[HarmonyPatch(nameof(AdvantureEvolutionMenu.UpdateText))]
[HarmonyPostfix]
public static void UpdateText(AdvantureEvolutionMenu __instance)
{
foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>())
{
FileLoader.DumpUntranslatedStrings(text.text);
text.text = StringStore.TranslateColorText(text.text);
}
}
[HarmonyPatch(nameof(AdvantureEvolutionMenu.RollEvolution))]
[HarmonyPostfix]
public static void RollEvolution(AdvantureEvolutionMenu __instance)
{
foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>())
{
FileLoader.DumpUntranslatedStrings(text.text);
text.text = StringStore.TranslateColorText(text.text);
}
}
[HarmonyPatch(nameof(AdvantureEvolutionMenu.RollEvolutionGood))]
[HarmonyPostfix]
public static void RollEvolutionGood(AdvantureEvolutionMenu __instance)
{
foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>())
{
FileLoader.DumpUntranslatedStrings(text.text);
text.text = StringStore.TranslateColorText(text.text);
}
}
} }
} }

View file

@ -12,48 +12,48 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects
[HarmonyPostfix] [HarmonyPostfix]
private static void Start(AdvantureMenu __instance) private static void Start(AdvantureMenu __instance)
{ {
__instance.difficulty.text = Utils.RemoveSizeTags(StringStore.TranslateText(__instance.difficulty.text)); TranslateAdvantureMenu(__instance);
foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>())
{
text.text = StringStore.TranslateText(text.text);
}
} }
[HarmonyPatch(nameof(AdvantureMenu.ChangeDifficulty))] [HarmonyPatch(nameof(AdvantureMenu.ChangeDifficulty))]
[HarmonyPostfix] [HarmonyPostfix]
private static void ChangeDifficulty(AdvantureMenu __instance) private static void ChangeDifficulty(AdvantureMenu __instance)
{ {
__instance.difficulty.text = Utils.RemoveSizeTags(StringStore.TranslateText(__instance.difficulty.text)); TranslateAdvantureMenu(__instance);
foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>())
{
text.text = StringStore.TranslateText(text.text);
}
} }
[HarmonyPatch(nameof(AdvantureMenu.GetChapterDifficulty))] [HarmonyPatch(nameof(AdvantureMenu.GetChapterDifficulty))]
[HarmonyPostfix] [HarmonyPostfix]
private static void GetChapterDifficulty(AdvantureMenu __instance) private static void GetChapterDifficulty(AdvantureMenu __instance)
{ {
foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>()) TranslateAdvantureMenu(__instance);
{
text.text = StringStore.TranslateText(text.text);
}
} }
[HarmonyPatch(nameof(AdvantureMenu.UpdateChapterDifficulty))] [HarmonyPatch(nameof(AdvantureMenu.UpdateChapterDifficulty))]
[HarmonyPostfix] [HarmonyPostfix]
private static void UpdateChapterDifficulty(AdvantureMenu __instance) private static void UpdateChapterDifficulty(AdvantureMenu __instance)
{ {
foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>()) TranslateAdvantureMenu(__instance);
{
text.text = StringStore.TranslateText(text.text);
}
} }
[HarmonyPatch(nameof(AdvantureMenu.UpdateStarCount))] [HarmonyPatch(nameof(AdvantureMenu.UpdateStarCount))]
[HarmonyPostfix] [HarmonyPostfix]
private static void UpdateStarCount(AdvantureMenu __instance) private static void UpdateStarCount(AdvantureMenu __instance)
{ {
TranslateAdvantureMenu(__instance);
}
public static void TranslateAdvantureMenu(AdvantureMenu __instance)
{
__instance.difficulty.text = Utils.RemoveSizeTags(StringStore.TranslateText(__instance.difficulty.text));
__instance.chapterDifficulty.text = StringStore.TranslateText(__instance.chapterDifficulty.text);
__instance.starCount.text = StringStore.TranslateText(__instance.starCount.text);
foreach (TextMeshProUGUI text in __instance.title)
{
text.text = StringStore.TranslateText(text.text);
}
foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>()) foreach (TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>())
{ {
text.text = StringStore.TranslateText(text.text); text.text = StringStore.TranslateText(text.text);

View file

@ -57,15 +57,14 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects
{ {
string origText = textTMP.text; string origText = textTMP.text;
string translatedText = origText; string translatedText = origText;
string pattern = "([^\\s]+)阶\n([^\\s]+)"; string pattern = "(\\d+)阶\n([\\s\\S]+)";
if (StringStore.TestRegex(translatedText, pattern)) if (StringStore.TestRegex(translatedText, pattern))
{ {
var regex = new Regex(pattern); var match = Regex.Match(translatedText, pattern, RegexOptions.Singleline);
var match = regex.Match(translatedText); string tier = match.Groups[1].Value;
string regularText = match.Groups[2].Value; string healthText = TranslateHPText(match.Groups[2].Value, __instance);
string halfText = TranslateHPText(regularText, __instance);
string fStr = StringStore.translationStringRegex[pattern]; string fStr = StringStore.translationStringRegex[pattern];
translatedText = string.Format(fStr, [match.Groups[1].Value, halfText]); translatedText = string.Format(fStr, [tier, healthText]);
} }
else else
{ {
@ -105,8 +104,7 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects
for(int i = 0; i < HPStrings.Count; i++) for(int i = 0; i < HPStrings.Count; i++)
{ {
var hp = HPStrings.ElementAt(i); var hp = HPStrings.ElementAt(i);
Regex regex = new Regex(hp.Key); if(Regex.Match(originalText, hp.Key, RegexOptions.Singleline).Success)
if(regex.IsMatch(originalText))
{ {
fIndex = i; fIndex = i;
found = true; found = true;
@ -160,6 +158,7 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects.MinorObjects
{ {
string fStr = HPStrings.ElementAt(fIndex).Value; string fStr = HPStrings.ElementAt(fIndex).Value;
string DPS = originalText.Split("\n")[1].Replace("DPS:", ""); string DPS = originalText.Split("\n")[1].Replace("DPS:", "");
DPS = StringStore.TranslateText(DPS);
switch (fIndex) switch (fIndex)
{ {

View file

@ -40,11 +40,22 @@ namespace PvZ_Fusion_Translator.Patches.GameObjects
GameObject contentObject = __instance.transform.FindChild("Scroll View/Viewport/Content").gameObject; GameObject contentObject = __instance.transform.FindChild("Scroll View/Viewport/Content").gameObject;
TextMeshProUGUI contentText = contentObject.GetComponent<TextMeshProUGUI>(); TextMeshProUGUI contentText = contentObject.GetComponent<TextMeshProUGUI>();
contentText.text = changelogText; contentText.text = changelogText;
contentText.margin = new Vector4(6, 2, 12, 0);
contentText.enableWordWrapping = true;
contentText.overflowMode = TextOverflowModes.ScrollRect;
Canvas.ForceUpdateCanvases();
contentText.ForceMeshUpdate();
//float textHeight = contentText.preferredHeight;
//RectTransform contentRect = contentObject.GetComponent<RectTransform>();
//contentRect.sizeDelta = new Vector2(contentRect.sizeDelta.x, textHeight);
ContentSizeFitter sizeFitter = contentObject.AddComponent<ContentSizeFitter>(); ContentSizeFitter sizeFitter = contentObject.AddComponent<ContentSizeFitter>();
sizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize; sizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
} }
} }
} }

View file

@ -12,13 +12,13 @@ namespace PvZ_Fusion_Translator.Patches.Managers
{ {
[HarmonyPatch(nameof(CanvasUpUI.Start))] [HarmonyPatch(nameof(CanvasUpUI.Start))]
[HarmonyPostfix] [HarmonyPostfix]
public static void Start(IZBottomMenu __instance) public static void Start(CanvasUpUI __instance)
{ {
TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString()); TMP_FontAsset fontAsset = FontStore.LoadTMPFont(Utils.Language.ToString());
foreach(TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>()) foreach(TextMeshProUGUI text in __instance.GetComponentsInChildren<TextMeshProUGUI>())
{ {
string originalKey = StringStore.translationString.FirstOrDefault(x => x.Value.Replace("\n", " ") == text.text).Key; string originalKey = StringStore.translationString.FirstOrDefault(x => (x.Value == text.text || x.Value.Replace("\n", "") == text.text)).Key;
text.text = (StringStore.translationString.ContainsKey(originalKey + "_MG")) ? StringStore.TranslateText(originalKey + "_MG") : StringStore.TranslateText(text.text); text.text = (StringStore.translationString.ContainsKey(originalKey + "_MG")) ? StringStore.TranslateText(originalKey + "_MG") : StringStore.TranslateText(text.text);
if(text.gameObject.name.Contains("LevelName")) if(text.gameObject.name.Contains("LevelName"))
{ {

View file

@ -13,7 +13,7 @@
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Configurations>Debug;DebugEnglish;ReleaseMulti;ReleaseEnglish;DebugTesting;ReleaseOther;DebugObfuscate;ReleaseObfuscate</Configurations> <Configurations>Debug;DebugEnglish;ReleaseMulti;ReleaseEnglish;DebugTesting;ReleaseOther;DebugObfuscate;ReleaseObfuscate</Configurations>
<GamePath>C:\Users\rainb\Desktop\PC_PVZ-Fusion_3.3.0_Part1</GamePath> <GamePath>C:\Users\rainb\Desktop\PC_PVZ-Fusion_3.3.1</GamePath>
<Platforms>AnyCPU;ARM64;x64</Platforms> <Platforms>AnyCPU;ARM64;x64</Platforms>
</PropertyGroup> </PropertyGroup>