Skip to content

Commit

Permalink
Fix bugs:
Browse files Browse the repository at this point in the history
Localizer disabled after downloading mods from Mod Browser
Localizer not shown in Mod Pack
Use try/catch instead of SafeWrap
  • Loading branch information
sgkoishi committed Feb 27, 2020
1 parent ccb3411 commit 0d36876
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
5 changes: 3 additions & 2 deletions Localizer/Helpers/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Globalization;
using System.Linq;
using System.Reflection;
Expand All @@ -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 &&
Expand Down
27 changes: 13 additions & 14 deletions Localizer/Localizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("<name>k__BackingField", "Localizer");
LoadedLocalizer.SetField("name", "Localizer");
this.SetField("<File>k__BackingField", LoadedLocalizer.File);
this.SetField("<Code>k__BackingField", LoadedLocalizer.Code);
Log = LogManager.GetLogger(nameof(Localizer));
Expand Down Expand Up @@ -118,20 +116,21 @@ public override void Load()
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("<name>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("<name>k__BackingField", "!Localizer");
LoadedLocalizer.SetField("name", "!Localizer");
}

private static int frameCounter;
Expand All @@ -140,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<UIText>("_modName")?.IsMouseHovering ?? false;
if (modName == "Localizer")
if (modName == "!Localizer")
{
frameCounter++;

Expand Down Expand Up @@ -185,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<UIText>("_modName").OnClick += (evt, element) =>
{
Expand Down
28 changes: 20 additions & 8 deletions Localizer/Package/Import/AutoImportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,18 @@ private void OnBeforeModCtor(object mod)
return;
}

Utils.SafeWrap(() =>
try
{
if (Localizer.Config.AutoImport)
{
var wrapped = new LoadedModWrapper(mod);
Utils.LogInfo($"Early auto import for mod: [{wrapped.Name}]");
Import(wrapped);
}
});
}
catch
{
}
}

private void OnPostSetupContent()
Expand Down Expand Up @@ -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)
Expand All @@ -105,15 +108,18 @@ void LoadPackedPackages()
}

_packageManage.AddPackage(pack);
});
}
catch
{
}
}
}

void LoadSourcePackages()
{
foreach (var dir in new DirectoryInfo(Localizer.SourcePackageDirPath).GetDirectories())
{
Utils.SafeWrap(() =>
try
{
var pack = _sourcePackageLoad.Load(dir.FullName, _fileLoad);
if (pack == null)
Expand All @@ -123,7 +129,10 @@ void LoadSourcePackages()

_packageManage.AddPackage(pack);
_packagePack.Pack(Path.Combine(dir.FullName, "Package.json"));
});
}
catch
{
}
}
}

Expand Down Expand Up @@ -169,7 +178,7 @@ void QueuePackageGroup(IPackageGroup packageGroup)
}
}

Utils.SafeWrap(() =>
try
{
_packageImport.Clear();

Expand All @@ -190,7 +199,10 @@ void QueuePackageGroup(IPackageGroup packageGroup)
Localizer.RefreshLanguages();

Utils.LogDebug("Auto import end.");
});
}
catch
{
}
}

protected override void DisposeUnmanaged()
Expand Down

0 comments on commit 0d36876

Please sign in to comment.