pub struct Game { /* private fields */ }Expand description
A Unity Il2Cpp game: its metadata and the type table from its binary.
Implementations§
Source§impl Game
impl Game
Sourcepub fn open(dir: impl AsRef<Path>) -> Result<Self>
pub fn open(dir: impl AsRef<Path>) -> Result<Self>
Opens a game folder: the one holding GameAssembly.so (or
GameAssembly.dll) and <Game>_Data.
Fails with Error::NotFound if either file isn’t there.
Examples found in repository?
More examples
12fn main() -> hao_il2cpp::Result<()> {
13 let mut args = std::env::args().skip(1);
14 let dir = args
15 .next()
16 .expect("usage: game_info <game folder> [type name] [method name]");
17 let game = Game::open(dir)?;
18
19 let Some(type_name) = args.next() else {
20 println!(
21 "{} types in {} assemblies:",
22 game.types().count(),
23 game.image_names().count()
24 );
25 for name in game.image_names() {
26 println!(" {}", name);
27 }
28 return Ok(());
29 };
30
31 let Some(ty) = game.find_type(&type_name) else {
32 println!("no type named {}; similar names:", type_name);
33 for t in game
34 .types()
35 .filter(|t| {
36 t.name()
37 .contains(type_name.rsplit('.').next().unwrap_or(&type_name))
38 })
39 .take(10)
40 {
41 println!(" {}", t.full_name());
42 }
43 return Ok(());
44 };
45
46 // With a method name, break every matching overload into its parts, to show
47 // that names, return types and parameters are each available separately.
48 if let Some(method_name) = args.next() {
49 let mut found = false;
50 for method in ty.methods().filter(|m| m.name() == method_name) {
51 found = true;
52 println!("{}.{}", ty.full_name(), method.name());
53 println!(" static: {}", method.is_static());
54 println!(" returns: {}", method.return_type_name());
55 let parameters: Vec<_> = method.parameters().collect();
56 if parameters.is_empty() {
57 println!(" params: (none)");
58 } else {
59 println!(" params:");
60 for (i, parameter) in parameters.iter().enumerate() {
61 println!(" {}: {} {}", i, parameter.type_name(), parameter.name());
62 }
63 }
64 println!();
65 }
66 if !found {
67 println!("no method named {} on {}", method_name, ty.full_name());
68 }
69 return Ok(());
70 }
71
72 print!("// {}\nclass {}", ty.image_name(), ty.full_name());
73 if let Some(base) = ty.base_type_name() {
74 print!(" : {}", base);
75 }
76 println!("\n{{");
77 for field in ty.fields() {
78 println!(" {};", field);
79 }
80 for method in ty.methods() {
81 println!(" {};", method);
82 }
83 for nested in ty.nested_types() {
84 println!(" class {}", nested.name());
85 }
86 println!("}}");
87 Ok(())
88}Sourcepub fn from_files(
metadata: impl AsRef<Path>,
binary: impl AsRef<Path>,
) -> Result<Self>
pub fn from_files( metadata: impl AsRef<Path>, binary: impl AsRef<Path>, ) -> Result<Self>
Opens a global-metadata.dat and its GameAssembly binary.
Sourcepub fn from_bytes(metadata: Vec<u8>, binary_bytes: &[u8]) -> Result<Self>
pub fn from_bytes(metadata: Vec<u8>, binary_bytes: &[u8]) -> Result<Self>
Reads a game from the contents of its global-metadata.dat and GameAssembly,
e.g. files uploaded to a web page.
Sourcepub fn new(metadata: Metadata, binary: Binary) -> Self
pub fn new(metadata: Metadata, binary: Binary) -> Self
Builds a game from metadata and a binary you’ve already read.
Without the binary’s bytes, bytes_at returns None;
use new_with_image to keep them.
Sourcepub fn metadata(&self) -> &Metadata
pub fn metadata(&self) -> &Metadata
The parsed global-metadata.dat, for what the views don’t cover.
Sourcepub fn types(&self) -> impl Iterator<Item = TypeView<'_>>
pub fn types(&self) -> impl Iterator<Item = TypeView<'_>>
Every type definition in the game.
Examples found in repository?
12fn main() -> hao_il2cpp::Result<()> {
13 let mut args = std::env::args().skip(1);
14 let dir = args
15 .next()
16 .expect("usage: game_info <game folder> [type name] [method name]");
17 let game = Game::open(dir)?;
18
19 let Some(type_name) = args.next() else {
20 println!(
21 "{} types in {} assemblies:",
22 game.types().count(),
23 game.image_names().count()
24 );
25 for name in game.image_names() {
26 println!(" {}", name);
27 }
28 return Ok(());
29 };
30
31 let Some(ty) = game.find_type(&type_name) else {
32 println!("no type named {}; similar names:", type_name);
33 for t in game
34 .types()
35 .filter(|t| {
36 t.name()
37 .contains(type_name.rsplit('.').next().unwrap_or(&type_name))
38 })
39 .take(10)
40 {
41 println!(" {}", t.full_name());
42 }
43 return Ok(());
44 };
45
46 // With a method name, break every matching overload into its parts, to show
47 // that names, return types and parameters are each available separately.
48 if let Some(method_name) = args.next() {
49 let mut found = false;
50 for method in ty.methods().filter(|m| m.name() == method_name) {
51 found = true;
52 println!("{}.{}", ty.full_name(), method.name());
53 println!(" static: {}", method.is_static());
54 println!(" returns: {}", method.return_type_name());
55 let parameters: Vec<_> = method.parameters().collect();
56 if parameters.is_empty() {
57 println!(" params: (none)");
58 } else {
59 println!(" params:");
60 for (i, parameter) in parameters.iter().enumerate() {
61 println!(" {}: {} {}", i, parameter.type_name(), parameter.name());
62 }
63 }
64 println!();
65 }
66 if !found {
67 println!("no method named {} on {}", method_name, ty.full_name());
68 }
69 return Ok(());
70 }
71
72 print!("// {}\nclass {}", ty.image_name(), ty.full_name());
73 if let Some(base) = ty.base_type_name() {
74 print!(" : {}", base);
75 }
76 println!("\n{{");
77 for field in ty.fields() {
78 println!(" {};", field);
79 }
80 for method in ty.methods() {
81 println!(" {};", method);
82 }
83 for nested in ty.nested_types() {
84 println!(" class {}", nested.name());
85 }
86 println!("}}");
87 Ok(())
88}Sourcepub fn find_type(&self, full_name: &str) -> Option<TypeView<'_>>
pub fn find_type(&self, full_name: &str) -> Option<TypeView<'_>>
Finds a type by its full name (Namespace.Name, Outer/Nested, List1`).
Examples found in repository?
12fn main() -> hao_il2cpp::Result<()> {
13 let mut args = std::env::args().skip(1);
14 let dir = args
15 .next()
16 .expect("usage: game_info <game folder> [type name] [method name]");
17 let game = Game::open(dir)?;
18
19 let Some(type_name) = args.next() else {
20 println!(
21 "{} types in {} assemblies:",
22 game.types().count(),
23 game.image_names().count()
24 );
25 for name in game.image_names() {
26 println!(" {}", name);
27 }
28 return Ok(());
29 };
30
31 let Some(ty) = game.find_type(&type_name) else {
32 println!("no type named {}; similar names:", type_name);
33 for t in game
34 .types()
35 .filter(|t| {
36 t.name()
37 .contains(type_name.rsplit('.').next().unwrap_or(&type_name))
38 })
39 .take(10)
40 {
41 println!(" {}", t.full_name());
42 }
43 return Ok(());
44 };
45
46 // With a method name, break every matching overload into its parts, to show
47 // that names, return types and parameters are each available separately.
48 if let Some(method_name) = args.next() {
49 let mut found = false;
50 for method in ty.methods().filter(|m| m.name() == method_name) {
51 found = true;
52 println!("{}.{}", ty.full_name(), method.name());
53 println!(" static: {}", method.is_static());
54 println!(" returns: {}", method.return_type_name());
55 let parameters: Vec<_> = method.parameters().collect();
56 if parameters.is_empty() {
57 println!(" params: (none)");
58 } else {
59 println!(" params:");
60 for (i, parameter) in parameters.iter().enumerate() {
61 println!(" {}: {} {}", i, parameter.type_name(), parameter.name());
62 }
63 }
64 println!();
65 }
66 if !found {
67 println!("no method named {} on {}", method_name, ty.full_name());
68 }
69 return Ok(());
70 }
71
72 print!("// {}\nclass {}", ty.image_name(), ty.full_name());
73 if let Some(base) = ty.base_type_name() {
74 print!(" : {}", base);
75 }
76 println!("\n{{");
77 for field in ty.fields() {
78 println!(" {};", field);
79 }
80 for method in ty.methods() {
81 println!(" {};", method);
82 }
83 for nested in ty.nested_types() {
84 println!(" class {}", nested.name());
85 }
86 println!("}}");
87 Ok(())
88}Sourcepub fn image_names(&self) -> impl Iterator<Item = &str>
pub fn image_names(&self) -> impl Iterator<Item = &str>
The assembly names in the game, e.g. Assembly-CSharp.dll.
Examples found in repository?
12fn main() -> hao_il2cpp::Result<()> {
13 let mut args = std::env::args().skip(1);
14 let dir = args
15 .next()
16 .expect("usage: game_info <game folder> [type name] [method name]");
17 let game = Game::open(dir)?;
18
19 let Some(type_name) = args.next() else {
20 println!(
21 "{} types in {} assemblies:",
22 game.types().count(),
23 game.image_names().count()
24 );
25 for name in game.image_names() {
26 println!(" {}", name);
27 }
28 return Ok(());
29 };
30
31 let Some(ty) = game.find_type(&type_name) else {
32 println!("no type named {}; similar names:", type_name);
33 for t in game
34 .types()
35 .filter(|t| {
36 t.name()
37 .contains(type_name.rsplit('.').next().unwrap_or(&type_name))
38 })
39 .take(10)
40 {
41 println!(" {}", t.full_name());
42 }
43 return Ok(());
44 };
45
46 // With a method name, break every matching overload into its parts, to show
47 // that names, return types and parameters are each available separately.
48 if let Some(method_name) = args.next() {
49 let mut found = false;
50 for method in ty.methods().filter(|m| m.name() == method_name) {
51 found = true;
52 println!("{}.{}", ty.full_name(), method.name());
53 println!(" static: {}", method.is_static());
54 println!(" returns: {}", method.return_type_name());
55 let parameters: Vec<_> = method.parameters().collect();
56 if parameters.is_empty() {
57 println!(" params: (none)");
58 } else {
59 println!(" params:");
60 for (i, parameter) in parameters.iter().enumerate() {
61 println!(" {}: {} {}", i, parameter.type_name(), parameter.name());
62 }
63 }
64 println!();
65 }
66 if !found {
67 println!("no method named {} on {}", method_name, ty.full_name());
68 }
69 return Ok(());
70 }
71
72 print!("// {}\nclass {}", ty.image_name(), ty.full_name());
73 if let Some(base) = ty.base_type_name() {
74 print!(" : {}", base);
75 }
76 println!("\n{{");
77 for field in ty.fields() {
78 println!(" {};", field);
79 }
80 for method in ty.methods() {
81 println!(" {};", method);
82 }
83 for nested in ty.nested_types() {
84 println!(" class {}", nested.name());
85 }
86 println!("}}");
87 Ok(())
88}Sourcepub fn generate(&self) -> Result<Vec<GeneratedAssembly>>
pub fn generate(&self) -> Result<Vec<GeneratedAssembly>>
Builds a declaration assembly for every image.
Sourcepub fn write_assemblies(&self, dir: impl AsRef<Path>) -> Result<Vec<PathBuf>>
pub fn write_assemblies(&self, dir: impl AsRef<Path>) -> Result<Vec<PathBuf>>
Generates the declaration assemblies and writes them to dir, returning their paths.
Sourcepub fn bytes_at(&self, vaddr: u64, len: usize) -> Option<Vec<u8>>
pub fn bytes_at(&self, vaddr: u64, len: usize) -> Option<Vec<u8>>
Reads raw bytes from the game binary at a virtual address, or None if
it’s unmapped or the game was built without the binary’s bytes.
Sourcepub fn method_pointer(&self, method: &MethodDefinition) -> Option<u64>
pub fn method_pointer(&self, method: &MethodDefinition) -> Option<u64>
Looks up the native code address of a method definition, or None if
it has no code (abstract, generic or stripped) or CodeRegistration
wasn’t found.