Skip to content

Commit

Permalink
Merge pull request #32 from chi-rei-den/squid
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
sgkoishi authored Feb 27, 2020
2 parents a24b18b + 0d36876 commit 35198b9
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 46 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
37 changes: 19 additions & 18 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 @@ -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<AutoImportService>();
}

Expand All @@ -107,30 +110,27 @@ public override void Load()
Hooks.InvokeBeforeLoad();
Kernel.Get<RefreshLanguageService>();

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("<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 @@ -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<UIText>("_modName")?.IsMouseHovering ?? false;
if (modName == "Localizer")
if (modName == "!Localizer")
{
frameCounter++;

Expand Down Expand Up @@ -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<UIText>("_modName").OnClick += (evt, element) =>
{
Expand Down Expand Up @@ -296,6 +296,7 @@ public override void Unload()

HookEndpointManager.RemoveAllOwnedBy(this);
Harmony.UnpatchAll(nameof(Localizer));
Harmony.UnpatchAll("ModBrowserMirror");
Kernel.Dispose();

PackageUI = null;
Expand Down
48 changes: 31 additions & 17 deletions Localizer/ModBrowser/Patches.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand All @@ -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("<PopulateModBrowser>"));
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("<OnActivate>"));
Localizer.Harmony.Patch(onActivate, null, null, new HarmonyMethod(NoroHelper.MethodInfo(() => OnActivateTranspiler(null))));
.FirstOrDefault(m => m.Name.Contains("<PopulateModBrowser>"));
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("<OnActivate>"));
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()
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
2 changes: 1 addition & 1 deletion Localizer/build.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
author = Chireiden Team
version = 1.5.0.5
version = 1.5.0.6
displayName = Localizer
hideCode = true
hideResources = false
Expand Down

0 comments on commit 35198b9

Please sign in to comment.