Ludeon Forums

RimWorld => Mods => Help => Topic started by: AncientGammoner on July 21, 2021, 11:54:28 AM

Title: Harmony Patching and Private Method Calls
Post by: AncientGammoner on July 21, 2021, 11:54:28 AM
Let me preface this by saying the only C# I know is from modding and most of that is from modifying existing code, so I'm very much a beginner.  My question is:

If I'm trying to patch a method using Harmony (like with a Prefix) and the original method calls out a private method that I also need to call out, how do I reference that?  Google mentions reflections and publicized assemblies and honestly I'm lost.  An example would be very helpful.
Title: Re: Harmony Patching and Private Method Calls
Post by: RawCode on July 21, 2021, 09:35:07 PM
please provide links to articles about reflection that made you lost and explain what exactly you do not understand.
Title: Re: Harmony Patching and Private Method Calls
Post by: AncientGammoner on July 22, 2021, 07:35:57 PM
I just haven't seen a good, clear example of the format for calling a private method with inputs and returning a value.

I'm trying to call:
public static class CaravanEnterMapUtility
     private static IntVec3 FindNearEdgeCell(Map map, Predicate<IntVec3> extraCellValidator)

What I've been able to cobble together so far doesn't work (below).  Does anyone have an actual example I could look at, I would think this is a rather common thing to do.

MethodInfo methodInfo = typeof(CaravanEnterMapUtility).GetMethod("FindNearEdgeCell", BindingFlags.NonPublic | BindingFlags.Instance);
var parameters = new object[] { map, extraCellValidator };
__result = (IntVec3)methodInfo.Invoke(null, parameters);

Edit: Actually I think I got it to work, it should be BindingFlags.Static not BindingFlags.Instance since the method is static