What mod tools to use?

Started by Greep, March 25, 2017, 06:18:43 PM

Previous topic - Next topic

Greep

So I've done a fair amount of programming, but I've never written any dlls for mods.  I've been looking at example mods to see what they've done, and I see some using harmony, I've seen some rely on hugslib, etc.  What's the best tools to use if I mostly want to modify existing functionality rather than developing content?
1.0 Mods: Raid size limiter:
https://ludeon.com/forums/index.php?topic=42721.0

MineTortoise:
https://ludeon.com/forums/index.php?topic=42792.0
HELLO!

(WIPish)Strategy Mode: The experienced player's "vanilla"
https://ludeon.com/forums/index.php?topic=43044.0

jimthenoob

grab yourself something simple like programmers notepad and you can manipulate a lot of the current functions through XML files and a bit of logic.

If you want to dive into the code I suggest grabbing visual studio or similar,  and you can then pretty much look at all the code in the game. there is a sticky around here for a quick little tutorial on how to make a simple DLL mod as well.

https://ludeon.com/forums/index.php?topic=3408.0

Greep

Oh sure I've been code diving and I'm about to start the dll portion.  But I just don't want to end up rewriting code halfway into it when I find I'm re-inventing the wheel.
1.0 Mods: Raid size limiter:
https://ludeon.com/forums/index.php?topic=42721.0

MineTortoise:
https://ludeon.com/forums/index.php?topic=42792.0
HELLO!

(WIPish)Strategy Mode: The experienced player's "vanilla"
https://ludeon.com/forums/index.php?topic=43044.0

Fluffy (l2032)

HugsLib has a few very useful things for modding, the ones I personally find the most useful are;
- A collection of hooks you can subscribe to for important events; game start, map load, etc. (implemented with virtuals on HugsLib.ModBase)
- A mod options screen you can add options to (and hugslib will take care of saving those options)
- A 'global' object to store custom data on.
Read more about it here: https://github.com/UnlimitedHugs/RimworldHugsLib/wiki

Harmony does a specific task; patching methods. Such patches can do extremely powerful things;
- Call a custom method before each call to another (base game or modded) method. So every time the game calls Foo(), you can make it call Bar() before calling Foo(). You can even stop it calling Foo() altogether, essentially replacing Foo() with Bar().
- Call a custom method after each call to another (base game or modded) method. So every time the game calls Foo(), you can call Bar() after Foo(), optionally changing the return value from Foo().
- Change what any (base game or modded) method does. You can replace IL code of any method with any other IL code (provided it is valid). This would allow you to surgically change the functionality of methods, without having to completely replace them, or alter the code that calls them.
Find out more about harmony here: https://github.com/pardeike/harmony/wiki

So should you use them?
That depends. RimWorld instantiates instances of classes that are related to things (e.g. pawns, items), workers for hediffs (diseases), stats, rooms, etc, etc automatically. If your modifications are not auto-instantiated, you'll need to hook up some kind of bootstrap method for it to run. Or you could use HugsLib, which will give you that bootstrap method, and more options for when to execute your code as well.

Do you want users to be able to change settings for your modifications? You could implement your own options screen, but that would need some way to be accessible, which means added UI clutter somewhere, and you'd have to sort out the UI + save/load for it, so you might as well let HugsLib handle it.

Do you need to store data that is specific to the game? There's no direct way to do this in vanilla anymore. You can attach data to the map, but the player could migrate to another map, and you'd lose the data. You could code a way to handle such cases, but you might as well let HugsLib handle it.

To make the choice even easier, HugsLib is lightweight, and pretty much everyone who plays with mods will already have it installed. There's really no reason for not using HugsLib if you ask me.

As for Harmony, if you want to modify things without adding new content, odds are that you'll find patches extremely useful. You could use detours - which would accomplish the same thing but in a much cruder way, and which would be much more likely to conflict with other mods.

In the distant past, if we wanted to change how a vanilla method Foo() operated, we had to create a new method Bar(), then change all the references from Foo() to Bar() [e.g. EdB's interface mod used to completely replace the entire UI]. With detours, we could replace Foo() with Bar() directly, but we'd still have to copy-paste much of Foo() into Bar(). With Harmony, we can simply change Foo() to do what we wanted it to do in the first place.

Greep

Ah yeah, I was somewhat worried that not everyone also used hugslib, but if pretty much everyone does, sounds like it's worth using.  All of those things you listed for it sound like things I want.

I've noticed that hugs also has detouring from the wiki, does this mean harmony isn't necessary if I  just wanted to replace functions?
1.0 Mods: Raid size limiter:
https://ludeon.com/forums/index.php?topic=42721.0

MineTortoise:
https://ludeon.com/forums/index.php?topic=42792.0
HELLO!

(WIPish)Strategy Mode: The experienced player's "vanilla"
https://ludeon.com/forums/index.php?topic=43044.0

Granitecosmos

Quote from: Fluffy (l2032) on March 25, 2017, 07:09:06 PM
So every time the game calls Foo(), you can make it call Bar() before calling Foo(). You can even stop it calling Foo() altogether, essentially replacing Foo() with Bar().

I approve of this.

Now, gentlemen, please ignore me and carry on.