From 5f5a831569815d5f0d7f9b2913561a764e6b4306 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sun, 13 Apr 2025 23:26:06 +0200 Subject: [PATCH] ignore (but print a warning about) invalid action ids in the config file, instead of failing to deserialize and reverting to default --- CHANGELOG.md | 1 + src/input.rs | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 651c1b8..8b00185 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Game store page: https://crispypin.itch.io/marble-machinations - click to collapse chapters in level list - input bindings for eraser (X), selection (B), blueprint list (Ctrl B), no tool (no default binding) ### fixed +- invalid action ids in the config file key bindings caused everything to revert to default. - when start and stop are bound to the same thing (as by default), only start works - When two input bindings had the same trigger but one has a strict subset of the others modifiers, both would activate when the one with more modifiers was pressed. For example (Ctrl+S -> Save) would also trigger (S -> Wire Tool). Now, Shift+S will still trigger Wire Tool, unless Shift+S (or eg. Shift+Ctrl+S) is bound to something else. diff --git a/src/input.rs b/src/input.rs index 6ad84c6..1353373 100644 --- a/src/input.rs +++ b/src/input.rs @@ -110,7 +110,7 @@ enum BindingState { Released, } -type InputMap = BTreeMap>; +type InputMap = BTreeMap>; #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(from = "InputMap", into = "InputMap")] @@ -390,10 +390,15 @@ pub struct Binding { } impl From for Input { - fn from(value: InputMap) -> Self { + fn from(map: InputMap) -> Self { let mut new = Self::default(); - for (action, loaded_bindings) in value { - new.bindings[action as usize] = loaded_bindings; + for (action, loaded_bindings) in map { + let temp_json = format!("\"{action}\""); + if let Ok(action) = serde_json::from_str::(&temp_json) { + new.bindings[action as usize] = loaded_bindings; + } else { + println!("'{action}' is not a valid action id, bindings discarded"); + } } new } @@ -405,8 +410,11 @@ impl From for InputMap { .bindings .iter() .enumerate() - // for this to panic, the .bindings array would have to be larger than ActionId::SIZE - .map(|(i, b)| (ActionId::from_usize(i).unwrap(), b.clone())) + .map(|(i, b)| { + // for this to panic, the .bindings array would have to be larger than ActionId::SIZE + let action = ActionId::from_usize(i).unwrap(); + (format!("{action:?}"), b.clone()) + }) .collect() } }