Skip to main content

unityrs_core/files/
changed.rs

1// Ported from UnityPy 1.25.2 (MIT): UnityPy/files/File.py. See THIRD_PARTY_LICENSES.
2//! Parent propagation (`files/File.py:145-148`). Weak parents never keep an
3//! input alive merely because a child was edited.
4use super::{BundleFile, File, Parent, ParentKind, WebFile};
5use std::sync::atomic::Ordering;
6
7impl Parent {
8    pub(crate) fn mark_changed(&self) {
9        match &self.kind {
10            ParentKind::Bundle { bundle, .. } => {
11                if let Some(file) = bundle.upgrade() {
12                    file.mark_changed();
13                }
14            }
15            ParentKind::Web { web } => {
16                if let Some(file) = web.upgrade() {
17                    file.mark_changed();
18                }
19            }
20        }
21    }
22}
23
24macro_rules! changed {
25    ($ty:ty) => {
26        impl $ty {
27            /// Mark this container and all its live parent containers changed.
28            pub fn mark_changed(&self) {
29                if let Some(parent) = self.parent() {
30                    parent.mark_changed();
31                }
32                self.changed.store(true, Ordering::Release);
33            }
34            /// Whether this container or one of its children was marked changed.
35            #[must_use]
36            pub fn is_changed(&self) -> bool {
37                self.changed.load(Ordering::Acquire)
38            }
39        }
40    };
41}
42changed!(BundleFile);
43changed!(WebFile);
44
45impl File {
46    /// Mark a parsed file and its parents changed. Raw readers have no flag.
47    pub fn mark_changed(&self) {
48        match self {
49            Self::Serialized(f) => f.mark_changed(),
50            Self::Bundle(f) => f.mark_changed(),
51            Self::Web(f) => f.mark_changed(),
52            Self::Raw(_) => {}
53        }
54    }
55    /// Whether this parsed file was marked changed.
56    #[must_use]
57    pub fn is_changed(&self) -> bool {
58        match self {
59            Self::Serialized(f) => f.is_changed(),
60            Self::Bundle(f) => f.is_changed(),
61            Self::Web(f) => f.is_changed(),
62            Self::Raw(_) => false,
63        }
64    }
65}