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
use const_combine::bounded::const_combine as combine;

use crate::class::ScriptClass;
use crate::types::{
    CName, EntityId, GameTime, ItemId, Opt, RedArray, RedString, Ref, ScriptRef, TweakDbId,
    Variant, WeakRef,
};

/// A trait for types that can be passed across the FFI boundary to the game engine without
/// any conversion.
///
/// # Safety
///
/// Implementations of this trait are only valid if the memory representation of Self
/// is idetical to the representation of type with name Self::NAME in-game.
pub unsafe trait NativeRepr {
    const NAME: &'static str;
}

unsafe impl NativeRepr for () {
    const NAME: &'static str = "Void";
}

unsafe impl NativeRepr for RedString {
    const NAME: &'static str = "String";
}

unsafe impl<A: NativeRepr> NativeRepr for RedArray<A> {
    const NAME: &'static str = combine!("array:", A::NAME);
}

unsafe impl<A: ScriptClass> NativeRepr for Ref<A> {
    const NAME: &'static str = combine!("handle:", A::NAME);
}

unsafe impl<A: ScriptClass> NativeRepr for WeakRef<A> {
    const NAME: &'static str = combine!("whandle:", A::NAME);
}

unsafe impl<'a, A: NativeRepr> NativeRepr for ScriptRef<'a, A> {
    const NAME: &'static str = combine!("script_ref:", A::NAME);
}

macro_rules! impl_native_repr {
    ($ty:ty, $name:literal) => {
        unsafe impl NativeRepr for $ty {
            const NAME: &'static str = $name;
        }
    };
    ($ty:ty, $name:literal, $native_name:literal) => {
        unsafe impl NativeRepr for $ty {
            const NAME: &'static str = $native_name;
        }
    };
}

impl_native_repr!(f32, "Float");
impl_native_repr!(f64, "Double");
impl_native_repr!(i64, "Int64");
impl_native_repr!(i32, "Int32");
impl_native_repr!(i16, "Int16");
impl_native_repr!(i8, "Int8");
impl_native_repr!(u64, "Uint64");
impl_native_repr!(u32, "Uint32");
impl_native_repr!(u16, "Uint16");
impl_native_repr!(u8, "Uint8");
impl_native_repr!(bool, "Bool");
impl_native_repr!(CName, "CName");
impl_native_repr!(TweakDbId, "TweakDBID");
impl_native_repr!(ItemId, "ItemID", "gameItemID");
impl_native_repr!(EntityId, "EntityID", "entEntityID");
impl_native_repr!(GameTime, "GameTime", "GameTime");
impl_native_repr!(Variant, "Variant", "Variant");

/// A trait for types that can be converted into a representation that can be passed across
/// the FFI boundary to the game.
pub trait IntoRepr: Sized {
    type Repr: NativeRepr;

    fn into_repr(self) -> Self::Repr;
}

impl<A: NativeRepr> IntoRepr for A {
    type Repr = A;

    #[inline]
    fn into_repr(self) -> Self::Repr {
        self
    }
}

impl IntoRepr for String {
    type Repr = RedString;

    #[inline]
    fn into_repr(self) -> Self::Repr {
        RedString::from(self)
    }
}

impl IntoRepr for &str {
    type Repr = RedString;

    #[inline]
    fn into_repr(self) -> Self::Repr {
        RedString::from(self)
    }
}

impl<A> IntoRepr for Vec<A>
where
    A: IntoRepr,
{
    type Repr = RedArray<A::Repr>;

    fn into_repr(self) -> Self::Repr {
        self.into_iter().map(IntoRepr::into_repr).collect()
    }
}

impl<A: NativeRepr + Default + PartialEq> IntoRepr for Opt<A> {
    type Repr = A;

    #[inline]
    fn into_repr(self) -> Self::Repr {
        match self {
            Self::Default => A::default(),
            Self::NonDefault(x) => x,
        }
    }
}

/// A trait for types that can be created from a representation passed across the FFI boundary.
pub trait FromRepr: Sized {
    type Repr: NativeRepr;

    fn from_repr(repr: Self::Repr) -> Self;
}

impl<A: NativeRepr> FromRepr for A {
    type Repr = A;

    #[inline]
    fn from_repr(repr: Self::Repr) -> Self {
        repr
    }
}

impl FromRepr for String {
    type Repr = RedString;

    #[inline]
    fn from_repr(repr: Self::Repr) -> Self {
        repr.into()
    }
}

impl<A> FromRepr for Vec<A>
where
    A: FromRepr,
{
    type Repr = RedArray<A::Repr>;

    fn from_repr(repr: Self::Repr) -> Self {
        repr.into_iter().map(FromRepr::from_repr).collect()
    }
}

impl<A: NativeRepr + Default + PartialEq> FromRepr for Opt<A> {
    type Repr = A;

    fn from_repr(repr: Self::Repr) -> Self {
        let repr = A::from_repr(repr);
        if repr == A::default() {
            return Self::Default;
        }
        Self::NonDefault(repr)
    }
}