Way of going about modifying existing getters using Harmony

Started by Hexdragon, August 24, 2023, 08:41:01 AM

Previous topic - Next topic

Hexdragon

Hello everyone!

I saw that getter are so called "Syntax sugar" and they actually don't excist at runtime, how do I work around this? I'm new to Harmony and patching but not new to coding.

I'm trying to change this getter in CompShearable:

protected override bool Active
{
get
{
if (!base.Active)
{
return false;
}
Pawn pawn = this.parent as Pawn;
return pawn == null || pawn.ageTracker.CurLifeStage.shearable;
}
}

Should I just make a new class called like CompShearableUpdated which is a copy of CompShearable except the getter I want to change, and then use Harmony to patch the methods where CompShearable is used and replace it with CompShearableUpdated?

If this is the solution how do I go about patching, I looked at Harmony's documentation and I couldn't fin any good examples.

protected override CompHasGatherableBodyResource GetComp(Pawn animal)
{
return animal.TryGetComp<CompShearable>();
}

My current guess is:

[HarmonyPatch(typeof(Building_Battery))]
[HarmonyPatch("GetComp", MethodType.Getter)]
static bool Prefix(Pawn animal, CompShearable __result) {
__result = animal.TryGetComp<CompShearable>();
Log.Message("Harmony Patch Applied");
return false;
}


But I think this is probably wrong and setting the __result equal to what I want the return value to be is not how it should be. I saw this thread : https://github.com/pardeike/Harmony/issues/70 and it seems they are using a completely different format, so I would have to make a NewHarmonyMethod or something and put it somehow into the prefix while also keeping the 'return false' init to replace the original behaviour?

Any help would be appreciated
I wish everyone a nice day!

wilkinsonwilfrid

The fact that patching can make your code more secure is something that should not be forgotten.