diff --git a/benches/physis_benchmark.rs b/benches/physis_benchmark.rs index 73939e5..083ee29 100755 --- a/benches/physis_benchmark.rs +++ b/benches/physis_benchmark.rs @@ -23,9 +23,9 @@ fn fetch_data() { } fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("hash calc", |b| b.iter(|| bench_calculate_hash())); - c.bench_function("gamedata reloading repositories", |b| b.iter(|| reload_repos())); - c.bench_function("gamedata extract", |b| b.iter(|| fetch_data())); + c.bench_function("hash calc", |b| b.iter(bench_calculate_hash)); + c.bench_function("gamedata reloading repositories", |b| b.iter(reload_repos)); + c.bench_function("gamedata extract", |b| b.iter(fetch_data)); } criterion_group!(benches, criterion_benchmark); diff --git a/src/blowfish.rs b/src/blowfish.rs index 2718505..ebb9d3b 100755 --- a/src/blowfish.rs +++ b/src/blowfish.rs @@ -71,8 +71,8 @@ impl Blowfish { let (l, r) = self.encrypt_pair(u32::from_le_bytes(l_bytes), u32::from_le_bytes(r_bytes)); - cursor.write(u32::to_le_bytes(l).as_slice()).ok()?; - cursor.write(u32::to_le_bytes(r).as_slice()).ok()?; + cursor.write_all(u32::to_le_bytes(l).as_slice()).ok()?; + cursor.write_all(u32::to_le_bytes(r).as_slice()).ok()?; } Some(cursor.into_inner()) @@ -84,8 +84,7 @@ impl Blowfish { padded_length = data.len() + (8 - (data.len() % 8)); } - let mut vec = Vec::with_capacity(padded_length); - vec.resize(padded_length, 0); + let mut vec = vec![0; padded_length]; vec[..data.len()].clone_from_slice(data); vec @@ -105,8 +104,8 @@ impl Blowfish { let (l, r) = self.decrypt_pair(u32::from_le_bytes(l_bytes), u32::from_le_bytes(r_bytes)); - cursor.write(u32::to_le_bytes(l).as_slice()).ok()?; - cursor.write(u32::to_le_bytes(r).as_slice()).ok()?; + cursor.write_all(u32::to_le_bytes(l).as_slice()).ok()?; + cursor.write_all(u32::to_le_bytes(r).as_slice()).ok()?; } Some(buffer) @@ -130,7 +129,7 @@ impl Blowfish { l ^= self.f(r); } - return (r ^ self.p[17], l ^ self.p[16]); + (r ^ self.p[17], l ^ self.p[16]) } fn decrypt_pair(&self, mut l: u32, mut r: u32) -> (u32, u32) { @@ -141,6 +140,6 @@ impl Blowfish { l ^= self.f(r); } - return (r ^ self.p[0], l ^ self.p[1]); + (r ^ self.p[0], l ^ self.p[1]) } } \ No newline at end of file diff --git a/src/dat.rs b/src/dat.rs index 2c58ce8..bfbb362 100755 --- a/src/dat.rs +++ b/src/dat.rs @@ -252,7 +252,7 @@ impl DatFile { let data = read_data_block(&self.file, *last_pos) .expect("Unable to read block data."); // write to buffer - buffer.write(data.as_slice()).ok()?; + buffer.write_all(data.as_slice()).ok()?; self.file.seek(SeekFrom::Start(last_pos + (compressed_block_sizes[current_block as usize] as u64))).ok()?; current_block += 1; @@ -281,7 +281,7 @@ impl DatFile { let data = read_data_block(&self.file, last_pos) .expect("Unable to read raw model block!"); - buffer.write(data.as_slice()).expect("Unable to write to memory buffer!"); + buffer.write_all(data.as_slice()).expect("Unable to write to memory buffer!"); data_sizes[i] += data.len() as u32; self.file.seek(SeekFrom::Start(last_pos + (compressed_block_sizes[current_block] as u64))) diff --git a/src/exd.rs b/src/exd.rs index ae15b98..498a601 100644 --- a/src/exd.rs +++ b/src/exd.rs @@ -74,7 +74,7 @@ impl EXD { (bool_data & bit) == bit }; - return match column.data_type { + match column.data_type { ColumnDataType::String => { let string_offset : u32 = Self::read_data_raw(cursor).unwrap(); diff --git a/src/exh.rs b/src/exh.rs index 66deac4..0bac662 100644 --- a/src/exh.rs +++ b/src/exh.rs @@ -76,6 +76,6 @@ pub struct EXH { impl EXH { pub fn from_existing(buffer : &MemoryBuffer) -> Option { - Some(EXH::read(&mut Cursor::new(&buffer)).ok()?) + EXH::read(&mut Cursor::new(&buffer)).ok() } } \ No newline at end of file diff --git a/src/fiin.rs b/src/fiin.rs index b0667ba..2e795ce 100644 --- a/src/fiin.rs +++ b/src/fiin.rs @@ -43,7 +43,7 @@ impl FileInfo { /// Parses an existing FIIN file. pub fn from_existing(buffer : &MemoryBuffer) -> Option { let mut cursor = Cursor::new(buffer); - Some(FileInfo::read(&mut cursor).ok()?) + FileInfo::read(&mut cursor).ok() } /// Creates a new FileInfo structure from a list of filenames. These filenames must be present in diff --git a/src/gamedata.rs b/src/gamedata.rs index 12410e6..e0b8f98 100755 --- a/src/gamedata.rs +++ b/src/gamedata.rs @@ -206,7 +206,7 @@ impl GameData { let exd_file = self.extract(&exd_path).unwrap(); - EXD::from_existing(&exh, &exd_file) + EXD::from_existing(exh, &exd_file) } pub fn apply_patch(&self, patch_path : &str) -> Result<(), PatchError> { diff --git a/src/installer.rs b/src/installer.rs index 0aad6c7..c78c763 100644 --- a/src/installer.rs +++ b/src/installer.rs @@ -83,9 +83,9 @@ pub fn install_game(installer_path : &str, game_directory : &str) -> Result<(), let mut new_file = File::create(last_filename).unwrap(); if last_filename == "data1.hdr" { - new_file.write(&installer_file[last_position + 30..position - 42])?; + new_file.write_all(&installer_file[last_position + 30..position - 42])?; } else { - new_file.write(&installer_file[last_position + 33..position - 42])?; + new_file.write_all(&installer_file[last_position + 33..position - 42])?; } } @@ -95,7 +95,7 @@ pub fn install_game(installer_path : &str, game_directory : &str) -> Result<(), let mut new_file = File::create(last_filename).unwrap(); - new_file.write(&installer_file[last_position + 33..installer_file.len() - 42])?; + new_file.write_all(&installer_file[last_position + 33..installer_file.len() - 42])?; fs::create_dir_all(format!("{game_directory}/boot"))?; fs::create_dir_all(format!("{game_directory}/game"))?; diff --git a/src/patch.rs b/src/patch.rs index c2d53e8..eb51680 100755 --- a/src/patch.rs +++ b/src/patch.rs @@ -295,7 +295,7 @@ fn wipe(mut file : &File, length : i32) -> Result<(), PatchError> { let mut length = length; while length > 0 { let num_bytes = min(WIPE_BUFFER.len() as i32, length); - file.write(&WIPE_BUFFER[0..num_bytes as usize])?; + file.write_all(&WIPE_BUFFER[0..num_bytes as usize])?; length -= num_bytes; } @@ -313,19 +313,19 @@ fn write_empty_file_block_at(mut file : &File, offset : i32, block_number : i32) file.seek(SeekFrom::Start(offset as u64))?; let block_size : i32 = 1 << 7; - file.write(block_size.to_le_bytes().as_slice())?; + file.write_all(block_size.to_le_bytes().as_slice())?; let unknown : i32 = 0; - file.write(unknown.to_le_bytes().as_slice())?; + file.write_all(unknown.to_le_bytes().as_slice())?; let file_size : i32 = 0; - file.write(file_size.to_le_bytes().as_slice())?; + file.write_all(file_size.to_le_bytes().as_slice())?; let num_blocks : i32 = block_number - 1; - file.write(num_blocks.to_le_bytes().as_slice())?; + file.write_all(num_blocks.to_le_bytes().as_slice())?; let used_blocks : i32 = 0; - file.write(used_blocks.to_le_bytes().as_slice())?; + file.write_all(used_blocks.to_le_bytes().as_slice())?; Ok(()) } @@ -392,7 +392,7 @@ pub(crate) fn apply_patch(data_dir : &str, patch_path : &str) -> Result<(), Patc ChunkType::Sqpk(pchunk) => { match pchunk.operation { SqpkOperation::AddData(add) => { - let filename = get_dat_path(&target_info.as_ref().unwrap(), add.main_id, add.sub_id, add.file_id); + let filename = get_dat_path(target_info.as_ref().unwrap(), add.main_id, add.sub_id, add.file_id); let (left, _) = filename.rsplit_once('/').unwrap(); fs::create_dir_all(left)?; @@ -404,37 +404,37 @@ pub(crate) fn apply_patch(data_dir : &str, patch_path : &str) -> Result<(), Patc new_file.seek(SeekFrom::Start(add.block_offset as u64))?; - new_file.write(&*add.block_data)?; + new_file.write_all(&*add.block_data)?; - wipe(&mut new_file, add.block_delete_number)?; + wipe(&new_file, add.block_delete_number)?; } SqpkOperation::DeleteData(delete) => { - let filename = get_dat_path(&target_info.as_ref().unwrap(), delete.main_id, delete.sub_id, delete.file_id); + let filename = get_dat_path(target_info.as_ref().unwrap(), delete.main_id, delete.sub_id, delete.file_id); - let mut new_file = OpenOptions::new() + let new_file = OpenOptions::new() .write(true) .create(true) .open(filename)?; - write_empty_file_block_at(&mut new_file, delete.block_offset, delete.block_number)?; + write_empty_file_block_at(&new_file, delete.block_offset, delete.block_number)?; } SqpkOperation::ExpandData(expand) => { - let filename = get_dat_path(&target_info.as_ref().unwrap(), expand.main_id, expand.sub_id, expand.file_id); + let filename = get_dat_path(target_info.as_ref().unwrap(), expand.main_id, expand.sub_id, expand.file_id); let (left, _) = filename.rsplit_once('/').unwrap(); fs::create_dir_all(left)?; - let mut new_file = OpenOptions::new() + let new_file = OpenOptions::new() .write(true) .create(true) .open(filename)?; - write_empty_file_block_at(&mut new_file, expand.block_offset, expand.block_number)?; + write_empty_file_block_at(&new_file, expand.block_offset, expand.block_number)?; } SqpkOperation::HeaderUpdate(header) => { let file_path = match header.file_kind { - TargetFileKind::Dat => get_dat_path(&target_info.as_ref().unwrap(), header.main_id, header.sub_id, header.file_id), - TargetFileKind::Index => get_index_path(&target_info.as_ref().unwrap(), header.main_id, header.sub_id, header.file_id) + TargetFileKind::Dat => get_dat_path(target_info.as_ref().unwrap(), header.main_id, header.sub_id, header.file_id), + TargetFileKind::Index => get_index_path(target_info.as_ref().unwrap(), header.main_id, header.sub_id, header.file_id) }; let (left, _) = file_path.rsplit_once('/') @@ -450,7 +450,7 @@ pub(crate) fn apply_patch(data_dir : &str, patch_path : &str) -> Result<(), Patc new_file.seek(SeekFrom::Start(1024))?; } - new_file.write(&*header.header_data)?; + new_file.write_all(&*header.header_data)?; } SqpkOperation::FileOperation(fop) => { match fop.operation { @@ -480,7 +480,7 @@ pub(crate) fn apply_patch(data_dir : &str, patch_path : &str) -> Result<(), Patc .open(new_path)?; new_file.seek(SeekFrom::Start(fop.offset as u64))?; - new_file.write(&mut data)?; + new_file.write_all(&data)?; } SqpkFileOperation::DeleteFile => { let new_path = data_dir.to_owned() + "/" + &fop.path; diff --git a/src/repository.rs b/src/repository.rs index 78c29ce..e2b624a 100755 --- a/src/repository.rs +++ b/src/repository.rs @@ -133,31 +133,27 @@ impl Repository { let name = String::from(path.file_stem().unwrap().to_str().unwrap()); - let repo_type: RepositoryType; - - if name == "ffxiv" { - repo_type = Base; + let repo_type = if name == "ffxiv" { + Base } else { - repo_type = Expansion { + Expansion { number: name[2..3].parse().unwrap() } - } + }; - let version: Option; - - if repo_type == Base { + let version = if repo_type == Base { let mut d = PathBuf::from(dir); d.pop(); d.pop(); d.push("ffxivgame.ver"); - version = read_version(d.as_path()); + read_version(d.as_path()) } else { let mut d = PathBuf::from(dir); d.push(format!("{}.ver", name)); - version = read_version(d.as_path()); - } + read_version(d.as_path()) + }; if version == None { return None; diff --git a/src/skeleton.rs b/src/skeleton.rs index 1180383..4dacad7 100644 --- a/src/skeleton.rs +++ b/src/skeleton.rs @@ -71,7 +71,7 @@ impl Skeleton { content : String } - let pak = HkPackfile::from_str(&mut std::str::from_utf8(&buffer).unwrap()) + let pak = HkPackfile::from_str(std::str::from_utf8(buffer).unwrap()) .expect("Failed to parse sidecar file!"); // find the root level object @@ -97,7 +97,7 @@ impl Skeleton { string_repr.push(']'); // then we turn all of newlines into commas, except of course for the last one! - string_repr = string_repr.replacen("\n", ",", string_repr.matches("\n").count() - 1); + string_repr = string_repr.replacen('\n', ",", string_repr.matches('\n').count() - 1); use serde::Deserialize;