1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! `hao` reads, edits, creates and writes .NET assemblies.
//!
//! The entry point is [`Module`]:
//!
//! ```no_run
//! use hao::Module;
//!
//! let module = Module::from_path("Example.dll").unwrap();
//!
//! for ty in module.all_types() {
//! let ty = ty.get();
//! println!("{}", ty.full_name());
//! for method in &ty.methods {
//! println!("\t{}", method.get().name);
//! }
//! }
//! ```
//!
//! Creating a module from scratch: a static class with one method, its body
//! built with the [`Emitter`](dotnet::il::Emitter):
//!
//! ```
//! use hao::prelude::{well_known::SystemType, *};
//!
//! let mut module = Module::new("Demo", ModuleKind::Dll, TargetRuntime::NetCore(9));
//! let object = module.corlib_type(SystemType::Object)?;
//! let math = module.add_type(TypeDef::new(
//! TypeAttributes::Public | TypeAttributes::Abstract | TypeAttributes::Sealed,
//! "Demo",
//! "MathUtil",
//! Some(object),
//! ));
//! let add = math.add_method(Method::new(
//! MethodFlags::Public | MethodFlags::Static,
//! "Add",
//! MethodSig::new_static(TypeSig::INT32, vec![TypeSig::INT32, TypeSig::INT32]),
//! ));
//! add.get_mut().body = Some(Emitter::build(|e| {
//! e.ldarg(0).ldarg(1).op(OpCode::Add).ret();
//! }));
//!
//! let bytes = module.write()?; // or module.write_to_path("Demo.dll")
//! assert!(Module::from_bytes(&bytes)?.find_method("Demo.MathUtil", "Add").is_some());
//! # Ok::<(), hao::error::HaoError>(())
//! ```
//!
//! - [`dotnet::entries`]: types, methods, fields and the other metadata entries, as shared
//! [`Entry`](dotnet::entries::Entry) handles, plus custom attribute values.
//! - [`dotnet::signature`]: type and method signatures.
//! - [`dotnet::il`]: method bodies, opcodes, and the [`Emitter`](dotnet::il::Emitter) for
//! building new ones.
//! - [`dotnet::module`]: creating modules ([`Module::new`]), importing references and resolving
//! assemblies.
//! - [`dotnet::csharp`]: C#-like output of declarations and IL.
//! - [`dotnet::md`]: the raw metadata tables and heaps.
//! - [`prelude`]: the common imports, as used above.
//! - [`error`]: [`HaoError`](error::HaoError), returned by everything that can fail.
//!
//! Modules are written with [`Module::write`], which rebuilds the metadata from the entries.
//!
//! Entries are backed by `Rc<RefCell<_>>`, or `Arc<RwLock<_>>` with the `sync` feature.
//!
//! Loading is strict: malformed metadata is an error rather than being skipped.
pub use ;