From 7f7e8e7774ef2ce8def6cc9e3241eff159cabc85 Mon Sep 17 00:00:00 2001 From: Connor Fitzgerald Date: Sat, 9 Mar 2024 03:22:18 -0500 Subject: [PATCH] Fix wasm (#582) --- .cargo/config.toml | 15 +++++++++++---- .github/workflows/ci.yml | 6 ++---- build.bash | 4 ++-- examples/Cargo.toml | 3 +++ rend3-framework/Cargo.toml | 1 + rend3-framework/src/assets.rs | 29 +++++++++++++---------------- 6 files changed, 32 insertions(+), 26 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index d5b77959..33c31dc1 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,7 +1,14 @@ [alias] -rend3-doc = ["doc", "--no-deps", "--lib", "--workspace", "--exclude", "scene-viewer", "--exclude", "rend3-cube-example"] +rend3-doc = [ + "doc", + "--no-deps", + "--lib", + "--workspace", + "--exclude", + "scene-viewer", + "--exclude", + "rend3-cube-example", +] [build] -rustflags = [ - "--cfg=web_sys_unstable_apis" -] \ No newline at end of file +rustflags = [] diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 96ff63ad..c534130b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,7 +6,7 @@ on: pull_request: env: - RUSTFLAGS: --cfg=web_sys_unstable_apis -D warnings + RUSTFLAGS: -D warnings RUSTDOCFLAGS: -D warnings jobs: @@ -66,15 +66,13 @@ jobs: - name: clippy run: | cargo +1.76 clippy --target ${{ matrix.target }} --profile ci - if: matrix.target != 'wasm32-unknown-unknown' - name: doc run: | cargo +1.76 doc --target ${{ matrix.target }} --profile ci --no-deps - if: matrix.target != 'wasm32-unknown-unknown' - name: download test resources - if: matrix.os != 'macos-14' + if: matrix.os != 'macos-14' && matrix.target != 'wasm32-unknown-unknown' run: | bash ./build.bash download-assets diff --git a/build.bash b/build.bash index ac700f03..06e441e0 100755 --- a/build.bash +++ b/build.bash @@ -12,7 +12,7 @@ case $1 in else WASM_BUILD_DIR=debug fi - RUSTFLAGS=--cfg=web_sys_unstable_apis cargo build --target wasm32-unknown-unknown $BUILD_FLAGS --bin $@ + cargo build --target wasm32-unknown-unknown $BUILD_FLAGS --bin $@ mkdir -p target/generated/ rm -rf target/generated/* cp -r examples/$1/resources target/generated/ || true @@ -28,7 +28,7 @@ case $1 in cargo clippy cargo test cargo rend3-doc - RUSTFLAGS=--cfg=web_sys_unstable_apis cargo clippy --target wasm32-unknown-unknown --workspace --exclude rend3-imgui --exclude rend3-imgui-example + cargo clippy --target wasm32-unknown-unknown --workspace --exclude rend3-imgui --exclude rend3-imgui-example cargo deny --all-features check ;; download-assets) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index a2cedfa4..73acd448 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -85,6 +85,9 @@ ndk-glue = { version = "0.7", features = ["logger"] } rend3-test = { version = "^0.3.0", path = "../rend3-test" } tokio = "1" +[target.'cfg(target_arch = "wasm32")'.dev-dependencies] +wasm-bindgen-test = { version = "0.3" } + [package.metadata.android] build_targets = ["aarch64-linux-android"] diff --git a/rend3-framework/Cargo.toml b/rend3-framework/Cargo.toml index c29d9344..a3b89678 100644 --- a/rend3-framework/Cargo.toml +++ b/rend3-framework/Cargo.toml @@ -34,6 +34,7 @@ pollster = "0.3" console_error_panic_hook = "0.1" console_log = "1" js-sys = "0.3" +gloo-net = { version = "0.5", default-features = false, features = ["http"] } once_cell = "1.8" wasm-bindgen = "0.2.87" wasm-bindgen-futures = "0.4" diff --git a/rend3-framework/src/assets.rs b/rend3-framework/src/assets.rs index 81e09ecd..1fb2ee7c 100644 --- a/rend3-framework/src/assets.rs +++ b/rend3-framework/src/assets.rs @@ -12,6 +12,13 @@ pub enum AssetError { #[source] error: std::io::Error, }, + #[cfg(target_arch = "wasm32")] + #[error("Could not read {path} from network")] + NetworkError { + path: SsoString, + #[source] + error: gloo_net::Error, + }, } pub enum AssetPath<'a> { @@ -86,28 +93,18 @@ impl AssetLoader { #[cfg(target_arch = "wasm32")] pub async fn get_asset(&self, path: AssetPath<'_>) -> Result, AssetError> { let full_path = path.get_path(&self.base); - let response = reqwest::get(&*full_path) - .await + + gloo_net::http::Request::get(&full_path) + .build() .map_err(|error| AssetError::NetworkError { path: SsoString::from(&*full_path), error, - })?; - - let status = response.status(); - if !status.is_success() { - return Err(AssetError::NetworkStatusError { - path: SsoString::from(&*full_path), - status, - }); - } - - Ok(response - .bytes() + })? + .binary() .await .map_err(|error| AssetError::NetworkError { path: SsoString::from(&*full_path), error, - })? - .to_vec()) + }) } }