diff --git a/abstutil/src/cli.rs b/abstutil/src/cli.rs index e4ac0082f5..0f002578b0 100644 --- a/abstutil/src/cli.rs +++ b/abstutil/src/cli.rs @@ -10,7 +10,7 @@ pub struct CmdArgs { } impl CmdArgs { - /// On native, initialize with real flags. On web, always empty. + /// On native, initialize with real flags. On web, transform URL query parameters into flags. /// /// Calling this has the side-effect of initializing logging on both native and web. This /// should probably be done independently, but for the moment, every app wants both. diff --git a/map_gui/Cargo.toml b/map_gui/Cargo.toml index f5009b5763..61fc7529d3 100644 --- a/map_gui/Cargo.toml +++ b/map_gui/Cargo.toml @@ -7,8 +7,6 @@ edition = "2018" [features] native = ["reqwest", "tokio"] wasm = ["js-sys", "wasm-bindgen", "wasm-bindgen-futures", "web-sys"] -# Just a marker to not use localhost URLs -wasm_s3 = [] # A marker to use a named release from S3 instead of dev for updating files release_s3 = [] diff --git a/map_gui/src/load.rs b/map_gui/src/load.rs index e60420f9b4..16f03c1785 100644 --- a/map_gui/src/load.rs +++ b/map_gui/src/load.rs @@ -148,21 +148,22 @@ mod wasm_loader { path: String, on_load: Box) -> Transition>, ) -> Box> { - // Note that files are only gzipepd on S3. When running locally, we just symlink the - // data/ directory, where files aren't compressed. - let url = if cfg!(feature = "wasm_s3") { - // Anytime data with a new binary format is uploaded, the web client has to be - // re-deployed too - format!( - "http://abstreet.s3-website.us-east-2.amazonaws.com/dev/data/{}.gz", - path.strip_prefix(&abstio::path("")).unwrap() - ) - } else { - format!( - "http://0.0.0.0:8000/{}", - path.strip_prefix(&abstio::path("")).unwrap() - ) - }; + // The current URL is of the index.html page. We can find the data directory relative + // to that. + let base_url = get_base_url().unwrap(); + let file_path = path.strip_prefix(&abstio::path("")).unwrap(); + // Note that files are gzipped on S3 and other deployments. When running locally, we + // just symlink the data/ directory, where files aren't compressed. + let url = + if base_url.contains("http://0.0.0.0") || base_url.contains("http://localhost") { + format!("{}/{}", base_url, file_path) + } else if base_url.contains("abstreet.s3-website") { + // The directory structure on S3 is a little weird -- the base directory has + // data/ alongside game/, fifteen_min/, etc. + format!("{}/../data/{}.gz", base_url, file_path) + } else { + format!("{}/{}.gz", base_url, file_path) + }; // Make the HTTP request nonblockingly. When the response is received, send it through // the channel. @@ -249,6 +250,23 @@ mod wasm_loader { self.panel.draw(g); } } + + /// Returns the base URL where the game is running, excluding query parameters and the + /// implicit index.html that might be there. + fn get_base_url() -> Result { + let window = web_sys::window().ok_or(anyhow!("no window?"))?; + let url = window.location().href().map_err(|err| { + anyhow!(err + .as_string() + .unwrap_or("window.location.href failed".to_string())) + })?; + // Consider using a proper url parsing crate. This works fine for now, though. + let url = url.split("?").next().ok_or(anyhow!("empty URL?"))?; + Ok(url + .trim_end_matches("index.html") + .trim_end_matches("/") + .to_string()) + } } pub struct FutureLoader diff --git a/release/deploy_actdev.sh b/release/deploy_actdev.sh index 8a571c2794..e423ceb027 100755 --- a/release/deploy_actdev.sh +++ b/release/deploy_actdev.sh @@ -5,7 +5,7 @@ set -e mkdir -p abst_actdev cd game -wasm-pack build --release --target web -- --no-default-features --features wasm,map_gui/wasm_s3 +wasm-pack build --release --target web -- --no-default-features --features wasm # Temporarily remove the symlink to the data directory rm -f pkg/system # Expand symlinks @@ -22,4 +22,6 @@ done cp -Rv data/system/study_areas abst_actdev/system gzip `find abst_actdev/ | grep bin | xargs` -echo "Go upload abst_actdev/ somewhere" +zip -r abst_actdev abst_actdev +rm -rf abst_actdev +echo "Go upload abst_actdev.zip to https://github.com/cyipt/actdev/releases" diff --git a/release/deploy_web.sh b/release/deploy_web.sh index 696e6eb1cf..89db30116f 100755 --- a/release/deploy_web.sh +++ b/release/deploy_web.sh @@ -7,7 +7,7 @@ set -e # The parking mapper doesn't work on WASM yet, so don't include it for tool in game santa fifteen_min osm_viewer; do cd $tool - wasm-pack build --release --target web -- --no-default-features --features wasm,map_gui/wasm_s3 + wasm-pack build --release --target web -- --no-default-features --features wasm cd pkg # Temporarily remove the symlink to the data directory; it's uploaded separately by the updater tool rm -f system