Fix Hook.Patch confusing .ctor with .cctor
This commit is contained in:
@@ -820,22 +820,42 @@ namespace Barotrauma
|
|||||||
if (classType == null) throw new InvalidOperationException($"Invalid class name '{className}'");
|
if (classType == null) throw new InvalidOperationException($"Invalid class name '{className}'");
|
||||||
|
|
||||||
const BindingFlags BINDING_FLAGS = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
const BindingFlags BINDING_FLAGS = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||||
const string CTOR = ".ctor";
|
|
||||||
|
|
||||||
MethodBase method = null;
|
MethodBase method = null;
|
||||||
if (parameters != null)
|
if (parameters != null)
|
||||||
{
|
{
|
||||||
var parameterTypes = parameters.Select(x => LuaUserData.GetType(x)).ToArray();
|
var parameterTypes = parameters.Select(x => LuaUserData.GetType(x)).ToArray();
|
||||||
// TODO: remove the casts once we can use C# 9 features
|
method = methodName switch
|
||||||
method = methodName == CTOR
|
{
|
||||||
? (MethodBase)classType.GetConstructor(BINDING_FLAGS, null, parameterTypes, null)
|
".cctor" => classType.TypeInitializer,
|
||||||
: (MethodBase)classType.GetMethod(methodName, BINDING_FLAGS, null, parameterTypes, null);
|
".ctor" => classType.GetConstructors(BINDING_FLAGS)
|
||||||
|
.Except(new[] { classType.TypeInitializer })
|
||||||
|
.Where(x => x.GetParameters().Select(x => x.ParameterType).SequenceEqual(parameterTypes))
|
||||||
|
.SingleOrDefault(),
|
||||||
|
_ => classType.GetMethod(methodName, BINDING_FLAGS, null, parameterTypes, null),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
method = methodName == CTOR
|
ConstructorInfo GetCtor()
|
||||||
? (MethodBase)classType.GetConstructor(BINDING_FLAGS, null, Array.Empty<Type>(), null)
|
{
|
||||||
: (MethodBase)classType.GetMethod(methodName, BINDING_FLAGS);
|
var ctors = classType.GetConstructors(BINDING_FLAGS)
|
||||||
|
.Except(new[] { classType.TypeInitializer })
|
||||||
|
.GetEnumerator();
|
||||||
|
|
||||||
|
if (!ctors.MoveNext()) return null;
|
||||||
|
var ctor = ctors.Current;
|
||||||
|
|
||||||
|
if (ctors.MoveNext()) throw new AmbiguousMatchException();
|
||||||
|
return ctor;
|
||||||
|
}
|
||||||
|
|
||||||
|
method = methodName switch
|
||||||
|
{
|
||||||
|
".cctor" => throw new InvalidOperationException("Type initializers can't have parameters."),
|
||||||
|
".ctor" => GetCtor(),
|
||||||
|
_ => classType.GetMethod(methodName, BINDING_FLAGS),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method == null)
|
if (method == null)
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ namespace TestProject.LuaCs
|
|||||||
if (patchId != null) args.Add(Stringify(patchId));
|
if (patchId != null) args.Add(Stringify(patchId));
|
||||||
args.Add(Stringify(className));
|
args.Add(Stringify(className));
|
||||||
args.Add(Stringify(methodName));
|
args.Add(Stringify(methodName));
|
||||||
if (parameters != null && parameters.Length > 0)
|
if (parameters != null)
|
||||||
{
|
{
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
sb.Append("{ ");
|
sb.Append("{ ");
|
||||||
@@ -101,7 +101,7 @@ namespace TestProject.LuaCs
|
|||||||
end
|
end
|
||||||
", LuaCsHook.HookMethodType.Before);
|
", LuaCsHook.HookMethodType.Before);
|
||||||
Assert.Equal(DataType.String, returnValue.Type);
|
Assert.Equal(DataType.String, returnValue.Type);
|
||||||
return new(returnValue.String, () => luaCs.RemovePrefix<T>(returnValue.String, methodName));
|
return new(returnValue.String, () => luaCs.RemovePrefix<T>(returnValue.String, methodName, parameters));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static PatchHandle AddPostfix<T>(this LuaCsSetup luaCs, string body, string methodName, string[]? parameters = null, string? patchId = null)
|
public static PatchHandle AddPostfix<T>(this LuaCsSetup luaCs, string body, string methodName, string[]? parameters = null, string? patchId = null)
|
||||||
@@ -113,7 +113,7 @@ namespace TestProject.LuaCs
|
|||||||
end
|
end
|
||||||
", LuaCsHook.HookMethodType.After);
|
", LuaCsHook.HookMethodType.After);
|
||||||
Assert.Equal(DataType.String, returnValue.Type);
|
Assert.Equal(DataType.String, returnValue.Type);
|
||||||
return new(returnValue.String, () => luaCs.RemovePostfix<T>(returnValue.String, methodName));
|
return new(returnValue.String, () => luaCs.RemovePostfix<T>(returnValue.String, methodName, parameters));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool RemovePrefix<T>(this LuaCsSetup luaCs, string patchId, string methodName, string[]? parameters = null)
|
public static bool RemovePrefix<T>(this LuaCsSetup luaCs, string patchId, string methodName, string[]? parameters = null)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using Barotrauma;
|
using Barotrauma;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using MoonSharp.Interpreter;
|
using MoonSharp.Interpreter;
|
||||||
|
using System;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using Xunit.Abstractions;
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
@@ -345,10 +346,10 @@ namespace TestProject.LuaCs
|
|||||||
{
|
{
|
||||||
using var postfixHandle = luaCs.AddPostfix<PatchTargetConstructor>(@$"
|
using var postfixHandle = luaCs.AddPostfix<PatchTargetConstructor>(@$"
|
||||||
instance.Ctor = {(int)PatchTargetConstructor.CtorType.Patched}
|
instance.Ctor = {(int)PatchTargetConstructor.CtorType.Patched}
|
||||||
", ".ctor");
|
", ".ctor", Array.Empty<string>());
|
||||||
using var prefixHandle = luaCs.AddPrefix<PatchTargetConstructor>(@$"
|
using var prefixHandle = luaCs.AddPrefix<PatchTargetConstructor>(@$"
|
||||||
instance.PrefixRan = true
|
instance.PrefixRan = true
|
||||||
", ".ctor");
|
", ".ctor", Array.Empty<string>());
|
||||||
var target = new PatchTargetConstructor();
|
var target = new PatchTargetConstructor();
|
||||||
Assert.Equal(PatchTargetConstructor.CtorType.Patched, target.Ctor);
|
Assert.Equal(PatchTargetConstructor.CtorType.Patched, target.Ctor);
|
||||||
Assert.True(target.PrefixRan);
|
Assert.True(target.PrefixRan);
|
||||||
|
|||||||
Reference in New Issue
Block a user