Skip to content

Commit

Permalink
Update MpMethodUtil (#441)
Browse files Browse the repository at this point in the history
`MpMethodUtil:GetMethod` now supports indexer getters/setters and async MoveNext methods, using new Harmony 2.3 features.

`MpMethodUtil:GetLocalFunc` should now support all method types besides `MethodType.Normal`. They previously weren't supported because `MpMethodUtil:GetLocalFunc` method used the `parentMethod` argument in local function prefixes, rather than the name of the method returned by `MpMethodUtil:GetMethod`.
  • Loading branch information
SokyranTheDragon authored Apr 16, 2024
1 parent 4e96ac2 commit 67d1537
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Source/Client/Util/MpMethodUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ public static MethodInfo GetLocalFunc(Type parentType, string parentMethod = nul
var displayClassPrefix = $"{DisplayClassPrefix}{parentId}_";

// Example: <DoWindowContents>g__Start|
var localFuncPrefix = $"<{parentMethod}>{LocalFunctionInfix}{localFunc}|";
var localFuncPrefix = $"<{parent.Name}>{LocalFunctionInfix}{localFunc}|";

// Example: <DoWindowContents>g__Start|10
var localFuncPrefixWithId = $"<{parentMethod}>{LocalFunctionInfix}{localFunc}|{parentId}";
var localFuncPrefixWithId = $"<{parent.Name}>{LocalFunctionInfix}{localFunc}|{parentId}";

var candidates = parentType.GetNestedTypes(AccessTools.all).
Where(t => t.Name.StartsWith(displayClassPrefix)).
Expand Down Expand Up @@ -142,12 +142,12 @@ public static MethodBase GetMethod(Type type, string methodName, MethodType meth

case MethodType.Getter:
if (methodName == null)
return null;
return AccessTools.DeclaredIndexer(type, args).GetGetMethod(true);
return AccessTools.DeclaredProperty(type, methodName).GetGetMethod(true);

case MethodType.Setter:
if (methodName == null)
return null;
return AccessTools.DeclaredIndexer(type, args).GetSetMethod(true);
return AccessTools.DeclaredProperty(type, methodName).GetSetMethod(true);

case MethodType.Constructor:
Expand All @@ -162,6 +162,11 @@ public static MethodBase GetMethod(Type type, string methodName, MethodType meth
if (methodName == null)
return null;
return AccessTools.EnumeratorMoveNext(AccessTools.DeclaredMethod(type, methodName, args));

case MethodType.Async:
if (methodName == null)
return null;
return AccessTools.AsyncMoveNext(AccessTools.DeclaredMethod(type, methodName, args));
}

return null;
Expand Down

0 comments on commit 67d1537

Please sign in to comment.