Skip to content

Commit

Permalink
Merge pull request #17 from AxeelAnder/master
Browse files Browse the repository at this point in the history
v1.1.1.7
  • Loading branch information
AxeelAnder authored Nov 10, 2019
2 parents 9c54b20 + 81adec8 commit 29c7bbf
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 13 deletions.
42 changes: 40 additions & 2 deletions Localizer/Helpers/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ public static object Field(this object o, string name, BindingFlags flags = All)
{
if(o is null)
throw new ArgumentNullException(nameof(o));
var field = o.GetType().GetField(name, flags) ??
throw new Exception($"Cannot find field: {o.GetType().FullName}.{name}");
var field = o.GetType().GetFieldRecursively(name, flags);
return field.GetValue(o);
}

public static void SetField(this object o, string name, object value, BindingFlags flags = All)
{
if(o is null)
throw new ArgumentNullException(nameof(o));
var field = o.GetType().GetFieldRecursively(name, flags);
field.SetValue(o, value);
}

public static object Field(this Type t, string name, BindingFlags flags = All)
{
if(t is null)
Expand All @@ -29,6 +36,15 @@ public static object Field(this Type t, string name, BindingFlags flags = All)
return field.GetValue(null);
}

public static void SetField(this Type t, string name, object value, BindingFlags flags = All)
{
if(t is null)
throw new ArgumentNullException(nameof(t));
var field = t.GetField(name, flags) ??
throw new Exception($"Cannot find field: {t.FullName}.{name}");
field.SetValue(null, value);
}

public static object Prop(this object o, string name, BindingFlags flags = All)
{
if(o is null)
Expand Down Expand Up @@ -79,6 +95,28 @@ public static object Method(this Type t, string name, object arg, BindingFlags f
return t.Method(name, new[] {arg}, flags);
}

public static FieldInfo GetFieldRecursively(this Type type, string name, BindingFlags flags = All)
{
if(type is null)
throw new ArgumentNullException(nameof(type));

var t = type;
FieldInfo result;
while ((result = t.GetField(name, flags)) is null)
{
t = t.BaseType;
if(t == typeof(object))
break;
}

if (result is null)
{
throw new Exception($"Cannot find field: {type.FullName}.{name}");
}

return result;
}

public static Module Tr()
{
return Assembly.GetAssembly(typeof(Main)).ManifestModule;
Expand Down
22 changes: 12 additions & 10 deletions Localizer/Localizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,26 @@ public sealed class Localizer : Mod
public Localizer()
{
Instance = this;
var mod = new LoadedModWrapper(Tr().GetType("Terraria.ModLoader.Core.AssemblyManager")
.Field("loadedMods")
.Method("get_Item", "!Localizer"));
this.SetField("<File>k__BackingField", mod.File);
this.SetField("<Code>k__BackingField", mod.Code);
Log = LogManager.GetLogger(nameof(Localizer));

HarmonyInstance = HarmonyInstance.Create(nameof(Localizer));
var prefix = new HarmonyMethod(typeof(Localizer).GetMethod(nameof(AfterLocalizerCtorHook), ReflectionHelper.All));
HarmonyInstance.Patch(Tr().GetType("Terraria.ModLoader.Core.AssemblyManager")
.GetMethod("Instantiate", ReflectionHelper.All), prefix);

State = OperationTiming.BeforeModCtor;
TmodFile = Instance.Prop("File") as TmodFile;
Init();
_initiated = true;
}

private static void AfterLocalizerCtorHook(object mod)
{
if (!_initiated)
{
State = OperationTiming.BeforeModCtor;
Log = Instance.Logger;
TmodFile = Instance.Prop("File") as TmodFile;
Init();
_initiated = true;
}

Hooks.InvokeBeforeModCtor(mod);
}

Expand Down Expand Up @@ -108,7 +110,7 @@ public override void PostSetupContent()
{
State = OperationTiming.BeforeContentLoad;
Hooks.InvokeBeforeSetupContent();
// CheckUpdate();
CheckUpdate();
}

public override void PostAddRecipes()
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.1.1.6
version = 1.1.1.7
displayName = [c/FF2800:L][c/FF5000:o][c/FF7800:c][c/FFA000:a][c/FFC800:l][c/FFF000:i][c/D7FF00:z][c/AFFF00:e][c/87FF00:r]
hideCode = true
hideResources = false
Expand Down
25 changes: 25 additions & 0 deletions LocalizerTest/Helper/ReflectionHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,23 @@ private static string StaticQux(string qux)
return $"StillQux: {qux}";
}
}

class Foo2 : Foo
{

}

public class ReflectionHelperTest
{
[Fact]
public void GetFieldRecursively_Correct()
{
var foo = new Foo2();
foo.GetType().GetFieldRecursively("bar").GetValue(foo).Should().Be("Bar");

(typeof(Foo2).GetFieldRecursively("staticBar").GetValue(foo) as string).Should().Be("StillBar");
}

[Fact]
public void Field_Correct()
{
Expand All @@ -38,6 +52,17 @@ public void Field_Correct()
(typeof(Foo).Field("staticBar") as string).Should().Be("StillBar");
}

[Fact]
public void SetField_Correct()
{
var foo = new Foo();
foo.SetField("bar", "Bar2");
(foo.Field("bar") as string).Should().Be("Bar2");

typeof(Foo).SetField("staticBar", "StillBar2");
(typeof(Foo).Field("staticBar") as string).Should().Be("StillBar2");
}

[Fact]
public void Prop_Correct()
{
Expand Down

0 comments on commit 29c7bbf

Please sign in to comment.