dragon_blade 2.1.0

A transpiler from Rust to Overwatch Workshop
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use std::collections::HashSet;

use crate::lang_types::*;

static mut COMPILER_ARRAY: Option<Compiler> = None;

pub(crate) struct Compiler {
    pub allow_action: bool,
    pub allow_rule: bool,

    pub scripts: Vec<String>,
    pub active_script: usize,

    pub global_variables: HashSet<&'static str>,
    pub player_variables: HashSet<&'static str>,
    pub subroutines: HashSet<&'static str>,
}
impl Compiler {
    pub fn new() -> Compiler {
        Compiler {
            allow_action: false,
            allow_rule: false,
            scripts: Vec::new(),
            active_script: 0,
            global_variables: HashSet::new(),
            player_variables: HashSet::new(),
            subroutines: HashSet::new(),
        }
    }

    pub fn push_action(&mut self, value: String) {
        if !self.allow_action {
            panic!("Action is not allowed here!");
        }
        let script = &mut self.scripts[self.active_script];
        script.push_str(&format!("\t\t{};\n", value));
    }
}

pub(crate) fn get_compiler() -> &'static mut Compiler {
    unsafe {
        if COMPILER_ARRAY.is_none() {
            COMPILER_ARRAY = Some(Compiler::new());
        }
        COMPILER_ARRAY.as_mut().unwrap()
    }
}

pub struct Script {
    id: usize,
    name: String,
}

// ------------------------------

#[doc(hidden)]
pub struct SlotLiteral(u8);
#[allow(non_snake_case)]
pub fn Slot(slot: u8) -> SlotLiteral {
    if slot >= 12 {
        panic!("Slot {} is out of range!", slot);
    }
    SlotLiteral(slot)
}
impl Cast<Self> for SlotLiteral {
    fn cast(self) -> Self {
        self
    }
}
impl Compile for SlotLiteral {
    fn from_string(_: String) -> Self {
        unimplemented!()
    }

    fn compile(&self) -> &str {
        unimplemented!()
    }

    fn take(self) -> String {
        format!("Slot {}", self.0)
    }
}

pub struct Everyone;

#[doc(hidden)]
pub trait PlayerSelector {
    fn compile(&self) -> String;
}
impl PlayerSelector for HeroLiteral {
    fn compile(&self) -> String {
        Compile::compile(self).to_string()
    }
}
impl PlayerSelector for SlotLiteral {
    fn compile(&self) -> String {
        Compile::compile(self).to_string()
    }
}
impl PlayerSelector for Everyone {
    fn compile(&self) -> String {
        "All".to_string()
    }
}

