From 2ee0946577a1da58aa7d922fb1e56e132b7a3b96 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Fri, 8 Aug 2025 10:39:37 +0800 Subject: [PATCH] Add circus pck detect support --- src/scripts/circus/archive/pck.rs | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/scripts/circus/archive/pck.rs b/src/scripts/circus/archive/pck.rs index 7c86dd4..fad4a73 100644 --- a/src/scripts/circus/archive/pck.rs +++ b/src/scripts/circus/archive/pck.rs @@ -102,6 +102,10 @@ impl ScriptBuilder for PckArchiveBuilder { writer, files, encoding, config, )?)) } + + fn is_this_format(&self, _filename: &str, buf: &[u8], buf_len: usize) -> Option { + is_this_format(&buf[..buf_len]).ok() + } } #[derive(Debug, Clone, StructPack, StructUnpack)] @@ -434,3 +438,32 @@ impl<'a, T: Write + Seek> Seek for PckArchiveFile<'a, T> { Ok(self.pos as u64) } } + +pub fn is_this_format(buf: &[u8]) -> Result { + let mut reader = MemReaderRef::new(buf); + let count = reader.read_u32()? as usize; + let mut score = if count > 0 && count < 0x40000 { + 5 + } else { + 0 + }; + let avail_count = ((buf.len() - 4) / 0x8).min(count); + score += ((avail_count / 2).min(10)) as u8; + if avail_count == 0 { + return Err(anyhow::anyhow!("No valid entries found in PCK archive")); + } + let mut prev_off = reader.read_u32()?; + let mut prev_size = reader.read_u32()?; + let mut index = 1; + while index < avail_count { + let off = reader.read_u32()?; + let size = reader.read_u32()?; + if off < prev_off || prev_off + prev_size != off { + return Err(anyhow::anyhow!("Invalid offset.")); + } + prev_off = off; + prev_size = size; + index += 1; + } + Ok(score) +}