> module
std::stub::coreclr
interact with a live CoreCLR (.NET) game
std::stub::coreclr provides runtime access to a CoreCLR (.NET) game's methods and objects from inside the injected process. It is the primary module for writing mods and trainers for Godot/.NET and other CoreCLR-based games.
Methods are resolved by assembly, type, and name, then invoked as raw native calls. Objects are read and written at known field offsets. Methods can be hooked with closures or captured-this references.
The runtime is not created directly; it is attached by an engine-specific module such as std::stub::godot. Once attached, all types in this module operate on the live process.
> functions
enumerate(obj: ManagedObject) -> Result<list<ManagedObject>, Error>Materialize any managed System.Collections.IEnumerable into a list of its elements, using the CLR's own foreach (a helper injected into the running runtime). This reads collections whose layout pointer math cannot — compiler-generated lazy iterators, LINQ chains and dictionary views — not just plain arrays and List<T>. Must be called on the cooperative (engine) thread, and warm_enumerate() must have run once off that thread first. Returns an empty list for a null argument.
from_address(addr: i64) -> ManagedObjectWrap a raw memory address as a ManagedObject. No validation is performed; the address is trusted.
Inject a .NET assembly, from its raw image bytes, into the running runtime. The bytes may come from a file (std::fs::read), an embedded resource, or a module built elsewhere. name is the assembly name (used for identity on Mono; cosmetic on CoreCLR).
null() -> ManagedObjectA null managed object reference.
> types
A CapturedRef is an atomic slot that holds the most recently captured this pointer from a capture_this() hook.
captured_ref.get() -> ManagedObjectThe current captured reference; null if no call has been intercepted yet.
A recoverable CoreCLR error. Match it as `Err(coreclr::Error::NotInitialized)`.
| Error::NotInitialized | The CoreCLR runtime was not initialized. |
| Error::NullPointer | A null MethodPtr, or a JIT resolution that returned null. |
| Error::Unsupported | An operation is only supported on x86_64. |
| Error::Clr(str) | An underlying CoreCLR operation failed, with its message. |
| Error::Memory(str) | A memory access failed (unreadable address), with its message. |
A ManagedObject is a reference to a live .NET object in the game's managed heap. Field access uses explicit offsets derived from static analysis of the game's assemblies.
CoreCLR object layout: at offset +0 is the MethodTable pointer, followed by instance fields in declaration order. Reference fields are 8-byte pointers; value fields are packed at their natural size. Use std::re::dotnet to discover field order from the game's DLLs.
System.String layout: +0 MethodTable, +8 length (int32), +12 UTF-16 characters.
List<T> layout: +0 MethodTable, +8 _items (T[] array reference), +16 _size (int32). The backing array has: +0 MethodTable, +8 length (nint), +16 elements.
managed_object.deref(offset: i64) -> ManagedObjectRead a pointer at the given byte offset and return it as a ManagedObject — the way to follow reference fields. The result may be null (check is_null()); only unreadable memory raises, so calls chain: obj.deref(8).deref(16).
managed_object.is_null() -> boolWhether this is a null reference.
managed_object.non_null() -> Option<ManagedObject>Some(self) when non-null, None otherwise. Prefer this over a null sentinel so callers get a compiler-checked Option instead of an easy-to-forget is_null() check.
Read count raw bytes starting at the given offset.
Read a 32-bit integer at the given byte offset (sign-extended).
Read a 64-bit integer at the given byte offset.
Read this object as a System.String. The object must point to a valid .NET string (+8 = length, +12 = UTF-16 chars).
Read a single unsigned byte at the given byte offset.
A MethodPtr is a native code pointer to a managed method. Calling and hooking use the System V AMD64 ABI and are only supported on x86_64.
Call the method and return the raw integer result. Arguments are marshalled to integer registers (see ARGUMENT MARSHALLING).
Call the method and return the floating-point result from XMM0.
method_ptr.call_ptr(args?: i64 | bool | f64 | ManagedObject | Decimal | ()) -> ManagedObjectCall the method and wrap the result as a ManagedObject. Use this when the method returns a managed reference.
Call the method and read the returned reference as a System.String. Raises if it can't be read.
method_ptr.capture_this() -> Result<CapturedRef, Error>Install a lightweight hook that saves the first argument (the this pointer) of every call into an atomic slot, then runs the original. Use this to capture object references from methods you cannot call yourself, such as singleton accessors the game calls.
Hook the method. The callback's declared parameters are marshalled from the call's arguments by their Xur types: an f32 or f64 from the next SSE register, a coreclr::ManagedObject from the next integer register, a decimal::Decimal from the next two integer registers, and anything else as an i64 from the next integer register. So a hook on a method taking a creature and a decimal amount can be written |this: coreclr::ManagedObject, amount: decimal::Decimal|. The callback returns None to run the original, or Some(value) to skip it and return value (an integer) instead. A callback error is logged and the original runs.
method_ptr.hook_args(callback: fn(..) -> (..))Like hook(), but the callback returns the (possibly modified) arguments — a tuple with one entry per parameter, or a single value for one parameter — which are written back before the original runs. Arguments returned unchanged are left exactly as they were. Use this to rewrite a call's arguments (e.g. zero a damage amount) while letting the method run and return a valid result.
method_ptr.looks_jitted() -> boolWhether the code at this address looks like a real JIT-compiled body rather than a jump/call stub (its first byte isn't a jmp, call, or int3). A not-yet-compiled method points at a stub that may be shared with unrelated methods, so gate hooks on this. False for a null pointer.
Redirect the JIT precode stub to point at a different method. This is a low-level operation for method replacement; prefer hook() for most uses.
A hex dump of count bytes at the method address. Useful for inspecting JIT precode stubs.
Follow the JIT precode stub to the code it currently targets. None for a null pointer, if it doesn't resolve, or off x86_64. A method that isn't compiled yet still resolves (to a thunk); check looks_jitted().
The Runtime handle is obtained from the engine module (e.g. godot::init()).
runtime.find_method(assembly: str, type_name: str, method: str, param_count: i64) -> Result<MethodPtr, Error>Like method() but disambiguates overloads by parameter count, searching loaded assemblies only. Use this when a type has several methods with the same name.
Load a .NET assembly from a file path into the runtime.
Resolve a managed method by assembly name, fully qualified type name, and method name. Searches loaded assemblies first, then falls back to metadata resolution. Fails if the method is not found.
List all method names on a type in a loaded assembly.
> errors
Fallible functions and methods return coreclr::Error: NotInitialized (no runtime attached yet), NullPointer (a null MethodPtr), Unsupported (not x86_64), Clr(message) (a CoreCLR operation failed) or Memory(message) (an unreadable address).
> argument marshalling
call(), call_ptr(), call_float() and call_string() pass every argument in integer registers (System V AMD64): an i64 as is; a bool as 0 or 1; an f64 cast to an integer (truncated); a ManagedObject as its address (checked readable first); a Decimal as the two 64-bit halves of a System.Decimal; and () as 0 (a null argument). Anything else raises.
Static methods take no implicit this argument. Instance methods take the object as the first argument:
// Static: ModelDb::get_AllRelics()
let relics = get_all_relics.call_ptr();
// Instance: player.get_Relics()
let list = get_relics.call_ptr(player);> memory safety
All pointer reads are validated before access using a /dev/null write probe. Stale or invalid references return an error rather than crashing the process. However, the GC can relocate objects between reads, so long-lived ManagedObject values should be refreshed each frame.
> examples
use std::stub::coreclr;
use std::stub::godot;
fn stub_main() {
let rt = godot::init(9).unwrap();
// Resolve methods
let get_instance = rt.method("sts2", "Some.Manager", "get_Instance").unwrap();
let get_name = rt.method("sts2", "Some.Entity", "get_Name").unwrap();
// Static call (no this arg)
let mgr = get_instance.call_ptr();
// Instance call
let name = get_name.call_string(mgr);
}// List<T> layout: +8 = _items (T[]), +16 = _size (int32)
// Array layout: +8 = length (nint), +16 = elements (8 bytes each)
fn read_list(list: coreclr::ManagedObject) -> [coreclr::ManagedObject] {
let result = [];
if list.is_null() { return result; }
let count = match list.read_i32(16) { Ok(c) => c, Err(_) => 0 };
if count <= 0 { return result; }
let items = list.deref(8);
if items.is_null() { return result; }
let i = 0;
while i < count {
let obj = items.deref(16 + i * 8);
if !obj.is_null() { result.push(obj); }
i += 1;
}
result
}// Capture a singleton reference from the game's own calls
let get_state = rt.method("asm", "GameState", "get_Instance").unwrap();
let captured = get_state.capture_this().unwrap();
// Later, read the captured reference
let state = captured.get();
if !state.is_null() {
let hp = state.read_i32(24).unwrap();
}let damage = rt.method("asm", "Player", "TakeDamage").unwrap();
damage.hook(|this_addr| {
// Skip damage entirely
Some(0)
});// Off the engine thread (e.g. at init): warm the helper once.
coreclr::warm_enumerate().unwrap();
// On the engine thread: read a collection the CLR returns as an
// IEnumerable (here a lazy iterator pointer math cannot walk).
let all = rt.method("asm", "ModelDb", "get_AllCards").unwrap();
for card in coreclr::enumerate(all.call_ptr()).unwrap() {
// ... use each element ...
}let bytes = std::fs::read("./Helper.dll").unwrap();
coreclr::load_library("Helper", bytes).unwrap();
let hello = rt.method("Helper", "Helper.Entry", "Hello").unwrap();