diff options
| author | troido <troido@protonmail.com> | 2020-04-03 21:55:32 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-04-03 21:55:32 +0200 |
| commit | bd39f100c470650596dc240d4e51bd4c080fd112 (patch) | |
| tree | 143b1a2d347c5c5111de7b3b0920ebbdd20c9a26 | |
| parent | 4e4f8d2462262ec8326e9f3e5a25ed580d098159 (diff) | |
process assemblage shortcuts before processing components
| -rw-r--r-- | src/assemblage.rs | 75 | ||||
| -rw-r--r-- | src/main.rs | 13 |
2 files changed, 35 insertions, 53 deletions
diff --git a/src/assemblage.rs b/src/assemblage.rs index 52fd9f2..6856151 100644 --- a/src/assemblage.rs +++ b/src/assemblage.rs @@ -6,7 +6,6 @@ use crate::{ parameter::{Parameter, ParameterType}, componentwrapper::{ComponentWrapper, ComponentType}, components::Serialise, - hashmap, Template, Result, aerr @@ -46,9 +45,9 @@ impl Assemblage { Ok(arguments) } - fn parse_definition_components(comps: &Value) -> Result<Vec<(ComponentType, HashMap<String, ComponentParameter>)>> { + fn parse_definition_components(comps: &[Value]) -> Result<Vec<(ComponentType, HashMap<String, ComponentParameter>)>> { let mut components = Vec::new(); - for tup in comps.as_array().ok_or(aerr!("components is not a json array"))? { + for tup in comps { if let Some(name) = tup.as_str() { components.push((ComponentType::from_str(name).ok_or(aerr!("not a valid componenttype"))?, HashMap::new())); } else { @@ -80,9 +79,8 @@ impl Assemblage { Ok(()) } - fn common_short_definitions(val: &Value) -> Result<Vec<(ComponentType, HashMap<String, ComponentParameter>)>> { + fn preprocess(val: &Value) -> Result<Vec<Value>> { let mut components = Vec::new(); - let name = if let Some(nameval) = val.get("name") { Some(nameval.as_str().ok_or(aerr!("name not a string"))?.to_string()) } else {None}; @@ -93,59 +91,45 @@ impl Assemblage { let height = val .get("height").ok_or(aerr!("defining a sprite requires also defining a height"))? .as_f64().ok_or(aerr!("height not a float"))?; - components.push(( - ComponentType::Visible, - hashmap!( - "name".to_string() => ComponentParameter::Constant( - Parameter::String(name.clone().unwrap_or(sprite.clone())) - ), - "sprite".to_string() => ComponentParameter::Constant( - Parameter::String(sprite) - ), - "height".to_string() => ComponentParameter::Constant( - Parameter::Float(height) - ) - ) - )); + components.push(json!(["Visible", { + "name": ["string", name.clone().unwrap_or(sprite.clone())], + "sprite": ["string", sprite], + "height": ["float", height] + }])); } - // item component is common too if let Some(action) = val.get("item") { - components.push(( - ComponentType::Item, - hashmap!( - "ent".to_string() => ComponentParameter::TemplateSelf, - "name".to_string() => if let Some(n) = name { - ComponentParameter::Constant(Parameter::String(n)) - } else { - ComponentParameter::TemplateName - }, - "action".to_string() => ComponentParameter::Constant( - Parameter::from_typed_json(ParameterType::Action, action).ok_or(aerr!("invalid item action"))? - ) - ) - )); + components.push(json!(["Item", { + "ent": ["self", null], + "name": if let Some(n) = name { + json!(["string", n]) + } else { + json!(["name", Value::Null]) + }, + "action": ["action", action] + }])); } - // and so is flags if let Some(flags) = val.get("flags") { - components.push(( - ComponentType::Flags, - hashmap!( - "flags".to_string() => ComponentParameter::Constant( - Parameter::from_typed_json(ParameterType::List, flags).ok_or(aerr!("failed to parse flags"))? - ) - ) - )); + components.push(json!(["Flags", { + "flags": ["list", flags] + }])); } Ok(components) } pub fn from_json(val: &Value) -> Result<Self>{ - let mut assemblage = Self { + let mut json_components: Vec<Value> = val + .get("components") + .unwrap_or(&json!([])) + .as_array() + .ok_or(aerr!("components is not a json array"))? + .to_vec(); + json_components.append(&mut Self::preprocess(val)?); + let assemblage = Self { arguments: Self::parse_definition_arguments(val.get("arguments").unwrap_or(&json!([])))?, - components: Self::parse_definition_components(val.get("components").unwrap_or(&json!([])))?, + components: Self::parse_definition_components(&json_components)?, save: val.get("save").unwrap_or(&json!(true)).as_bool().ok_or(aerr!("assemblage save not a bool"))?, extract: val .get("extract") @@ -167,7 +151,6 @@ impl Assemblage { }) .collect::<Result<Vec<(String, ComponentType, String)>>>()? }; - assemblage.components.append(&mut Self::common_short_definitions(val)?); assemblage.validate()?; Ok(assemblage) } diff --git a/src/main.rs b/src/main.rs index 86999f6..2e6042f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,13 +80,6 @@ fn main() -> Result<()>{ ); println!("content directory: {:?}", content_dir); let loader = WorldLoader::new(content_dir.join("maps")); - - let save_dir = config.save_dir.unwrap_or( - FileStorage::default_save_dir().expect("couldn't find any save directory") - ); - println!("save directory: {:?}", content_dir); - let storage = FileStorage::new(save_dir); - let encyclopedia = Encyclopedia::from_json( serde_json::from_str( &fs::read_to_string( @@ -96,6 +89,12 @@ fn main() -> Result<()>{ )? )? )?; + + let save_dir = config.save_dir.unwrap_or( + FileStorage::default_save_dir().expect("couldn't find any save directory") + ); + println!("save directory: {:?}", content_dir); + let storage = FileStorage::new(save_dir); let mut world = World::new(encyclopedia, loader, Box::new(storage), RoomId::from_str("room")); |
