hao
A library for reading, editing and writing .NET assemblies in Rust.
- Loads every metadata table into a graph of shared
Entryhandles. Types own their fields, methods, properties, events, nested types and generic parameters. - Decodes signatures and CIL method bodies. Branch targets are instruction indices and operands are resolved to their entries, so edits don't need manual offset fixups.
- Writes modules back out by rebuilding the metadata from scratch, like dnlib's
ModuleWriter. ReadyToRun images are converted back to IL only images. - Writes C#-like declarations (optionally with IL listings) through
CSharpWriter. - Supports compressed (
#~) and uncompressed (#-) metadata.
Loading
use Module;
let module = from_path?;
for ty in module.all_types
Module::from_path also resolves referenced assemblies from the module's directory and the
installed .NET shared framework. Use Module::from_path_no_resolve to skip that.
A module can also be loaded from bytes with Module::from_bytes, or from a PE already parsed
with pewter via Module::from_pe (or Module::from_pe_image for a hao::io::PEImage), which
avoids re-parsing the PE.
Patching IL
use ;
let mut module = from_path_no_resolve?;
let write_line = module.import_method?;
let add = module.find_method.unwrap;
add.edit_body;
module.write_to_path?;
import_type/import_method/import_field reuse existing references or definitions and add new
ones when needed. insert and remove keep branch targets and exception ranges pointing at the
same instructions. See examples/patch_method.rs.
Creating assemblies
use ;
let mut module = new;
let answer = module.add_static_class?;
let max = module.import_corlib_method?;
// public static int Get(int a) => a > 0 ? Math.Max(a, 42) : 0;
let body = build;
let signature = new_static;
answer.add_method;
module.write_to_path?;
TargetRuntime::NetFramework references mscorlib instead, for Mono and Unity.
il::Emitter has a method per common instruction (ldarg, call, stloc, ...), structured
control flow (if_else, if_then, while_loop, try_catch) and call_native for calling a
native function pointer. To patch an existing method, MethodBody::prefix runs code at its start
and postfix before every return. Method::with_attribute with Module::corlib_attribute adds
attributes such as [UnmanagedCallersOnly].
Members of other loaded modules are referenced with import_foreign_method,
import_foreign_type and import_foreign_signature, which copy the needed type and assembly
references across.
Custom attributes
let value = attribute.decode?; // fixed and named arguments
let tag = value.fixed.as_str;
let weight = value.named.and_then;
let attribute = new?;
Enum arguments need the enum's type, so load the module with its dependencies when an attribute uses enums from another assembly.
Unity and Mono
PathAssemblyResolver::for_game(game_dir) resolves from a Unity game's *_Data/Managed folder
and its bundled Mono. Assemblies referencing mscorlib are written the same way, and the tests
run a patched .NET Framework build with Mono when it is installed.
C# output
use ;
println!;
println!;
println!; // IL listing
let with_il = module.to_csharp_with;
Thread safety
Entries use Rc<RefCell<T>> by default. Enable the sync feature to use Arc<RwLock<T>>
instead, which makes Module Send + Sync.
Reflection
The reflect feature derives facet::Facet across the modeled metadata graph, so a consumer
such as a scripting runtime can register hao's types by walking the shapes in hao::reflect::roots
rather than listing them. It implies sync, since facet can reflect Arc/RwLock but not
Rc/RefCell. bitflags sets are reflected opaquely; read their names through
[bitflags::Flags::FLAGS].
Tests
The tests build tests/fixtures/Sample with the dotnet SDK and run the rewritten assemblies with
it. Tests that need the sample are skipped when the SDK is not installed. The ignored tests
load and rewrite every assembly in the installed shared framework: