variable storage component
This commit is contained in:
parent
2071ee21fd
commit
464e670be4
|
@ -155,6 +155,15 @@ impl From<&Variable> for Variable2 {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct MemoryDescriptor2 {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct MemoryMap2 {
|
||||
pub descriptors: Vec<MemoryDescriptor2>
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ControllerDescription2 {
|
||||
pub name: String,
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
use crate::prelude::*;
|
||||
|
||||
pub struct MemoryMapComponent {
|
||||
map: MemoryMap,
|
||||
}
|
||||
|
||||
impl RetroCallbacks for MemoryMapComponent {
|
||||
fn set_memory_maps(&mut self, memory_map: &MemoryMap) -> Option<bool> {
|
||||
self.map = memory_map.clone();
|
||||
Some(true)
|
||||
}
|
||||
}
|
||||
|
||||
impl RetroComponent for MemoryMapComponent {}
|
|
@ -5,7 +5,9 @@ mod camera;
|
|||
mod fps;
|
||||
mod input;
|
||||
mod logs;
|
||||
mod memory;
|
||||
mod paths;
|
||||
mod variables;
|
||||
|
||||
pub use camera::CameraInfoComponent;
|
||||
pub use fps::SleepFramerateLimitComponent;
|
||||
|
@ -14,3 +16,4 @@ pub use logs::StderrCallTraceComponent;
|
|||
pub use logs::StderrLogComponent;
|
||||
pub use logs::StderrSysInfoLogComponent;
|
||||
pub use paths::PathBufComponent;
|
||||
pub use variables::VariableStoreComponent;
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
use std::collections::BTreeMap;
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct VariableStoreComponent {
|
||||
base_variables: BTreeMap<String, Vec<String>>,
|
||||
override_variables: BTreeMap<String, String>,
|
||||
update: bool,
|
||||
}
|
||||
|
||||
impl VariableStoreComponent {
|
||||
pub fn insert(&mut self, key: impl ToString, value: impl ToString) {
|
||||
let key = key.to_string();
|
||||
let value = value.to_string();
|
||||
if let Some(options) = self.base_variables.get(&key) {
|
||||
if !options.contains(&value) {
|
||||
panic!("Invalid value {:?} for variable {:?} (expected one of {:?})", value, key, options);
|
||||
}
|
||||
}
|
||||
self.override_variables.insert(key, value);
|
||||
self.update = true;
|
||||
}
|
||||
}
|
||||
|
||||
impl RetroComponent for VariableStoreComponent {}
|
||||
|
||||
impl RetroCallbacks for VariableStoreComponent {
|
||||
fn set_variables(&mut self, variables: &[Variable2]) -> Option<bool> {
|
||||
for v in variables {
|
||||
if let Some(x) = self.override_variables.get(&v.key) {
|
||||
if !v.options.contains(x) {
|
||||
panic!("Invalid value {:?} for variable {:?} (expected one of {:?})", x, v.key, v.options);
|
||||
}
|
||||
}
|
||||
self.base_variables.insert(v.key.to_owned(), v.options.to_owned());
|
||||
}
|
||||
Some(true)
|
||||
}
|
||||
|
||||
fn get_variable(&mut self, key: &str) -> Option<String> {
|
||||
self.update = false;
|
||||
self.override_variables.get(key)
|
||||
.or_else(|| self.base_variables.get(key)
|
||||
.and_then(|opts| opts.first()))
|
||||
.cloned()
|
||||
}
|
||||
|
||||
fn get_variable_update(&mut self) -> Option<bool> {
|
||||
Some(self.update)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue