From 2b0af3ea5263601cb6e95951bda0e4c943a3402b Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Fri, 5 Oct 2018 10:41:50 -0700 Subject: [PATCH] fixing name of map edits... --- abstutil/src/io.rs | 10 ++++++++-- sim/src/helpers.rs | 35 +++++++++++++++++++++-------------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/abstutil/src/io.rs b/abstutil/src/io.rs index d001348139..2f238ba144 100644 --- a/abstutil/src/io.rs +++ b/abstutil/src/io.rs @@ -8,6 +8,7 @@ use std::collections::{BTreeMap, BTreeSet}; use std::fs::File; use std::hash::Hash; use std::io::{Error, ErrorKind, Read, Write}; +use std::path::Path; pub fn to_json(obj: &T) -> String { serde_json::to_string_pretty(obj).unwrap() @@ -101,11 +102,16 @@ pub fn deserialize_multimap< Ok(map) } -// Just list all things from a directory, return sorted by name. +// Just list all things from a directory, return sorted by name, with file extension removed. pub fn list_all_objects(dir: &str, map_name: &str) -> Vec { let mut results: BTreeSet = BTreeSet::new(); for entry in std::fs::read_dir(format!("../data/{}/{}/", dir, map_name)).unwrap() { - let name = entry.unwrap().file_name().into_string().unwrap(); + let name = Path::new(&entry.unwrap().file_name()) + .file_stem() + .unwrap() + .to_os_string() + .into_string() + .unwrap(); results.insert(name); } results.into_iter().collect() diff --git a/sim/src/helpers.rs b/sim/src/helpers.rs index 7b04290a16..2d19b5f918 100644 --- a/sim/src/helpers.rs +++ b/sim/src/helpers.rs @@ -22,7 +22,7 @@ pub struct SimFlags { #[structopt(long = "run_name", default_value = "unnamed")] pub run_name: String, - /// Name of map edits + /// Name of map edits. Shouldn't be a full path or have the ".json" #[structopt(long = "edits_name", default_value = "no_edits")] pub edits_name: String, } @@ -45,11 +45,7 @@ pub fn load(flags: SimFlags, savestate_every: Option) -> (Map, ControlMap, flame::start("read sim savestate"); let sim: Sim = abstutil::read_json(&flags.load).expect("loading sim state failed"); flame::end("read sim savestate"); - // TODO assuming the relative path :( - let edits: MapEdits = abstutil::read_json(&format!( - "../data/edits/{}/{}.json", - sim.map_name, flags.edits_name - )).unwrap_or(MapEdits::new()); + let edits = load_edits(&sim.map_name, &flags); let map_path = format!("../data/maps/{}.abst", sim.map_name); let map = Map::new(&map_path, edits.road_edits.clone()) .expect(&format!("Couldn't load map from {}", map_path)); @@ -58,10 +54,7 @@ pub fn load(flags: SimFlags, savestate_every: Option) -> (Map, ControlMap, } else if flags.load.contains("data/scenarios/") { info!("Seeding the simulation from scenario {}", flags.load); let scenario: Scenario = abstutil::read_json(&flags.load).expect("loading scenario failed"); - let edits: MapEdits = abstutil::read_json(&format!( - "../data/edits/{}/{}.json", - scenario.map_name, flags.edits_name - )).unwrap_or(MapEdits::new()); + let edits = load_edits(&scenario.map_name, &flags); let map_path = format!("../data/maps/{}.abst", scenario.map_name); let map = Map::new(&map_path, edits.road_edits.clone()) .expect(&format!("Couldn't load map from {}", map_path)); @@ -82,10 +75,7 @@ pub fn load(flags: SimFlags, savestate_every: Option) -> (Map, ControlMap, .trim_right_matches(".abst") .to_string(); info!("Loading map {}", flags.load); - let edits: MapEdits = abstutil::read_json(&format!( - "../data/edits/{}/{}.json", - map_name, flags.edits_name - )).unwrap_or(MapEdits::new()); + let edits = load_edits(&map_name, &flags); let map = Map::new(&flags.load, edits.road_edits.clone()).expect("Couldn't load map"); let control_map = ControlMap::new(&map, edits.stop_signs, edits.traffic_signals); flame::start("create sim"); @@ -386,3 +376,20 @@ fn pick_bldg_from_driving_lane( ) -> BuildingID { pick_bldg_from_sidewalk(rng, map, map.get_sidewalk_from_driving_lane(start).unwrap()) } + +fn load_edits(map_name: &str, flags: &SimFlags) -> MapEdits { + if flags.edits_name == "no_edits" { + return MapEdits::new(); + } + if flags.edits_name.contains("data/") || flags.edits_name.contains(".json") { + panic!( + "{} should just be a plain name, not a full path", + flags.edits_name + ); + } + let edits: MapEdits = abstutil::read_json(&format!( + "../data/edits/{}/{}.json", + map_name, flags.edits_name + )).unwrap(); + edits +}