firefly-rust 0.16.0

Rust SDK for making Firefly Zero games
Documentation
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
use core::time::Duration;

use crate::*;

/// System settings. Can be requested using [`get_settings`].
#[derive(Clone, Debug)]
pub struct Settings {
    /// The preferred color scheme of the player.
    pub theme: Theme,

    /// The configured interface language.
    pub language: Language,

    /// If true, the screen is rotated 180 degrees.
    ///
    /// In other words, the player holds the device upside-down.
    /// The touchpad is now on the right and the buttons are on the left.
    pub rotate_screen: bool,

    /// The player has photosensitivity. The app should avoid any rapid flashes.
    pub reduce_flashing: bool,

    /// The player wants increased contrast for colors.
    ///
    /// If set, the black and white colors in the default
    /// palette are adjusted automatically. All other colors
    /// in the default palette or all colors in a custom palette
    /// should be adjusted by the app.
    pub contrast: bool,

    /// If true, the player wants to see easter eggs, holiday effects, and weird jokes.
    pub easter_eggs: bool,
}

#[derive(PartialEq, Eq, Copy, Clone, Debug, Default)]
pub enum Language {
    /// en 🇬🇧 💂
    #[default]
    English,
    /// nl 🇳🇱 🧀
    Dutch,
    /// fr 🇫🇷 🥐
    French,
    /// de 🇩🇪 🥨
    German,
    /// it 🇮🇹 🍕
    Italian,
    /// pl 🇵🇱 🥟
    Polish,
    /// ro 🇷🇴 🧛
    Romanian,
    /// ru 🇷🇺 🪆
    Russian,
    /// es 🇪🇸 🐂
    Spanish,
    /// sv 🇸🇪 ❄️
    Swedish,
    /// tr 🇹🇷 🕌
    Turkish,
    /// uk 🇺🇦 ✊
    Ukrainian,
    /// tp 🇨🇦 🙂
    TokiPona,
}

impl Language {
    #[must_use]
    pub fn from_code(b: [u8; 2]) -> Option<Self> {
        let code = match b {
            [b'd', b'e'] => Self::German,
            [b'e', b'n'] => Self::English,
            [b'e', b's'] => Self::Spanish,
            [b'f', b'r'] => Self::French,
            [b'i', b't'] => Self::Italian,
            [b'n', b'l'] => Self::Dutch,
            [b'p', b'o'] => Self::Polish,
            [b'r', b'o'] => Self::Romanian,
            [b'r', b'u'] => Self::Russian,
            [b's', b'v'] => Self::Swedish,
            [b't', b'p'] => Self::TokiPona,
            [b't', b'r'] => Self::Turkish,
            [b'u', b'k'] => Self::Ukrainian,
            _ => return None,
        };
        Some(code)
    }

    #[must_use]
    pub fn code_array(self) -> [u8; 2] {
        match self {
            Self::English => [b'e', b'n'],
            Self::Dutch => [b'n', b'l'],
            Self::French => [b'f', b'r'],
            Self::German => [b'd', b'e'],
            Self::Italian => [b'i', b't'],
            Self::Polish => [b'p', b'o'],
            Self::Romanian => [b'r', b'o'],
            Self::Russian => [b'r', b'u'],
            Self::Spanish => [b'e', b's'],
            Self::Swedish => [b's', b'v'],
            Self::Turkish => [b't', b'r'],
            Self::Ukrainian => [b'u', b'k'],
            Self::TokiPona => [b't', b'p'],
        }
    }

    #[must_use]
    pub fn code_str(self) -> &'static str {
        match self {
            Self::English => "en",
            Self::Dutch => "nl",
            Self::French => "fr",
            Self::German => "de",
            Self::Italian => "it",
            Self::Polish => "po",
            Self::Romanian => "ro",
            Self::Russian => "ru",
            Self::Spanish => "es",
            Self::Swedish => "sv",
            Self::Turkish => "tr",
            Self::Ukrainian => "uk",
            Self::TokiPona => "tp",
        }
    }

    /// The language name in English.
    #[must_use]
    pub fn name_english(self) -> &'static str {
        match self {
            Self::English => "English",
            Self::Dutch => "Dutch",
            Self::French => "French",
            Self::German => "German",
            Self::Italian => "Italian",
            Self::Polish => "Polish",
            Self::Romanian => "Romanian",
            Self::Russian => "Russian",
            Self::Spanish => "Spanish",
            Self::Swedish => "Swedish",
            Self::TokiPona => "TokiPona",
            Self::Turkish => "Turkish",
            Self::Ukrainian => "Ukrainian",
        }
    }

    /// The language name in the language itself (endonym).
    #[must_use]
    pub fn name_native(self) -> &'static str {
        match self {
            Self::English => "English",
            Self::Dutch => "Nederlands",
            Self::French => "Français",
            Self::German => "Deutsch",
            Self::Italian => "Italiano",
            Self::Polish => "Polski",
            Self::Romanian => "Română",
            Self::Russian => "Русский",
            Self::Spanish => "Español",
            Self::Swedish => "Svenska",
            Self::TokiPona => "toki pona",
            Self::Turkish => "Türkçe",
            Self::Ukrainian => "Українська",
        }
    }

    /// ISO 8859 encoding slug for the language.
    ///
    /// Useful for dynamically loading the correct font for the given language.
    #[must_use]
    pub fn encoding(self) -> &'static str {
        match self {
            // Just like English, Dutch has very little non-ASCII characters
            // which can be avoided in translations to make it possible to stick
            // to the smaller fonts.
            Self::English | Self::Dutch | Self::TokiPona => "ascii",
            Self::Italian | Self::Spanish | Self::Swedish => "iso_8859_1",
            Self::German | Self::French => "iso_8859_2",
            Self::Russian | Self::Ukrainian => "iso_8859_5",
            Self::Turkish => "iso_8859_9",
            Self::Polish => "iso_8859_13",
            Self::Romanian => "iso_8859_16",
        }
    }
}

/// The preferred color scheme of the peer.
///
/// Can be useful for:
///
/// * Making UI that matches the system UI.
/// * Preventing image flashes by making the UI background
///   the same as in the system UI.
/// * Providing and auto-switching the dark and light mode.
#[derive(Clone, Copy, Debug)]
pub struct Theme {
    pub id: u8,
    /// The main color of text and boxes.
    pub primary: Color,
    /// The color of disable options, muted text, etc.
    pub secondary: Color,
    /// The color of important elements, active options, etc.
    pub accent: Color,
    /// The background color, the most contrast color to primary.
    pub bg: Color,
}

impl Default for Theme {
    fn default() -> Self {
        Self {
            id: 0,
            primary: Color::Black,
            secondary: Color::LightGray,
            accent: Color::Green,
            bg: Color::White,
        }
    }
}

/// The same as the stdlib `dbg!` but prints using [`log_debug`].
#[macro_export]
macro_rules! dbg {
    () => {
        $crate::log_debug!(
            "[{}:{}:{}]",
            $crate::file!(),
            $crate::line!(),
            $crate::column!()
        )
    };
    ($val:expr $(,)?) => {
        match $val {
            tmp => {
                $crate::log_debug!(
                    "[{}:{}:{}] {} = {:#?}",
                    $crate::file!(),
                    $crate::line!(),
                    $crate::column!(),
                    $crate::stringify!($val),
                    &&tmp as &dyn $crate::fmt::Debug,
                );
                tmp
            }
        }
    };
}

/// A convenience macro for debug logging using [`log_debug`] function.
///
/// * When called without arguments, will print the current file name and line number.
/// * When called with a string, just prints that string.
/// * When called with multiple arguments, prints a string produced by [`format!`].
#[macro_export]
macro_rules! log_debug {
    () => {
        $crate::log_debug(concat!(file!(), ":", line!()));
    };
    ($f: literal) => {
        $crate::log_debug($f);
    };
    ($f: expr) => {
        $crate::log_debug(&$f);
    };
    ($f: literal, $( $a:expr ),* ) => {
        $crate::log_debug(&alloc::format!($f, $( $a ),* ));
    };
}

/// Log a debug message.
pub fn log_debug(t: &str) {
    let ptr = t.as_ptr() as u32;
    let len = t.len() as u32;
    unsafe {
        bindings::log_debug(ptr, len);
    }
}

/// A convenience macro for error logging using [`log_error`] function.
///
/// Equivalent to [`log_debug!`] but for errors.
#[macro_export]
macro_rules! log_error {
    () => {
        $crate::log_error(concat!(file!(), ":", line!()));
    };
    ($f: literal) => {
        $crate::log_error($f);
    };
    ($f: expr) => {
        $crate::log_error(&$f);
    };
    ($f: literal, $( $a:expr ),* ) => {
        $crate::log_error(&alloc::format!($f, $( $a ),* ));
    };
}

