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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
//! # Runtime `const` tweaking
//!
//! This library starts a web server at `http://127.0.0.1:9938` where you can change the values of `const` variables in your crate.
//!
//! `f64` & `bool` are the types that are currently supported.
//!
//! ## Example
//! ```rust
//! // Tweak `VALUE` when running in debug mode
//! // This will render a slider in the web GUI because the type here is a `f64`
//! #[const_tweaker::tweak]
//! const VALUE: f64 = 0.0;
//!
//! // Enter a GUI/Game loop
//! loop {
//!     // ...
//!
//!     // Print the constant value that can be changed from the website.
//!     println!("VALUE: {}", VALUE);
//! #   break;
//! }
//! ```
//!
//! Some widgets have customizable options, as seen in the examples below:
//!
//! `f64`:
//! ```rust
//! // Spawns a slider
//! #[const_tweaker::tweak]
//! const DEFAULT_VALUE: f64 = 0.0;
//!
//! // Spawns a slider with 10 steps from 0-1
//! #[const_tweaker::tweak(min = 0.0, max = 1.0, step = 0.1)]
//! const CUSTOM_VALUE: f64 = 0.0;
//! ```
//!
//! `bool`:
//! ```rust
//! // Spawns a checkbox
//! #[const_tweaker::tweak]
//! const DEFAULT_VALUE: bool = true;
//! ```
//!
//! `&str`
//! ```rust
//! // Spaws a textbox
//! #[const_tweaker::tweak]
//! const DEFAULT_VALUE: &str = "Hi";
//! ```

// Ignore the lazy_static warning about the mutex
#![allow(clippy::mutex_atomic)]

use async_std::task;
use dashmap::DashMap;
use horrorshow::{html, owned_html, Raw, Render};
use serde::{de::DeserializeOwned, Deserialize};
use std::{cmp::Ordering, fmt::Display, string::ToString, sync::Mutex, thread};
use tide::{Request, Response};

pub use const_tweaker_attribute::tweak;
#[doc(hidden)]
pub use ctor::ctor;

/// Type representing the const field with metadata.
#[doc(hidden)]
#[derive(Debug)]
pub enum Field {
    F32 {
        value: f32,
        /// Minimum value of slider.
        min: f32,
        /// Maximum value of slider.
        max: f32,
        /// Step increase of slider.
        step: f32,

        /// Rust module location.
        module: String,
        /// Rust file location.
        file: String,
        /// Rust line number in file.
        line: u32,
    },
    F64 {
        value: f64,
        /// Minimum value of slider.
        min: f64,
        /// Maximum value of slider.
        max: f64,
        /// Step increase of slider.
        step: f64,

        /// Rust module location.
        module: String,
        /// Rust file location.
        file: String,
        /// Rust line number in file.
        line: u32,
    },
    I8 {
        value: i8,
        /// Minimum value of slider.
        min: i8,
        /// Maximum value of slider.
        max: i8,
        /// Step increase of slider.
        step: i8,

        /// Rust module location.
        module: String,
        /// Rust file location.
        file: String,
        /// Rust line number in file.
        line: u32,
    },
    U8 {
        value: u8,
        /// Minimum value of slider.
        min: u8,
        /// Maximum value of slider.
        max: u8,
        /// Step increase of slider.
        step: u8,

        /// Rust module location.
        module: String,
        /// Rust file location.
        file: String,
        /// Rust line number in file.
        line: u32,
    },
    I16 {
        value: i16,
        /// Minimum value of slider.
        min: i16,
        /// Maximum value of slider.
        max: i16,
        /// Step increase of slider.
        step: i16,

        /// Rust module location.
        module: String,
        /// Rust file location.
        file: String,
        /// Rust line number in file.
        line: u32,
    },
    U16 {
        value: u16,
        /// Minimum value of slider.
        min: u16,
        /// Maximum value of slider.
        max: u16,
        /// Step increase of slider.
        step: u16,

        /// Rust module location.
        module: String,
        /// Rust file location.
        file: String,
        /// Rust line number in file.
        line: u32,
    },
    I32 {
        value: i32,
        /// Minimum value of slider.
        min: i32,
        /// Maximum value of slider.
        max: i32,
        /// Step increase of slider.
        step: i32,

        /// Rust module location.
        module: String,
        /// Rust file location.
        file: String,
        /// Rust line number in file.
        line: u32,
    },
    U32 {
        value: u32,
        /// Minimum value of slider.
        min: u32,
        /// Maximum value of slider.
        max: u32,
        /// Step increase of slider.
        step: u32,

        /// Rust module location.
        module: String,
        /// Rust file location.
        file: String,
        /// Rust line number in file.
        line: u32,
    },
    I64 {
        value: i64,
        /// Minimum value of slider.
        min: i64,
        /// Maximum value of slider.
        max: i64,
        /// Step increase of slider.
        step: i64,

        /// Rust module location.
        module: String,
        /// Rust file location.
        file: String,
        /// Rust line number in file.
        line: u32,
    },
    U64 {
        value: u64,
        /// Minimum value of slider.
        min: u64,
        /// Maximum value of slider.
        max: u64,
        /// Step increase of slider.
        step: u64,

        /// Rust module location.
        module: String,
        /// Rust file location.
        file: String,
        /// Rust line number in file.
        line: u32,
    },
    Usize {
        value: usize,
        /// Minimum value of slider.
        min: usize,
        /// Maximum value of slider.
        max: usize,
        /// Step increase of slider.
        step: usize,

        /// Rust module location.
        module: String,
        /// Rust file location.
        file: String,
        /// Rust line number in file.
        line: u32,
    },
    Bool {
        value: bool,

        /// Rust module location.
        module: String,
        /// Rust file location.
        file: String,
        /// Rust line number in file.
        line: u32,
    },
    String {
        value: String,

        /// Rust module location.
        module: String,
        /// Rust file location.
        file: String,
        /// Rust line number in file.
        line: u32,
    },
}

