Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rfuzzo committed Dec 26, 2023
1 parent 283ddd6 commit c2f2f4d
Show file tree
Hide file tree
Showing 8 changed files with 491 additions and 376 deletions.
31 changes: 31 additions & 0 deletions src/archive/dependency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::io::{Read, Result, Write};

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

use crate::io::FromReader;

#[allow(dead_code)]
#[derive(Debug, Clone, Copy)]
pub(crate) struct Dependency {
hash: u64,
}

impl Dependency {
pub(crate) fn new(hash: u64) -> Self {
Self { hash }
}

pub(crate) fn write<W: Write>(&self, writer: &mut W) -> Result<()> {
writer.write_u64::<LittleEndian>(self.hash)?;
Ok(())
}
}
#[warn(dead_code)]

impl FromReader for Dependency {
fn from_reader<R: Read>(reader: &mut R) -> Result<Self> {
Ok(Dependency {
hash: reader.read_u64::<LittleEndian>()?,
})
}
}
84 changes: 84 additions & 0 deletions src/archive/file_entry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::io::{Read, Result, Write};

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

use crate::io::FromReader;

#[derive(Debug, Clone, Copy)]
pub(crate) struct FileEntry {
name_hash_64: u64,
timestamp: u64, //SystemTime,
num_inline_buffer_segments: u32,
segments_start: u32,
segments_end: u32,
resource_dependencies_start: u32,
resource_dependencies_end: u32,
sha1_hash: [u8; 20],
}

impl FileEntry {
pub(crate) fn new(
name_hash_64: u64,
timestamp: u64,
num_inline_buffer_segments: u32,
segments_start: u32,
segments_end: u32,
resource_dependencies_start: u32,
resource_dependencies_end: u32,
sha1_hash: [u8; 20],
) -> Self {
Self {
name_hash_64,
timestamp,
num_inline_buffer_segments,
segments_start,
segments_end,
resource_dependencies_start,
resource_dependencies_end,
sha1_hash,
}
}

pub(crate) fn write<W: Write>(&self, writer: &mut W) -> Result<()> {
writer.write_u64::<LittleEndian>(self.name_hash_64)?;
writer.write_u64::<LittleEndian>(self.timestamp)?;
writer.write_u32::<LittleEndian>(self.num_inline_buffer_segments)?;
writer.write_u32::<LittleEndian>(self.segments_start)?;
writer.write_u32::<LittleEndian>(self.segments_end)?;
writer.write_u32::<LittleEndian>(self.resource_dependencies_start)?;
writer.write_u32::<LittleEndian>(self.resource_dependencies_end)?;
writer.write_all(self.sha1_hash.as_slice())?;
Ok(())
}

pub(crate) fn name_hash_64(&self) -> u64 {
self.name_hash_64
}

pub(crate) fn segments_start(&self) -> u32 {
self.segments_start
}

pub(crate) fn segments_end(&self) -> u32 {
self.segments_end
}
}

impl FromReader for FileEntry {
fn from_reader<R: Read>(reader: &mut R) -> Result<Self> {
let mut entry = FileEntry {
name_hash_64: reader.read_u64::<LittleEndian>()?,
timestamp: reader.read_u64::<LittleEndian>()?,
num_inline_buffer_segments: reader.read_u32::<LittleEndian>()?,
segments_start: reader.read_u32::<LittleEndian>()?,
segments_end: reader.read_u32::<LittleEndian>()?,
resource_dependencies_start: reader.read_u32::<LittleEndian>()?,
resource_dependencies_end: reader.read_u32::<LittleEndian>()?,
sha1_hash: [0; 20],
};

reader.read_exact(&mut entry.sha1_hash[..])?;

Ok(entry)
}
}
51 changes: 51 additions & 0 deletions src/archive/file_segment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::io::{Read, Result, Write};

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

use crate::io::FromReader;

#[derive(Debug, Clone, Copy)]
pub(crate) struct FileSegment {
offset: u64,
z_size: u32,
size: u32,
}

impl FileSegment {
pub(crate) fn new(offset: u64, z_size: u32, size: u32) -> Self {
Self {
offset,
z_size,
size,
}
}

pub(crate) fn write<W: Write>(&self, writer: &mut W) -> Result<()> {
writer.write_u64::<LittleEndian>(self.offset)?;
writer.write_u32::<LittleEndian>(self.z_size)?;
writer.write_u32::<LittleEndian>(self.size)?;
Ok(())
}

pub(crate) fn offset(&self) -> u64 {
self.offset
}

pub(crate) fn z_size(&self) -> u32 {
self.z_size
}

pub(crate) fn size(&self) -> u32 {
self.size
}
}

