use crate::cas::{self, Cas, CasRaw};
use crate::{Result, bad, capacity, past_end};
use primase_foundations::bytes::Endian;
use primase_foundations::compress::oodle::Oodle;
use primase_foundations::compress::{Compressor, Decompressor};
use sha1::{Digest, Sha1};
const W: &str = "fbmod";
pub const MAGIC: [u8; 8] = *b"FROSTY\0\x01";
pub const VERSION: u32 = 6;
const FIXED: usize = 8 + 4 + 8 + 4 + 1;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum EntryKind {
Embedded,
Ebx,
Res,
Chunk,
}
impl EntryKind {
pub fn id(self) -> u8 {
match self {
Self::Embedded => 0,
Self::Ebx => 1,
Self::Res => 2,
Self::Chunk => 3,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ResInfo {
pub res_type: u32,
pub rid: u64,
pub meta: Vec<u8>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ChunkInfo {
pub range_start: u32,
pub range_end: u32,
pub logical_offset: u32,
pub logical_size: u32,
pub h32: u32,
pub first_mip: i32,
pub added_bundles: Vec<u32>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Body {
Embedded,
Ebx,
Res(ResInfo),
Chunk(ChunkInfo),
}
impl Body {
pub fn kind(&self) -> EntryKind {
match self {
Self::Embedded => EntryKind::Embedded,
Self::Ebx => EntryKind::Ebx,
Self::Res(_) => EntryKind::Res,
Self::Chunk(_) => EntryKind::Chunk,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Stored {
pub index: usize,
pub size: u64,
pub flags: u8,
pub handler: u32,
pub user: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Entry {
pub name: String,
pub stored: Option<Stored>,
pub bundles: Vec<u32>,
pub body: Body,
}
impl Entry {
pub fn kind(&self) -> EntryKind {
self.body.kind()
}
pub fn index(&self) -> Option<usize> {
self.stored.as_ref().map(|s| s.index)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Fbmod {
pub version: u32,
pub profile: String,
pub game_version: u32,
pub title: String,
pub author: String,
pub category: String,
pub mod_version: String,
pub description: String,
pub link: String,
pub entries: Vec<Entry>,
pub payloads: Vec<Vec<u8>>,
}
struct Cur<'a> {
c: primase_foundations::bytes::Cursor<'a>,
}
impl<'a> Cur<'a> {
fn take(&mut self, n: usize) -> Result<&'a [u8]> {
self.c.take(n).map_err(|e| past_end(W, e, self.c.bytes().len()))
}
fn arr<const N: usize>(&mut self) -> Result<[u8; N]> {
self.c.array().map_err(|e| past_end(W, e, self.c.bytes().len()))
}
fn u8(&mut self) -> Result<u8> {
self.c.u8().map_err(|e| past_end(W, e, self.c.bytes().len()))
}
fn u32(&mut self) -> Result<u32> {
self.c
.u32(Endian::Little)
.map_err(|e| past_end(W, e, self.c.bytes().len()))
}
fn i32(&mut self) -> Result<i32> {
self.c
.i32(Endian::Little)
.map_err(|e| past_end(W, e, self.c.bytes().len()))
}
fn u64(&mut self) -> Result<u64> {
self.c
.u64(Endian::Little)
.map_err(|e| past_end(W, e, self.c.bytes().len()))
}
fn position(&self) -> usize {
self.c.position()
}
fn count(&mut self, min_bytes: usize) -> Result<usize> {
let n = self.u32()? as usize;
capacity(W, n, min_bytes, self.c.remaining())
}
fn string(&mut self) -> Result<String> {
let at = self.position();
let rest = self.c.bytes().get(at..).unwrap_or_default();
let mut parts = rest.splitn(2, |&b| b == 0);
let (Some(text), Some(_)) = (parts.next(), parts.next()) else {
return Err(bad(W, at, "unterminated string, expected a zero byte"));
};
let s = std::str::from_utf8(text).map_err(|_| bad(W, at, "string is not UTF-8"))?;
self.take(text.len().saturating_add(1))?;
Ok(s.to_owned())
}
fn u32s(&mut self) -> Result<Vec<u32>> {
let n = self.count(4)?;
(0..n).map(|_| self.u32()).collect()
}
}
fn sha1(data: &[u8]) -> [u8; 20] {
Sha1::digest(data).into()
}
pub fn read(bytes: &[u8]) -> Result<Fbmod> {
let mut c = Cur {
c: primase_foundations::bytes::Cursor::new(bytes),
};
if c.arr::<8>()? != MAGIC {
return Err(bad(W, 0, "not a FROSTY mod"));
}
let version = c.u32()?;
if version != VERSION {
return Err(bad(W, 8, format!("version {version}, expected {VERSION}")));
}
let table_at = usize::try_from(c.u64()?).map_err(|_| bad(W, 12, "payload table offset"))?;
let payload_count = c.u32()? as usize;
let plen = usize::from(c.u8()?);
let profile = std::str::from_utf8(c.take(plen)?)
.map_err(|_| bad(W, FIXED, "profile is not UTF-8"))?
.to_owned();
let game_version = c.u32()?;
let mut details = Vec::with_capacity(6);
for _ in 0..6 {
details.push(c.string()?);
}
let [title, author, category, mod_version, description, link]: [String; 6] =
details.try_into().map_err(|_| bad(W, c.position(), "details"))?;
let n = c.count(10)?;
let mut entries = Vec::with_capacity(n);
for _ in 0..n {
let at = c.position();
let kind = c.u8()?;
let index = c.i32()?;
let name = c.string()?;
let index = match usize::try_from(index) {
Ok(i) if i < payload_count => Some(i),
_ if index == -1 => None,
_ => return Err(bad(W, at, format!("payload {index} of {payload_count}: {name}"))),
};
let (stored, sha) = match index {
Some(index) => {
let sha = c.arr::<20>()?;
let size = c.u64()?;
let flags = c.u8()?;
let handler = c.u32()?;
let user = c.string()?;
(
Some(Stored {
index,
size,
flags,
handler,
user,
}),
Some(sha),
)
}
None => (None, None),
};
let bundles = c.u32s()?;
let body = match kind {
0 => Body::Embedded,
1 => Body::Ebx,
2 => {
let res_type = c.u32()?;
let rid = c.u64()?;
let len = c.u32()? as usize;
Body::Res(ResInfo {
res_type,
rid,
meta: c.take(len)?.to_vec(),
})
}
3 => Body::Chunk(ChunkInfo {
range_start: c.u32()?,
range_end: c.u32()?,
logical_offset: c.u32()?,
logical_size: c.u32()?,
h32: c.u32()?,
first_mip: c.i32()?,
added_bundles: c.u32s()?,
}),
k => return Err(bad(W, at, format!("unknown entry kind {k}: {name}"))),
};
entries.push((
Entry {
name,
stored,
bundles,
body,
},
sha,
at,
));
}
if c.position() != table_at {
return Err(bad(
W,
c.position(),
format!("entries end here, payload table at {table_at:#x}"),
));
}
let mut spans = Vec::with_capacity(capacity(
W,
payload_count,
16,
bytes.len().saturating_sub(c.position()),
)?);
for _ in 0..payload_count {
spans.push((c.u64()?, c.u64()?));
}
let mut payloads = Vec::with_capacity(payload_count);
let mut next = 0u64;
for (i, (offset, size)) in spans.into_iter().enumerate() {
if offset != next {
return Err(bad(
W,
c.position(),
format!("payload {i} at {offset:#x}, expected {next:#x}"),
));
}
next = offset
.checked_add(size)
.ok_or_else(|| bad(W, c.position(), format!("payload {i} size")))?;
let size = usize::try_from(size).map_err(|_| bad(W, c.position(), format!("payload {i} size")))?;
payloads.push(c.take(size)?.to_vec());
}
if c.position() != bytes.len() {
return Err(bad(
W,
c.position(),
format!(
"{} bytes after the last payload, expected the file to end there",
bytes.len().saturating_sub(c.position())
),
));
}
let mut out = Vec::with_capacity(entries.len());
for (entry, sha, at) in entries {
if let (Some(s), Some(sha)) = (&entry.stored, sha)
&& payloads.get(s.index).map(|p| sha1(p)) != Some(sha)
{
return Err(bad(
W,
at,
format!("SHA-1 of payload {} differs: {}", s.index, entry.name),
));
}
out.push(entry);
}
Ok(Fbmod {
version,
profile,
game_version,
title,
author,
category,
mod_version,
description,
link,
entries: out,
payloads,
})
}
struct Out(Vec<u8>);
impl Out {
fn u32(&mut self, v: u32) {
self.0.extend(v.to_le_bytes());
}
fn string(&mut self, s: &str) -> Result<()> {
if s.contains('\0') {
return Err(bad(W, self.0.len(), format!("string {s:?} holds a zero byte")));
}
self.0.extend(s.as_bytes());
self.0.push(0);
Ok(())
}
fn u32s(&mut self, v: &[u32]) -> Result<()> {
self.u32(u32::try_from(v.len()).map_err(|_| bad(W, self.0.len(), "list length"))?);
v.iter().for_each(|&x| self.u32(x));
Ok(())
}
}
pub fn write(m: &Fbmod) -> Result<Vec<u8>> {
if m.version != VERSION {
return Err(bad(W, 8, format!("version {}, expected {VERSION}", m.version)));
}
let mut o = Out(Vec::new());
o.0.extend(MAGIC);
o.u32(m.version);
o.0.extend(0u64.to_le_bytes());
o.u32(u32::try_from(m.payloads.len()).map_err(|_| bad(W, 0, "payload count"))?);
let profile = u8::try_from(m.profile.len()).map_err(|_| bad(W, FIXED - 1, "profile over 255 bytes"))?;
o.0.push(profile);
o.0.extend(m.profile.as_bytes());
o.u32(m.game_version);
for s in [
&m.title,
&m.author,
&m.category,
&m.mod_version,
&m.description,
&m.link,
] {
o.string(s)?;
}
o.u32(u32::try_from(m.entries.len()).map_err(|_| bad(W, 0, "entry count"))?);
for e in &m.entries {
o.0.push(e.kind().id());
match &e.stored {
Some(s) => {
let payload = m.payloads.get(s.index).ok_or_else(|| {
bad(
W,
o.0.len(),
format!("payload {} of {}: {}", s.index, m.payloads.len(), e.name),
)
})?;
o.u32(u32::try_from(s.index).map_err(|_| bad(W, o.0.len(), "payload index"))?);
o.string(&e.name)?;
o.0.extend(sha1(payload));
o.0.extend(s.size.to_le_bytes());
o.0.push(s.flags);
o.u32(s.handler);
o.string(&s.user)?;
}
None => {
o.0.extend((-1i32).to_le_bytes());
o.string(&e.name)?;
}
}
o.u32s(&e.bundles)?;
match &e.body {
Body::Embedded | Body::Ebx => {}
Body::Res(r) => {
o.u32(r.res_type);
o.0.extend(r.rid.to_le_bytes());
o.u32(u32::try_from(r.meta.len()).map_err(|_| bad(W, o.0.len(), "metadata length"))?);
o.0.extend(&r.meta);
}
Body::Chunk(c) => {
for v in [
c.range_start,
c.range_end,
c.logical_offset,
c.logical_size,
c.h32,
] {
o.u32(v);
}
o.0.extend(c.first_mip.to_le_bytes());
o.u32s(&c.added_bundles)?;
}
}
}
let table_at = o.0.len() as u64;
let mut offset = 0u64;
for p in &m.payloads {
o.0.extend(offset.to_le_bytes());
o.0.extend((p.len() as u64).to_le_bytes());
offset = offset.saturating_add(p.len() as u64);
}
for p in &m.payloads {
o.0.extend(p);
}
if let Some(slot) = o.0.get_mut(12..20) {
slot.copy_from_slice(&table_at.to_le_bytes());
}
Ok(o.0)
}
impl Fbmod {
pub fn add_payload(&mut self, raw: &[u8]) -> Result<usize> {
let encoded = CasRaw.compress(raw)?;
if let Some(i) = self.payloads.iter().position(|p| *p == encoded) {
return Ok(i);
}
self.payloads.push(encoded);
Ok(self.payloads.len().saturating_sub(1))
}
pub fn drop_unused_payloads(&mut self) -> usize {
let mut used = vec![false; self.payloads.len()];
for s in self.entries.iter().filter_map(|e| e.stored.as_ref()) {
if let Some(u) = used.get_mut(s.index) {
*u = true;
}
}
let mut renumbered = Vec::with_capacity(used.len());
let mut next = 0usize;
for &u in &used {
renumbered.push(next);
next = next.saturating_add(usize::from(u));
}
let mut keep = used.iter();
self.payloads.retain(|_| keep.next().copied().unwrap_or(false));
for s in self.entries.iter_mut().filter_map(|e| e.stored.as_mut()) {
if let Some(&n) = renumbered.get(s.index) {
s.index = n;
}
}
used.len().saturating_sub(next)
}
}
impl Fbmod {
pub fn payload(&self, i: usize, oodle: Option<&Oodle>) -> Result<Vec<u8>> {
let raw = self
.payloads
.get(i)
.ok_or_else(|| bad("fbmod", 0, format!("payload {i} of {}", self.payloads.len())))?;
let size = cas::blocks(raw)?
.iter()
.try_fold(0usize, |s, b| s.checked_add(b.decoded_size))
.ok_or_else(|| bad("fbmod", 0, format!("payload {i} size")))?;
Ok(Cas { oodle }.decompress(raw, size)?)
}
}