Skip to content

Commit

Permalink
Merge pull request #65 from nicoco007/fix-argument-variations
Browse files Browse the repository at this point in the history
Fix argument variations not working
  • Loading branch information
Auros authored Feb 19, 2024
2 parents a07091b + 7074bbd commit 84e677c
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ public MethodInfo Patch(IAffinity affinity, MethodInfo affinityMethod, AffinityP

Type[]? types = null;
if (patch.ArgumentTypes is not null && patch.ArgumentTypes.Length != 0)
{
types = patch.ArgumentTypes;
ParseSpecialArguments(types, patch.ArgumentVariations);
}

MethodBase originalMethod = patch.MethodType switch
{
Expand Down Expand Up @@ -154,6 +157,35 @@ public void Unpatch(MethodInfo contract)
}
}

// adapted from https://github.com/pardeike/Harmony/blob/77d37bee5bffd053681b34ba70a650d6d2d45486/Harmony/Public/Attributes.cs#L335-L366
private void ParseSpecialArguments(Type[] argumentTypes, ArgumentType[]? argumentVariations)
{
if (argumentVariations is null || argumentVariations.Length == 0)
{
return;
}

if (argumentTypes.Length < argumentVariations.Length)
{
throw new ArgumentException("argumentVariations contains more elements than argumentTypes", nameof(argumentVariations));
}

for (var i = 0; i < argumentTypes.Length; i++)
{
var type = argumentTypes[i];
switch (argumentVariations[i])
{
case ArgumentType.Ref:
case ArgumentType.Out:
argumentTypes[i] = type.MakeByRefType();
break;
case ArgumentType.Pointer:
argumentTypes[i] = type.MakePointerType();
break;
}
}
}

// https://stackoverflow.com/questions/9505117/creating-delegates-dynamically-with-parameter-names
private Type CreateDelegateType(MethodInfo method)
{
Expand Down

0 comments on commit 84e677c

Please sign in to comment.