impl FromReader for FileSegment {
fn from_reader<R: Read>(reader: &mut R) -> Result<Self> {
Ok(FileSegment {
offset: reader.read_u64::<LittleEndian>()?,
z_size: reader.read_u32::<LittleEndian>()?,
size: reader.read_u32::<LittleEndian>()?,
})
}
}
86 changes: 86 additions & 0 deletions src/archive/header.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::io::{Read, Result, Write};

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

use crate::io::FromReader;

#[derive(Debug, Clone, Copy)]
pub(crate) struct Header {
magic: u32,
version: u32,
index_position: u64,
index_size: u32,
debug_position: u64,
debug_size: u32,
filesize: u64,
}

impl Header {
pub(crate) fn new(
index_position: u64,
index_size: u32,
debug_position: u64,
debug_size: u32,
filesize: u64,
) -> Self {
Self {
magic: Header::HEADER_MAGIC,
version: Header::HEADER_VERSION,
index_position,
index_size,
debug_position,
debug_size,
filesize,
}
}

pub(crate) const HEADER_MAGIC: u32 = 1380009042;
pub(crate) const HEADER_VERSION: u32 = 12;
pub(crate) const HEADER_SIZE: usize = 40;
pub(crate) const HEADER_EXTENDED_SIZE: u64 = 0xAC;

pub(crate) fn index_position(&self) -> u64 {
self.index_position
}
}

// impl Default for Header {
// fn default() -> Self {
// Self {
// magic: 1380009042,
// version: 12,
// index_position: Default::default(),
// index_size: Default::default(),
// debug_position: Default::default(),
// debug_size: Default::default(),
// filesize: Default::default(),
// }
// }
// }

impl FromReader for Header {
fn from_reader<R: Read>(reader: &mut R) -> Result<Self> {
Ok(Header {
magic: reader.read_u32::<LittleEndian>()?,
version: reader.read_u32::<LittleEndian>()?,
index_position: reader.read_u64::<LittleEndian>()?,
index_size: reader.read_u32::<LittleEndian>()?,
debug_position: reader.read_u64::<LittleEndian>()?,
debug_size: reader.read_u32::<LittleEndian>()?,
filesize: reader.read_u64::<LittleEndian>()?,
})
}
}
impl Header {
pub(crate) fn write<W: Write>(&self, writer: &mut W) -> Result<()> {
writer.write_u32::<LittleEndian>(self.magic)?;
writer.write_u32::<LittleEndian>(self.version)?;
writer.write_u64::<LittleEndian>(self.index_position)?;
writer.write_u32::<LittleEndian>(self.index_size)?;
writer.write_u64::<LittleEndian>(self.debug_position)?;
writer.write_u32::<LittleEndian>(self.debug_size)?;
writer.write_u64::<LittleEndian>(self.filesize)?;

Ok(())
}
}
46 changes: 46 additions & 0 deletions src/archive/index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::io::{Read, Result};

use byteorder::{LittleEndian, ReadBytesExt};

use crate::io::FromReader;

#[derive(Debug, Clone)]
pub(crate) struct Index {
/// Offset from the beginning of this struct, should be 8
file_table_offset: u32,
/// byte size of the table
file_table_size: u32,
crc: u64,
file_entry_count: u32,
file_segment_count: u32,
resource_dependency_count: u32,
}

impl Index {
pub(crate) fn file_entry_count(&self) -> u32 {
self.file_entry_count
}

pub(crate) fn file_segment_count(&self) -> u32 {
self.file_segment_count
}

pub(crate) fn resource_dependency_count(&self) -> u32 {
self.resource_dependency_count
}
}

impl FromReader for Index {
fn from_reader<R: Read>(cursor: &mut R) -> Result<Self> {
let index = Index {
file_table_offset: cursor.read_u32::<LittleEndian>()?,
file_table_size: cursor.read_u32::<LittleEndian>()?,
crc: cursor.read_u64::<LittleEndian>()?,
file_entry_count: cursor.read_u32::<LittleEndian>()?,
file_segment_count: cursor.read_u32::<LittleEndian>()?,
resource_dependency_count: cursor.read_u32::<LittleEndian>()?,
};

Ok(index)
}
}
Loading

0 comments on commit c2f2f4d

Please sign in to comment.