impl Field {
    /// The full module path where the constant lives.
    pub fn module_path(&self) -> &str {
        match self {
            Field::F32 { module, .. }
            | Field::F64 { module, .. }
            | Field::I8 { module, .. }
            | Field::U8 { module, .. }
            | Field::I16 { module, .. }
            | Field::U16 { module, .. }
            | Field::I32 { module, .. }
            | Field::U32 { module, .. }
            | Field::I64 { module, .. }
            | Field::U64 { module, .. }
            | Field::Usize { module, .. }
            | Field::Bool { module, .. }
            | Field::String { module, .. } => &*module,
        }
    }

    /// The file with line number.
    pub fn file(&self) -> String {
        match self {
            Field::F32 { file, line, .. }
            | Field::F64 { file, line, .. }
            | Field::I8 { file, line, .. }
            | Field::U8 { file, line, .. }
            | Field::I16 { file, line, .. }
            | Field::U16 { file, line, .. }
            | Field::I32 { file, line, .. }
            | Field::U32 { file, line, .. }
            | Field::I64 { file, line, .. }
            | Field::U64 { file, line, .. }
            | Field::Usize { file, line, .. }
            | Field::Bool { file, line, .. }
            | Field::String { file, line, .. } => format!("{}:{}", file, line),
        }
    }

    /// Just the line number in the file.
    pub fn line_number(&self) -> u32 {
        match self {
            Field::F32 { line, .. }
            | Field::F64 { line, .. }
            | Field::I8 { line, .. }
            | Field::U8 { line, .. }
            | Field::I16 { line, .. }
            | Field::U16 { line, .. }
            | Field::I32 { line, .. }
            | Field::U32 { line, .. }
            | Field::I64 { line, .. }
            | Field::U64 { line, .. }
            | Field::Usize { line, .. }
            | Field::Bool { line, .. }
            | Field::String { line, .. } => *line,
        }
    }

    /// Create a HTML widget from this field with it's metadata.
    pub fn to_html_widget(&self, key: &str) -> String {
        match self {
            Field::F32 {
                value,
                min,
                max,
                step,
                ..
            } => Field::render_slider(key, *value, *min, *max, *step, "f32").to_string(),
            Field::F64 {
                value,
                min,
                max,
                step,
                ..
            } => Field::render_slider(key, *value, *min, *max, *step, "f64").to_string(),
            Field::I8 {
                value,
                min,
                max,
                step,
                ..
            } => Field::render_slider(key, *value, *min, *max, *step, "i8").to_string(),
            Field::U8 {
                value,
                min,
                max,
                step,
                ..
            } => Field::render_slider(key, *value, *min, *max, *step, "u8").to_string(),
            Field::I16 {
                value,
                min,
                max,
                step,
                ..
            } => Field::render_slider(key, *value, *min, *max, *step, "i16").to_string(),
            Field::U16 {
                value,
                min,
                max,
                step,
                ..
            } => Field::render_slider(key, *value, *min, *max, *step, "u16").to_string(),
            Field::I32 {
                value,
                min,
                max,
                step,
                ..
            } => Field::render_slider(key, *value, *min, *max, *step, "i32").to_string(),
            Field::U32 {
                value,
                min,
                max,
                step,
                ..
            } => Field::render_slider(key, *value, *min, *max, *step, "u32").to_string(),
            Field::I64 {
                value,
                min,
                max,
                step,
                ..
            } => Field::render_slider(key, *value, *min, *max, *step, "i64").to_string(),
            Field::U64 {
                value,
                min,
                max,
                step,
                ..
            } => Field::render_slider(key, *value, *min, *max, *step, "u64").to_string(),
            Field::Usize {
                value,
                min,
                max,
                step,
                ..
            } => Field::render_slider(key, *value, *min, *max, *step, "usize").to_string(),
            Field::Bool { value, .. } => Field::render_bool(key, *value).to_string(),
            Field::String { value, .. } => Field::render_string(key, value).to_string(),
        }
    }

