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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use std::marker::PhantomData;
use std::mem::transmute;
use std::ops::{Deref, DerefMut};
use std::{fmt, ptr, slice};

use crate::{raw, Il2CppClass, Il2CppObject, Type, WrapRaw};

/// An il2cpp array
#[repr(transparent)]
pub struct Il2CppArray<T: Type>(raw::Il2CppArray, PhantomData<[T]>);

impl<T: Type> Il2CppArray<T> {
    /// Creates an array from an iterator
    pub fn new<'a, I>(items: I) -> &'a mut Self
    where
        I: IntoIterator<Item = T::Held<'a>>,
        I::IntoIter: ExactSizeIterator<Item = T::Held<'a>>,
    {
        let items = items.into_iter();
        let len = items.len();
        let arr = unsafe { raw::array_new(T::class().raw(), len) }.unwrap();
        let data_ptr =
            ((arr as *mut _ as isize) + (raw::kIl2CppSizeOfArray as isize)) as *mut T::Held<'a>;
        for (i, elem) in items.into_iter().enumerate() {
            unsafe {
                let ptr = data_ptr.add(i);
                ptr::write_unaligned(ptr, elem);
            }
        }
        unsafe { Self::wrap_mut(arr) }
    }

    /// Slice of values in the array
    pub fn as_slice(&self) -> &[T::Held<'_>] {
        let ptr = ((self as *const _ as isize) + (raw::kIl2CppSizeOfArray as isize))
            as *const T::Held<'_>;
        let len = self.len();
        unsafe { slice::from_raw_parts(ptr, len) }
    }

    /// Mutable slice of values in the array
    pub fn as_mut_slice(&mut self) -> &mut [T::Held<'_>] {
        let ptr =
            ((self as *mut _ as isize) + (raw::kIl2CppSizeOfArray as isize)) as *mut T::Held<'_>;
        let len = self.len();
        unsafe { slice::from_raw_parts_mut(ptr, len) }
    }

    /// Length of the array
    pub fn len(&self) -> usize {
        let raw = self.raw();
        let bounds: Option<&raw::Il2CppArrayBounds> = unsafe { transmute(raw.bounds) };
        match bounds {
            Some(bounds) => bounds.length,
            None => raw.max_length,
        }
    }

    /// Whether the array is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

unsafe impl<T: Type> WrapRaw for Il2CppArray<T> {
    type Raw = raw::Il2CppArray;
}

unsafe impl<T: Type> Type for Il2CppArray<T> {
    type Held<'a> = Option<&'a mut Self>;
    type HeldRaw = *mut Self;

    const NAMESPACE: &'static str = "System";
    const CLASS_NAME: &'static str = "Array";

    fn class() -> &'static Il2CppClass {
        let class = unsafe { raw::array_class_get(T::class().raw(), 1) };
        unsafe { Il2CppClass::wrap(class) }
    }

    fn matches_reference_argument(ty: &crate::Il2CppType) -> bool {
        ty.class().is_assignable_from(Self::class())
    }

    fn matches_value_argument(_: &crate::Il2CppType) -> bool {
        false
    }

    fn matches_reference_parameter(ty: &crate::Il2CppType) -> bool {
        Self::class().is_assignable_from(ty.class())
    }

    fn matches_value_parameter(_: &crate::Il2CppType) -> bool {
        false
    }
}

impl<T: Type> fmt::Debug for Il2CppArray<T>
where
    for<'a> T::Held<'a>: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Il2CppArray")
            .field(&self.as_slice())
            .finish()
    }
}

impl<T: Type> Deref for Il2CppArray<T> {
    type Target = Il2CppObject;

    fn deref(&self) -> &Self::Target {
        unsafe { Il2CppObject::wrap(&self.raw().obj) }
    }
}

impl<T: Type> DerefMut for Il2CppArray<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { Il2CppObject::wrap_mut(&mut self.raw_mut().obj) }
    }
}

#[cfg(feature = "serde")]
mod serde {
    use super::{fmt, ptr, raw, Il2CppArray, PhantomData, Type, WrapRaw};

    use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
    use serde::ser::{Serialize, Serializer};

    struct ArrayVisitor<T: Type>(PhantomData<Il2CppArray<T>>);

    impl<'de, T: Type> ArrayVisitor<T>
    where
        T::Held<'de>: Deserialize<'de>,
    {
        fn visit_self<A>(mut seq: A, len: usize) -> Result<&'de mut Il2CppArray<T>, A::Error>
        where
            A: SeqAccess<'de>,
        {
            let arr = unsafe { raw::array_new(T::class().raw(), len) }.unwrap();
            let data_ptr = ((arr as *mut _ as isize) + (raw::kIl2CppSizeOfArray as isize))
                as *mut T::Held<'de>;
            for i in 0..len {
                unsafe {
                    let ptr = data_ptr.add(i);
                    ptr::write_unaligned(ptr, seq.next_element()?.unwrap());
                }
            }
            Ok(unsafe { Il2CppArray::wrap_mut(arr) })
        }

        fn visit_vec<A>(mut seq: A) -> Result<Vec<T::Held<'de>>, A::Error>
        where
            A: SeqAccess<'de>,
        {
            let mut vec = Vec::new();
            while let Some(elem) = seq.next_element()? {
                vec.push(elem);
            }
            Ok(vec)
        }
    }

    impl<'de, T: Type> Visitor<'de> for ArrayVisitor<T>
    where
        T::Held<'de>: Deserialize<'de>,
    {
        type Value = &'de mut Il2CppArray<T>;

        fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            formatter.write_str("an array of C# compatible values")
        }

        fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
        where
            A: SeqAccess<'de>,
        {
            match seq.size_hint() {
                Some(len) => Self::visit_self(seq, len),
                None => Self::visit_vec(seq).map(Il2CppArray::new),
            }
        }
    }

    impl<'de, T: Type> Deserialize<'de> for &'de mut Il2CppArray<T>
    where
        T::Held<'de>: Deserialize<'de>,
    {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            deserializer.deserialize_seq(ArrayVisitor(Default::default()))
        }
    }

    impl<T: Type> Serialize for Il2CppArray<T>
    where
        for<'a> T::Held<'a>: Serialize,
    {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            <[T::Held<'_>] as Serialize>::serialize(self.as_slice(), serializer)
        }
    }
}