Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix WASM asset loading path handling for some web servers. #239

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions framework_crates/bones_asset/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::PathBuf;
use anyhow::Context;
use async_channel::Sender;
use bones_utils::{default, futures::future::Boxed as BoxedFuture, HashMap};
use path_absolutize::Absolutize;

use crate::{AssetLocRef, ChangedAsset};

Expand Down Expand Up @@ -100,11 +101,8 @@ impl AssetIo for FileAssetIo {
None => core_dir.clone(),
};
// Make sure absolute paths are relative to pack.
let path = if loc.path.is_absolute() {
loc.path.strip_prefix("/").unwrap().to_owned()
} else {
loc.path
};
let path = loc.path.absolutize_from("/").unwrap();
let path = path.strip_prefix("/").unwrap();
let path = base_dir.join(path);
std::fs::read(&path).with_context(|| format!("Could not load file: {path:?}"))
})
Expand Down Expand Up @@ -168,11 +166,9 @@ pub struct WebAssetIo {
impl WebAssetIo {
/// Create a new [`WebAssetIo`] with the given URL as the core pack root URL.
pub fn new(asset_url: &str) -> Self {
let mut asset_url = asset_url.to_string();
if !asset_url.ends_with('/') {
asset_url.push('/');
Self {
asset_url: asset_url.into(),
}
Self { asset_url }
}
}

Expand All @@ -185,11 +181,14 @@ impl AssetIo for WebAssetIo {
let loc = loc.to_owned();
let asset_url = self.asset_url.clone();
Box::pin(async move {
tracing::info!(?loc, "Loading asset in WebAssetIo");
if loc.pack.is_some() {
return Err(anyhow::format_err!("Cannot load asset packs on WASM yet"));
}
let url = format!("{}{}", asset_url, loc.path.to_str().unwrap());
let url = format!(
"{}{}",
asset_url,
loc.path.absolutize_from("/").unwrap().to_str().unwrap()
);
let (sender, receiver) = async_channel::bounded(1);
let req = ehttp::Request::get(&url);
ehttp::fetch(req, move |resp| {
Expand Down
Loading