SiraUtil icon indicating copy to clipboard operation
SiraUtil copied to clipboard

Add TargetMethod() to Affinity

Open Aeroluna opened this issue 3 years ago • 4 comments

Useful to getting types that are not available at compile time Adding TargetMethods() too would be nice https://harmony.pardeike.net/articles/patching-auxilary.html#auxilary-patch-methods

Aeroluna avatar Aug 25 '22 17:08 Aeroluna

Could you provide an example of how you'd want this to look like with the Affinity system?

Auros avatar Aug 25 '22 18:08 Auros

Adding a [AffinityTargetMethod] attribute to complement [HarmonyTargetMethod] Instead of needing to specific method to patch from [AffinityPatch], call a method that returns what method to patch.

[HarmonyTargetMethod]
private static MethodBase CalculateMethod()
[AffinityTargetMethod]
private MethodBase CalculateMethod()

An example is where you need to target a private type

[AffinityPostfix]
[AffinityPatch(typeof(BeatmapObjectsInTimeRowProcessor.SliderTailData), "GetCopy")]
// not possible, as this is a private class
private void Postfix()
[AffinityTargetMethod]
private MethodBase TargetMethod()
{
  Type type = Type.GetType("BeatmapObjectsInTimeRowProcessor+SliderTailData,BeatmapCore");
  return AccessTools.Method(type, "GetCopy");
}

[AffinityPostfix]
private void Postfix()

Aeroluna avatar Aug 25 '22 22:08 Aeroluna

What about when you want multiple target methods for different affinity patches? How do you think they should be distinguished?

Auros avatar Aug 25 '22 23:08 Auros

Following how Harmony does it: only allow one target method for a class and disallow using individual annotations with [AffinityTargetMethod] https://github.com/BepInEx/HarmonyX/blob/master/Harmony/Public/PatchClassProcessor.cs#L142

[AffinityTargetMethod]
private MethodBase TargetMethod()
{
  return AccessTools.Method(typeof(something), "something");
}

[AffinityPostfix]
[AffinityPatch(typeof(something), "something")]
// invalid to use this while [AffinityTargetMethod] defined and will throw
private void Postfix()

Aeroluna avatar Aug 26 '22 00:08 Aeroluna