fixing name of map edits...

This commit is contained in:
Dustin Carlino 2018-10-05 10:41:50 -07:00
parent 30b0ace7bb
commit 2b0af3ea52
2 changed files with 29 additions and 16 deletions

View File

@ -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<T: Serialize>(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<String> {
let mut results: BTreeSet<String> = 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()

View File

@ -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<Tick>) -> (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<Tick>) -> (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<Tick>) -> (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<R: Rng + ?Sized>(
) -> 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
}