FieldInjection

v1.1.0·Updated 2022-06-03

A utility for other mods to inject monobehaviour fields

About this mod Author’s original text

This mod is a utility for other mods to utilise MonoBehaviour field serialisation injection.

Instructions for players

  • To install the mod, just extract FieldInjector.dll into your Mods folder.

Instructions for programmers

If you want to utilise custom MonoBehaviours with properly serialised fields from asset bundles, reference this mod as a dependency.

Writing a custom MonoBehaviour

This is fairly simple, and there is very little difference between a normal unity MonoBehaviour and this. There is only a small amount of boilerplate code to add:

using UnityEngine;

class MyScript: MonoBehaviour

{

// all of your normal MonoBehaviour code can go here

if!UNITY_EDITOR

public TestMB(IntPtr ptr): base(ptr) { }

endif

}

the #if instruction means that this code should compile in both the Unity Editor and in a MelonLoader mod, so that you can keep your code unified and test in the editor with ease.

Injecting a custom MonoBehaviour

This is what this mod is used for, and it is very simple:

FieldInjector.SerialisationHandler.Inject();

in the OnApplicationStart method is all that is needed. Do not register the class in Il2Cpp with MelonLoader or UnhollowerBaseLib - this mod does that itself and it won't be able to inject the class with fields if it's already injected without fields. Makers of frameworks that load code should consider registering fields for loaded behaviours automatically.

Using them in Custom Items / Maps / etc

No extra work should be required to use the injected behaviours in asset bundles. Simply attach the component and it will load in the game.

Changelog

  • v1.0: Release
  • v1.1: Added support for enum fields, cleaned code/memory allocation and added FieldInjector.SerializeField attribute to force serialisation of a non-public field.

Install & use Install: follow the bundled instructions

  1. Unzip the archive from playbuff and read the bundled readme / instructions first.
  2. Most mods rely on the game's mod loader — the usual route is a community mod manager (such as r2modman): pick this game, install, and it handles the loader and dependencies.
  3. For a manual install, put the mod files into the matching Mod / plugins folder inside the game directory.
  4. Launch the game to confirm it loaded; mods with listed dependencies need those installed as well.

Install locations vary a lot between mods — the bundled readme is the most reliable guide.

File & checksum

Filename
FieldInjection-1.1.0.zip
File size
20.9 KiB