Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/ltk_meta/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use miette::Diagnostic;

use crate::header::ReadHeaderError;

use super::property::Kind;

#[derive(Debug, thiserror::Error, Diagnostic)]
Expand All @@ -15,6 +17,9 @@ pub enum Error {
#[error("Invalid size - expected {0}, got {1} bytes")]
InvalidSize(u64, u64),

#[error("Error reading header - {0}")]
ReadHeaderError(#[from] ReadHeaderError),

#[error("Container type {0:?} cannot be nested!")]
InvalidNesting(Kind),
#[error("Invalid map key type {0:?}, only primitive types can be used as keys.")]
Expand Down
196 changes: 196 additions & 0 deletions crates/ltk_meta/src/header.rs
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");
}
}
93 changes: 93 additions & 0 deletions crates/ltk_meta/src/lazy.rs
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> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of consuming self here 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.

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,
}
2 changes: 2 additions & 0 deletions crates/ltk_meta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,6 @@ pub use tree::*;
mod error;
pub use error::*;

pub mod header;
pub mod lazy;
pub mod traits;
13 changes: 8 additions & 5 deletions crates/ltk_meta/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ pub use builder::Builder;

mod object;
use ltk_hash::BinHash;
use ltk_primitives::PrefixString;
pub use object::{BinObject, Builder as ObjectBuilder};

mod read;
mod write;

pub use write::*;

#[cfg(test)]
mod tests;

use indexmap::IndexMap;

use crate::property::NoMeta;
use crate::{header::PropHeader, property::NoMeta};

/// The complete contents of a League of Legends property bin file.
///
Expand Down Expand Up @@ -56,7 +59,7 @@ pub struct Bin<M = NoMeta> {
///
/// Property bins can depend on other property bins in a similar fashion
/// to importing code libraries.
pub dependencies: Vec<String>,
pub dependencies: Vec<PrefixString<u16>>,

/// Data overrides (currently not fully implemented).
data_overrides: Vec<()>,
Expand Down Expand Up @@ -91,10 +94,10 @@ impl<M> Bin<M> {
/// ```
pub fn new(
objects: impl IntoIterator<Item = BinObject<M>>,
dependencies: impl IntoIterator<Item = impl Into<String>>,
dependencies: impl IntoIterator<Item = impl Into<PrefixString<u16>>>,
) -> Self {
Self {
version: 3,
version: PropHeader::LATEST_VERSION,
is_override: false,
objects: objects
.into_iter()
Expand Down Expand Up @@ -168,7 +171,7 @@ impl<M> Bin<M> {
}

/// Adds a dependency to the tree.
pub fn add_dependency(&mut self, dependency: impl Into<String>) {
pub fn add_dependency(&mut self, dependency: impl Into<PrefixString<u16>>) {
self.dependencies.push(dependency.into());
}

Expand Down
Loading
Loading