Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.0 fixes and other refactors #178

Merged
merged 4 commits into from
Aug 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions R2API.Test/ReflectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,11 @@ public void TestReflectionStructPublicPropertyGetAndSet() {

[Fact]
public void TestReflectionItemDropAPI() {
var method = typeof(PickupIndex).GetMethodCached("get_itemIndex");
Assert.NotNull(method);


var nextElementUniform = typeof(Xoroshiro128Plus)
.GetMethods()
.First(x => x.Name == "NextElementUniform"
&& x.GetParameters()[0].ParameterType.GUID == typeof(List<>).GUID);

var nextElementUniform = typeof(Xoroshiro128Plus).GetMethodWithConstructedGenericParameter("NextElementUniform", typeof(List<>));
Assert.NotNull(nextElementUniform);

var nextElementUniformExact = nextElementUniform.MakeGenericMethod(typeof(PickupIndex));
Assert.NotNull(nextElementUniformExact);
}

[Fact]
Expand Down
27 changes: 9 additions & 18 deletions R2API/ItemDropAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,19 @@ private static void RunOnBuildDropTable(On.RoR2.Run.orig_BuildDropTable orig, Ru
}
// These lists should be replaced soon.
self.availableTier1DropList.Clear();
self.availableTier1DropList.AddRange(GetDefaultDropList(ItemTier.Tier1).Select(x => PickupCatalog.FindPickupIndex(x))
self.availableTier1DropList.AddRange(GetDefaultDropList(ItemTier.Tier1).Select(PickupCatalog.FindPickupIndex)
.ToList());

self.availableTier2DropList.Clear();
self.availableTier2DropList.AddRange(GetDefaultDropList(ItemTier.Tier2).Select(x => PickupCatalog.FindPickupIndex(x))
self.availableTier2DropList.AddRange(GetDefaultDropList(ItemTier.Tier2).Select(PickupCatalog.FindPickupIndex)
.ToList());

self.availableTier3DropList.Clear();
self.availableTier3DropList.AddRange(GetDefaultDropList(ItemTier.Tier3).Select(x => PickupCatalog.FindPickupIndex(x))
self.availableTier3DropList.AddRange(GetDefaultDropList(ItemTier.Tier3).Select(PickupCatalog.FindPickupIndex)
.ToList());

self.availableEquipmentDropList.Clear();
self.availableEquipmentDropList.AddRange(GetDefaultEquipmentDropList().Select(x => PickupCatalog.FindPickupIndex(x))
self.availableEquipmentDropList.AddRange(GetDefaultEquipmentDropList().Select(PickupCatalog.FindPickupIndex)
.ToList());

self.availableLunarDropList.Clear();
Expand Down Expand Up @@ -290,21 +290,14 @@ private static void DropRewards(ILContext il) {

cursor.Emit(OpCodes.Stloc_0);

cursor.GotoNext(x => x.MatchCall(typeof(PickupIndex).GetMethodCached("get_itemIndex")));

var itemIndex = Reflection.ReadLocalIndex(cursor.Next.Next.OpCode, cursor.Next.Next.Operand);

cursor.GotoNext(x => x.MatchCall(typeof(PickupIndex).GetConstructorCached(new[] { typeof(ItemIndex) })));
cursor.GotoPrev(x => x.OpCode == OpCodes.Ldloca_S);

var pickupIndex = (VariableDefinition)cursor.Next.Operand;
var nextElementUniform = typeof(Xoroshiro128Plus)
.GetMethodWithConstructedGenericParameter("NextElementUniform", typeof(List<>))
.MakeGenericMethod(typeof(PickupIndex));
cursor.GotoNext(MoveType.After, x => x.MatchCallOrCallvirt(nextElementUniform));
var pickupIndex = Reflection.ReadLocalIndex(cursor.Next.OpCode, cursor.Next.Operand);

cursor.Goto(0);

cursor.GotoNext(x => x.MatchStloc(itemIndex));
cursor.Emit(OpCodes.Stloc_S, itemIndex);


cursor.Emit(OpCodes.Ldarg_0);
cursor.Emit(OpCodes.Dup);
cursor.Emit(OpCodes.Ldfld, typeof(BossGroup).GetFieldCached("rng"));
Expand All @@ -321,8 +314,6 @@ private static void DropRewards(ILContext il) {
});

cursor.Emit(OpCodes.Stloc_S, pickupIndex);
cursor.Emit(OpCodes.Ldloca_S, pickupIndex);
cursor.Emit(OpCodes.Call, typeof(PickupIndex).GetMethodCached("get_itemIndex"));
}

public static void AddDrops(ItemDropLocation dropLocation, params PickupSelection[] pickups) {
Expand Down
8 changes: 4 additions & 4 deletions R2API/R2API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class R2API : BaseUnityPlugin {
public const string PluginVersion = "0.0.1";


private const int GameBuild = 4892828;
private const int GameBuild = 5381045;

internal new static ManualLogSource Logger { get; set; }

Expand Down Expand Up @@ -107,9 +107,9 @@ public static bool IsLoaded(string submodule) {
}

private static void AddHookLogging() {
ModManager.OnHook += (hookOwner, @base, arg3, arg4) => LogMethod(@base, hookOwner);
ModManager.OnDetour += (hookOwner, @base, arg3) => LogMethod(@base, hookOwner);
ModManager.OnNativeDetour += (hookOwner, @base, arg3, arg4) => LogMethod(@base, hookOwner);
ModManager.OnHook += (hookOwner, @base, _, __) => LogMethod(@base, hookOwner);
ModManager.OnDetour += (hookOwner, @base, _) => LogMethod(@base, hookOwner);
ModManager.OnNativeDetour += (hookOwner, @base, _, __) => LogMethod(@base, hookOwner);

HookEndpointManager.OnAdd += (@base, @delegate) => LogMethod(@base, @delegate.Method.Module.Assembly);
HookEndpointManager.OnModify += (@base, @delegate) => LogMethod(@base, @delegate.Method.Module.Assembly);
Expand Down
2 changes: 1 addition & 1 deletion R2API/R2API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>7.2</LangVersion>
<LangVersion>preview</LangVersion>
<Version>2.0.0</Version>
</PropertyGroup>

Expand Down
27 changes: 25 additions & 2 deletions R2API/Utils/Reflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,34 @@ public static MethodInfo GetMethodCached<T>(string name) =>
/// </summary>
/// <param name="T">The type to search</param>
/// <param name="name">The name of the method to find</param>
/// <returns></returns>
/// <returns>The found <see cref="MethodInfo"/></returns>
public static MethodInfo GetMethodCached(this Type T, string name) =>
MethodCache.GetOrAddOnNull((T, name), x => x.T.GetMethod(x.name, AllFlags)
?? throw new Exception($"Could not find {nameof(MethodInfo)} on {T.FullName} with the name {name}"));

/// <summary>
/// Gets the generic method of the specified type with the specified generic type definition parameter
/// </summary>
/// <param name="T">The type to search</param>
/// <param name="name">The name of the method to find</param>
/// <param name="genericTypeDefinition">The generic type definition parameter</param>
/// <returns>The found <see cref="MethodInfo"/></returns>
public static MethodInfo GetMethodWithConstructedGenericParameter(this Type T, string name, Type genericTypeDefinition) {
return T.GetMethods().First(method => {
if (method.Name != name) {
return false;
}

var parameterType = method.GetParameters().First().ParameterType;
if (!parameterType.IsConstructedGenericType) {
return false;
}

var t = parameterType.GetGenericArguments().First();
return parameterType == genericTypeDefinition.MakeGenericType(t);
});
}

/// <summary>
/// Gets the method on the specified type and caches it. This overload is used when the method is ambiguous
/// </summary>
Expand Down Expand Up @@ -777,7 +800,7 @@ private static FastReflectionDelegate GenerateCallDelegate(this MethodInfo metho
throw new ArgumentException("Method cannot be null.", nameof(method));
}

var dmd = new DynamicMethodDefinition(
using var dmd = new DynamicMethodDefinition(
$"CallDelegate<{method.Name}>", typeof(object), new[] { typeof(object), typeof(object[]) });
var il = dmd.GetILProcessor();

Expand Down