1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Ported from UnityPy 1.25.2 (MIT): UnityPy/files/SerializedFile.py. See THIRD_PARTY_LICENSES.
//! The options every file loaded through one environment shares.
//!
//! A [`FileContext`] is built once (usually by [`crate::env::EnvOptions`]) and
//! `Arc`'d into every file, so a `SerializedFile` deep inside a bundle reads
//! with the same version policy, providers, codecs, budget and limits as its
//! parent.
//!
//! Owner: the scaffold (frozen fields; implemented constructors).
use std::fmt;
use std::sync::Arc;
use crate::budget::{MemoryBudget, Unbounded};
use crate::codec::{BlockDecryptor, Codecs};
use crate::limits::Limits;
use crate::provider::ProviderChain;
use crate::version::VersionPolicy;
use crate::walk::{FloatMode, ReadOptions, WriteRules};
/// Options shared by every file of one load.
#[derive(Clone)]
#[non_exhaustive]
pub struct FileContext {
/// Optional mutable binding settings, snapshotted while each file loads.
pub runtime_config: Option<Arc<crate::config::RuntimeConfig>>,
/// UnityPy's `config.FALLBACK_UNITY_VERSION`, plus opt-in, reported overrides.
pub version: VersionPolicy,
/// How a float32 signalling NaN reaches the value (plan section 3.10).
pub floats: FloatMode,
/// The writer's alignment rules (plan section 3.11).
pub write: WriteRules,
/// Typetree sources for stripped files, keyed by (class, version).
pub providers: Arc<ProviderChain>,
/// Block codecs by compression type.
pub codecs: Arc<Codecs>,
/// Custom block decryption, overriding the built-in UnityCN decryptor.
pub decryptor: Option<Arc<dyn BlockDecryptor>>,
/// Load-local UnityCN key. Configure with `set_assetbundle_decrypt_key`.
pub(crate) assetbundle_decrypt_key: Option<[u8; 16]>,
/// Where decoded buffers are paid for.
pub budget: Arc<dyn MemoryBudget>,
/// Hard bounds.
pub limits: Limits,
/// Read the first AssetBundle object while a `SerializedFile` loads, and
/// fail the file when it fails, as UnityPy does (`SerializedFile.py:316-324`).
pub eager_asset_bundle: bool,
/// UnityPy config: parse trees at load, but omit them on save from v13.
/// Below v13 save retains the mandatory trees (Tier C writing).
pub serialized_file_parse_typetree: bool,
}
impl FileContext {
/// Whether the Python setting preserves the file's stored tree flag.
pub(crate) fn parse_typetree_enabled(&self) -> bool {
self.runtime_config
.as_ref()
.is_none_or(|config| config.snapshot().1)
}
/// UnityPy 1.25.2's behaviour: no fallback version, no providers, the
/// UnityPy codecs, no decryptor, no budget, `FloatMode::UnityPy`,
/// `WriteRules::UnityPy`, the AssetBundle read at load.
#[must_use]
pub fn unitypy() -> Self {
Self {
runtime_config: None,
version: VersionPolicy::default(),
floats: FloatMode::UnityPy,
write: WriteRules::UnityPy,
providers: Arc::new(ProviderChain::empty()),
codecs: Arc::new(Codecs::unitypy()),
decryptor: None,
assetbundle_decrypt_key: None,
budget: Arc::new(Unbounded),
limits: Limits::default(),
eager_asset_bundle: true,
serialized_file_parse_typetree: true,
}
}
/// Sets this load's UnityCN key (16 bytes, including for UTF-8 strings).
/// An invalid length leaves the previous key intact. No global state.
pub fn set_assetbundle_decrypt_key(
&mut self,
key: impl AsRef<[u8]>,
) -> Result<(), crate::Error> {
let key = key.as_ref();
let key = <[u8; 16]>::try_from(key)
.map_err(|_| crate::Error::UnityCnKeyLength { length: key.len() })?;
self.assetbundle_decrypt_key = Some(key);
Ok(())
}
/// The walker's options for this context.
#[must_use]
pub fn read_options(&self) -> ReadOptions {
ReadOptions {
floats: self.floats,
limits: self.limits,
}
}
/// Version policy with the binding's current fallback snapshot.
#[must_use]
pub fn version_policy(&self) -> VersionPolicy {
let mut policy = self.version.clone();
if let Some(config) = &self.runtime_config {
policy.fallback = config.snapshot().0;
}
policy
}
}
impl Default for FileContext {
fn default() -> Self {
Self::unitypy()
}
}
impl fmt::Debug for FileContext {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FileContext")
.field("version", &self.version)
.field("floats", &self.floats)
.field("write", &self.write)
.field("providers", &self.providers)
.field("decryptor", &self.decryptor.is_some())
.field("budget", &self.budget)
.field("limits", &self.limits)
.field("eager_asset_bundle", &self.eager_asset_bundle)
.finish_non_exhaustive()
}
}