xur_

> module

std::re::dotnet::runtime

host a .NET runtime

use std::re::dotnet::runtime; feature re-dotnet-runtime native only

std::re::dotnet::runtime starts and drives a .NET runtime inside the Xur process. Assemblies are loaded from disk, from bytes, or straight from a dotnet::Module built in the script, and static methods are called through native function pointers.

Pointers and managed object addresses are plain integers. A Runtime runs on CoreCLR, or on Mono when a script is handed one for a Mono game; the methods below say where the backends differ.

Backed by hao-runtime.

> functions

# coreclr() -> Result<Runtime, str>

Start CoreCLR with the newest installed .NET framework, or return the one already running. Only one CoreCLR can run in a process.

> types

┌ enum Backend
Backend::CoreClr
Backend::Mono
┌ struct BoundAssembly

A .NET assembly bound with `hao_runtime_macros::bind!`.

assembly_namestr
assembly_pathstr
thunks_namestr
coreclr_thunksbytes
mono_thunksbytes
function_countusize
coreclrlist<usize>
monolist<usize>
init()
┌ struct CoreClrOptions

Where to find .NET and which framework version to run.

dotnet_rootOption<PathBuf>Defaults to `DOTNET_ROOT` or the standard install locations.
framework_versionOption<str>A `Microsoft.NETCore.App` version such as `9.0.21`. Defaults to the newest installed.
┌ struct Guid

A COM interface id / class id.

data1u32
data2u16
data3u16
data4bytes
┌ type Runtime
# runtime.attach_thread()

Let the calling thread run managed code. Needed on Mono for threads it didn't start; does nothing on CoreCLR.

# runtime.backend() -> str

Which runtime this is: "CoreClr" or "Mono".

# runtime.dotnet_major() -> Option<i64>

The major .NET version the runtime was started with, such as 9; None on Mono.

# runtime.enumerate(obj: i64) -> Result<list<i64>, str>

Walk the managed IEnumerable at address obj and return the addresses of its elements. Handles lazy iterators and LINQ chains. On CoreCLR call warm_enumerate() first, and call this on the game's managed thread. On Mono the addresses stay valid only until the next garbage collection.

# runtime.find_method_from_loaded(assembly: str, type_name: str, method: str, param_count: i32) -> Result<i64, str>

Like method_pointer_from_loaded(), but picks the overload with param_count parameters.

# runtime.function_pointer(assembly: str, type_name: str, method: str) -> Result<i64, str>

A native pointer to static method method of type_name in assembly. On CoreCLR the method must be [UnmanagedCallersOnly]; on Mono the pointer takes an extra, trailing exception argument.

# runtime.invoke(ptr: i64, args: list<i64>) -> Result<i64, str>

Call the method pointer ptr with integer (or pointer) arguments and return its integer result. The calling thread is registered with the runtime first.

# runtime.invoke_float(ptr: i64, args: list<i64>) -> Result<f64, str>

Like invoke(), for a method that returns a floating-point number.

# runtime.load_assembly(path: str) -> Result<(), str>

Load the assembly at path and make it available to later loads by name. On CoreCLR, dependencies next to it are found too.

# runtime.load_assembly_bytes(name: str, bytes: bytes) -> Result<(), str>

Load an assembly held in memory; name is its assembly name.

# runtime.load_module(module: Module) -> Result<(), str>

Load a dotnet::Module built or edited in the script, without writing it to disk.

# runtime.method_names(assembly: str, type_name: str) -> Result<list<str>, str>

The names of a type's methods in a loaded assembly. On CoreCLR the list includes inherited methods such as ToString; on Mono only the type's own.

# runtime.method_pointer(assembly: str, type_name: str, method: str) -> Result<i64, str>

A native pointer to any static method, which is JIT-compiled first. It uses the managed calling convention, which matches the C one for primitive and pointer arguments, and takes no exception argument, so a managed exception thrown through it is unhandled.

# runtime.method_pointer_from_loaded(assembly: str, type_name: str, method: str) -> Result<i64, str>

Like method_pointer(), but finds the type in whichever load context actually loaded the assembly, such as a game's own. On Mono it is the same as method_pointer().

# runtime.resume_gc() -> Result<(), str>

End a no-GC region started by suppress_gc(). Does nothing if there isn't one, or on Mono.

# runtime.suppress_gc(budget: i64) -> Result<bool, str>

Ask for no garbage collection during the next budget bytes of managed allocation, for example while scanning pointers. Returns whether the runtime agreed; always false on Mono.

# runtime.type_names(assembly: str) -> Result<list<str>, str>

The full names of every type in a loaded assembly.

# runtime.warm_enumerate() -> Result<(), str>

Prepare enumerate() so it does no setup later. On CoreCLR this must be called off the game's managed thread, for example at start-up; on Mono it is only an optimisation.

> examples

use std::io;
use std::re::dotnet::runtime;

fn main() -> Result<(), str> {
    let rt = runtime::coreclr()?;
    rt.load_assembly("MyLib.dll")?;
    let add = rt.method_pointer("MyLib", "MyLib.Math", "Add")?;
    let sum = rt.invoke(add, [2, 3])?;
    io::println("2 + 3 = {}", sum);
    Ok(())
}