summaryrefslogtreecommitdiff
path: root/ecs-macros/src/lib.rs
blob: 8bef0b681ac89ff82634fb652b2bdbfb4d56e21e (plain)
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use std::path::PathBuf as FsPathBuf;

use proc_macro::TokenStream;
use quote::{format_ident, quote, ToTokens};
use syn::spanned::Spanned;
use syn::{
    parse,
    Attribute,
    GenericParam,
    Generics,
    Ident,
    Item,
    ItemEnum,
    ItemStruct,
    ItemUnion,
    Path,
    Type,
};
use toml::value::{Table as TomlTable, Value as TomlValue};

macro_rules! syn_path {
    ($first_segment: ident $(::$segment: ident)*) => {
        ::syn::Path {
            leading_colon: None,
            segments: ::syn::punctuated::Punctuated::from_iter([
                syn_path_segment!($first_segment),
                $(syn_path_segment!($segment),)*
            ])
        }
    };
}

macro_rules! syn_path_segment {
    ($segment: ident) => {
        ::syn::PathSegment {
            ident: ::proc_macro2::Ident::new(
                stringify!($segment),
                ::proc_macro2::Span::call_site(),
            ),
            arguments: ::syn::PathArguments::None,
        }
    };
}

#[proc_macro_derive(Component, attributes(component))]
pub fn component_derive(input: TokenStream) -> TokenStream
{
    let item: TypeItem = parse::<Item>(input).unwrap().try_into().unwrap();

    let ComponentAttribute { ref_type, ref_mut_type } = item
        .attribute::<ComponentAttribute>("component")
        .unwrap_or_default();

    let item_ident = item.ident();

    let (impl_generics, type_generics, where_clause) = item.generics().split_for_impl();

    let ecs_path = find_engine_ecs_crate_path().unwrap_or_else(|| syn_path!(ecs));

    let (id_or_ids, get_id) = if !item.generics().params.is_empty() {
        let id_lut_ident =
            format_ident!("{}_ID_LUT", item_ident.to_string().to_uppercase());

        let id_lut = quote! {
            static #id_lut_ident: LazyLock<Mutex<HashMap<TypeId, Uid>>> =
                LazyLock::new(|| Mutex::new(HashMap::new()));
        };

        let generics = item.generics().params.iter().map(|param| match param {
            GenericParam::Type(type_param) => type_param.ident.clone(),
            GenericParam::Lifetime(_) => panic!("Lifetime generics are not supported"),
            GenericParam::Const(_) => panic!("Const generics are not supported"),
        });

        let get_id = quote! {
            *#id_lut_ident
                .try_lock()
                .unwrap()
                .entry(TypeId::of::<(#(#generics,)*)>())
                .or_insert_with(|| Uid::new_unique(UidKind::Component))
        };

        (id_lut, get_id)
    } else {
        let id_lazylock_ident =
            format_ident!("{}_ID", item_ident.to_string().to_uppercase());

        let id_lazylock = quote! {
            static #id_lazylock_ident: LazyLock<Uid> = LazyLock::new(|| {
                Uid::new_unique(UidKind::Component)
            });
        };

        let get_id = quote! {
            *#id_lazylock_ident
        };

        (id_lazylock, get_id)
    };

    let mod_ident = format_ident!(
        "__ecs_priv_component_impl_{}",
        item_ident.to_string().to_lowercase()
    );

    quote! {
        mod #mod_ident {
            use ::std::any::{Any, TypeId};
            use ::std::sync::{LazyLock, Mutex};
            use ::std::collections::HashMap;

            use #ecs_path::component::Component;
            use #ecs_path::system::ComponentRefMut;
            use #ecs_path::system::ComponentRef;
            use #ecs_path::uid::{Uid, Kind as UidKind};
            use #ecs_path::system::Input as SystemInput;
            use #ecs_path::type_name::TypeName;

            use super::*;

            #id_or_ids

            impl #impl_generics Component for #item_ident #type_generics
            #where_clause
            {
                type Component = Self;

                type RefMut<'component> = #ref_mut_type;
                type Ref<'component> = #ref_type;

                fn id() -> Uid
                {
                    #get_id
                }

                fn self_id(&self) -> Uid
                {
                    Self::id()
                }

                fn as_any_mut(&mut self) -> &mut dyn Any
                {
                    self
                }

                fn as_any(&self) -> &dyn Any
                {
                    self
                }
            }

            impl #impl_generics SystemInput for #item_ident #type_generics
            #where_clause
            {
            }

            impl #impl_generics TypeName for #item_ident #type_generics
            #where_clause
            {
                fn type_name(&self) -> &'static str
                {
                    std::any::type_name::<Self>()
                }
            }
        }
    }
    .into()
}

