A framework for the Godot engine to create TTRPG games for Advanced 5th Edition, Pathfinder 2nd Edition, and more

Got it to compile to a WASM, just compile it with the wasm32-unknown-unknown target

+85 -35
+6 -19
Cargo.toml
··· 1 [package] 2 name = "dice-wire" 3 version = "0.1.0" 4 - edition = "2024" 5 6 [lib] 7 - crate-type = ["cdylib", "rlib"] 8 - 9 - [features] 10 - default = ["console_error_panic_hook"] 11 12 [dependencies] 13 - wasm-bindgen = "0.2.84" 14 - 15 - # The `console_error_panic_hook` crate provides better debugging of panics by 16 - # logging them with `console.error`. This is great for development, but requires 17 - # all the `std::fmt` and `std::panicking` infrastructure, so isn't great for 18 - # code size when deploying. 19 - console_error_panic_hook = { version = "0.1.7", optional = true } 20 - 21 - [dev-dependencies] 22 - wasm-bindgen-test = "0.3.34" 23 - 24 - [profile.release] 25 - # Tell `rustc` to optimize for small code size. 26 - opt-level = "s"
··· 1 [package] 2 + authors = ["Cass Forest"] 3 + edition = "2024" 4 name = "dice-wire" 5 version = "0.1.0" 6 7 [lib] 8 + crate-type = ["cdylib"] 9 10 [dependencies] 11 + rmp = "0.8.15" 12 + rmpv = { version = "1.3.1", features = ["serde"] } 13 + wasm-bindgen = "0.2.114"
+79 -6
src/lib.rs
··· 1 - mod utils; 2 - 3 use wasm_bindgen::prelude::*; 4 5 #[wasm_bindgen] 6 - extern "C" { 7 - fn alert(s: &str); 8 } 9 10 #[wasm_bindgen] 11 - pub fn greet() { 12 - alert("Hello, dice-wire!"); 13 }
··· 1 + use std::collections::HashMap; 2 use wasm_bindgen::prelude::*; 3 + use rmpv::Value; 4 5 #[wasm_bindgen] 6 + pub struct DiceWire { 7 + hooks: Hooks 8 + } 9 + 10 + impl DiceWire { 11 + pub fn new() -> Self { 12 + Self { 13 + hooks: Hooks::new() 14 + } 15 + } 16 + } 17 + 18 + #[wasm_bindgen] 19 + pub struct Hooks { 20 + events: HashMap<String, Vec<Hook>> 21 + } 22 + 23 + impl Hooks { 24 + pub fn new() -> Self { 25 + Self { 26 + events: HashMap::new() 27 + } 28 + } 29 + 30 + pub fn on<F>(&mut self, event: String, callback: F) 31 + where 32 + F: Fn(&Value) + 'static + Send + Sync 33 + { 34 + self.events.entry(event) 35 + .or_insert_with(Vec::new) 36 + .push(Hook::new(callback, false)); 37 + } 38 + 39 + pub fn once<F>(&mut self, event: String, callback: F) 40 + where 41 + F: Fn(&Value) + 'static + Send + Sync 42 + { 43 + self.events.entry(event) 44 + .or_insert_with(Vec::new) 45 + .push(Hook::new(callback, true)); 46 + } 47 + 48 + pub fn emit(&mut self, event: String, data: &Value) { 49 + if let Some(event_list) = self.events.get_mut(&event) { 50 + for event in event_list.iter_mut() { 51 + event.call(data); 52 + } 53 + } 54 + } 55 } 56 57 #[wasm_bindgen] 58 + pub struct Hook { 59 + func: Box<dyn Fn(&Value) -> () + Sync + Send>, 60 + once: bool, 61 + called: bool 62 + } 63 + 64 + impl Hook { 65 + pub fn new<F>(callback: F, once: bool) -> Self 66 + where 67 + F: Fn(&Value) -> () + 'static + Send + Sync 68 + { 69 + Self { 70 + func: Box::new(callback), 71 + once, 72 + called: false 73 + } 74 + } 75 + 76 + pub fn call(&mut self, data: &Value) { 77 + if self.once { 78 + if !self.called { 79 + (self.func)(data); 80 + self.called = true; 81 + } 82 + } else { 83 + (self.func)(data); 84 + } 85 + } 86 }
-10
src/utils.rs
··· 1 - pub fn set_panic_hook() { 2 - // When the `console_error_panic_hook` feature is enabled, we can call the 3 - // `set_panic_hook` function at least once during initialization, and then 4 - // we will get better error messages if our code ever panics. 5 - // 6 - // For more details see 7 - // https://github.com/rustwasm/console_error_panic_hook#readme 8 - #[cfg(feature = "console_error_panic_hook")] 9 - console_error_panic_hook::set_once(); 10 - }
···