Skip to content

Commit

Permalink
Reintroduce android support
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Nov 28, 2023
1 parent da9dbed commit 938fbe7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ version = "0.7.1"
bevy = {version = "0.12", default-features = false, features = ["bevy_asset"]}

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
isahc = "1"
surf = {version = "2.3", default-features = false, features = ["h1-client-rustls"]}

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = {version = "0.3.22", default-features = false}
Expand Down
33 changes: 10 additions & 23 deletions src/web_asset_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,34 +83,21 @@ async fn get<'a>(path: PathBuf) -> Result<Box<Reader<'a>>, AssetReaderError> {

#[cfg(not(target_arch = "wasm32"))]
async fn get<'a>(path: PathBuf) -> Result<Box<Reader<'a>>, AssetReaderError> {
use std::future::Future;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};

use isahc::http::StatusCode;
use isahc::{get_async, AsyncBody, Error, Response, ResponseFuture};

struct ContinuousPoll<'a>(ResponseFuture<'a>);

impl<'a> Future for ContinuousPoll<'a> {
type Output = Result<Response<AsyncBody>, Error>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
// Always wake - blocks on single threaded executor.
cx.waker().wake_by_ref();

Pin::new(&mut self.0).poll(cx)
}
}
use bevy::asset::io::VecReader;
use surf::StatusCode;

let response = ContinuousPoll(get_async(path.to_str().unwrap()))
.await
.unwrap();
let mut response = surf::get(path.to_str().unwrap()).await.unwrap();

match response.status() {
StatusCode::OK => Ok(Box::new(response.into_body()) as _),
StatusCode::NOT_FOUND => Err(AssetReaderError::NotFound(path)),
StatusCode::Ok => Ok(Box::new(VecReader::new(
response
.body_bytes()
.await
.map_err(|_| AssetReaderError::NotFound(path.to_path_buf()))?,
)) as _),
StatusCode::NotFound => Err(AssetReaderError::NotFound(path)),
code => Err(AssetReaderError::Io(io::Error::new(
io::ErrorKind::Other,
format!("unexpected status code {code}"),
Expand Down

0 comments on commit 938fbe7

Please sign in to comment.