feat: Try simpler approach
This commit is contained in:
parent
b376e6429b
commit
ac4a0a9942
3 changed files with 6 additions and 133 deletions
91
Cargo.lock
generated
91
Cargo.lock
generated
|
@ -8,100 +8,9 @@ version = "1.0.57"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "08f9b8508dccb7687a1d6c4ce66b2b0ecef467c94667de27d8d7fe1f8d2a9cdc"
|
checksum = "08f9b8508dccb7687a1d6c4ce66b2b0ecef467c94667de27d8d7fe1f8d2a9cdc"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "bitflags"
|
|
||||||
version = "1.3.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "cfg-if"
|
|
||||||
version = "1.0.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "fastrand"
|
|
||||||
version = "1.7.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "c3fcf0cee53519c866c09b5de1f6c56ff9d647101f81c1964fa632e148896cdf"
|
|
||||||
dependencies = [
|
|
||||||
"instant",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "instant"
|
|
||||||
version = "0.1.12"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
|
|
||||||
dependencies = [
|
|
||||||
"cfg-if",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "libc"
|
|
||||||
version = "0.2.124"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "21a41fed9d98f27ab1c6d161da622a4fa35e8a54a8adc24bbf3ddd0ef70b0e50"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "redox_syscall"
|
|
||||||
version = "0.2.13"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "62f25bc4c7e55e0b0b7a1d43fb893f4fa1361d0abe38b9ce4f323c2adfe6ef42"
|
|
||||||
dependencies = [
|
|
||||||
"bitflags",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "remove_dir_all"
|
|
||||||
version = "0.5.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
|
|
||||||
dependencies = [
|
|
||||||
"winapi",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "tempfile"
|
|
||||||
version = "3.3.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4"
|
|
||||||
dependencies = [
|
|
||||||
"cfg-if",
|
|
||||||
"fastrand",
|
|
||||||
"libc",
|
|
||||||
"redox_syscall",
|
|
||||||
"remove_dir_all",
|
|
||||||
"winapi",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "winapi"
|
|
||||||
version = "0.3.9"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
|
||||||
dependencies = [
|
|
||||||
"winapi-i686-pc-windows-gnu",
|
|
||||||
"winapi-x86_64-pc-windows-gnu",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "winapi-i686-pc-windows-gnu"
|
|
||||||
version = "0.4.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "winapi-x86_64-pc-windows-gnu"
|
|
||||||
version = "0.4.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wwise_fix"
|
name = "wwise_fix"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"tempfile",
|
|
||||||
]
|
]
|
||||||
|
|
|
@ -7,4 +7,3 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.57"
|
anyhow = "1.0.57"
|
||||||
tempfile = "3.3.0"
|
|
||||||
|
|
47
src/main.rs
47
src/main.rs
|
@ -1,13 +1,11 @@
|
||||||
use anyhow::bail;
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use tempfile::NamedTempFile;
|
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::ffi::{OsStr, OsString};
|
use std::ffi::{OsStr, OsString};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, Read, Seek, SeekFrom, Write};
|
use std::io::{Seek, SeekFrom, Write};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
// Offset within the bank file to the version number within the BKHD section.
|
// Offset within the bank file to the version number within the BKHD section.
|
||||||
|
@ -20,45 +18,12 @@ fn fix_file<P>(path: P) -> Result<()>
|
||||||
where
|
where
|
||||||
P: AsRef<Path>,
|
P: AsRef<Path>,
|
||||||
{
|
{
|
||||||
let tmp_path = {
|
let mut f = File::open(&path).with_context(|| "failed to open sound bank file")?;
|
||||||
let mut f = File::open(&path).with_context(|| "failed to open sound bank file")?;
|
|
||||||
let mut tmp = NamedTempFile::new().with_context(|| "failed to open temporary file")?;
|
|
||||||
|
|
||||||
// Copy everything until the version number
|
f.seek(SeekFrom::Start(OFFSET_VERSION_NUMBER))
|
||||||
io::copy(
|
.with_context(|| "failed to seek to version number offset")?;
|
||||||
&mut std::io::Read::by_ref(&mut f).take(OFFSET_VERSION_NUMBER),
|
f.write(&BANK_VERSION)
|
||||||
&mut tmp,
|
.with_context(|| "failed to write version number")?;
|
||||||
)
|
|
||||||
.with_context(|| "failed to copy to tempfile")?;
|
|
||||||
|
|
||||||
// Write the hard coded version number
|
|
||||||
tmp.write(&BANK_VERSION)
|
|
||||||
.with_context(|| "failed to write version number")?;
|
|
||||||
|
|
||||||
// Copy the rest of the file
|
|
||||||
f.seek(SeekFrom::Start(OFFSET_VERSION_NUMBER + 4))
|
|
||||||
.with_context(|| "failed to skip to rest of content")?;
|
|
||||||
io::copy(&mut f, &mut tmp).with_context(|| "failed to copy to tempfile")?;
|
|
||||||
tmp.into_temp_path()
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Err(_) = fs::rename(&tmp_path, &path) {
|
|
||||||
// Attempt manual copy-and-delete
|
|
||||||
let mut tmp = File::open(&tmp_path).with_context(|| "failed to open tempfile for reading")?;
|
|
||||||
let mut f = File::open(&path).with_context(|| "failed to open destination file")?;
|
|
||||||
if let Err(err) = io::copy(&mut tmp, &mut f) {
|
|
||||||
let tmp_path = tmp_path
|
|
||||||
.keep()
|
|
||||||
.with_context(|| "failed to persist temporary file")?;
|
|
||||||
bail!(
|
|
||||||
"failed to rename or move temporary file to override the sound bank: {}!\n\
|
|
||||||
Please manually move the file from {} to {}.",
|
|
||||||
err,
|
|
||||||
tmp_path.display(),
|
|
||||||
path.as_ref().display()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue