~quf/no-cutscene-kiseki

7348c73c6fdbabc87365f29fe5031f7bdf257860 — Lukas Himbert 10 months ago a8d63ac
simplify, clean up for japanese prep
1 files changed, 15 insertions(+), 24 deletions(-)

M src/patch.rs
M src/patch.rs => src/patch.rs +15 -24
@@ 16,11 16,13 @@ impl ExeVersion {
            ExeVersion::NisaV1_05Jp => "bin\\x64\\ed8_3_PC_JP.exe",
        }
    }
}

pub fn exe_hash_matches_1_05<T: AsRef<[u8]>>(data: T) -> bool {
    &blake2::Blake2b512::digest(data.as_ref())[..]
        == hex_literal::hex!("bbce84df472cdeefd60464715aadfd7e35ca6a9107da320ea137b9424fc3009a4ecf55090f7a439a1ec4969df02bd190e46dd102c1f9bfbfa18d0b8babc989ff")
    pub fn expected_blake2b_hash(&self) -> [u8; 64] {
        match self {
            ExeVersion::NisaV1_05En => hex_literal::hex!("bbce84df472cdeefd60464715aadfd7e35ca6a9107da320ea137b9424fc3009a4ecf55090f7a439a1ec4969df02bd190e46dd102c1f9bfbfa18d0b8babc989ff"),
            ExeVersion::NisaV1_05Jp => hex_literal::hex!("2e846b7f10bda3d3ae191d4391b35b879683f73628e9bcb8a93b10448e65ecf22a496e3fe90c06aa140bb75bdfc09965367d4209c6311272e67d9a208104da7d"),
        }
    }
}

enum Language {


@@ 51,29 53,17 @@ fn identify_language() -> anyhow::Result<Language> {
pub fn identify_version(base_path: &std::path::Path) -> anyhow::Result<ExeVersion> {
    let language = identify_language().context("couldn't identify game language")?;

    let (exe_path, known_hashes) = match language {
        Language::En | Language::Fr => (
            "bin\\x64\\ed8_3_PC.exe",
            &[(
                ExeVersion::NisaV1_05En,
                hex_literal::hex!("bbce84df472cdeefd60464715aadfd7e35ca6a9107da320ea137b9424fc3009a4ecf55090f7a439a1ec4969df02bd190e46dd102c1f9bfbfa18d0b8babc989ff"),
            )],
        ),
        Language::Jp => (
            "bin\\x64\\ed8_3_PC_JP.exe",
            &[(
                ExeVersion::NisaV1_05Jp,
                hex_literal::hex!("2e846b7f10bda3d3ae191d4391b35b879683f73628e9bcb8a93b10448e65ecf22a496e3fe90c06aa140bb75bdfc09965367d4209c6311272e67d9a208104da7d"),
            )],
        ),
    let (exe_path, known_versions) = match language {
        Language::En | Language::Fr => ("bin\\x64\\ed8_3_PC.exe", &[ExeVersion::NisaV1_05En]),
        Language::Jp => ("bin\\x64\\ed8_3_PC_JP.exe", &[ExeVersion::NisaV1_05Jp]),
    };

    let path = base_path.join(exe_path);
    let data = std::fs::read(&path).with_context(|| format!("couldn't read executable file {}", path.display()))?;
    let hash = blake2::Blake2b512::digest(&data);
    for (exe_version, expected_hash) in known_hashes.iter().copied() {
        if &hash[..] == expected_hash {
            return Ok(exe_version);
    for version in known_versions.into_iter().copied() {
        if &hash[..] == version.expected_blake2b_hash() {
            return Ok(version);
        }
    }



@@ 203,7 193,8 @@ impl Drop for SuspendedGameProcess {
fn run_and_patch_game(exe_path: &std::path::Path) -> anyhow::Result<()> {
    // Check that we got the right exe
    let exe_data = std::fs::read(exe_path).context("couldn't check exe data")?;
    if !exe_hash_matches_1_05(&exe_data) {
    let exe_hash = blake2::Blake2b512::digest(&exe_data);
    if &exe_hash[..] != ExeVersion::NisaV1_05En.expected_blake2b_hash() {
        return Err(anyhow::Error::msg("Unexpected executable. Are you sure this is the right version?"));
    }



@@ 248,6 239,6 @@ fn run_and_patch_game(exe_path: &std::path::Path) -> anyhow::Result<()> {
pub fn run_and_patch_new(path: &std::path::Path, version: ExeVersion) -> anyhow::Result<()> {
    match version {
        ExeVersion::NisaV1_05En => run_and_patch_game(&path.join(version.relative_path())),
        ExeVersion::NisaV1_05Jp => todo!(),
        ExeVersion::NisaV1_05Jp => Err(anyhow::Error::msg("TODO: japanese is not supported yet")),
    }
}