actually, serialize MapFixes as json for now. will switch to binary when this is large later, but for now, this is easier to handle schema changes and debug.

This commit is contained in:
Dustin Carlino 2019-09-19 14:39:31 -07:00
parent 9a58136fc1
commit 0ae0a88abb
11 changed files with 32 additions and 22 deletions

View File

@ -29,20 +29,18 @@ pub fn write_json<T: Serialize>(path: &str, obj: &T) -> Result<(), Error> {
Ok(())
}
pub fn read_json<T: DeserializeOwned>(path: &str) -> Result<T, Error> {
pub fn read_json<T: DeserializeOwned>(path: &str, timer: &mut Timer) -> Result<T, Error> {
if !path.ends_with(".json") && !path.ends_with(".geojson") {
panic!("read_json needs {} to end with .json or .geojson", path);
}
// TODO easier way to map_err for anything in a block that has ?
inner_read_json(path).map_err(|e| Error::new(e.kind(), format!("read_json({}): {}", path, e)))
}
fn inner_read_json<T: DeserializeOwned>(path: &str) -> Result<T, Error> {
timer.start(&format!("parse {}", path));
// TODO timer.read_file isn't working here. :(
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let obj: T = serde_json::from_str(&contents)?;
timer.stop(&format!("parse {}", path));
Ok(obj)
}
@ -171,7 +169,11 @@ pub fn load_all_objects<T: DeserializeOwned>(dir: &str, map_name: &str) -> Vec<(
.into_string()
.unwrap();
let load: T = if path_str.ends_with(".json") {
read_json(&format!("../data/{}/{}/{}.json", dir, map_name, name)).unwrap()
read_json(
&format!("../data/{}/{}/{}.json", dir, map_name, name),
&mut timer,
)
.unwrap()
} else if path_str.ends_with(".bin") {
read_binary(
&format!("../data/{}/{}/{}.bin", dir, map_name, name),

View File

@ -85,7 +85,7 @@ pub fn path_raw_map(map_name: &str) -> String {
}
pub fn path_fixes(name: &str) -> String {
format!("../data/fixes/{}.bin", name)
format!("../data/fixes/{}.json", name)
}
pub fn path_camera_state(map_name: &str) -> String {

View File

@ -1,11 +1,11 @@
use abstutil;
use abstutil::Timer;
use geojson::{GeoJson, PolygonType, Value};
use geom::{GPSBounds, LonLat};
use map_model::NeighborhoodBuilder;
pub fn convert(geojson_path: &str, map_name: String, gps_bounds: &GPSBounds) {
println!("Extracting neighborhoods from {}...", geojson_path);
let document: GeoJson = abstutil::read_json(geojson_path).unwrap();
let document: GeoJson = abstutil::read_json(geojson_path, &mut Timer::throwaway()).unwrap();
match document {
GeoJson::FeatureCollection(c) => {
for f in c.features.into_iter() {

View File

@ -143,7 +143,7 @@ fn launch_test(test: &ABTest, ui: &mut UI, ctx: &mut EventCtx) -> ABTestMode {
&mut ui.primary,
&ui.cs,
ctx,
MapEdits::load(&test.map_name, &test.edits1_name),
MapEdits::load(&test.map_name, &test.edits1_name, &mut timer),
);
ui.primary
.map
@ -181,7 +181,7 @@ fn launch_test(test: &ABTest, ui: &mut UI, ctx: &mut EventCtx) -> ABTestMode {
&mut secondary,
&ui.cs,
ctx,
MapEdits::load(&test.map_name, &test.edits2_name),
MapEdits::load(&test.map_name, &test.edits2_name, &mut timer),
);
secondary
.map

View File

@ -1,6 +1,6 @@
use crate::render::ExtraShapeID;
use crate::ui::PerMapUI;
use abstutil;
use abstutil::Timer;
use ezgui::Color;
use geom::Pt2D;
use map_model::{AreaID, BuildingID, BusStopID, IntersectionID, LaneID, RoadID, TurnID};
@ -89,7 +89,8 @@ struct ModifiedColors {
impl ColorScheme {
pub fn load() -> Result<ColorScheme, Error> {
let modified: ModifiedColors = abstutil::read_json("../data/color_scheme.json")?;
let modified: ModifiedColors =
abstutil::read_json("../data/color_scheme.json", &mut Timer::throwaway())?;
let mut map: HashMap<String, Color> = default_colors();
for (name, c) in &modified.map {
map.insert(name.clone(), *c);

View File

@ -55,7 +55,7 @@ impl UI {
ctx.canvas.center_on_map_pt(rand_focus_pt);
} else {
let path = abstutil::path_camera_state(primary.map.get_name());
match abstutil::read_json::<CameraState>(&path) {
match abstutil::read_json::<CameraState>(&path, &mut Timer::throwaway()) {
Ok(ref loaded) => {
println!("Loaded {}", path);
ctx.canvas.cam_x = loaded.cam_x;

View File

@ -1,4 +1,5 @@
use crate::{ControlStopSign, ControlTrafficSignal, IntersectionID, LaneID, LaneType};
use abstutil::Timer;
use serde_derive::{Deserialize, Serialize};
use std::collections::BTreeMap;
@ -24,11 +25,15 @@ impl MapEdits {
}
}
pub fn load(map_name: &str, edits_name: &str) -> MapEdits {
pub fn load(map_name: &str, edits_name: &str, timer: &mut Timer) -> MapEdits {
if edits_name == "no_edits" {
return MapEdits::new(map_name.to_string());
}
abstutil::read_json(&abstutil::path1_json(map_name, abstutil::EDITS, edits_name)).unwrap()
abstutil::read_json(
&abstutil::path1_json(map_name, abstutil::EDITS, edits_name),
timer,
)
.unwrap()
}
pub fn save(&self) {

View File

@ -418,7 +418,7 @@ pub struct Hints {
impl Hints {
pub fn load() -> Hints {
if let Ok(h) = abstutil::read_json::<Hints>("../data/hints.json") {
if let Ok(h) = abstutil::read_json::<Hints>("../data/hints.json", &mut Timer::throwaway()) {
h
} else {
Hints {

View File

@ -348,8 +348,7 @@ impl MapFixes {
let mut results = BTreeMap::new();
for name in abstutil::list_all_objects("fixes", "") {
let fixes: MapFixes =
abstutil::read_binary(&abstutil::path_fixes(&name), timer).unwrap();
let fixes: MapFixes = abstutil::read_json(&abstutil::path_fixes(&name), timer).unwrap();
let (new_roads, new_intersections) = fixes.all_touched_ids();
if !seen_roads.is_disjoint(&new_roads) {
// The error could be much better (which road and other MapFixes), but since we

View File

@ -75,7 +75,10 @@ impl SimFlags {
let mut map: Map =
abstutil::read_binary(&abstutil::path_map(&sim.map_name), timer).unwrap();
map.apply_edits(MapEdits::load(map.get_name(), &sim.edits_name), timer);
map.apply_edits(
MapEdits::load(map.get_name(), &sim.edits_name, timer),
timer,
);
map.recalculate_pathfinding_after_edits(timer);
(map, sim, rng)

View File

@ -178,7 +178,7 @@ impl Model {
}
let path = abstutil::path_fixes(&name);
abstutil::write_binary(&path, &fixes).unwrap();
abstutil::write_json(&path, &fixes).unwrap();
println!("Wrote {}", path);
self.all_fixes.insert(name, fixes);