mirror of
https://github.com/redstrate/Physis.git
synced 2025-04-25 13:57:45 +00:00
Separate standard, model, etc. reading into their own methods
This commit is contained in:
parent
6173ce422f
commit
a471ce1080
1 changed files with 172 additions and 159 deletions
41
src/dat.rs
41
src/dat.rs
|
@ -166,19 +166,30 @@ impl DatFile {
|
|||
})
|
||||
}
|
||||
|
||||
/// Reads from a certain offset inside of the dat file. This offset will be fixed automatically
|
||||
/// by the function.
|
||||
///
|
||||
/// If the block of data is successfully parsed, it returns the file data - otherwise is None.
|
||||
pub fn read_from_offset(&mut self, offset: u32) -> Option<MemoryBuffer> {
|
||||
let offset: u64 = (offset * 0x80) as u64;
|
||||
let offset = (offset * 0x80) as u64;
|
||||
|
||||
self.file.seek(SeekFrom::Start(offset))
|
||||
.expect("TODO: panic message");
|
||||
.expect("Unable to find offset in file.");
|
||||
|
||||
let file_info = FileInfo::read(&mut self.file)
|
||||
.expect("Failed to parse file info.");
|
||||
|
||||
match file_info.file_type {
|
||||
FileType::Empty => None,
|
||||
FileType::Standard => {
|
||||
let standard_file_info = file_info.standard_info.unwrap();
|
||||
FileType::Standard => self.read_standard_file(offset, &file_info),
|
||||
FileType::Model => self.read_model_file(offset, &file_info),
|
||||
FileType::Texture => self.read_texture_file(offset, &file_info)
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads a standard file block.
|
||||
fn read_standard_file(&mut self, offset : u64, file_info : &FileInfo) -> Option<MemoryBuffer> {
|
||||
let standard_file_info = file_info.standard_info.as_ref().unwrap();
|
||||
|
||||
let mut blocks: Vec<Block> = Vec::with_capacity(standard_file_info.num_blocks as usize);
|
||||
|
||||
|
@ -197,10 +208,12 @@ impl DatFile {
|
|||
|
||||
Some(data)
|
||||
}
|
||||
FileType::Model => {
|
||||
|
||||
/// Reads a model file block.
|
||||
fn read_model_file(&mut self, offset : u64, file_info : &FileInfo) -> Option<MemoryBuffer> {
|
||||
let mut buffer = Cursor::new(Vec::new());
|
||||
|
||||
let model_file_info = file_info.model_info.unwrap();
|
||||
let model_file_info = file_info.model_info.as_ref().unwrap();
|
||||
|
||||
let base_offset = offset + (file_info.size as u64);
|
||||
|
||||
|
@ -305,21 +318,23 @@ impl DatFile {
|
|||
|
||||
Some(buffer.into_inner())
|
||||
}
|
||||
FileType::Texture => {
|
||||
|
||||
/// Reads a texture file block.
|
||||
fn read_texture_file(&mut self, offset : u64, file_info : &FileInfo) -> Option<MemoryBuffer> {
|
||||
let mut data: Vec<u8> = Vec::with_capacity(file_info.file_size as usize);
|
||||
|
||||
let texture_file_info = file_info.texture_info.unwrap();
|
||||
let texture_file_info = file_info.texture_info.as_ref().unwrap();
|
||||
|
||||
// write the header if it exists
|
||||
if texture_file_info.lods[0].compressed_offset != 0 {
|
||||
let original_pos = self.file.stream_position().unwrap();
|
||||
|
||||
self.file.seek(SeekFrom::Start(offset + file_info.size as u64));
|
||||
self.file.seek(SeekFrom::Start(offset + file_info.size as u64)).ok()?;
|
||||
let mut header = vec![0u8; texture_file_info.lods[0].compressed_offset as usize];
|
||||
self.file.read(&mut header);
|
||||
self.file.read(&mut header).ok()?;
|
||||
data.append(&mut header);
|
||||
|
||||
self.file.seek(SeekFrom::Start(original_pos));
|
||||
self.file.seek(SeekFrom::Start(original_pos)).ok()?;
|
||||
}
|
||||
|
||||
for i in 0..texture_file_info.num_blocks {
|
||||
|
@ -327,7 +342,7 @@ impl DatFile {
|
|||
|
||||
for _ in 0..texture_file_info.lods[i as usize].block_count {
|
||||
data.append(&mut read_data_block(&self.file, running_block_total).unwrap());
|
||||
self.file.seek(SeekFrom::Start(running_block_total));
|
||||
self.file.seek(SeekFrom::Start(running_block_total)).ok()?;
|
||||
running_block_total += i16::read(&mut self.file).unwrap() as u64;
|
||||
}
|
||||
}
|
||||
|
@ -335,5 +350,3 @@ impl DatFile {
|
|||
Some(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue