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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use std::ffi::CString;
use std::marker::PhantomData;

use sealed::sealed;

use crate::invocable::{GlobalMetadata, MethodMetadata};
use crate::systems::RttiSystemMut;
use crate::types::{CName, NativeClass};
use crate::{class_kind, NativeRepr, RttiSystem, ScriptClass};

/// A list of exports to register with the game.
#[derive(Debug)]
pub struct ExportList<H, T> {
    head: H,
    tail: T,
}

impl<H, T> ExportList<H, T> {
    /// Create a new `ExportList` with the given head and tail.
    pub const fn new(head: H, tail: T) -> Self {
        Self { head, tail }
    }
}

/// A trait for types to be exported to the game.
#[sealed]
pub trait Exportable {
    fn register(&self);
    fn post_register(&self);
}

#[sealed]
impl<H, T> Exportable for ExportList<H, T>
where
    H: Exportable,
    T: Exportable,
{
    #[inline]
    fn register(&self) {
        self.head.register();
        self.tail.register();
    }

    #[inline]
    fn post_register(&self) {
        self.head.post_register();
        self.tail.post_register();
    }
}

/// A type representing an empty list of exports.
#[derive(Debug)]
pub struct ExportNil;

#[sealed]
impl Exportable for ExportNil {
    #[inline]
    fn register(&self) {}

    #[inline]
    fn post_register(&self) {}
}

/// A single class export.
/// This can be used to define a custom class to be exported to the game.
/// This type should not be used for structs, use [`StructExport`] instead.
#[derive(Debug)]
pub struct ClassExport<C: 'static> {
    base: &'static str,
    methods: &'static [MethodMetadata<C>],
    static_methods: &'static [GlobalMetadata],
}

impl<C: ScriptClass> ClassExport<C> {
    pub fn builder() -> ClassExportBuilder<C> {
        ClassExportBuilder {
            base: "IScriptable",
            methods: &[],
            static_methods: &[],
        }
    }
}

#[sealed]
impl<C: Default + Clone + ScriptClass<Kind = class_kind::Native>> Exportable for ClassExport<C> {
    fn register(&self) {
        let mut rtti = RttiSystemMut::get();
        let name_cstr = CString::new(C::NAME).expect("name should be valid");
        let base = rtti
            .get_class(CName::new(self.base))
            .expect("base should exist");
        let handle = NativeClass::<C>::new_handle(&name_cstr, Some(base));
        rtti.register_class(handle);
    }

    fn post_register(&self) {
        let (converted_methods, converted_static_methods) = {
            let rtti_ro = RttiSystem::get();
            let class = rtti_ro
                .get_class(CName::new(C::NAME))
                .expect("class should exist");
            let converted_methods = self
                .methods
                .iter()
                .map(|m| m.to_rtti(class))
                .collect::<Vec<_>>();
            let converted_static_methods = self
                .static_methods
                .iter()
                .map(|m| m.to_rtti_static_method(class))
                .collect::<Vec<_>>();
            (converted_methods, converted_static_methods)
        };

        let mut rtti_rw = RttiSystemMut::get();
        let class = rtti_rw
            .get_class(CName::new(C::NAME))
            .expect("class should exist");

        for method in converted_methods {
            class.add_method(method);
        }
        for static_method in converted_static_methods {
            class.add_static_method(static_method);
        }
    }
}

/// A builder for [`ClassExport`].
#[derive(Debug)]
pub struct ClassExportBuilder<C: 'static> {
    base: &'static str,
    methods: &'static [MethodMetadata<C>],
    static_methods: &'static [GlobalMetadata],
}

impl<C> ClassExportBuilder<C> {
    /// Set the base class of the class to be exported.
    /// This is set to `IScriptable` by default.
    pub const fn base(mut self, base: &'static str) -> Self {
        self.base = base;
        self
    }

    /// Set the methods of the class to be exported.
    /// See the [`methods!`](crate::methods) macro for a convenient way to define methods.
    pub const fn methods(mut self, methods: &'static [MethodMetadata<C>]) -> Self {
        self.methods = methods;
        self
    }

    /// Set the static methods of the class to be exported.
    /// See the [`static_methods!`](crate::static_methods) macro for a convenient way to define methods.
    pub const fn static_methods(mut self, static_methods: &'static [GlobalMetadata]) -> Self {
        self.static_methods = static_methods;
        self
    }

    /// Build the final [`ClassExport`] instance.
    pub const fn build(self) -> ClassExport<C> {
        ClassExport {
            base: self.base,
            methods: self.methods,
            static_methods: self.static_methods,
        }
    }
}

/// A single struct export.
/// This can be used to define a custom struct to be exported to the game.
#[derive(Debug)]
pub struct StructExport<C> {
    base: Option<&'static str>,
    static_methods: &'static [GlobalMetadata],
    _phantom: PhantomData<fn() -> C>,
}

