diff --git a/psoutils/src/quest/bin.rs b/psoutils/src/quest/bin.rs index 033cc69..83c9f72 100644 --- a/psoutils/src/quest/bin.rs +++ b/psoutils/src/quest/bin.rs @@ -1,5 +1,5 @@ use std::fs::File; -use std::io::{BufReader, Cursor, Read}; +use std::io::{BufReader, Cursor, Read, Write}; use std::path::Path; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -264,6 +264,19 @@ impl QuestBin { Ok(()) } + pub fn to_compressed_file(&self, path: &Path) -> Result<(), QuestBinError> { + let compressed_bytes = self.to_compressed_bytes()?; + let mut file = File::create(path)?; + file.write_all(compressed_bytes.as_ref())?; + Ok(()) + } + + pub fn to_uncompressed_file(&self, path: &Path) -> Result<(), QuestBinError> { + let mut file = File::create(path)?; + self.write_uncompressed_bytes(&mut file)?; + Ok(()) + } + pub fn to_uncompressed_bytes(&self) -> Result, QuestBinError> { let mut buffer = Cursor::new(Vec::::new()); self.write_uncompressed_bytes(&mut buffer)?; @@ -284,6 +297,8 @@ impl QuestBin { #[cfg(test)] pub mod tests { + use tempfile::TempDir; + use super::*; pub fn validate_quest_58_bin(bin: &QuestBin) { @@ -340,6 +355,18 @@ pub mod tests { Ok(()) } + #[test] + pub fn write_compressed_quest_58_bin() -> Result<(), QuestBinError> { + let data = include_bytes!("../../test-assets/q058-ret-gc.bin"); + let bin = QuestBin::from_compressed_bytes(data)?; + let tmp_dir = TempDir::new()?; + let bin_path = tmp_dir.path().join("quest58.bin"); + bin.to_compressed_file(&bin_path)?; + let bin = QuestBin::from_compressed_file(&bin_path)?; + validate_quest_58_bin(&bin); + Ok(()) + } + #[test] pub fn read_uncompressed_quest_58_bin() -> Result<(), QuestBinError> { let path = Path::new("test-assets/q058-ret-gc.uncompressed.bin"); @@ -348,6 +375,18 @@ pub mod tests { Ok(()) } + #[test] + pub fn write_uncompressed_quest_58_bin() -> Result<(), QuestBinError> { + let data = include_bytes!("../../test-assets/q058-ret-gc.bin"); + let bin = QuestBin::from_compressed_bytes(data)?; + let tmp_dir = TempDir::new()?; + let bin_path = tmp_dir.path().join("quest58.bin"); + bin.to_uncompressed_file(&bin_path)?; + let bin = QuestBin::from_uncompressed_file(&bin_path)?; + validate_quest_58_bin(&bin); + Ok(()) + } + #[test] pub fn read_compressed_quest_118_bin() -> Result<(), QuestBinError> { let path = Path::new("test-assets/q118-vr-gc.bin"); @@ -356,6 +395,18 @@ pub mod tests { Ok(()) } + #[test] + pub fn write_compressed_quest_118_bin() -> Result<(), QuestBinError> { + let data = include_bytes!("../../test-assets/q118-vr-gc.bin"); + let bin = QuestBin::from_compressed_bytes(data)?; + let tmp_dir = TempDir::new()?; + let bin_path = tmp_dir.path().join("quest118.bin"); + bin.to_compressed_file(&bin_path)?; + let bin = QuestBin::from_compressed_file(&bin_path)?; + validate_quest_118_bin(&bin); + Ok(()) + } + #[test] pub fn read_uncompressed_quest_118_bin() -> Result<(), QuestBinError> { let path = Path::new("test-assets/q118-vr-gc.uncompressed.bin"); @@ -363,4 +414,16 @@ pub mod tests { validate_quest_118_bin(&bin); Ok(()) } + + #[test] + pub fn write_uncompressed_quest_118_bin() -> Result<(), QuestBinError> { + let data = include_bytes!("../../test-assets/q118-vr-gc.bin"); + let bin = QuestBin::from_compressed_bytes(data)?; + let tmp_dir = TempDir::new()?; + let bin_path = tmp_dir.path().join("quest118.bin"); + bin.to_uncompressed_file(&bin_path)?; + let bin = QuestBin::from_uncompressed_file(&bin_path)?; + validate_quest_118_bin(&bin); + Ok(()) + } } diff --git a/psoutils/src/quest/dat.rs b/psoutils/src/quest/dat.rs index fdd4618..a3233c8 100644 --- a/psoutils/src/quest/dat.rs +++ b/psoutils/src/quest/dat.rs @@ -1,6 +1,6 @@ use std::fmt::{Display, Formatter}; use std::fs::File; -use std::io::{BufReader, Cursor, Read}; +use std::io::{BufReader, Cursor, Read, Write}; use std::path::Path; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -255,6 +255,19 @@ impl QuestDat { Ok(()) } + pub fn to_compressed_file(&self, path: &Path) -> Result<(), QuestDatError> { + let compressed_bytes = self.to_compressed_bytes()?; + let mut file = File::create(path)?; + file.write_all(compressed_bytes.as_ref())?; + Ok(()) + } + + pub fn to_uncompressed_file(&self, path: &Path) -> Result<(), QuestDatError> { + let mut file = File::create(path)?; + self.write_uncompressed_bytes(&mut file)?; + Ok(()) + } + pub fn to_uncompressed_bytes(&self) -> Result, QuestDatError> { let mut buffer = Cursor::new(Vec::::new()); self.write_uncompressed_bytes(&mut buffer)?; @@ -276,6 +289,8 @@ impl QuestDat { #[cfg(test)] pub mod tests { + use tempfile::TempDir; + use super::*; pub fn validate_quest_58_dat(dat: &QuestDat) { @@ -523,6 +538,18 @@ pub mod tests { Ok(()) } + #[test] + pub fn write_compressed_quest_58_dat() -> Result<(), QuestDatError> { + let data = include_bytes!("../../test-assets/q058-ret-gc.dat"); + let dat = QuestDat::from_compressed_bytes(data)?; + let tmp_dir = TempDir::new()?; + let dat_path = tmp_dir.path().join("quest58.dat"); + dat.to_compressed_file(&dat_path)?; + let dat = QuestDat::from_compressed_file(&dat_path)?; + validate_quest_58_dat(&dat); + Ok(()) + } + #[test] pub fn read_uncompressed_quest_58_dat() -> Result<(), QuestDatError> { let path = Path::new("test-assets/q058-ret-gc.uncompressed.dat"); @@ -531,6 +558,18 @@ pub mod tests { Ok(()) } + #[test] + pub fn write_uncompressed_quest_58_dat() -> Result<(), QuestDatError> { + let data = include_bytes!("../../test-assets/q058-ret-gc.dat"); + let dat = QuestDat::from_compressed_bytes(data)?; + let tmp_dir = TempDir::new()?; + let dat_path = tmp_dir.path().join("quest58.dat"); + dat.to_uncompressed_file(&dat_path)?; + let dat = QuestDat::from_uncompressed_file(&dat_path)?; + validate_quest_58_dat(&dat); + Ok(()) + } + #[test] pub fn read_compressed_quest_118_dat() -> Result<(), QuestDatError> { let path = Path::new("test-assets/q118-vr-gc.dat"); @@ -539,6 +578,18 @@ pub mod tests { Ok(()) } + #[test] + pub fn write_compressed_quest_118_dat() -> Result<(), QuestDatError> { + let data = include_bytes!("../../test-assets/q118-vr-gc.dat"); + let dat = QuestDat::from_compressed_bytes(data)?; + let tmp_dir = TempDir::new()?; + let dat_path = tmp_dir.path().join("quest118.dat"); + dat.to_compressed_file(&dat_path)?; + let dat = QuestDat::from_compressed_file(&dat_path)?; + validate_quest_118_dat(&dat); + Ok(()) + } + #[test] pub fn read_uncompressed_quest_118_dat() -> Result<(), QuestDatError> { let path = Path::new("test-assets/q118-vr-gc.uncompressed.dat"); @@ -546,4 +597,16 @@ pub mod tests { validate_quest_118_dat(&dat); Ok(()) } + + #[test] + pub fn write_uncompressed_quest_118_dat() -> Result<(), QuestDatError> { + let data = include_bytes!("../../test-assets/q118-vr-gc.dat"); + let dat = QuestDat::from_compressed_bytes(data)?; + let tmp_dir = TempDir::new()?; + let dat_path = tmp_dir.path().join("quest118.dat"); + dat.to_uncompressed_file(&dat_path)?; + let dat = QuestDat::from_uncompressed_file(&dat_path)?; + validate_quest_118_dat(&dat); + Ok(()) + } } diff --git a/psoutils/src/quest/qst.rs b/psoutils/src/quest/qst.rs index ca86cff..89c459d 100644 --- a/psoutils/src/quest/qst.rs +++ b/psoutils/src/quest/qst.rs @@ -372,6 +372,12 @@ impl QuestQst { Ok(()) } + pub fn to_file(&self, path: &Path) -> Result<(), QuestQstError> { + let mut file = File::create(path)?; + self.write_bytes(&mut file)?; + Ok(()) + } + pub fn to_bytes(&self) -> Result, QuestQstError> { let mut buffer = Cursor::new(Vec::::new()); self.write_bytes(&mut buffer)?; @@ -405,6 +411,8 @@ impl QuestQst { #[cfg(test)] mod tests { + use tempfile::TempDir; + use crate::quest::bin::tests::{validate_quest_118_bin, validate_quest_58_bin}; use crate::quest::dat::tests::{validate_quest_118_dat, validate_quest_58_dat}; @@ -543,6 +551,30 @@ mod tests { Ok(()) } + #[test] + pub fn write_quest_58_qst_to_file() -> Result<(), QuestQstError> { + let online_data = include_bytes!("../../test-assets/q058-ret-gc.online.qst"); + let offline_data = include_bytes!("../../test-assets/q058-ret-gc.offline.qst"); + + let tmp_dir = TempDir::new()?; + + let mut reader = Cursor::new(online_data); + let qst = QuestQst::from_bytes(&mut reader)?; + let qst_path = tmp_dir.path().join("quest58.online.qst"); + qst.to_file(&qst_path)?; + let qst = QuestQst::from_file(&qst_path)?; + validate_quest_58_qst(&qst, 1438, 15097, true)?; + + let mut reader = Cursor::new(offline_data); + let qst = QuestQst::from_bytes(&mut reader)?; + let qst_path = tmp_dir.path().join("quest58.offline.qst"); + qst.to_file(&qst_path)?; + let qst = QuestQst::from_file(&qst_path)?; + validate_quest_58_qst(&qst, 1571, 15105, false)?; + + Ok(()) + } + #[test] pub fn read_quest_118_qst_from_file() -> Result<(), QuestQstError> { let qst = QuestQst::from_file(Path::new("test-assets/q118-vr-gc.online.qst"))?; @@ -554,6 +586,30 @@ mod tests { Ok(()) } + #[test] + pub fn write_quest_118_qst_to_file() -> Result<(), QuestQstError> { + let online_data = include_bytes!("../../test-assets/q118-vr-gc.online.qst"); + let offline_data = include_bytes!("../../test-assets/q118-vr-gc.offline.qst"); + + let tmp_dir = TempDir::new()?; + + let mut reader = Cursor::new(online_data); + let qst = QuestQst::from_bytes(&mut reader)?; + let qst_path = tmp_dir.path().join("quest118.online.qst"); + qst.to_file(&qst_path)?; + let qst = QuestQst::from_file(&qst_path)?; + validate_quest_118_qst(&qst, 14208, 11802, true)?; + + let mut reader = Cursor::new(offline_data); + let qst = QuestQst::from_bytes(&mut reader)?; + let qst_path = tmp_dir.path().join("quest118.offline.qst"); + qst.to_file(&qst_path)?; + let qst = QuestQst::from_file(&qst_path)?; + validate_quest_118_qst(&qst, 14801, 11810, false)?; + + Ok(()) + } + #[test] pub fn create_qst_from_quest_58_bindat_files() -> Result<(), QuestQstError> { let mut bin = QuestBin::from_compressed_file(Path::new("test-assets/q058-ret-gc.bin"))?;