pub mod bundle;
pub(crate) mod cab;
pub use cab::WriteableCab;
mod changed;
pub mod save;
pub mod serialized;
pub use save::{SaveError, SaveLayout, SaveOptions};
mod teardown;
pub mod web;
use std::sync::{Arc, OnceLock, Weak};
use bytes::Bytes;
pub use bundle::{
BlockInfo, BundleCounts, BundleFile, BundleHeader, BundlePacker, BundleSaveOptions, DirEntry,
FileSlot, LayoutKind,
};
pub use serialized::{
External, ObjectInfo, ScriptRef, SerializedFile, SerializedHeader, SerializedType,
};
pub use web::{WebFile, WebPacker, WebSaveOptions};
use crate::detect::{FileType, ParsedAs, check_entry_type, parsed_as, probe_container};
use crate::error::{Error, LimitError, LimitKind, Unsupported};
use crate::limits::{Charge, ChargeError, Meter, MeteredVec, heap_cost};
use crate::options::FileContext;
use crate::raw::RawStr;
use crate::stream::ReaderKind;
#[derive(Clone, Debug)]
pub enum File {
Serialized(Arc<SerializedFile>),
Bundle(Arc<BundleFile>),
Web(Arc<WebFile>),
Raw(Arc<RawFile>),
}
impl File {
pub(crate) fn mark_dependency_root(&self) {
self.set_dependency_path(MeteredVec::new(self.meter()));
}
fn set_dependency_path(&self, path: MeteredVec<usize>) {
let identity = match self {
Self::Serialized(file) => &file.dependency_path,
Self::Bundle(file) => &file.dependency_path,
Self::Web(file) => &file.dependency_path,
Self::Raw(_) => return,
};
let set = identity.0.set(path);
debug_assert!(set.is_ok());
}
#[must_use]
pub fn container_files(&self) -> &[FileSlot] {
match self {
Self::Bundle(b) => b.files(),
Self::Web(w) => w.files(),
_ => &[],
}
}
#[must_use]
pub fn container_directory(&self) -> &[DirEntry] {
match self {
Self::Bundle(b) => b.directory(),
Self::Web(w) => w.directory(),
_ => &[],
}
}
pub fn container_entry(&self, i: usize) -> Result<File, Error> {
match self {
Self::Bundle(b) => b.entry(i),
Self::Web(w) => w.entry(i),
_ => Err(Error::Malformed {
what: "container entry",
detail: "leaf file".into(),
}),
}
}
pub fn child_container(&self, i: usize) -> Result<Option<Self>, Error> {
if let Some(file) = self.parsed_container_entry(i) {
return Ok(matches!(file, Self::Bundle(_) | Self::Web(_)).then(|| file.clone()));
}
let container = match self {
Self::Bundle(bundle) => crate::env::registry::probe_bundle_entry(bundle, i)?,
Self::Web(web) => probe_container(check_entry_type(
&web.entry_bytes(i)?,
&web.context().limits,
web.meter(),
))?,
_ => false,
};
if container {
self.container_entry(i).map(Some)
} else {
Ok(None)
}
}
#[must_use]
pub fn meter(&self) -> &Arc<Meter> {
match self {
Self::Serialized(f) => f.meter(),
Self::Bundle(f) => f.meter(),
Self::Web(f) => f.meter(),
Self::Raw(f) => f.meter(),
}
}
pub(crate) fn parsed_container_entry(&self, i: usize) -> Option<&Self> {
let entries = match self {
Self::Bundle(b) => &b.entries,
Self::Web(w) => &w.entries,
_ => return None,
};
entries.get(i)?.get()?.as_ref().ok()
}
#[must_use]
pub fn file_type(&self) -> FileType {
match self {
Self::Serialized(_) => FileType::AssetsFile,
Self::Bundle(_) => FileType::BundleFile,
Self::Web(_) => FileType::WebFile,
Self::Raw(f) => f.file_type(),
}
}
}
#[derive(Default)]
struct DependencyPath(OnceLock<MeteredVec<usize>>);
impl DependencyPath {
fn parse_child(
&self,
meter: &Arc<Meter>,
index: usize,
parse: impl FnOnce() -> Result<File, Error>,
) -> Result<File, Error> {
let Some(parent) = self.0.get() else {
return parse();
};
let file = parse()?;
if matches!(file, File::Raw(_)) {
return Ok(file);
}
meter.count_decode(parent.len() as u64 + 1)?;
let mut path = MeteredVec::with_capacity(meter, parent.len().saturating_add(1))?;
path.extend_from_slice(parent.as_slice())?;
path.push(index)?;
file.set_dependency_path(path);
Ok(file)
}
}
#[derive(Debug)]
pub struct RawFile {
pub name: RawStr,
pub data: Bytes,
ty: FileType,
reader: ReaderKind,
charge: Charge,
}
impl RawFile {
pub fn new(
name: RawStr,
data: Bytes,
ty: FileType,
meter: &Arc<Meter>,
) -> Result<Arc<Self>, ChargeError> {
Self::with_reader(name, data, ty, meter, ReaderKind::Memory)
}
pub fn with_reader(
name: RawStr,
data: Bytes,
ty: FileType,
meter: &Arc<Meter>,
reader: ReaderKind,
) -> Result<Arc<Self>, ChargeError> {
let charge = meter.charge(heap_cost(
u64::try_from(std::mem::size_of::<Self>() + 16).unwrap_or(u64::MAX),
))?;
Ok(Arc::new(Self {
name,
data,
ty,
reader,
charge,
}))
}
#[must_use]
pub fn reader_kind(&self) -> ReaderKind {
self.reader
}
#[must_use]
pub fn meter(&self) -> &Arc<Meter> {
self.charge.meter()
}
#[must_use]
pub fn file_type(&self) -> FileType {
self.ty
}
}
#[derive(Clone, Debug)]
pub struct Parent {
pub name: RawStr,
pub kind: ParentKind,
pub depth: u32,
pub pinned: u64,
}
#[derive(Clone, Debug)]
pub enum ParentKind {
#[non_exhaustive]
Bundle {
version_engine: RawStr,
bundle: Weak<BundleFile>,
dir_index: usize,
},
Web {
web: Weak<WebFile>,
},
}
pub fn parse_file(
name: RawStr,
data: Bytes,
ty: FileType,
parent: Option<Parent>,
meter: &Arc<Meter>,
ctx: &Arc<FileContext>,
) -> Result<File, Error> {
parse_file_with_reader(name, data, ty, parent, meter, ctx, ReaderKind::Memory)
}
pub fn parse_file_with_reader(
name: RawStr,
data: Bytes,
ty: FileType,
parent: Option<Parent>,
meter: &Arc<Meter>,
ctx: &Arc<FileContext>,
reader: ReaderKind,
) -> Result<File, Error> {
parse_file_at_depth(name, data, ty, parent, meter, ctx, reader, 0)
}
#[allow(
clippy::too_many_arguments,
reason = "internal reader API plus independent ZIP depth"
)]
pub(crate) fn parse_file_at_depth(
name: RawStr,
data: Bytes,
ty: FileType,
parent: Option<Parent>,
meter: &Arc<Meter>,
ctx: &Arc<FileContext>,
reader: ReaderKind,
source_depth: u32,
) -> Result<File, Error> {
parse_file_with_state(
name,
data,
ty,
parent,
meter,
ctx,
reader,
source_depth,
&mut None,
)
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn parse_file_with_state(
name: RawStr,
data: Bytes,
ty: FileType,
parent: Option<Parent>,
meter: &Arc<Meter>,
ctx: &Arc<FileContext>,
reader: ReaderKind,
source_depth: u32,
endian: &mut Option<crate::stream::Endian>,
) -> Result<File, Error> {
let kind = parsed_as(ty, name.as_bytes(), parent.is_some());
if matches!(kind, ParsedAs::BundleFile | ParsedAs::WebFile)
&& parent.as_ref().map_or(source_depth, |p| p.depth) > ctx.limits.max_container_depth
{
return Err(Error::Limit(LimitError::refused(
LimitKind::ContainerDepth,
u64::from(parent.as_ref().map_or(source_depth, |p| p.depth)),
u64::from(ctx.limits.max_container_depth),
)));
}
Ok(match kind {
ParsedAs::SerializedFile => File::Serialized(SerializedFile::parse_with_state(
name, data, parent, meter, ctx, reader, endian,
)?),
ParsedAs::BundleFile => File::Bundle(BundleFile::open_at_depth(
name,
data,
parent,
meter,
ctx,
reader,
source_depth,
)?),
ParsedAs::WebFile => File::Web(WebFile::open_with_state(
name,
data,
parent,
meter,
ctx,
reader,
source_depth,
endian,
)?),
ParsedAs::Zip => return Err(Error::Unsupported(Unsupported::Zip)),
ParsedAs::Raw | ParsedAs::Error => {
File::Raw(RawFile::with_reader(name, data, ty, meter, reader)?)
}
})
}