Question about code execution within the modding framework of this game

Started by Bert Zangle, June 19, 2018, 09:45:59 AM

Previous topic - Next topic

Bert Zangle

Hey everyone

Sort of new Rimworld player here, but already compeletely hooked to the game. I recently started poking around in the modding sections, both on Steam, and on the forums here. Seriously, this game has one of the best modding community I've ever encountered. I'm in awe.

Now, for my question... It's a really basic, sort of high-level question about the viability of an idea I have for a new mod.

(Disclaimer: I'm a somewhat competent programmer, but have very little modding experience.)

What are the limitations of executing external code called from within a mod?

To be more concrete: Ideally, I would like my mod to call some (cheap) TensorFlow function, then use the result in-game. Alternatively, the mod could execute some Python script using numpy methods. But I strongly suspect that's not possible, right?

On the other hand, I saw that the modding tutorial mention C# code. Maybe related: I also read about Harmony, which by my (currently very limited) understanding allows runtime execution of code by a mod. So maybe it is possible after all?

Alright, that was very longwinded... Here's the tl;dr:

I want to prototype my idea for a (dialogue) mod. Minimally, I need to execute code which runs a forward pass of a neural network, ideally using an efficient ML/LA library implementation.

My question: Can this be done in the modding environment of this game, or do I have to come up with ideas that don't require additional code execution?

neitsa

Quote from: Bert Zangle on June 19, 2018, 09:45:59 AM
On the other hand, I saw that the modding tutorial mention C# code.

Yep, RimWorld is using Unity3D which itself runs on Mono, so everything is C#.

Quote
Maybe related: I also read about Harmony, which by my (currently very limited) understanding allows runtime execution of code by a mod. So maybe it is possible after all?

Harmony allows you to patch the main game code and alter how it's working. There are mainly three options with Harmony:

* Hooks a function before it is called (Prefix)
* Hooks a function after it is called (PostFix)
* Change the code of a function (Transpiling), either entirely or just some bits here and there.

You can also change fields, getters and setters values. See Harmony Wiki for the whole picture.

Quote
What are the limitations of executing external code called from within a mod?

You technically can, although in your case, you would either need a python interpreter available locally or do some network calls to a remote API.

Quote
To be more concrete: Ideally, I would like my mod to call some (cheap) TensorFlow function, then use the result in-game. Alternatively, the mod could execute some Python script using numpy methods. But I strongly suspect that's not possible, right?

If I understand correctly, you would need a Python interpreter already installed or embed a portable python install with your mod (or have a remote server to call your own API, but I think it's frowned upon). That sounds like a tough prerequisite.

Although it is possible to have some forms of threading in Unity (so your calls to python or a remote servers are not blocking the main Unity thread), it's not very easy to deal with especially as, as modders, we don't have have full control over everything. It's still possible, but not easy.

Have you considered using TensorFlowSharp for example?

Quote
Alright, that was very longwinded... Here's the tl;dr:

I want to prototype my idea for a (dialogue) mod. Minimally, I need to execute code which runs a forward pass of a neural network, ideally using an efficient ML/LA library implementation.

My question: Can this be done in the modding environment of this game, or do I have to come up with ideas that don't require additional code execution?

From my point of view, you should try to stick doing everything in C#, in you mod, without resorting in calling anything outside the game. Note that you can embed other C# libraries with you mod, so you're not restricted to your own code.

RawCode

you can bind native libs with c#
you can ship native libs with your mods
you do not need any tools for this, this is supported from box as dllimport

Harmony is wrapper over !single! operation,
https://github.com/pardeike/Harmony/blob/master/Harmony/ILCopying/Memory.cs#L71

sadly author of harmony left no reference to original discussion of technique on this forum and completely forgot about true original author of technique
https://github.com/sschoener/cities-skylines-detour

and answer to your question - there are no limits, you can load native image and execute it, just make sure that you provide images for each platform or your mod will fail for some users.