Modifying game base code

Started by battlemage64, February 18, 2020, 08:53:53 PM

Previous topic - Next topic

battlemage64

Is there a way to modify/rewrite a class in the game's base code? For my mod I want to modify the code of the Pawn_MindState class (specifically just a few lines) but I'm not sure how to patch/modify it (or if I have to overwrite it completely, how to do that). Is such a thing possible? If not, is there a workaround?

edit: maybe Harmony would work for this? If so can someone link a tutorial on how to use it because the description sounds like it would work but I have no idea how to use it.
I love to mod and boy am I bad at it

K

Harmony is certainly the tool for this. Harmony allows you to execute code before and after a method with prefixes and postfixes. If what you're trying to do is a little bit more involved you can also use a transpiler which can modify the method directly, though they're significantly more complicated to use.

If you explain what you're trying to do a bit more, then it'll be easier to give you good answers.

battlemage64

#2
I looked into it a bit -- seems like Harmony would work. My mod adds umbrellas, which would prevent the pawn from getting the Soaking Wet debuff due to rain (but ideally they'd still get it from water tiles). My current ideas are to either: use a Postfix to remove the Soaking Wet thought if the pawn has an umbrella (simple but doesn't check whether the thought came from a water tile or rain) or try and use a transpiler to insert the code directly into the method (but that would be more complicated and I don't really know how any of Harmony works).

(edit: hey, the Transpiler tutorial uses RimWorld as an example, that's shockingly convenient!)

(edit 2: actually, I can just use a Postfix that removes any instances of the Soaking Wet thought then rechecks the pawn's location and re-adds Soaking Wet if they're on water. A bit inefficient but whatever.)
I love to mod and boy am I bad at it

LWM

The transpiler is super elegant when it works.  If you would like a hand with it, someone could probably help you, especially if you can show exactly what change to the C# code you want to make!

If you use a postfix, and they had a soaking wet from earlier but are now getting rained on, would that cause a (rare) happy pawn?

As far as I can tell, Pardeike wrote Harmony so he could make the Zombieland mod for RimWorld?

battlemage64

I'll probably start with a Postfix; even if it causes some minor logic problems it's better than accidentally f***ing up the whole method because I got a bit of IL code wrong (which is basically 100% likely as I don't use low-level languages much). You're right that it might remove a Soaking Wet if the pawn had it from earlier but I'm working on ways of preventing that (like maybe only removing the thought if it was just added, which means its remaining duration = the max duration). Thank you!

Also, huh. I didn't know that about pardeike. It makes sense, I guess, since Harmony is for a pretty niche case.
I love to mod and boy am I bad at it

K

Transpilers can have pretty bad compatibility issues under the right (wrong?) conditions so it's probably best to avoid them unless you absolutely know what you're doing. Learning to read and write CIL isn't very fun, either.

Harmony was created for RimWorld, though it works for any game that runs on a .NET language (mostly Unity games), so you'll find that most all the examples are written for RimWorld.

LWM

Quote from: K on February 19, 2020, 09:55:49 AM
Transpilers can have pretty bad compatibility issues under the right (wrong?) conditions so it's probably best to avoid them unless you absolutely know what you're doing. Learning to read and write CIL isn't very fun, either.
I would disagree.  A carefully written Transpiler should play well with other carefully written Transpilers.

Okay, okay, "carefully written" is funny there.  But from my point of view, there are several places in DeepStorage where I have to basically cross my fingers and hope that no one uses a Prefix that stops my Prefix from happening.  So they are inherently less compatible.

And if you're adding only one line or something, it's usually fairly easy - once you've done 20 of the damn things ;)

K

Quote from: LWM on February 19, 2020, 02:01:09 PM
I would disagree.  A carefully written Transpiler should play well with other carefully written Transpilers.

That's fair. Of course, the question becomes whether you can write a carefully written transpiler or not. And in any case, I think that there's relatively few cases that truly necessitate a transpiler, since most things can be accomplished with prefixes and postfixes, albeit sometimes inefficiently.