Skip to main content

unityrs_core/provider/
no_tree.rs

1// Ported from UnityPy 1.25.2 (MIT): UnityPy/helpers/Tpk.py. See THIRD_PARTY_LICENSES.
2//! [`NoTree`]: which of UnityPy's exceptions a class with no tree raises.
3
4/// Why no tree answers a class: the exception UnityPy raises there, the
5/// TPK's (`helpers/Tpk.py:42-55`, `:58-64`, `:472-481`), which a binding
6/// raises where the core reports no tree
7/// ([`crate::walk::ParseError::NoTypeTree`]).
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
9#[non_exhaustive]
10pub enum NoTree {
11    /// The TPK does not hold the class: `KeyError`.
12    UnknownClass,
13    /// No entry at or below the version, or the entry there marks the
14    /// class removed: `ValueError` "Could not find exact version".
15    NoEntry,
16    /// The entry picked has no release root: `AssertionError`.
17    NoReleaseRoot,
18    /// The file has no version (below format 7: `ObjectReader.version` is
19    /// `None`), which the TPK compares with its first entry:
20    /// `NotImplementedError` "Unsupported comparison".
21    NoVersion,
22}
23
24const _: () = crate::send_sync::<NoTree>();
25
26impl NoTree {
27    /// The name of the exception UnityPy raises.
28    #[must_use]
29    pub fn exception(self) -> &'static str {
30        match self {
31            Self::UnknownClass => "KeyError",
32            Self::NoEntry => "ValueError",
33            Self::NoReleaseRoot => "AssertionError",
34            Self::NoVersion => "NotImplementedError",
35        }
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use std::sync::Arc;
42
43    use super::NoTree;
44    use crate::provider::{ProviderChain, ProviderError, TreeRequest, TypeTreeProvider};
45    use crate::typetree::Tree;
46    use crate::version::UnityVersion;
47
48    /// A provider with no tree, that says `why` (or nothing) for every class.
49    #[derive(Debug)]
50    struct Says(Option<NoTree>);
51
52    impl TypeTreeProvider for Says {
53        fn name(&self) -> &'static str {
54            "says"
55        }
56
57        fn tree(&self, _: &TreeRequest<'_>) -> Result<Option<Arc<Tree>>, ProviderError> {
58            Ok(None)
59        }
60
61        fn why_none(&self, _: i32, _: Option<&UnityVersion>) -> Option<NoTree> {
62            self.0
63        }
64    }
65
66    /// A provider that keeps the trait's default.
67    #[derive(Debug)]
68    struct Silent;
69
70    impl TypeTreeProvider for Silent {
71        fn name(&self) -> &'static str {
72            "silent"
73        }
74
75        fn tree(&self, _: &TreeRequest<'_>) -> Result<Option<Arc<Tree>>, ProviderError> {
76            Ok(None)
77        }
78    }
79
80    fn chain(providers: Vec<Arc<dyn TypeTreeProvider>>) -> ProviderChain {
81        ProviderChain::new(providers)
82    }
83
84    /// The chain's reason is the last provider's that states one, the one a
85    /// fallback that raised in turn would leave; a provider that keeps the
86    /// default states none, and so does an empty chain.
87    #[test]
88    fn the_chains_reason_is_the_last_stated() {
89        let v = UnityVersion::from_packed(2 << 48);
90        let key = || -> Arc<dyn TypeTreeProvider> { Arc::new(Says(Some(NoTree::UnknownClass))) };
91        let value = || -> Arc<dyn TypeTreeProvider> { Arc::new(Says(Some(NoTree::NoEntry))) };
92        let why = |c: &ProviderChain| (c.why_none(7, Some(&v)), c.why_none(7, None));
93        let both = |w: Option<NoTree>| (w, w);
94        assert_eq!(why(&chain(vec![])), both(None));
95        assert_eq!(why(&chain(vec![Arc::new(Silent)])), both(None));
96        assert_eq!(
97            why(&chain(vec![key(), value()])),
98            both(Some(NoTree::NoEntry))
99        );
100        assert_eq!(
101            why(&chain(vec![value(), key()])),
102            both(Some(NoTree::UnknownClass))
103        );
104        let last_silent = chain(vec![key(), Arc::new(Silent)]);
105        assert_eq!(why(&last_silent), both(Some(NoTree::UnknownClass)));
106    }
107}