    /// Render a slider widget for the number types.
    fn render_slider<'a, T>(
        key: &'a str,
        value: T,
        min: T,
        max: T,
        step: T,
        http_path: &'a str,
    ) -> impl Render + ToString + 'a
    where
        T: Display + 'a,
    {
        owned_html! {
            div (class="column") {
                input (type="range",
                    id=key.to_string(),
                    min=min.to_string(),
                    max=max.to_string(),
                    step=step.to_string(),
                    defaultValue=value.to_string(),
                    style="width: 100%",
                    // The value is a string, convert it to a number so it can be properly
                    // deserialized by serde
                    oninput=send(key, "Number(this.value)", http_path))
                { }
            }
            div (class="column is-narrow") {
                span (id=format!("{}_label", key), class="is-small")
                { : value.to_string() }
            }
        }
    }

    /// Render the bool widget.
    fn render_bool<'a>(key: &'a str, value: bool) -> impl Render + ToString + 'a {
        owned_html! {
            div (class="column") {
                input (type="checkbox",
                    id=key,
                    value=value.to_string(),
                    onclick=send(key, "this.checked", "bool"))
                { }
            }
            div (class="column is-narrow") {
                span (id=format!("{}_label", key))
                { : value.to_string() }
            }
        }
    }

    /// Render the string widget.
    fn render_string<'a>(key: &'a str, value: &'a str) -> impl Render + ToString + 'a {
        owned_html! {
            div (class="column") {
                input (type="text",
                    id=key,
                    value=value,
                    style="width: 100%",
                    onchange=send(key, "this.value", "string"))
                { }
            }
            div (class="column is-narrow") {
                span (id=format!("{}_label", key))
                { : value.to_string() }
            }
        }
    }
}

/// A struct used for deserializing POST request JSON data.
#[derive(Debug, Deserialize)]
struct PostData<T> {
    key: String,
    value: T,
}

lazy_static::lazy_static! {
    /// The list of fields with their data.
    #[doc(hidden)]
    pub static ref DATA: DashMap<&'static str, Field> = DashMap::new();
    /// The last known size of the DATA map, used to detect whether the page should refresh.
    static ref LAST_MAP_SIZE: Mutex<usize> = Mutex::new(0);
}

/// Launch the `const` tweaker web service.
///
/// This will launch a web server at `http://127.0.01:9938`.
#[ctor::ctor]
fn run() {
    // Run a blocking web server in a new thread
    thread::spawn(|| {
        task::block_on(async {
            let mut app = tide::new();
            // The main site
            app.at("/").get(main_site);
            // Whether the page should be refreshed or not
            app.at("/should_refresh").get(should_refresh);

            // Setting the data
            app.at("/set/f32").post(|r| handle_set_value(r, set_f32));
            app.at("/set/f64").post(|r| handle_set_value(r, set_f64));
            app.at("/set/bool").post(|r| handle_set_value(r, set_bool));
            app.at("/set/string")
                .post(|r| handle_set_value(r, set_string));
            app.listen("127.0.0.1:9938").await
        })
        .expect("Running web server failed");
    });
}

/// Build the actual site.
async fn main_site(_: Request<()>) -> Response {
    // Set LAST_MAP_SIZE to it's initial value
    let mut last_map_size = LAST_MAP_SIZE.lock().unwrap();
    *last_map_size = DATA.len();

    let body = html! {
        style { : include_str!("bulma.css") }
        style { : "* { font-family: sans-serif}" }
        body {
            // Title
            section (class="hero is-primary") {
                div (class="hero-body") {
                    div (class="container") {
                        h1 (class="title is-1") { : "Const Tweaker Web Interface" }
                    }
                }
            }
            // All the widgets
            : render_widgets();
            // The error message
            div (class="container") {
                div (class="notification is-danger") {
                    span(id="status") { }
                }
            }
        }
        script { : Raw(include_str!("send.js")) }
    };

    Response::new(200)
        .body_string(format!("{}", body))
        .set_header("content-type", "text/html;charset=utf-8")
}

/// Render all widgets.
fn render_widgets() -> impl Render {
    owned_html! {
        // All modules go in their own panels
        @for module in modules().into_iter() {
            section (class="section") {
                div (class="container box") {
                    h3 (class="title is-3") { : format!("Module: \"{}\"", module) }
                    : render_module(&module)
                }

                // The textbox to copy the output from
                div (class="container box") {
                    h4 (class="title is-4") { : "Changes" }
                    div (class="columns") {
                        div (class="column") {
                            textarea (
                                class="textarea",
                                style="font-family: monospace",
                                id=format!("{}_output", module.replace("::", "_")),
                                readonly,
                                placeholder="No changes")
                        }
                        div (class="column is-narrow control") {
                            button (class="button is-link", onclick=format!("copy_text(\"{}\")", module)) {
                                : "Copy"
                            }
                        }
                    }
                }
            }
        }
    }
}

/// Render a module of widgets.
fn render_module<'a>(module: &'a str) -> impl Render + 'a {
    let mut data = DATA
        .iter()
        .filter(|kv| kv.value().module_path() == module)
        .collect::<Vec<_>>();

    data.sort_by(|a, b| {
        a.value()
            .line_number()
            .partial_cmp(&b.value().line_number())
            .unwrap_or(Ordering::Equal)
    });

    owned_html! {
        // All widgets go into their own column box
        @for ref_multi in data.iter() {
            : render_widget(ref_multi.key(), ref_multi.value())
        }
    }
}

/// Render a single widget.
fn render_widget<'a>(key: &'a str, field: &'a Field) -> impl Render + 'a {
    owned_html! {
        div (class="columns") {
            div (class="column is-narrow") {
                // module::CONSTANT
                span (class="is-small") { : key }

                br {}
                // file:line
                span (class="tag") { : field.file() }
            }
            : Raw(field.to_html_widget(key))
        }
    }
}

/// The javascript call to send the updated data.
fn send(key: &str, look_for: &str, data_type: &str) -> String {
    format!(
        "send('{}', {}, '{}')",
        key.replace("\\", "\\\\"),
        look_for,
        data_type
    )
}

/// Whether the webpage should refresh itself or not.
async fn should_refresh(_request: Request<()>) -> Response {
    let mut last_map_size = LAST_MAP_SIZE.lock().unwrap();

    if *last_map_size == DATA.len() {
        // Don't need to do anything, just send an empty response
        Response::new(200)
    } else {
        // There is a size mismatch of the map, reload the page
        *last_map_size = DATA.len();

        Response::new(200).body_string("refresh".to_string())
    }
}

/// Handle setting of values.
async fn handle_set_value<T, F>(mut request: Request<()>, set_value: F) -> Response
where
    T: DeserializeOwned,
    F: Fn(&mut Field, T),
{
    let post_data: PostData<T> = request.body_json().await.expect("Could not decode JSON");
    set_value(
        &mut DATA
            .get_mut(&*post_data.key)
            .expect("Could not get item from map"),
        post_data.value,
    );

    Response::new(200)
}

/// Set a f32 value when the field matches the proper variant.
fn set_f32(field: &mut Field, new_value: f32) {
    match field {
        Field::F32 { ref mut value, .. } => {
            *value = new_value;
        }
        _ => panic!("Unexpected type, please report an issue"),
    }
}

/// Set a f64 value when the field matches the proper variant.
fn set_f64(field: &mut Field, new_value: f64) {
    match field {
        Field::F64 { ref mut value, .. } => {
            *value = new_value;
        }
        _ => panic!("Unexpected type, please report an issue"),
    }
}

/// Set a bool value when the field matches the proper variant.
fn set_bool(field: &mut Field, new_value: bool) {
    match field {
        Field::Bool { ref mut value, .. } => {
            *value = new_value;
        }
        _ => panic!("Unexpected type, please report an issue"),
    }
}

/// Set a string value when the field matches the proper variant.
fn set_string(field: &mut Field, new_value: String) {
    match field {
        Field::String { ref mut value, .. } => {
            *value = new_value;
        }
        _ => panic!("Unexpected type, please report an issue"),
    }
}

/// Get a list of all modules.
fn modules() -> Vec<String> {
    let mut modules: Vec<_> = DATA
        .iter()
        .map(|kv| kv.value().module_path().to_string())
        .collect::<_>();

    // Remove duplicate entries
    modules.sort();
    modules.dedup();

    modules
}