Skip to main content

unityrs_core/files/bundle/
write.rs

1// Ported from UnityPy 1.25.2 (MIT): UnityPy/files/BundleFile.py. See THIRD_PARTY_LICENSES.
2// Ported from UnityPy git 417d595 (MIT): UnityPy/files/BundleFile.py. See THIRD_PARTY_LICENSES.
3//! Bundle save and block/directory layout (`files/BundleFile.py:188-393`).
4//! Output and all temporary buffers charge the opened input's meter.
5
6use super::{BundleFile, BundleHeader, LayoutKind};
7use crate::codec::{Codec, encode};
8use crate::files::File;
9use crate::files::save::{SaveError, SaveLayout, SaveOptions, Writer, narrow, required};
10use crate::stream::Endian;
11use crate::version::FlagsClass;
12use bytes::Bytes;
13
14/// Bundle packer. Tuple flags follow the code's order, not its docstring.
15#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
16pub enum BundlePacker {
17    /// Store blocks and blocks-info with flags 0x40.
18    #[default]
19    None,
20    /// Keep the header flags and the first block's flags for every block.
21    Original,
22    /// UnityPy 1.25.2's (194, 2): HC level 9, 128 KiB chunks.
23    Lz4,
24    /// Raw LZMA1 with UnityPy's settings, (65, 1).
25    Lzma,
26    /// HEAD 417d595's HC level 9 packer and cumulative-output chunk rule.
27    /// For 1.25.2 HC use `Flags { data: 195, blocks: 3 }`.
28    Lz4Hc,
29    /// `(data_flag, block_info_flag)` as `save_fs` actually takes them.
30    Flags {
31        /// Header layout bits and blocks-info codec.
32        data: u32,
33        /// Data blocks' flags and codec.
34        blocks: u16,
35    },
36}
37
38/// Bundle save layout and compression. Children use the same layout and
39/// their default packer, as UnityPy calls their `save()` with no arguments.
40#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
41pub struct BundleSaveOptions {
42    /// Metadata layout, including the held serialized files' layout.
43    pub layout: SaveLayout,
44    /// The enclosing bundle's packer.
45    pub packer: BundlePacker,
46}
47
48const _: () = crate::send_sync::<BundlePacker>();
49const _: () = crate::send_sync::<BundleSaveOptions>();
50
51impl BundleFile {
52    /// Rebuild the bundle from its live ordered `files` dictionary.
53    /// Repeated names collapse to their final row at the first position.
54    /// Saving retains change flags and leaves the original input intact.
55    /// `None` provides canonical idempotence. `Original` adopts the first
56    /// loaded block's codec, so a stored first chunk can change the next
57    /// `Original` save's block layout, as in UnityPy.
58    ///
59    /// Format 9 and named HC follow HEAD 417d595. Earlier formats retain
60    /// 1.25.2's other packers. Formats above 9 refuse before output charges.
61    /// Legacy signatures ignore the packer, as UnityPy does.
62    pub fn save(&self, options: BundleSaveOptions) -> Result<Bytes, SaveError> {
63        self.save_impl(options, None, &self.header, None)
64    }
65
66    /// Save an ordered caller-supplied directory of already serialized children.
67    /// The caller owns and meters the names and payload snapshots.
68    pub fn save_with_entries(
69        &self,
70        options: BundleSaveOptions,
71        entries: &[(crate::RawStr, u32, Bytes)],
72    ) -> Result<Bytes, SaveError> {
73        self.save_impl(options, Some(entries), &self.header, None)
74    }
75
76    /// Save charged caller-owned directory and header snapshots. `mode` selects
77    /// the FS/legacy body API; `None` dispatches from the supplied signature.
78    pub fn save_with_snapshot(
79        &self,
80        options: BundleSaveOptions,
81        entries: &[(crate::RawStr, u32, Bytes)],
82        header: &BundleHeader,
83        block_flags: u16,
84        mode: Option<(u32, u16)>,
85    ) -> Result<Bytes, SaveError> {
86        if let Some((data, blocks)) = mode {
87            self.save_fs_impl(
88                data,
89                blocks,
90                SaveOptions {
91                    layout: options.layout,
92                },
93                false,
94                Some(entries),
95                header,
96            )
97        } else {
98            self.save_impl(options, Some(entries), header, Some(block_flags))
99        }
100    }
101
102    fn save_impl(
103        &self,
104        options: BundleSaveOptions,
105        entries: Option<&[(crate::RawStr, u32, Bytes)]>,
106        header: &BundleHeader,
107        block_flags: Option<u16>,
108    ) -> Result<Bytes, SaveError> {
109        Self::save_version(header)?;
110        let child_options = SaveOptions {
111            layout: options.layout,
112        };
113        if header.layout == LayoutKind::WebRaw {
114            return self.save_web_raw_impl(child_options, entries, header);
115        }
116        let (data, blocks) = if header.signature.as_bytes() == b"UnityFS" {
117            match options.packer {
118                BundlePacker::None => (64, 64),
119                BundlePacker::Original => {
120                    let flags = required(header.data_flags, "data flags")?;
121                    // Tier C writing: an empty bundle has no first block.
122                    let blocks = block_flags
123                        .map_or_else(|| self.blocks.first().map_or(flags, |b| b.flags), u32::from);
124                    (flags, narrow(blocks, "block flags")?)
125                }
126                BundlePacker::Lz4 => (194, 2),
127                BundlePacker::Lzma => (65, 1),
128                BundlePacker::Lz4Hc => (195, 3),
129                BundlePacker::Flags { data, blocks } => (data, blocks),
130            }
131        } else {
132            (64, 64)
133        };
134        self.save_fs_impl(
135            data,
136            blocks,
137            child_options,
138            options.packer == BundlePacker::Lz4Hc,
139            entries,
140            header,
141        )
142    }
143
144    /// Save the FS layout, with header/blocks-info flags first and data
145    /// block flags second. Encryption is removed, never emitted: exactly
146    /// the encryption mask's bits and each block's 0x100 bit are cleared.
147    /// This Tier C correction applies in both layout modes.
148    pub fn save_fs(
149        &self,
150        data_flag: u32,
151        block_info_flag: u16,
152        options: SaveOptions,
153    ) -> Result<Bytes, SaveError> {
154        self.save_fs_impl(
155            data_flag,
156            block_info_flag,
157            options,
158            false,
159            None,
160            &self.header,
161        )
162    }
163
164    /// Save the FS body using an ordered supplied child directory.
165    pub fn save_fs_with_entries(
166        &self,
167        data_flag: u32,
168        block_info_flag: u16,
169        options: SaveOptions,
170        entries: &[(crate::RawStr, u32, Bytes)],
171    ) -> Result<Bytes, SaveError> {
172        self.save_fs_impl(
173            data_flag,
174            block_info_flag,
175            options,
176            false,
177            Some(entries),
178            &self.header,
179        )
180    }
181
182    fn save_fs_impl(
183        &self,
184        data_flag: u32,
185        block_info_flag: u16,
186        options: SaveOptions,
187        named_hc: bool,
188        entries: Option<&[(crate::RawStr, u32, Bytes)]>,
189        header: &BundleHeader,
190    ) -> Result<Bytes, SaveError> {
191        Self::save_version(header)?;
192        if header.layout != LayoutKind::Fs {
193            return Err(SaveError::Unsupported("FS layout on a legacy bundle"));
194        }
195        let mask = match required(header.flags_class, "flags class")? {
196            FlagsClass::New => 0x1400,
197            FlagsClass::Old => 0x200,
198        };
199        let data_flag = data_flag & !mask;
200        let block_info_flag =
201            block_info_flag & !(narrow::<u16, _>(mask, "encryption mask")? | 0x100);
202        if data_flag & 0x40 == 0 {
203            return Err(SaveError::Missing("directory flag 0x40"));
204        }
205        let (file_data, directory) = if let Some(entries) = entries {
206            self.save_supplied_entries(options, entries)?
207        } else {
208            self.save_entries(options)?
209        };
210        let packed = encode::registered_chunks(
211            &file_data,
212            block_info_flag,
213            &self.context().codecs,
214            (named_hc || header.format >= 9).then_some(header.format),
215            self.meter(),
216        )?;
217        let mut info = Writer::new(self.meter(), Endian::Big)?;
218        info.bytes(&[0; 16])?;
219        info.count(packed.blocks.len())?;
220        for block in packed.blocks.as_slice() {
221            info.u32(block.uncompressed)?;
222            info.u32(block.compressed)?;
223            info.u16(block.flags)?;
224            if let Some(offset) = block.offset {
225                info.u64(offset)?;
226            }
227        }
228        info.bytes(&directory)?;
229        let info_len = narrow(info.len(), "blocks-info size")?;
230        let compressed = encode::registered_compress(
231            info.slice(),
232            Codec::from_flags(data_flag),
233            &self.context().codecs,
234            self.meter(),
235        )?;
236        let mut out = self.save_header(options, header)?;
237        let extra = usize::from(header.signature.as_bytes() != b"UnityFS");
238        let mut header_size = add(out.len(), 20 + extra)?;
239        if header.block_alignment {
240            header_size = aligned(header_size, 16)?;
241        }
242        let body_size = if data_flag & 0x80 != 0 {
243            let start = if data_flag & 0x200 != 0 {
244                aligned(header_size, 16)?
245            } else {
246                header_size
247            };
248            add(add(start, packed.data.len())?, compressed.len())?
249        } else {
250            let start = add(header_size, compressed.len())?;
251            let start = if data_flag & 0x200 != 0 {
252                aligned(start, 16)?
253            } else {
254                start
255            };
256            add(start, packed.data.len())?
257        };
258        out.i64(narrow(body_size, "bundle size")?)?;
259        out.u32(narrow(compressed.len(), "compressed blocks-info size")?)?;
260        out.u32(info_len)?;
261        out.u32(data_flag)?;
262        if extra != 0 {
263            out.bytes(&[0])?;
264        }
265        if header.block_alignment {
266            out.align(16)?;
267        }
268        if data_flag & 0x80 != 0 {
269            if data_flag & 0x200 != 0 {
270                out.align(16)?;
271            }
272            out.bytes(&packed.data)?;
273            out.bytes(&compressed)?;
274        } else {
275            out.bytes(&compressed)?;
276            if data_flag & 0x200 != 0 {
277                out.align(16)?;
278            }
279            out.bytes(&packed.data)?;
280        }
281        out.finish()
282    }
283
284    fn save_supplied_entries(
285        &self,
286        options: SaveOptions,
287        entries: &[(crate::RawStr, u32, Bytes)],
288    ) -> Result<(Bytes, Bytes), SaveError> {
289        let mut data = Writer::new(self.meter(), Endian::Big)?;
290        let mut directory = Writer::metadata(self.meter(), Endian::Big, options.layout)?;
291        directory.count(entries.len())?;
292        for (name, flags, bytes) in entries {
293            self.meter().count_decode(8)?;
294            directory.i64(narrow(data.len(), "entry offset")?)?;
295            directory.i64(narrow(bytes.len(), "entry size")?)?;
296            directory.u32(*flags)?;
297            directory.cstr(name.as_bytes())?;
298            data.bytes(bytes)?;
299        }
300        directory.end_metadata()?;
301        Ok((data.finish()?, directory.finish()?))
302    }
303
304    pub(super) fn save_version(header: &BundleHeader) -> Result<(), SaveError> {
305        if header.format > 9 {
306            return Err(SaveError::HeadTierWritesNotImplemented);
307        }
308        Ok(())
309    }
310
311    pub(super) fn save_header(
312        &self,
313        options: SaveOptions,
314        header: &BundleHeader,
315    ) -> Result<Writer, SaveError> {
316        let mut out = Writer::metadata(self.meter(), Endian::Big, options.layout)?;
317        out.cstr(header.signature.as_bytes())?;
318        out.u32(header.format)?;
319        out.cstr(header.version_player.as_bytes())?;
320        out.cstr(header.version_engine.as_bytes())?;
321        Ok(out)
322    }
323
324    fn save_entries(&self, options: SaveOptions) -> Result<(Bytes, Bytes), SaveError> {
325        let cabs = self.writeable_cabs()?;
326        for cab in cabs.as_slice() {
327            cab.validate_name(options.layout, false)?;
328        }
329        let mut data = Writer::new(self.meter(), Endian::Big)?;
330        let mut directory = Writer::metadata(self.meter(), Endian::Big, options.layout)?;
331        directory.count(add(self.files.len(), cabs.len())?)?;
332        for slot in &self.files {
333            self.meter().count_decode(8)?;
334            let row = required(self.directory.get(slot.dir_index), "directory row")?;
335            let bytes = self.save_child(slot.dir_index, options)?;
336            directory.i64(narrow(data.len(), "entry offset")?)?;
337            directory.i64(narrow(bytes.len(), "entry size")?)?;
338            directory.u32(row.flags.unwrap_or(0))?;
339            directory.cstr(slot.name.as_bytes())?;
340            data.bytes(&bytes)?;
341        }
342        for cab in cabs.as_slice() {
343            let bytes = cab.save()?;
344            directory.i64(narrow(data.len(), "entry offset")?)?;
345            directory.i64(narrow(bytes.len(), "entry size")?)?;
346            directory.u32(cab.flags())?;
347            directory.cstr(cab.name().as_bytes())?;
348            data.bytes(&bytes)?;
349        }
350        directory.end_metadata()?;
351        Ok((data.finish()?, directory.finish()?))
352    }
353
354    pub(super) fn save_child(
355        &self,
356        index: usize,
357        options: SaveOptions,
358    ) -> Result<Bytes, SaveError> {
359        match self.entry(index)? {
360            File::Raw(raw) => Ok(raw.data.clone()),
361            File::Serialized(sf) => sf.save(options),
362            File::Bundle(bundle) => bundle.save(BundleSaveOptions {
363                layout: options.layout,
364                packer: BundlePacker::None,
365            }),
366            File::Web(web) => web.save(crate::files::WebSaveOptions {
367                layout: options.layout,
368                packer: crate::files::WebPacker::None,
369            }),
370        }
371    }
372}
373
374pub(super) fn add(a: usize, b: usize) -> Result<usize, SaveError> {
375    a.checked_add(b).ok_or(SaveError::Range("bundle size"))
376}
377
378pub(super) fn aligned(n: usize, alignment: usize) -> Result<usize, SaveError> {
379    add(n, (alignment - n % alignment) % alignment)
380}