/// Log an error message.
pub fn log_error(t: &str) {
    let ptr = t.as_ptr() as u32;
    let len = t.len() as u32;
    unsafe {
        bindings::log_error(ptr, len);
    }
}

/// Set the random seed.
pub fn set_seed(seed: u32) {
    unsafe {
        bindings::set_seed(seed);
    }
}

/// Get a random value.
#[must_use]
pub fn get_random() -> u32 {
    unsafe { bindings::get_random() }
}

// Get the time passed since the app was started.
#[must_use]
pub fn get_time() -> Duration {
    let micros = unsafe { bindings::get_time() };
    Duration::from_micros(micros)
}

/// Get the Peer's name.
///
/// The name is guaranteed to be a valid ASCII string
/// and have between 1 and 16 characters.
#[cfg(feature = "alloc")]
#[must_use]
pub fn get_name_buf(p: Peer) -> alloc::string::String {
    let mut buf = [0u8; 16];
    let name = get_name(p, &mut buf);
    alloc::string::String::from(name)
}

/// Get the Peer's name.
///
/// The name is guaranteed to be a valid ASCII string
/// and have between 1 and 16 characters.
#[must_use]
pub fn get_name(p: Peer, buf: &mut [u8; 16]) -> &str {
    let ptr = buf.as_ptr() as u32;
    let len = unsafe { bindings::get_name(u32::from(p.0), ptr) };
    let buf = &buf[..len as usize];
    unsafe { core::str::from_utf8_unchecked(buf) }
}

/// Get the peer's system settings.
///
/// **IMPORTANT:** This is the only function that accepts as input not only [`Peer`]
/// but also [`Me`], which might lead to a state drift if used incorrectly.
/// See [the docs](https://docs.fireflyzero.com/dev/net/) for more info.
#[must_use]
pub fn get_settings<P: AnyPeer>(p: P) -> Settings {
    let raw = unsafe { bindings::get_settings(u32::from(p.into_u8())) };
    let code = [(raw >> 8) as u8, raw as u8];
    let language = Language::from_code(code).unwrap_or_default();
    let flags = raw >> 16;
    let theme = raw >> 32;
    let theme = Theme {
        id: theme as u8,
        primary: parse_color(theme >> 20),
        secondary: parse_color(theme >> 16),
        accent: parse_color(theme >> 12),
        bg: parse_color(theme >> 8),
    };
    Settings {
        theme,
        language,
        rotate_screen: (flags & 0b0001) != 0,
        reduce_flashing: (flags & 0b0010) != 0,
        contrast: (flags & 0b0100) != 0,
        easter_eggs: (flags & 0b1000) != 0,
    }
}

fn parse_color(c: u64) -> Color {
    Color::from((c as u8 & 0xf) + 1)
}

/// Exit the app after the current update is finished.
pub fn quit() {
    unsafe { bindings::quit() }
}

/// Restart the app after the current update is finished.
pub fn restart() {
    unsafe { bindings::restart() }
}

mod bindings {
    #[link(wasm_import_module = "misc")]
    unsafe extern "C" {
        pub(crate) unsafe fn log_debug(ptr: u32, len: u32);
        pub(crate) unsafe fn log_error(ptr: u32, len: u32);
        pub(crate) unsafe fn set_seed(seed: u32);
        pub(crate) unsafe fn get_random() -> u32;
        pub(crate) unsafe fn get_time() -> u64;
        pub(crate) unsafe fn get_name(idx: u32, ptr: u32) -> u32;
        pub(crate) unsafe fn get_settings(idx: u32) -> u64;
        pub(crate) unsafe fn restart();
        pub(crate) unsafe fn quit();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_language_code_roundtrip() {
        let mut valid_codes = 0;
        let letters = "abcdefghijklmnopqrstuvwxyz";
        for fst in letters.as_bytes() {
            for snd in letters.as_bytes() {
                let given = [*fst, *snd];
                let Some(lang) = Language::from_code(given) else {
                    continue;
                };
                valid_codes += 1;
                let actual = lang.code_array();
                assert_eq!(actual, given);
            }
        }
        assert!(valid_codes > 8);
    }
}