impl<C> StructExport<C> {
    pub fn builder() -> StructExportBuilder<C> {
        StructExportBuilder {
            base: None,
            static_methods: &[],
            _phantom: PhantomData,
        }
    }
}

#[sealed]
impl<C: Default + Clone + NativeRepr> Exportable for StructExport<C> {
    fn register(&self) {
        let mut rtti = RttiSystemMut::get();
        let name_cstr = CString::new(C::NAME).expect("name should be valid");
        let base = self
            .base
            .map(|base| &*rtti.get_class(CName::new(base)).expect("base should exist"));
        let handle = NativeClass::<C>::new_handle(&name_cstr, base);
        rtti.register_class(handle);
    }

    fn post_register(&self) {
        let converted_static_methods = {
            let rtti_ro = RttiSystem::get();
            let class = rtti_ro
                .get_class(CName::new(C::NAME))
                .expect("class should exist");
            self.static_methods
                .iter()
                .map(|m| m.to_rtti_static_method(class))
                .collect::<Vec<_>>()
        };

        let mut rtti_rw = RttiSystemMut::get();
        let class = rtti_rw
            .get_class(CName::new(C::NAME))
            .expect("class should exist");

        for static_method in converted_static_methods {
            class.add_static_method(static_method);
        }
    }
}

/// A builder for [`StructExport`].
#[derive(Debug)]
pub struct StructExportBuilder<C> {
    base: Option<&'static str>,
    static_methods: &'static [GlobalMetadata],
    _phantom: PhantomData<fn() -> C>,
}

impl<C> StructExportBuilder<C> {
    /// Set the base type of the struct to be exported.
    /// Structs do not have a base type by default.
    pub const fn base(mut self, base: &'static str) -> Self {
        self.base = Some(base);
        self
    }

    /// Set the static methods of the struct to be exported.
    /// See the [`static_methods!`](crate::static_methods) macro for a convenient way to define methods.
    pub const fn static_methods(mut self, static_methods: &'static [GlobalMetadata]) -> Self {
        self.static_methods = static_methods;
        self
    }

    /// Build the final [`StructExport`] instance.
    pub const fn build(self) -> StructExport<C> {
        StructExport {
            base: self.base,
            static_methods: self.static_methods,
            _phantom: PhantomData,
        }
    }
}

/// A single global function export.
#[derive(Debug)]
pub struct GlobalExport(pub GlobalMetadata);

#[sealed]
impl Exportable for GlobalExport {
    #[inline]
    fn register(&self) {}

    fn post_register(&self) {
        let converted = self.0.to_rtti();

        let mut rtti = RttiSystemMut::get();
        rtti.register_function(converted);
    }
}

/// Creates a list of exports to be registered within the game's RTTI system.
///
/// # Example
/// ```rust
/// use std::cell::Cell;
///
/// use red4ext_rs::{ClassExport, Exportable, GlobalExport, ScriptClass, class_kind, exports, methods, global};
/// use red4ext_rs::types::IScriptable;
///
/// fn exports() -> impl Exportable {
///     exports![
///         GlobalExport(global!(c"GlobalExample", global_example)),
///         ClassExport::<MyClass>::builder()
///            .base("IScriptable")
///            .methods(methods![
///                c"Value" => MyClass::value,
///                c"SetValue" => MyClass::set_value,
///            ])
///            .build(),
///     ]
/// }
///
/// fn global_example() -> String {
///   "Hello, world!".to_string()
/// }
///
/// #[derive(Debug, Default, Clone)]
/// #[repr(C)]
/// struct MyClass {
///     // You must include the base native class in your Rust struct.
///     base: IScriptable,
///     value: Cell<i32>,
/// }
///
/// impl MyClass {
///    fn value(&self) -> i32 {
///       self.value.get()
///    }
///
///    fn set_value(&self, value: i32) {
///       self.value.set(value)
///    }
/// }
///
/// unsafe impl ScriptClass for MyClass {
///    const NAME: &'static str = "MyClass";
///    type Kind = class_kind::Native;
/// }
#[macro_export]
macro_rules! exports {
    [$export:expr, $($tt:tt)*] => {
        $crate::ExportList::new($export, exports!($($tt)*))
    };
    [$export:expr] => {
        $crate::ExportList::new($export, $crate::ExportNil)
    };
    [] => { $crate::ExportNil }
}

/// Define a list of methods to register with the game. Usually used in conjuction with
/// [`exports!`].
#[macro_export]
macro_rules! methods {
    [$( $($mod:ident)* $name:literal => $ty:ident::$id:ident),*$(,)?] => {
        const { &[$($crate::method!($($mod)* $name, $ty::$id)),*] }
    };
}

/// Define a list of static methods to register with the game. Usually used in conjuction with
/// [`exports!`].
#[macro_export]
macro_rules! static_methods {
    [$( $($mod:ident)* $name:literal => $ty:ident::$id:ident),*$(,)?] => {
        const { &[$($crate::global!($($mod)* $name, $ty::$id)),*] }
    };
}