hao-runtime
Host the .NET runtime in a Rust process and call into managed code.
| feature | runtime | needs |
|---|---|---|
coreclr (default) |
.NET 8+, loading libcoreclr directly |
a .NET install (DOTNET_ROOT or the standard locations) |
mono |
the Mono embedding API, as used by Unity | libmonosgen-2.0 (system Mono, or a game's own Mono) |
use Runtime;
let runtime = coreclr?;
runtime.load_assembly?;
// A static [UnmanagedCallersOnly] method.
let add = runtime.function_pointer?;
let add: extern "C" fn = unsafe ;
assert_eq!;
With Mono, any static method can be called, and the pointer it returns takes an extra trailing
*mut *mut c_void argument that receives an exception:
let runtime = mono?;
method_pointerreturns a native-callable pointer to a static method whether or not it is[UnmanagedCallersOnly], by JIT-compiling it (via reflection on CoreCLR,mono_compile_methodon Mono). IL you patch before loading takes effect when the method is compiled. Unlikefunction_pointerit has no trailing exception argument, so a managed exception crosses it unhandled.load_assembly_bytesloads an assembly from memory, andload_module(thehaofeature, on withcoreclr) loads ahaoModuleyou built or edited without writing a file.- CoreCLR is configured in memory: nothing is written to disk, and no
.runtimeconfig.jsonis needed. Runtime::mono_attachattaches to a Mono runtime already running in the process, for a mod loaded into a Unity Mono game (pointMonoOptions.libraryat the game's own Mono). Loading assemblies and calling methods then work as usual. Attaching to a foreign CoreCLR is not supported.- Only one runtime of each kind can run per process, so
Runtime::coreclr()andRuntime::mono()return the existing instance when called again. - On Mono, threads have to be attached before running managed code.
attach_threaddoes this, and generated bindings call it for you.
Calling methods by hand is fiddly; hao-runtime-macros generates safe
bindings instead. The bindings module holds the support code those bindings use.
Documentation
The API docs are on docs.rs. Start with
Runtime, then:
attach |
attach to a game's already-running CoreCLR from a native mod |
rewrite |
rewrite an assembly's IL with hao before the runtime JITs it |
jit |
call JIT'd methods and resolve precode |
bindings |
support for bind! from hao-runtime-macros |
profiler |
a minimal CoreCLR profiler |
console |
a console for an injected mod's logs |
| Feature | Default | |
|---|---|---|
coreclr |
yes | the CoreCLR backend |
mono |
the Mono backend | |
hao |
(with coreclr) |
Runtime::load_module for modules built or edited with hao |
reflect |
facet reflection roots, as in hao |
Examples
generate_and_call: builds a class with hao (arithmetic, a call intoSystem.Math, and a loop built with the IL emitter), loads it and calls it.patch_in_memory: reads an assembly, patches two methods' IL, and loads the patched copy next to the original to compare them.hook_managed: hooks managed methods with Rust functions, like Harmony prefixes and postfixes, by rewriting their IL to call a native function pointer.mono: the Mono backend, including a call that throws.call_any_method: calls plain static methods (no[UnmanagedCallersOnly]) withmethod_pointer, including one whose IL was patched before loading.mono_attach: attaches to a Mono runtime already running in the process and calls into it.