[tech demo] Tired from rerolling your base colonists, this mod just for you!

Started by RawCode, August 10, 2014, 03:44:45 AM

Previous topic - Next topic

RawCode

This mod uses unsafe stuff, it may not harm your computer or game, but likely to corrupt savegames, use backups (actually it work only on new games and wont corrupt anything, but still use backups).

What this mod does and how:

1) Using cctor feature of c# it injects arbitrary code into game at preinit stage, before game actually is started:

        static Loader()
        {
            ///Log.Error("<CINIT>(..cctor) Section invocation ((static)Type Constructor)");
            Thread branchA = new Thread(new ThreadStart(Loader.watchInternalState));
            branchA.IsBackground = true;
            main = Thread.CurrentThread;
            branchA.Start();
            //Log.Warning("THREAD STARTED");
        }


2) Separate thread used to monitor state of internal objects (and cause exceptions at random due single threaded nature of rimworld and lack of syncblocks):


                    //Log.Warning("CHECK INITTERD");
                    if (fz.TopLayer is Page_MainMenu)
                    {
                        //Log.Warning("CHECK PASSED");
                        fz.Add((Layer)new MainMenuImpl());
                    }


3) In our case it monitor what screen currently is shown, in order to keep resources check performed with delay:


                Thread.Sleep(1024);
                if (!Application.isPlaying)
                    break;
                Thread.Sleep(1024);


4) MainMenuImpl is reimplementation* of vanilla class that looks just like vanilla class, but act a bit differently:


due to 2 seconds delay you still can hit "normal" button, this by design.

5) All other windows also reimplemented and linked. (not all actually and linked one way only).

6) Final window - character creation have custom payload that alter colonist generation:


Enjoy.


[attachment deleted by admin: too old]

Argain

Sooo, this is basically starting a modified instance of RimWorld on a separate thread?

RawCode

There are two possible ways to trigger "events":

Mod native classes and add hooks to them.
Monitor application state.

I have valid method of bytecode injection, but this method is harmful for end users and may result in completely unexpected bugs, since it is platform\version\bitwide specific and may work fine on w7 but cause random bugs on w8, this fine for private hacks, but unacceptable for public.

Second thread used to monitor application state, it will check current top layer of application and replace it if that layer have predefined type.
In current sample i replace only very first GUI layer.
This is tech demo, real mod will come a bit later and will feature refined way of injection.