diff --git a/Localizer/Helpers/Extensions.cs b/Localizer/Helpers/Extensions.cs index db3d005..a5c204b 100644 --- a/Localizer/Helpers/Extensions.cs +++ b/Localizer/Helpers/Extensions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Globalization; using System.Linq; using System.Reflection; @@ -22,7 +22,8 @@ public static void Import(this ModTranslation modTranslation, BaseEntry entry, C && !string.IsNullOrWhiteSpace(entry.Origin)) { Utils.LogWarn( - $"Mismatch origin text when importing \"{modTranslation.Key}\", Origin in mod: {modTranslation.GetDefault()}, Origin in package: {entry.Origin}"); + $"Mismatch origin text when importing \"{modTranslation.Key}\"{Environment.NewLine}Origin in mod: {modTranslation.GetDefault()}{Environment.NewLine}Origin in package: {entry.Origin}{Environment.NewLine}Translation in package: {entry.Translation}"); + return; } if (modTranslation.GetDefault() != null && entry.Translation != null && diff --git a/Localizer/Localizer.cs b/Localizer/Localizer.cs index 4013c2e..0056df6 100644 --- a/Localizer/Localizer.cs +++ b/Localizer/Localizer.cs @@ -52,8 +52,6 @@ public Localizer() { Instance = this; LoadedLocalizer = new LoadedModWrapper("Terraria.ModLoader.Core.AssemblyManager".Type().ValueOf("loadedMods").Invoke("get_Item", "!Localizer")); - LoadedLocalizer.File.SetField("k__BackingField", "Localizer"); - LoadedLocalizer.SetField("name", "Localizer"); this.SetField("k__BackingField", LoadedLocalizer.File); this.SetField("k__BackingField", LoadedLocalizer.Code); Log = LogManager.GetLogger(nameof(Localizer)); @@ -93,6 +91,11 @@ private static void Init() Kernel = new LocalizerKernel(); Kernel.Init(); + if (LanguageManager.Instance.ActiveCulture == GameCulture.Chinese) + { + ModBrowser.Patches.Patch(); + } + var autoImportService = Kernel.Get(); } @@ -107,30 +110,27 @@ public override void Load() Hooks.InvokeBeforeLoad(); Kernel.Get(); - if (LanguageManager.Instance.ActiveCulture == GameCulture.Chinese) - { - ModBrowser.Patches.Patch(); - } var onInit = "Terraria.ModLoader.UI.UIModItem".Type().Method("OnInitialize"); Harmony.Patch(onInit, postfix: new HarmonyMethod(NoroHelper.MethodInfo(() => UIModItemPostfix(null)))); var drawSelf = "Terraria.ModLoader.UI.UIModItem".Type().Method("DrawSelf"); Harmony.Patch(drawSelf, postfix: new HarmonyMethod(NoroHelper.MethodInfo(() => DrawSelfPostfix(null, null)))); - var refStr = ""; - var setEnabledPrefix = "Terraria.ModLoader.ModLoader".Type().Method("SetModEnabled"); - Harmony.Patch(setEnabledPrefix, prefix: new HarmonyMethod(NoroHelper.MethodInfo(() => EnabledPrefix(ref refStr)))); + var populateFromJson = "Terraria.ModLoader.UI.ModBrowser.UIModBrowser".Type().Method("PopulateFromJson"); + Harmony.Patch(populateFromJson, prefix: new HarmonyMethod(NoroHelper.MethodInfo(() => PopulateFromJsonPrefix())), + postfix: new HarmonyMethod(NoroHelper.MethodInfo(() => PopulateFromJsonPostfix()))); + } - var isEnabledPrefix = "Terraria.ModLoader.ModLoader".Type().Method("IsEnabled"); - Harmony.Patch(isEnabledPrefix, prefix: new HarmonyMethod(NoroHelper.MethodInfo(() => EnabledPrefix(ref refStr)))); + private static void PopulateFromJsonPrefix() + { + LoadedLocalizer.File.SetField("k__BackingField", "Localizer"); + LoadedLocalizer.SetField("name", "Localizer"); } - private static void EnabledPrefix(ref string modName) + private static void PopulateFromJsonPostfix() { - if (modName == "Localizer") - { - modName = "!Localizer"; - } + LoadedLocalizer.File.SetField("k__BackingField", "!Localizer"); + LoadedLocalizer.SetField("name", "!Localizer"); } private static int frameCounter; @@ -139,7 +139,7 @@ private static void DrawSelfPostfix(object __instance, SpriteBatch spriteBatch) var current = __instance as UIPanel; var modName = __instance.ValueOf("_mod")?.ValueOf("Name")?.ToString(); var modNameHovering = current.ValueOf("_modName")?.IsMouseHovering ?? false; - if (modName == "Localizer") + if (modName == "!Localizer") { frameCounter++; @@ -184,7 +184,7 @@ private static void DrawSelfPostfix(object __instance, SpriteBatch spriteBatch) private static void UIModItemPostfix(object __instance) { var modName = __instance.ValueOf("_mod")?.ValueOf("Name")?.ToString(); - if (modName == "Localizer") + if (modName == "!Localizer") { __instance.ValueOf("_modName").OnClick += (evt, element) => { @@ -296,6 +296,7 @@ public override void Unload() HookEndpointManager.RemoveAllOwnedBy(this); Harmony.UnpatchAll(nameof(Localizer)); + Harmony.UnpatchAll("ModBrowserMirror"); Kernel.Dispose(); PackageUI = null; diff --git a/Localizer/ModBrowser/Patches.cs b/Localizer/ModBrowser/Patches.cs index 52ac6c1..f0ac8e9 100644 --- a/Localizer/ModBrowser/Patches.cs +++ b/Localizer/ModBrowser/Patches.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -8,33 +9,46 @@ namespace Localizer.ModBrowser { public static class Patches { + public static HarmonyInstance HarmonyInstance { get; set; } + public static void Patch() { Utils.LogInfo($"Patching ModBrowser, tML version: {ModLoader.version}"); - if (!string.IsNullOrEmpty(GetModListURL())) - { - var populateModBrowser = "Terraria.ModLoader.UI.ModBrowser.UIModBrowser".Type() - .GetMethods(NoroHelper.Any) - .FirstOrDefault(m => m.Name.Contains("")); - Localizer.Harmony.Patch(populateModBrowser, null, null, new HarmonyMethod(NoroHelper.MethodInfo(() => PopulateModBrowserTranspiler(null)))); - } + HarmonyInstance = HarmonyInstance.Create("ModBrowserMirror"); - if (!string.IsNullOrEmpty(GetModDownloadURL())) + try { - var fromJson = "Terraria.ModLoader.UI.ModBrowser.UIModDownloadItem".Type().Method("FromJson"); - Localizer.Harmony.Patch(fromJson, null, null, new HarmonyMethod(NoroHelper.MethodInfo(() => FromJSONTranspiler(null)))); - - if (!string.IsNullOrEmpty(GetModDescURL())) + if (!string.IsNullOrEmpty(GetModListURL())) { - var onActivate = "Terraria.ModLoader.UI.UIModInfo".Type() + var populateModBrowser = "Terraria.ModLoader.UI.ModBrowser.UIModBrowser".Type() .GetMethods(NoroHelper.Any) - .FirstOrDefault(m => m.Name.Contains("")); - Localizer.Harmony.Patch(onActivate, null, null, new HarmonyMethod(NoroHelper.MethodInfo(() => OnActivateTranspiler(null)))); + .FirstOrDefault(m => m.Name.Contains("")); + HarmonyInstance.Patch(populateModBrowser, null, null, new HarmonyMethod(NoroHelper.MethodInfo(() => PopulateModBrowserTranspiler(null)))); + Utils.LogInfo("PopulateModBrowser Patched"); } - } - Utils.LogInfo("ModBrowser Patched"); + if (!string.IsNullOrEmpty(GetModDownloadURL())) + { + var fromJson = "Terraria.ModLoader.UI.ModBrowser.UIModDownloadItem".Type().Method("FromJson"); + HarmonyInstance.Patch(fromJson, null, null, new HarmonyMethod(NoroHelper.MethodInfo(() => FromJSONTranspiler(null)))); + Utils.LogInfo("FromJson Patched"); + + if (!string.IsNullOrEmpty(GetModDescURL())) + { + var onActivate = "Terraria.ModLoader.UI.UIModInfo".Type() + .GetMethods(NoroHelper.Any) + .FirstOrDefault(m => m.Name.Contains("")); + HarmonyInstance.Patch(onActivate, null, null, new HarmonyMethod(NoroHelper.MethodInfo(() => OnActivateTranspiler(null)))); + Utils.LogInfo("OnActivate Patched"); + } + } + Utils.LogInfo("ModBrowser Patched"); + } + catch (Exception e) + { + Utils.LogInfo($"ModBrowser Patch exception: {e}"); + } } private static string GetModListURL() diff --git a/Localizer/Package/Import/AutoImportService.cs b/Localizer/Package/Import/AutoImportService.cs index 44bf7ef..1afe31c 100644 --- a/Localizer/Package/Import/AutoImportService.cs +++ b/Localizer/Package/Import/AutoImportService.cs @@ -52,7 +52,7 @@ private void OnBeforeModCtor(object mod) return; } - Utils.SafeWrap(() => + try { if (Localizer.Config.AutoImport) { @@ -60,7 +60,10 @@ private void OnBeforeModCtor(object mod) Utils.LogInfo($"Early auto import for mod: [{wrapped.Name}]"); Import(wrapped); } - }); + } + catch + { + } } private void OnPostSetupContent() @@ -96,7 +99,7 @@ void LoadPackedPackages() list.AddRange(Directory.GetFiles(Path.Combine(Terraria.Main.SavePath, "Mods"), "*.locpack")); foreach (var file in list) { - Utils.SafeWrap(() => + try { var pack = _packedPackageLoad.Load(file, _fileLoad); if (pack == null) @@ -105,7 +108,10 @@ void LoadPackedPackages() } _packageManage.AddPackage(pack); - }); + } + catch + { + } } } @@ -113,7 +119,7 @@ void LoadSourcePackages() { foreach (var dir in new DirectoryInfo(Localizer.SourcePackageDirPath).GetDirectories()) { - Utils.SafeWrap(() => + try { var pack = _sourcePackageLoad.Load(dir.FullName, _fileLoad); if (pack == null) @@ -123,7 +129,10 @@ void LoadSourcePackages() _packageManage.AddPackage(pack); _packagePack.Pack(Path.Combine(dir.FullName, "Package.json")); - }); + } + catch + { + } } } @@ -169,7 +178,7 @@ void QueuePackageGroup(IPackageGroup packageGroup) } } - Utils.SafeWrap(() => + try { _packageImport.Clear(); @@ -190,7 +199,10 @@ void QueuePackageGroup(IPackageGroup packageGroup) Localizer.RefreshLanguages(); Utils.LogDebug("Auto import end."); - }); + } + catch + { + } } protected override void DisposeUnmanaged() diff --git a/Localizer/build.txt b/Localizer/build.txt index 6cef504..ca28aa0 100644 --- a/Localizer/build.txt +++ b/Localizer/build.txt @@ -1,5 +1,5 @@ author = Chireiden Team -version = 1.5.0.5 +version = 1.5.0.6 displayName = Localizer hideCode = true hideResources = false