Skip to main content

Game

Struct Game 

Source
pub struct Game { /* private fields */ }
Expand description

A Unity Il2Cpp game: its metadata and the type table from its binary.

Implementations§

Source§

impl Game

Source

pub fn open(dir: impl AsRef<Path>) -> Result<Self>

Opens a game folder: the one holding GameAssembly.so and <Game>_Data.

Examples found in repository?
examples/generate_dlls.rs (line 31)
26fn main() -> hao_il2cpp::Result<()> {
27    let mut args = std::env::args().skip(1);
28    let (Some(game), Some(out)) = (args.next(), args.next()) else {
29        panic!("usage: generate_dlls <game folder> <output folder>");
30    };
31    let paths = Game::open(game)?.write_assemblies(&out)?;
32    println!("wrote {} assemblies to {}", paths.len(), out);
33    Ok(())
34}
More examples
Hide additional examples
examples/game_info.rs (line 17)
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}
Source

pub fn from_files( metadata: impl AsRef<Path>, binary: impl AsRef<Path>, ) -> Result<Self>

Opens a global-metadata.dat and its GameAssembly binary.

Source

pub fn from_bytes(metadata: Vec<u8>, binary: &[u8]) -> Result<Self>

Reads a game from the contents of its global-metadata.dat and GameAssembly, e.g. files uploaded to a web page.

Source

pub fn new(metadata: Metadata, binary: Binary) -> Self

Source

pub fn metadata(&self) -> &Metadata

Source

pub fn binary(&self) -> &Binary

Source

pub fn types(&self) -> impl Iterator<Item = TypeView<'_>>

Every type definition in the game.

Examples found in repository?
examples/game_info.rs (line 22)
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}
Source

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?
examples/game_info.rs (line 31)
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}
Source

pub fn image_names(&self) -> impl Iterator<Item = &str>

The assembly names in the game, e.g. Assembly-CSharp.dll.

Examples found in repository?
examples/game_info.rs (line 23)
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}
Source

pub fn generate(&self) -> Result<Vec<GeneratedAssembly>>

Builds a declaration assembly for every image.

Source

pub fn write_assemblies(&self, dir: impl AsRef<Path>) -> Result<Vec<PathBuf>>

Generates the declaration assemblies and writes them to dir, returning their paths.

Examples found in repository?
examples/generate_dlls.rs (line 31)
26fn main() -> hao_il2cpp::Result<()> {
27    let mut args = std::env::args().skip(1);
28    let (Some(game), Some(out)) = (args.next(), args.next()) else {
29        panic!("usage: generate_dlls <game folder> <output folder>");
30    };
31    let paths = Game::open(game)?.write_assemblies(&out)?;
32    println!("wrote {} assemblies to {}", paths.len(), out);
33    Ok(())
34}

Auto Trait Implementations§

§

impl Freeze for Game

§

impl RefUnwindSafe for Game

§

impl Send for Game

§

impl Sync for Game

§

impl Unpin for Game

§

impl UnsafeUnpin for Game

§

impl UnwindSafe for Game

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.