-
Notifications
You must be signed in to change notification settings - Fork 8
feat(meta): LazyBin & LazyObject #149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alanpq
wants to merge
2
commits into
main
Choose a base branch
from
feat/bin-streaming
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| use std::io::{Read, Write}; | ||
|
|
||
| use byteorder::{ReadBytesExt, WriteBytesExt, LE}; | ||
| use ltk_primitives::PrefixString; | ||
|
|
||
| #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub struct PropHeader { | ||
| pub version: u32, | ||
| /// List of other property bins this file depends on. | ||
| /// | ||
| /// Property bins can depend on other property bins in a similar fashion | ||
| /// to importing code libraries. | ||
| pub dependencies: Option<Vec<PrefixString<u16>>>, | ||
| pub object_count: u32, | ||
| } | ||
|
|
||
| #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub struct PatchHeader { | ||
| pub version: u32, | ||
| pub override_count: u32, | ||
| } | ||
|
|
||
| #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub enum Header { | ||
| Prop(PropHeader), | ||
| Patch(PatchHeader, PropHeader), | ||
| } | ||
|
|
||
| impl From<PropHeader> for Header { | ||
| fn from(value: PropHeader) -> Self { | ||
| Self::Prop(value) | ||
| } | ||
| } | ||
| impl From<(PatchHeader, PropHeader)> for Header { | ||
| fn from(value: (PatchHeader, PropHeader)) -> Self { | ||
| Self::Patch(value.0, value.1) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum ReadHeaderError { | ||
| #[error("Invalid file signature")] | ||
| InvalidFileSignature, | ||
| #[error("Invalid file version '{0}'")] | ||
| InvalidFileVersion(u32), | ||
|
|
||
| #[error(transparent)] | ||
| ReaderError(#[from] ltk_io_ext::ReaderError), | ||
| #[error(transparent)] | ||
| StringReadError(#[from] ltk_primitives::StringReadError), | ||
|
|
||
| #[error("IO Error - {0}")] | ||
| IOError(#[from] std::io::Error), | ||
| } | ||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum WriteHeaderError { | ||
| #[error(transparent)] | ||
| StringWriteError(#[from] ltk_primitives::StringWriteError), | ||
|
|
||
| #[error("IO Error - {0}")] | ||
| IOError(#[from] std::io::Error), | ||
| } | ||
| impl Header { | ||
| pub fn from_reader(reader: &mut (impl Read + ?Sized)) -> Result<Self, ReadHeaderError> { | ||
| let magic = reader.read_u32::<LE>()?; | ||
| match magic { | ||
| PropHeader::MAGIC => PropHeader::from_reader(reader).map(Self::Prop), | ||
| PatchHeader::MAGIC => Ok(Self::Patch( | ||
| PatchHeader::from_reader(reader)?, | ||
| PropHeader::from_reader(reader)?, | ||
| )), | ||
| _ => Err(ReadHeaderError::InvalidFileSignature), | ||
| } | ||
| } | ||
|
|
||
| pub fn to_writer(&self, writer: &mut (impl Write + ?Sized)) -> Result<(), WriteHeaderError> { | ||
| match self { | ||
| Header::Prop(prop_header) => { | ||
| writer.write_u32::<LE>(PropHeader::MAGIC)?; | ||
| prop_header.to_writer(writer)?; | ||
| } | ||
| Header::Patch(patch_header, prop_header) => { | ||
| writer.write_u32::<LE>(PatchHeader::MAGIC)?; | ||
| patch_header.to_writer(writer)?; | ||
| writer.write_u32::<LE>(PropHeader::MAGIC)?; | ||
| prop_header.to_writer(writer)?; | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub fn prop_header(&self) -> &PropHeader { | ||
| match &self { | ||
| Header::Prop(prop) => prop, | ||
| Header::Patch(_, prop) => prop, | ||
| } | ||
| } | ||
| pub fn into_parts(self) -> (PropHeader, Option<PatchHeader>) { | ||
| match self { | ||
| Header::Prop(prop) => (prop, None), | ||
| Header::Patch(patch, prop) => (prop, Some(patch)), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl PropHeader { | ||
| pub const LATEST_VERSION: u32 = 3; | ||
| pub const MAGIC: u32 = u32::from_le_bytes(*b"PROP"); | ||
| pub fn from_reader(reader: &mut (impl Read + ?Sized)) -> Result<Self, ReadHeaderError> { | ||
| use ReadHeaderError::*; | ||
|
|
||
| let version = reader.read_u32::<LE>()?; | ||
| if !matches!(version, 1..=3) { | ||
| // TODO (alan): distinguish override/non-override version in err? | ||
| return Err(InvalidFileVersion(version)); | ||
| } | ||
|
|
||
| let dependencies = match version { | ||
| 2.. => { | ||
| let dep_count = reader.read_u32::<LE>()?; | ||
| let mut dependencies = Vec::with_capacity(dep_count as _); | ||
| for _ in 0..dep_count { | ||
| dependencies.push(PrefixString::from_reader(reader)?); | ||
| } | ||
| Some(dependencies) | ||
| } | ||
| _ => None, | ||
| }; | ||
|
|
||
| let object_count = reader.read_u32::<LE>()?; | ||
|
|
||
| Ok(Self { | ||
| version, | ||
| dependencies, | ||
| object_count, | ||
| }) | ||
| } | ||
|
|
||
| pub fn to_writer(&self, writer: &mut (impl Write + ?Sized)) -> Result<(), WriteHeaderError> { | ||
| writer.write_u32::<LE>(self.version)?; | ||
| writer.write_u32::<LE>( | ||
| self.dependencies | ||
| .as_ref() | ||
| .map(|d| d.len().try_into().unwrap()) | ||
| .unwrap_or_default(), | ||
| )?; | ||
|
|
||
| if let Some(deps) = self.dependencies.as_ref() { | ||
| for dep in deps { | ||
| dep.to_writer(writer)?; | ||
| } | ||
| } | ||
|
|
||
| writer.write_u32::<LE>(self.object_count)?; | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl PatchHeader { | ||
| pub const MAGIC: u32 = u32::from_le_bytes(*b"PTCH"); | ||
| pub fn from_reader(reader: &mut (impl Read + ?Sized)) -> Result<Self, ReadHeaderError> { | ||
| use ReadHeaderError::*; | ||
|
|
||
| let version = reader.read_u32::<LE>()?; | ||
| if version != 1 { | ||
| return Err(InvalidFileVersion(version)); | ||
| } | ||
|
|
||
| let override_count = reader.read_u32::<LE>()?; | ||
|
|
||
| let magic = reader.read_u32::<LE>()?; | ||
| if magic != PropHeader::MAGIC { | ||
| // TODO (alan): repr this in the error | ||
| log::error!( | ||
| "Expected PROP ({:#x}) section after PTCH ({:#x}), got '{:#x}'", | ||
| PropHeader::MAGIC, | ||
| PatchHeader::MAGIC, | ||
| magic | ||
| ); | ||
| return Err(InvalidFileSignature); | ||
| } | ||
|
|
||
| Ok(Self { | ||
| version, | ||
| override_count, | ||
| }) | ||
| } | ||
|
|
||
| pub fn to_writer(&self, _writer: &mut (impl Write + ?Sized)) -> Result<(), WriteHeaderError> { | ||
| todo!("TODO: implement PTCH header write"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| use std::io::{self, Read, Seek}; | ||
|
|
||
| use byteorder::{ReadBytesExt as _, LE}; | ||
| use ltk_hash::{BinHash, ReadBytesExt as _}; | ||
|
|
||
| use crate::{ | ||
| header::{Header, PatchHeader, PropHeader, ReadHeaderError}, | ||
| traits::ReaderExt, | ||
| PropertyValueEnum, | ||
| }; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct LazyBin<'r, R: Read + Seek + ?Sized> { | ||
| pub header: PropHeader, | ||
| pub patch: Option<PatchHeader>, | ||
| reader: &'r mut R, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct LazyObject<'r, R: Read + Seek + ?Sized> { | ||
| pub header: PropHeader, | ||
| reader: &'r mut R, | ||
|
|
||
| pub size: u32, | ||
| pub prop_count: u16, | ||
| } | ||
|
|
||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum ReadErr { | ||
| #[error(transparent)] | ||
| Error(#[from] crate::Error), | ||
| #[error(transparent)] | ||
| ReadHeader(#[from] ReadHeaderError), | ||
| #[error(transparent)] | ||
| Io(#[from] io::Error), | ||
| } | ||
|
|
||
| impl<'r, R: Read + Seek + ?Sized> LazyBin<'r, R> { | ||
| pub fn from_reader(reader: &'r mut R) -> Result<Self, ReadErr> { | ||
| let (header, patch) = Header::from_reader(reader)?.into_parts(); | ||
| Ok(Self { | ||
| header, | ||
| patch, | ||
| reader, | ||
| }) | ||
| } | ||
|
|
||
| pub fn object(self, path: impl Into<BinHash>) -> Result<Option<LazyObject<'r, R>>, ReadErr> { | ||
| let path = path.into(); | ||
|
|
||
| self.reader.seek_relative( | ||
| i64::from(self.header.object_count) * i64::try_from(size_of::<BinHash>()).unwrap(), | ||
| )?; | ||
|
|
||
| for _ in 0..self.header.object_count { | ||
| let size = self.reader.read_u32::<LE>()?; | ||
| let cur_path = self.reader.read_bin_hash::<LE>()?; | ||
| if cur_path == path { | ||
| return Ok(Some(LazyObject { | ||
| prop_count: self.reader.read_u16::<LE>()?, | ||
| header: self.header, | ||
| reader: self.reader, | ||
| size, | ||
| })); | ||
| } | ||
| self.reader.seek_relative(i64::from(size) - 4)?; | ||
| } | ||
|
|
||
| Ok(None) | ||
| } | ||
| } | ||
| impl<'r, R: Read + Seek + ?Sized> LazyObject<'r, R> { | ||
| pub fn properties<'a>( | ||
| &'a mut self, | ||
| legacy: bool, | ||
| ) -> impl Iterator<Item = Result<(BinHash, PropertyValueEnum), ReadErr>> + use<'r, 'a, R> { | ||
| (0..self.prop_count).map(move |_| { | ||
| let name = self.reader.read_bin_hash::<LE>()?; | ||
| let kind = self.reader.read_property_kind(legacy)?; | ||
| let value = kind.read(self.reader, legacy)?; | ||
| Ok((name, value)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct LazyField<'r, R: Read + Seek + ?Sized> { | ||
| pub header: PropHeader, | ||
| pub reader: &'r mut R, | ||
|
|
||
| pub size: u32, | ||
| pub prop_count: u16, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,4 +78,6 @@ pub use tree::*; | |
| mod error; | ||
| pub use error::*; | ||
|
|
||
| pub mod header; | ||
| pub mod lazy; | ||
| pub mod traits; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of consuming
selfhere for no reason, we should be able to have it as&mut self.Right now you need to re-parse the header for each object you want to fetch lazily.
Fixable if we keep a lookup table of all entries offset and size, that way you can easily hop around the stream to the entry you need.