Skip to content

Commit

Permalink
Bundle data/MANIFEST.json in wasm. Now loading all maps from HTTP wor…
Browse files Browse the repository at this point in the history
…ks! #21
  • Loading branch information
dabreegster committed Oct 8, 2020
1 parent 4d66e4e commit 62b3af3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 31 deletions.
14 changes: 7 additions & 7 deletions abstutil/src/abst_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use std::collections::{BTreeMap, BTreeSet};

use serde::{Deserialize, Serialize};

use crate::Timer;

#[derive(Serialize, Deserialize)]
pub struct Manifest {
// Keyed by path, starting with "data/"
Expand All @@ -23,13 +21,15 @@ pub struct Entry {
}

impl Manifest {
pub fn write(&self, path: String) {
println!("- Wrote {}", path);
crate::write_json(path, self);
#[cfg(not(target_arch = "wasm32"))]
pub fn load() -> Manifest {
crate::maybe_read_json(crate::path("MANIFEST.json"), &mut crate::Timer::throwaway())
.unwrap()
}

pub fn load(path: String) -> Result<Manifest, std::io::Error> {
crate::maybe_read_json(path, &mut Timer::throwaway())
#[cfg(target_arch = "wasm32")]
pub fn load() -> Manifest {
crate::from_json(&include_bytes!("../../data/MANIFEST.json").to_vec()).unwrap()
}

pub fn all_map_names(&self) -> BTreeSet<String> {
Expand Down
9 changes: 3 additions & 6 deletions game/src/common/city_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use widgetry::{
};

use crate::app::App;
use crate::game::{DrawBaselayer, PopupMsg, State, Transition};
use crate::game::{DrawBaselayer, State, Transition};
use crate::helpers::nice_map_name;
use crate::render::DrawArea;

Expand Down Expand Up @@ -59,10 +59,7 @@ impl CityPicker {

let mut other_cities = vec![Line("Other cities").draw(ctx)];
let mut this_city = vec![];
for name in abstutil::Manifest::load(abstutil::path("MANIFEST.json"))
.unwrap()
.all_map_names()
{
for name in abstutil::Manifest::load().all_map_names() {
if let Some((_, color, _)) = regions.iter().find(|(n, _, _)| &name == n) {
let btn = Btn::txt(&name, Text::from(Line(nice_map_name(&name)).fg(*color)))
.tooltip(Text::new());
Expand Down Expand Up @@ -199,7 +196,7 @@ fn switch_map(
})
} else {
// TODO Some kind of UI for running the updater from here!
Transition::Replace(PopupMsg::new(
Transition::Replace(crate::game::PopupMsg::new(
ctx,
"Missing data",
vec![
Expand Down
3 changes: 1 addition & 2 deletions game/src/pregame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ impl State for MainMenu {
}
"Sandbox mode" => {
// We might've left with a synthetic map loaded.
let map_path = if abstutil::Manifest::load(abstutil::path("MANIFEST.json"))
.unwrap()
let map_path = if abstutil::Manifest::load()
.all_map_names()
.contains(app.primary.map.get_name())
{
Expand Down
28 changes: 12 additions & 16 deletions updater/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use abstutil::{Entry, Manifest};
use std::collections::BTreeMap;
use std::error::Error;
use std::fs::{create_dir_all, remove_file, set_permissions, File, Permissions};
Expand All @@ -7,6 +6,8 @@ use std::process::Command;

use walkdir::WalkDir;

use abstutil::{Entry, Manifest, Timer};

const MD5_BUF_READ_SIZE: usize = 4096;
const TMP_DOWNLOAD_NAME: &str = "tmp_download.zip";

Expand Down Expand Up @@ -37,10 +38,7 @@ async fn main() {
async fn download() {
let cities = Cities::load_or_create();
let local = generate_manifest();
let truth = filter_manifest(
Manifest::load("data/MANIFEST.json".to_string()).unwrap(),
cities,
);
let truth = filter_manifest(Manifest::load(), cities);

// Anything local need deleting?
for path in local.entries.keys() {
Expand Down Expand Up @@ -76,10 +74,7 @@ async fn download() {
fn just_compare() {
let cities = Cities::load_or_create();
let local = generate_manifest();
let truth = filter_manifest(
Manifest::load("data/MANIFEST.json".to_string()).unwrap(),
cities,
);
let truth = filter_manifest(Manifest::load(), cities);

// Anything local need deleting?
for path in local.entries.keys() {
Expand All @@ -100,7 +95,11 @@ fn upload() {
let remote_base = "/home/dabreegster/Dropbox/abstreet_data";

let mut local = generate_manifest();
let remote = Manifest::load(format!("{}/MANIFEST.json", remote_base)).unwrap_or(Manifest {
let remote: Manifest = abstutil::maybe_read_json(
format!("{}/MANIFEST.json", remote_base),
&mut Timer::throwaway(),
)
.unwrap_or(Manifest {
entries: BTreeMap::new(),
});

Expand Down Expand Up @@ -147,17 +146,14 @@ fn upload() {
}
}

local.write(format!("{}/MANIFEST.json", remote_base));
local.write("data/MANIFEST.json".to_string());
abstutil::write_json(format!("{}/MANIFEST.json", remote_base), &local);
abstutil::write_json("data/MANIFEST.json".to_string(), &local);
}

async fn check_links() {
let client = reqwest::Client::new();

for (file, entry) in Manifest::load("data/MANIFEST.json".to_string())
.unwrap()
.entries
{
for (file, entry) in Manifest::load().entries {
println!("> Check remote for {}", file);
let url = entry.dropbox_url.unwrap();
let url = format!("{}{}", &url[..url.len() - 1], "1");
Expand Down

0 comments on commit 62b3af3

Please sign in to comment.