pub enum Event {
    OngoingGlobal(Box<dyn PlayerSelector>),
    OngoingPlayer(TeamLiteral, Box<dyn PlayerSelector>),
    PlayerEarnedElimination(TeamLiteral, Box<dyn PlayerSelector>),
    PlayerDealtFinalBlow(TeamLiteral, Box<dyn PlayerSelector>),
    PlayerDealtDamage(TeamLiteral, Box<dyn PlayerSelector>),
    PlayerTookDamage(TeamLiteral, Box<dyn PlayerSelector>),
    PlayerDied(TeamLiteral, Box<dyn PlayerSelector>),
    PlayerDealtHealing(TeamLiteral, Box<dyn PlayerSelector>),
    PlayerReceivedHealing(TeamLiteral, Box<dyn PlayerSelector>),
    PlayerJoined(TeamLiteral, Box<dyn PlayerSelector>),
    PlayerLeft(TeamLiteral, Box<dyn PlayerSelector>),
    SubroutineBody(Subroutine, Box<dyn PlayerSelector>),
    PlayerDealtKnockback(TeamLiteral, Box<dyn PlayerSelector>),
    PlayerReceivedKnockback(TeamLiteral, Box<dyn PlayerSelector>),
}
#[allow(non_snake_case)]
pub fn OngoingGlobal() -> Event {
    Event::OngoingGlobal(Box::new(Everyone))
}
#[allow(non_snake_case)]
pub fn OngoingPlayer<S: PlayerSelector + 'static>(team: TeamLiteral, selector: S) -> Event {
    Event::OngoingPlayer(team, Box::new(selector))
}
#[allow(non_snake_case)]
pub fn PlayerEarnedElimination<S: PlayerSelector + 'static>(
    team: TeamLiteral,
    selector: S,
) -> Event {
    Event::PlayerEarnedElimination(team, Box::new(selector))
}
#[allow(non_snake_case)]
pub fn PlayerDealtFinalBlow<S: PlayerSelector + 'static>(team: TeamLiteral, selector: S) -> Event {
    Event::PlayerDealtFinalBlow(team, Box::new(selector))
}
#[allow(non_snake_case)]
pub fn PlayerDealtDamage<S: PlayerSelector + 'static>(team: TeamLiteral, selector: S) -> Event {
    Event::PlayerDealtDamage(team, Box::new(selector))
}
#[allow(non_snake_case)]
pub fn PlayerTookDamage<S: PlayerSelector + 'static>(team: TeamLiteral, selector: S) -> Event {
    Event::PlayerTookDamage(team, Box::new(selector))
}
#[allow(non_snake_case)]
pub fn PlayerDied<S: PlayerSelector + 'static>(team: TeamLiteral, selector: S) -> Event {
    Event::PlayerDied(team, Box::new(selector))
}
#[allow(non_snake_case)]
pub fn PlayerDealtHealing<S: PlayerSelector + 'static>(team: TeamLiteral, selector: S) -> Event {
    Event::PlayerDealtHealing(team, Box::new(selector))
}
#[allow(non_snake_case)]
pub fn PlayerReceivedHealing<S: PlayerSelector + 'static>(team: TeamLiteral, selector: S) -> Event {
    Event::PlayerReceivedHealing(team, Box::new(selector))
}
#[allow(non_snake_case)]
pub fn PlayerJoined<S: PlayerSelector + 'static>(team: TeamLiteral, selector: S) -> Event {
    Event::PlayerJoined(team, Box::new(selector))
}
#[allow(non_snake_case)]
pub fn PlayerLeft<S: PlayerSelector + 'static>(team: TeamLiteral, selector: S) -> Event {
    Event::PlayerLeft(team, Box::new(selector))
}
#[allow(non_snake_case)]
pub fn SubroutineBody(subroutine: Subroutine) -> Event {
    Event::SubroutineBody(subroutine, Box::new(Everyone))
}
#[allow(non_snake_case)]
pub fn PlayerDealtKnockback<S: PlayerSelector + 'static>(team: TeamLiteral, selector: S) -> Event {
    Event::PlayerDealtKnockback(team, Box::new(selector))
}
#[allow(non_snake_case)]
pub fn PlayerReceivedKnockback<S: PlayerSelector + 'static>(
    team: TeamLiteral,
    selector: S,
) -> Event {
    Event::PlayerReceivedKnockback(team, Box::new(selector))
}

impl Event {
    /// returns a tuple with the event, team and selector as strings
    pub(crate) fn take(self) -> Vec<String> {
        match self {
            Event::OngoingGlobal(_) => vec!["Ongoing - Global".to_string()],
            Event::OngoingPlayer(t, s) => {
                vec!["Ongoing - Each Player".to_string(), t.take(), s.compile()]
            }
            Event::PlayerEarnedElimination(t, s) => {
                vec![
                    "Player Earned Elimination".to_string(),
                    t.take(),
                    s.compile(),
                ]
            }
            Event::PlayerDealtFinalBlow(t, s) => {
                vec!["Player Dealt Final Blow".to_string(), t.take(), s.compile()]
            }
            Event::PlayerDealtDamage(t, s) => {
                vec!["Player Dealt Damage".to_string(), t.take(), s.compile()]
            }
            Event::PlayerTookDamage(t, s) => {
                vec!["Player Took Damage".to_string(), t.take(), s.compile()]
            }
            Event::PlayerDied(t, s) => vec!["Player Died".to_string(), t.take(), s.compile()],
            Event::PlayerDealtHealing(t, s) => {
                vec!["Player Dealt Healing".to_string(), t.take(), s.compile()]
            }
            Event::PlayerReceivedHealing(t, s) => {
                vec!["Player Received Healing".to_string(), t.take(), s.compile()]
            }
            Event::PlayerJoined(t, s) => {
                vec!["Player Joined Match".to_string(), t.take(), s.compile()]
            }
            Event::PlayerLeft(t, s) => {
                vec!["Player Left Match".to_string(), t.take(), s.compile()]
            }
            Event::SubroutineBody(s, _) => vec!["Subroutine".to_string(), s.take()],
            Event::PlayerDealtKnockback(t, s) => {
                vec!["Player Dealt Knockback".to_string(), t.take(), s.compile()]
            }
            Event::PlayerReceivedKnockback(t, s) => {
                vec![
                    "Player Received Knockback".to_string(),
                    t.take(),
                    s.compile(),
                ]
            }
        }
    }
}

pub struct Conditions(Vec<Boolean>);
pub fn conditions<const C: usize>(conditions: [Boolean; C]) -> Conditions {
    Conditions(conditions.into())
}

pub struct Rule {
    pub name: &'static str,
    pub event: Event,
    pub conditions: Conditions,
    pub actions: fn(),
}

impl Script {
    pub fn new_unnamed() -> Self {
        let mut compiler = get_compiler();

        if compiler.scripts.is_empty() {
            compiler.allow_rule = true;
        }

        let id = compiler.scripts.len();
        compiler.scripts.push(String::new());
        compiler.active_script = id;

        Script {
            id,
            name: format!("unnamed script {}", id + 1),
        }
    }
    pub fn new(name: &str) -> Self {
        let mut compiler = get_compiler();

        if compiler.scripts.is_empty() {
            compiler.allow_rule = true;
        }

        let id = compiler.scripts.len();
        compiler.scripts.push(String::new());
        compiler.active_script = id;

        Script {
            id,
            name: name.to_string(),
        }
    }
    pub fn add(&self, rule: Rule) {
        let mut compiler = get_compiler();
        if !compiler.allow_rule {
            panic!("Invalid attempt to add rule");
        }
        compiler.allow_rule = false;
        compiler.active_script = self.id;
        let output = &mut compiler.scripts[compiler.active_script];

        output.push_str(&format!("rule(\"{}\")\n{{\n\tevent\n\t{{\n", rule.name));
        let event_body = rule.event.take();
        for param in event_body {
            output.push_str(&format!("\t\t{param};\n"));
        }
        output.push_str("\t}\n");

        if !rule.conditions.0.is_empty() {
            output.push_str("\tconditions\n\t{\n");
            for condition in rule.conditions.0 {
                output.push_str(&format!("\t\t{} == True;\n", condition.take()));
            }
            output.push_str("\t}\n");
        }

        output.push_str("\tactions\n\t{\n");
        compiler.allow_action = true;
        (rule.actions)();
        compiler.allow_action = false;
        output.push_str("\t}\n}\n");

        compiler.allow_rule = true;
    }
    pub fn generate(&self) -> String {
        let compiler = get_compiler();

        let rules = compiler.scripts[self.id].as_str();

        let mut variables = String::new();
        if !compiler.global_variables.is_empty() || !compiler.player_variables.is_empty() {
            variables.push_str("variables\n{\n");
            if !compiler.global_variables.is_empty() {
                variables.push_str("\tglobal:\n");
                for (i, v) in compiler.global_variables.iter().enumerate() {
                    variables.push_str(&format!("\t\t{i}: {v}\n"));
                }
            }
            if !compiler.player_variables.is_empty() {
                variables.push_str("\tplayer:\n");
                for (i, v) in compiler.player_variables.iter().enumerate() {
                    variables.push_str(&format!("\t\t{i}: {v}\n"));
                }
            }
            variables.push_str("}\n\n");
        }

        let mut subroutines = String::new();
        if !compiler.subroutines.is_empty() {
            subroutines.push_str("subroutines\n{\n");
            for (i, v) in compiler.subroutines.iter().enumerate() {
                subroutines.push_str(&format!("\t{i}: {v}\n"));
            }
            subroutines.push_str("}\n\n");
        }
        let name = &self.name;
        let date = chrono::Utc::now()
            .format("%Y-%m-%d %H:%M:%S UTC")
            .to_string();
        format!("// Generated by [{name}] at {date}\n\n{variables}{subroutines}{rules}")
    }
}

