Skip to content

C# games

Games that run C# keep their classes, fields and methods by name in their own runtime. Unity games use Mono or IL2CPP. Others, and mod loaders, use .NET’s CoreCLR. Wraith reads all three the same way, so a plugin can find things by name instead of by signature, and names usually survive game updates.

The Workbench’s Classes panel shows what’s there. For what’s only Unity’s, like its engine calls and main thread, see Unity.

This page is about the game’s C#, from a plugin in any language. To write a plugin in C# itself, see Setup and Your plugin in the game’s .NET below.

The game starts its runtime after Wraith’s loaded plugins, and .NEThas started. managed.running() settles once it’s going, and managed.runtimeKind() says which it is, mono, il2cpp or coreclr.

import { Wraith } from "wraith";
await Wraith.managed().running();

Mark structs and their members as the game’s C#, and Wraith finds them by name. Name each the way your language likes, and give its C# name in [Managed].

[Managed("Game.Player")]
struct Player {
[0x10] f32 health;
[Managed] static Player* s_main;
[Managed("TakeDamage")] void takeDamage(i32 amount);
[Managed("get_Level")] i32 level();
[Managed("Greet")] static string greet(string name);
}
  • [Managed("Game.Player")] on a struct names its class. Without one, the struct’s name is the class’s.
  • [Managed] on a method or static finds it by its own name, or the C# one you give it. Properties are their get_ and set_ methods.
  • Outside a struct, give the class too, like [Managed("Game.Player:TakeDamage")].
  • Field offsets don’t move with names, so copy them from the Classes panel. They count the object’s header.

Then call them like any other function a schema declares, from any language. Wraith converts what you pass, calls the C# method on a thread that can, and converts what it gives back:

  • Numbers, bool and enums go as they are.
  • string and cstring become C# strings, and come back as UTF-8. Ones that come back are good till that thread’s next call.
  • Objects go both ways as usize or a pointer, their address. Hold ones you keep, see Keeping objects.
  • An instance method’s object is its this, like player.takeDamage(5).

If the method throws, or the game’s C# isn’t running yet, the log says so and the call gives zero.

Give each overload you want a name of its own, with the same C# name. Wraith picks the one whose parameters fit: by how many there are, then what kind, then the exact size, so i64 is C#’s long and i32 its int.

[Managed("MyGame.Game.Gui.ChatGui")]
struct ChatGui {
[Managed("Print")] void print(string message);
[Managed("Print")] void printTagged(string message, string tag);
}

If more than one still fits, like usize for both nint and nuint, finding it fails, saying so.

A [Managed] method’s address is Wraith’s way of calling it, not the method’s own code. To hook a method, use managed.findMethod, or Class:Method in address text. See Finding things as you go.

The managed area looks things up by name: find, fields, methods, fieldOffset, staticAddress and findMethod, which gives a method’s code to hook. Its last argument is how many parameters it takes, or -1 for any. classOf and readString read live objects.

import { Wraith } from "wraith";
const managed = Wraith.managed();
const update = managed.findMethod("Game.Player", "Update", -1);
const offset = managed.fieldOffset("Game.Player", "health");

Address text takes Class:Method too, like Game.Player:Update+0x3a, so hooks and tweaks can name methods. See Finding addresses.

invoke calls a method by name, for when there’s no schema. Give it the object for an instance method, or 0 for a static one. It picks the overload whose parameters fit what you pass.

import { Wraith } from "wraith";
const n = Wraith.managed().invoke("System.Int32", "Parse", 0, [{ kind: "text", text: "42" }]);
// n.integer is 42n

Each argument is a Managed.Value, a kind and its value: flag, integer, number, text, or object for everything else as an address. Enums are integers. A struct or a ref parameter is an object, the address of its bytes. Results come back the same way. If the method throws, invoke fails with Threw, saying what it threw.

A call runs right where it’s made when the runtime allows it, and on Wraith’s own thread when it doesn’t:

  • Mono and IL2CPP need a thread they know, like Unity’s main thread. Others go to Wraith’s thread, which is fine for plain C#.
  • CoreCLR takes any thread, but not a fiber, which is what a plugin’s tasks run on. From a task, calls go to Wraith’s thread.

Either way the caller waits, and gets the same result.

The garbage collector can’t see what your plugin keeps in its own memory, so a C# object kept past the call it came from can be collected under you. managed.hold(object) keeps it till managed.release(handle), from any thread, and whatever your plugin still holds is released when it goes.

import { Wraith } from "wraith";
const managed = Wraith.managed();
const handle = managed.hold(transform);
// later
managed.release(handle);

Mono’s and IL2CPP’s objects stay where they are. CoreCLR moves them as it collects, so there an object’s address is only good till the collector next runs. Hold what you keep, and get where it is now with managed.heldAt(handle) each time you use it. It works the same under Mono and IL2CPP, so code that does it runs anywhere.

const handle = managed.hold(player);
// later
const now = managed.heldAt(handle);

Mod loaders unload their plugins and load them again. What Wraith found in one that’s gone gives nothing, and it looks again the next time it’s used, so [Managed] methods keep working across a reload.

A C# plugin that .NET runs, rather than one compiled with NativeAOT, runs in the game’s own CoreCLR when the game has one, since a process only gets one. Each plugin gets an AssemblyLoadContext of its own, so its assemblies are apart from the game’s and from other plugins’, and it all goes when the plugin unloads.

Being in the same runtime, it can also use .NET’s own reflection on the game’s assemblies, as well as everything above.

C# 14’s extension blocks let a plugin add static members to types that aren’t its own, like a Find on one of the game’s classes or on a type from your schemas, which reads well for game code:

static unsafe class PlayerExtensions
{
extension(Player)
{
public static Player* Main => Player.InstanceOrFail();
}
}
// then
Player.Main->Health = 100;

If the game, or its mod loader, starts .NET after Wraith loads plugins, say "clrMode": "wait" in its wraith.json. It loads once that .NET is running, rather than starting the installed one. "provided" only loads it in games running .NET already. See the manifest.

Mono and IL2CPP games can’t run C# plugins in their own runtime. There, a C# plugin runs in the installed .NET 10 beside it, or compiled with NativeAOT, and reaches the game’s C# through this page’s API like any other plugin.

Made with ♥ by Asgard