#[proc_macro_derive(Sole, attributes(sole))]
pub fn sole_derive(input: TokenStream) -> TokenStream
{
    let item: TypeItem = parse::<Item>(input).unwrap().try_into().unwrap();

    let item_ident = item.ident();

    let sole_attr = item.attribute::<SoleAttribute>("sole").unwrap_or_default();

    let drop_last = sole_attr.drop_last;

    let (impl_generics, type_generics, where_clause) = item.generics().split_for_impl();

    let ecs_path = find_engine_ecs_crate_path().unwrap_or_else(|| syn_path!(ecs));

    quote! {
        impl #impl_generics #ecs_path::sole::Sole for #item_ident #type_generics
        #where_clause
        {
            fn drop_last(&self) -> bool
            {
                #drop_last
            }

            fn as_any_mut(&mut self) -> &mut dyn std::any::Any
            {
                self
            }

            fn as_any(&self) -> &dyn std::any::Any
            {
                self
            }
        }

        impl #impl_generics #ecs_path::type_name::TypeName for #item_ident #type_generics
        #where_clause
        {
            fn type_name(&self) -> &'static str
            {
                std::any::type_name::<Self>()
            }
        }
    }
    .into()
}

trait FromAttribute: Sized
{
    fn from_attribute(attribute: &Attribute) -> Result<Self, syn::Error>;
}

enum TypeItem
{
    Struct(ItemStruct),
    Enum(ItemEnum),
    Union(ItemUnion),
}

impl TypeItem
{
    fn ident(&self) -> &Ident
    {
        match self {
            Self::Struct(struct_item) => &struct_item.ident,
            Self::Enum(enum_item) => &enum_item.ident,
            Self::Union(union_item) => &union_item.ident,
        }
    }

    fn attribute<Attr: FromAttribute>(&self, attr_ident: &str) -> Option<Attr>
    {
        let item_attrs = match &self {
            Self::Struct(struct_item) => &struct_item.attrs,
            &Self::Enum(enum_item) => &enum_item.attrs,
            &Self::Union(union_item) => &union_item.attrs,
        };

        let mut attr: Option<&Attribute> = None;

        for item_attr in item_attrs {
            if attr.is_some() {
                panic!("Expected only one {} attribute", attr_ident);
            }

            if item_attr.path().get_ident()? == attr_ident {
                attr = Some(item_attr);
            }
        }

        Some(Attr::from_attribute(attr?).unwrap())
    }

    fn generics(&self) -> &Generics
    {
        match self {
            Self::Struct(struct_item) => &struct_item.generics,
            Self::Enum(enum_item) => &enum_item.generics,
            Self::Union(union_item) => &union_item.generics,
        }
    }
}

impl TryFrom<Item> for TypeItem
{
    type Error = syn::Error;

    fn try_from(item: Item) -> Result<Self, Self::Error>
    {
        match item {
            Item::Struct(struct_item) => Ok(Self::Struct(struct_item)),
            Item::Enum(enum_item) => Ok(Self::Enum(enum_item)),
            Item::Union(union_item) => Ok(Self::Union(union_item)),
            _ => Err(syn::Error::new(
                item.span(),
                "Expected a struct, a enum or a union",
            )),
        }
    }
}