#[doc(hidden)]
/// Do not use this function. Exposed for internal use only.
pub fn __register_global_variable__(name: &'static str) {
    get_compiler().global_variables.insert(name);
}

#[doc(hidden)]
/// Do not use this function. Exposed for internal use only.
pub fn __register_player_variable__(name: &'static str) {
    get_compiler().player_variables.insert(name);
}

#[doc(hidden)]
/// Do not use this function. Exposed for internal use only.
pub fn __register_subroutine__(name: &'static str) {
    get_compiler().subroutines.insert(name);
}

#[macro_export]
macro_rules! bind_player_variables {
    ($($name:ident : $type:ty ;)*) => {
        mod player_variables {
            use super::ExprType::{self, *};

            #[allow(non_camel_case_types)]
            pub trait PlayerVariableHolder {
                $(
                    fn $name(self) -> Variable<$type>;
                )*
            }
            impl PlayerVariableHolder for Player {
                $(
                    fn $name(self) -> Variable<$type> {
                        let name = stringify!($name);
                        super::__register_player_variable__(name);
                        Variable::__new__(Some(self), name)
                    }
                )*
            }
        }
        pub use player_variables::PlayerVariableHolder;
    };
}

#[macro_export]
macro_rules! bind_global_variables {
    ($name:ident : $type:ident) => {
        mod global_variables {
            use super::ExprType::{self, *};

            #[allow(non_camel_case_types)]
            pub trait GlobalVariableHolder {
                fn $name() -> Variable<$type>;
            }
            impl GlobalVariableHolder for Global {
                fn $name() -> Variable<$type> {
                    let name = stringify!($name);
                    super::__register_global_variable__(name);
                    Variable::__new__(None, name)
                }
            }
        }
        pub use global_variables::GlobalVariableHolder;
    };
}

#[macro_export]
macro_rules! bind_subroutines {
    ($($name:ident ;)*) => {
        #[allow(non_camel_case_types)]
        pub trait SubroutineHolder {
            $(
                fn $name() -> Subroutine;
            )*
        }
        impl SubroutineHolder for Subroutine {
            $(
                fn $name() -> Subroutine {
                    let name = stringify!($name);
                    __register_subroutine__(name);
                    Subroutine::__new__(name)
                }
            )*
        }
    };
}

pub trait WriteToFile {
    fn write_to_file(&self, path: &str);
}

impl WriteToFile for String {
    fn write_to_file(&self, path: &str) {
        std::fs::write(path, self)
            .expect(format!("Failed to write contents to file {}", path).as_str());
    }
}

pub trait CopyToClipboard {
    fn copy_to_clipboard(&self);
}

impl CopyToClipboard for String {
    fn copy_to_clipboard(&self) {
        let mut ctx: clipboard::ClipboardContext =
            clipboard::ClipboardProvider::new().expect("Failed to access clipboard");
        clipboard::ClipboardProvider::set_contents(&mut ctx, self.clone())
            .expect("Failed to copy to clipboard");
    }
}