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
//! Code generation for ArcDPS plugins.

mod abi;
mod callbacks;
mod export;
mod parse;

#[cfg(feature = "extras")]
mod extras;

use cfg_if::cfg_if;
use proc_macro2::{Span, TokenStream};
use quote::{quote, quote_spanned};
use syn::{Expr, Ident, LitStr};

#[cfg(feature = "extras")]
use extras::ExtrasGen;

/// Creates plugin exports for ArcDPS.
#[proc_macro]
pub fn export(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = syn::parse_macro_input!(item as ArcDpsGen);

    let export = input.build_export();

    let extras_funcs = {
        cfg_if! {
            if #[cfg(feature = "extras")] {
                let name = input.gen_name_cstr();
                input.extras.build(name)
            } else {
                quote! {}
            }
        }
    };

    let result = quote! {
        mod __arcdps_addon_export {
            use super::*;

            #export

            #extras_funcs
        }
    };

    result.into()
}

/// Helper to generate code.
///
/// Holds information about macro input.
pub(crate) struct ArcDpsGen {
    name: Option<LitStr>,
    sig: Expr,
    init: Option<Expr>,
    release: Option<Expr>,
    update_url: Option<Expr>,

    raw_combat: Option<Expr>,
    combat: Option<Expr>,

    raw_combat_local: Option<Expr>,
    combat_local: Option<Expr>,

    raw_imgui: Option<Expr>,
    imgui: Option<Expr>,

    raw_options_end: Option<Expr>,
    options_end: Option<Expr>,

    raw_options_windows: Option<Expr>,
    options_windows: Option<Expr>,

    raw_wnd_filter: Option<Expr>,
    wnd_filter: Option<Expr>,

    raw_wnd_nofilter: Option<Expr>,
    wnd_nofilter: Option<Expr>,

    #[cfg(feature = "extras")]
    extras: ExtrasGen,
}

impl Default for ArcDpsGen {
    fn default() -> Self {
        Self {
            name: None,
            sig: Expr::Verbatim(TokenStream::new()),
            init: None,
            release: None,
            update_url: None,

            raw_combat: None,
            combat: None,

            raw_combat_local: None,
            combat_local: None,

            raw_imgui: None,
            imgui: None,

            raw_options_end: None,
            options_end: None,

            raw_options_windows: None,
            options_windows: None,

            raw_wnd_filter: None,
            wnd_filter: None,

            raw_wnd_nofilter: None,
            wnd_nofilter: None,

            #[cfg(feature = "extras")]
            extras: ExtrasGen::default(),
        }
    }
}

/// Helper to represent callback information.
pub(crate) struct CallbackInfo {
    pub function: TokenStream,
    pub value: TokenStream,
}

impl CallbackInfo {
    /// Creates a new callback info from token streams.
    pub fn new(function: TokenStream, value: TokenStream) -> Self {
        Self { function, value }
    }

    /// Creates a new callback info with no contents.
    pub fn empty() -> Self {
        Self::new(TokenStream::new(), TokenStream::new())
    }

    /// Helper to build a callback.
    ///
    /// `raw` is the value of the raw callback if passed to the macro.
    /// `safe` is the value of the safe callback if passed to the macro.
    /// `name` is the name of the abstract wrapper function for the safe version.
    /// `wrapper` generates the abstract wrapper if needed.
    pub fn build(
        raw: Option<&Expr>,
        safe: Option<&Expr>,
        name: &str,
        wrapper: impl FnOnce(&Ident, &Expr, Span) -> TokenStream,
    ) -> CallbackInfo {
        Self::build_optional(raw, safe, name, wrapper)
            .unwrap_or_else(|| CallbackInfo::new(quote! {}, quote! { ::std::option::Option::None }))
    }

    /// Helper to build an optional callback.
    ///
    /// See `build` for more info.
    pub fn build_optional(
        raw: Option<&Expr>,
        safe: Option<&Expr>,
        name: &str,
        wrapper: impl FnOnce(&Ident, &Expr, Span) -> TokenStream,
    ) -> Option<CallbackInfo> {
        if let Some(raw) = raw {
            let span = syn::Error::new_spanned(raw, "").span();
            let value = quote_spanned!(span=> ::std::option::Option::Some((#raw) as _) );

            Some(CallbackInfo::new(quote! {}, value))
        } else if let Some(safe) = safe {
            let name = Ident::new(name, Span::call_site());
            let span = syn::Error::new_spanned(safe, "").span();
            let func = wrapper(&name, safe, span);
            let value = quote_spanned!(span=> ::std::option::Option::Some(self::#name as _) );

            Some(CallbackInfo::new(func, value))
        } else {
            None
        }
    }

    /// Returns the callback info as tuple.
    pub fn into_tuple(self) -> (TokenStream, TokenStream) {
        (self.function, self.value)
    }
}