impl ToTokens for TypeItem
{
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream)
    {
        match self {
            Self::Struct(struct_item) => struct_item.to_tokens(tokens),
            Self::Enum(enum_item) => enum_item.to_tokens(tokens),
            Self::Union(union_item) => union_item.to_tokens(tokens),
        }
    }
}

#[derive(Debug, Default)]
struct SoleAttribute
{
    drop_last: bool,
}

impl FromAttribute for SoleAttribute
{
    fn from_attribute(attribute: &Attribute) -> Result<Self, syn::Error>
    {
        let mut drop_last = false;

        attribute.parse_nested_meta(|meta| {
            if meta
                .path
                .get_ident()
                .is_some_and(|flag| flag == "drop_last")
            {
                drop_last = true;

                return Ok(());
            }

            Err(meta.error("Unrecognized token"))
        })?;

        Ok(Self { drop_last })
    }
}

fn find_engine_ecs_crate_path() -> Option<Path>
{
    let cargo_manifest_dir = FsPathBuf::from(std::env::var("CARGO_MANIFEST_DIR").ok()?);

    let cargo_crate_name = std::env::var("CARGO_CRATE_NAME").ok()?;
    let cargo_pkg_name = std::env::var("CARGO_PKG_NAME").ok()?;

    if cargo_pkg_name == "ecs" && cargo_crate_name != "ecs" {
        // Macro is used by a ecs crate example/test/benchmark
        return Some(syn_path!(ecs));
    }

    let crate_manifest = std::fs::read_to_string(cargo_manifest_dir.join("Cargo.toml"))
        .ok()?
        .parse::<TomlTable>()
        .expect("Failed to parse crate manifest file");

    let package = match crate_manifest.get("package")? {
        TomlValue::Table(package) => Some(package),
        _ => None,
    }?;

    let package_name = match package.get("name")? {
        TomlValue::String(package_name) => Some(package_name),
        _ => None,
    }?;

    if package_name == "ecs" {
        return Some(syn_path!(crate));
    }

    let crate_dependencies = match crate_manifest.get("dependencies")? {
        TomlValue::Table(dependencies) => Some(dependencies),
        _ => None,
    }?;

    crate_dependencies.iter().find_map(|(crate_dep_name, _)| {
        if crate_dep_name == "engine" {
            return Some(syn_path!(engine::ecs));
        }

        None
    })
}

#[derive(Debug)]
struct ComponentAttribute
{
    ref_type: proc_macro2::TokenStream,
    ref_mut_type: proc_macro2::TokenStream,
}

impl FromAttribute for ComponentAttribute
{
    fn from_attribute(attribute: &Attribute) -> Result<Self, syn::Error>
    {
        let mut ref_type: Option<Type> = None;
        let mut ref_mut_type: Option<Type> = None;

        attribute.parse_nested_meta(|meta| {
            let Some(flag) = meta.path.get_ident() else {
                return Err(meta.error("Not a single identifier"));
            };

            if flag == "ref_type" {
                let value = meta.value()?;

                ref_type = Some(value.parse::<Type>()?);

                return Ok(());
            } else if flag == "ref_mut_type" {
                let value = meta.value()?;

                ref_mut_type = Some(value.parse::<Type>()?);

                return Ok(());
            }

            Err(meta.error("Unrecognized token"))
        })?;

        Ok(Self {
            ref_type: ref_type
                .map(|ref_type| ref_type.into_token_stream())
                .unwrap_or_else(|| Self::default().ref_type),
            ref_mut_type: ref_mut_type
                .map(|ref_mut_type| ref_mut_type.into_token_stream())
                .unwrap_or_else(|| Self::default().ref_mut_type),
        })
    }
}

impl Default for ComponentAttribute
{
    fn default() -> Self
    {
        Self {
            ref_type: quote! { ComponentRef<'component, Self> },
            ref_mut_type: quote! { ComponentRefMut<'component, Self> },
        }
    }
}