Skip to content

Commit

Permalink
Add path_join
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKnauth committed Sep 17, 2023
1 parent b78fe92 commit d129c5b
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/game_engine/unity/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::file_format::macho;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[cfg(feature = "alloc")]
use std::{path::Path, fs::File, io, io::Read};
use std::{fs::File, io, io::Read};

#[cfg(feature = "derive")]
pub use asr_derive::MonoClass as Class;
Expand Down Expand Up @@ -143,9 +143,9 @@ impl Module {
let offsets = Offsets::new(version, is_64_bit, BinaryFormat::MachO);

let process_path = process.get_path().ok()?;
let contents_path = Path::new(path_parent(path_parent(&process_path)?)?);
let mono_module_path = contents_path.join("Frameworks").join("libmonobdwgc-2.0.dylib");
let mono_module_bytes = file_read_all_bytes(mono_module_path).ok()?;
let contents_path = path_parent(path_parent(&process_path)?)?;
let mono_module_path = path_join(&path_join(contents_path, "Frameworks"), "libmonobdwgc-2.0.dylib");
let mono_module_bytes = file_read_all_bytes(&mono_module_path).ok()?;

let mono_assembly_foreach_offset: u32 = macho::get_function_offset(&mono_module_bytes, b"_mono_assembly_foreach")?;

Expand Down Expand Up @@ -832,9 +832,9 @@ fn detect_version_dylib(process: &Process) -> Option<Version> {
const UNITY_PLAYER_VERSION_LEN: usize = UNITY_PLAYER_VERSION.len();

let process_path = process.get_path().ok()?;
let contents_path = Path::new(path_parent(path_parent(&process_path)?)?);
let info_plist_path = contents_path.join("Info.plist");
let info_plist_bytes = file_read_all_bytes(info_plist_path).ok()?;
let contents_path = path_parent(path_parent(&process_path)?)?;
let info_plist_path = path_join(contents_path, "Info.plist");
let info_plist_bytes = file_read_all_bytes(&info_plist_path).ok()?;
// example: "Unity Player version 2020.2.2f1 "
let upv = memchr::memmem::find(&info_plist_bytes, UNITY_PLAYER_VERSION)?;
let version_string: ArrayCString<6> = macho::slice_read(&info_plist_bytes, upv + UNITY_PLAYER_VERSION_LEN).ok()?;
Expand Down Expand Up @@ -886,7 +886,12 @@ fn path_parent(path: &str) -> Option<&str> {
}

#[cfg(feature = "alloc")]
fn file_read_all_bytes<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
fn path_join(base: &str, sub: &str) -> String {
format!("{}/{}", base, sub)
}

#[cfg(feature = "alloc")]
fn file_read_all_bytes(path: &str) -> io::Result<Vec<u8>> {
let mut f = File::open(path)?;
let mut buffer: Vec<u8> = Vec::new();
f.read_to_end(&mut buffer)?;
Expand Down

0 comments on commit d129c5b

Please sign in to comment.