use std::future::Future;
use std::pin::Pin;
use anyhow::Result;
use futures_channel::{mpsc, oneshot};
use instant::Instant;
use serde::de::DeserializeOwned;
#[cfg(not(target_arch = "wasm32"))]
use tokio::runtime::Runtime;
use abstio::MapName;
use abstutil::Timer;
use geom::Duration;
use widgetry::{Color, EventCtx, GfxCtx, Line, Panel, State, Text, Transition, UpdateType};
use crate::tools::PopupMsg;
use crate::AppLike;
#[cfg(not(target_arch = "wasm32"))]
pub use native_loader::{FileLoader, RawFileLoader};
#[cfg(target_arch = "wasm32")]
pub use wasm_loader::{FileLoader, RawFileLoader};
pub struct MapLoader;
impl MapLoader {
pub fn new_state<A: AppLike + 'static>(
ctx: &mut EventCtx,
app: &A,
name: MapName,
on_load: Box<dyn FnOnce(&mut EventCtx, &mut A) -> Transition<A>>,
) -> Box<dyn State<A>> {
if app.map().get_name() == &name {
return Box::new(MapAlreadyLoaded {
on_load: Some(on_load),
});
}
if let Some(extra_font) = match name.city.country.as_ref() {
"ly" => Some("NotoSansArabic-Regular.ttf"),
"jp" => Some("NotoSerifCJKtc-Regular.otf"),
"tw" => Some("NotoSerifCJKtc-Regular.otf"),
_ => None,
} {
if !ctx.is_font_loaded(extra_font) {
return RawFileLoader::<A>::new_state(
ctx,
abstio::path(format!("system/extra_fonts/{}", extra_font)),
Box::new(move |ctx, app, bytes| match bytes {
Ok(bytes) => {
ctx.load_font(extra_font, bytes);
Transition::Replace(MapLoader::new_state(ctx, app, name, on_load))
}
Err(err) => Transition::Replace(PopupMsg::new_state(
ctx,
"Error",
vec![format!("Couldn't load {}", extra_font), err.to_string()],
)),
}),
);
}
}
FileLoader::<A, map_model::Map>::new_state(
ctx,
name.path(),
Box::new(move |ctx, app, timer, map| {
match map {
Ok(mut map) => {
map.map_loaded_directly();
app.map_switched(ctx, map, timer);
(on_load)(ctx, app)
}
Err(err) => Transition::Replace(PopupMsg::new_state(
ctx,
"Error",
vec![
format!("Couldn't load {}", name.describe()),
err.to_string(),
],
)),
}
}),
)
}
}
struct MapAlreadyLoaded<A: AppLike> {
on_load: Option<Box<dyn FnOnce(&mut EventCtx, &mut A) -> Transition<A>>>,
}
impl<A: AppLike + 'static> State<A> for MapAlreadyLoaded<A> {
fn event(&mut self, ctx: &mut EventCtx, app: &mut A) -> Transition<A> {
(self.on_load.take().unwrap())(ctx, app)
}
fn draw(&self, _: &mut GfxCtx, _: &A) {}
}
#[cfg(not(target_arch = "wasm32"))]
mod native_loader {
use super::*;
pub struct FileLoader<A: AppLike, T> {
path: String,
on_load:
Option<Box<dyn FnOnce(&mut EventCtx, &mut A, &mut Timer, Result<T>) -> Transition<A>>>,
}
impl<A: AppLike + 'static, T: 'static + DeserializeOwned> FileLoader<A, T> {
pub fn new_state(
_: &mut EventCtx,
path: String,
on_load: Box<dyn FnOnce(&mut EventCtx, &mut A, &mut Timer, Result<T>) -> Transition<A>>,
) -> Box<dyn State<A>> {
Box::new(FileLoader {
path,
on_load: Some(on_load),
})
}
}
impl<A: AppLike + 'static, T: 'static + DeserializeOwned> State<A> for FileLoader<A, T> {
fn event(&mut self, ctx: &mut EventCtx, app: &mut A) -> Transition<A> {
debug!("Loading {}", self.path);
ctx.loading_screen(format!("load {}", self.path), |ctx, timer| {
let file = abstio::read_object(self.path.clone(), timer);
(self.on_load.take().unwrap())(ctx, app, timer, file)
})
}
fn draw(&self, g: &mut GfxCtx, _: &A) {
g.clear(Color::BLACK);
}
}
pub struct RawFileLoader<A: AppLike> {
path: String,
on_load: Option<Box<dyn FnOnce(&mut EventCtx, &mut A, Result<Vec<u8>>) -> Transition<A>>>,
}
impl<A: AppLike + 'static> RawFileLoader<A> {
pub fn new_state(
_: &mut EventCtx,
path: String,
on_load: Box<dyn FnOnce(&mut EventCtx, &mut A, Result<Vec<u8>>) -> Transition<A>>,
) -> Box<dyn State<A>> {
Box::new(RawFileLoader {
path,
on_load: Some(on_load),
})
}
}
impl<A: AppLike + 'static> State<A> for RawFileLoader<A> {
fn event(&mut self, ctx: &mut EventCtx, app: &mut A) -> Transition<A> {
debug!("Loading {}", self.path);
let bytes = abstio::slurp_file(&self.path);
(self.on_load.take().unwrap())(ctx, app, bytes)
}
fn draw(&self, g: &mut GfxCtx, _: &A) {
g.clear(Color::BLACK);
}
}
}
#[cfg(target_arch = "wasm32")]
mod wasm_loader {
use std::io::Read;
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::JsFuture;
use web_sys::{Request, RequestInit, RequestMode, Response};
use super::*;
pub struct FileLoader<A: AppLike, T> {
response: oneshot::Receiver<Result<Vec<u8>>>,
on_load:
Option<Box<dyn FnOnce(&mut EventCtx, &mut A, &mut Timer, Result<T>) -> Transition<A>>>,
panel: Panel,
started: Instant,
url: String,
}
impl<A: AppLike + 'static, T: 'static + DeserializeOwned> FileLoader<A, T> {
pub fn new_state(
ctx: &mut EventCtx,
path: String,
on_load: Box<dyn FnOnce(&mut EventCtx, &mut A, &mut Timer, Result<T>) -> Transition<A>>,
) -> Box<dyn State<A>> {
let base_url = ctx
.prerender
.assets_base_url()
.expect("assets_base_url must be specified for wasm builds via `Settings`");
let url = if ctx.prerender.assets_are_gzipped() {
format!("{}/{}.gz", base_url, path)
} else {
format!("{}/{}", base_url, path)
};
let (tx, rx) = oneshot::channel();
let url_copy = url.clone();
debug!("Loading {}", url_copy);
wasm_bindgen_futures::spawn_local(async move {
let mut opts = RequestInit::new();
opts.method("GET");
opts.mode(RequestMode::Cors);
let request = Request::new_with_str_and_init(&url_copy, &opts).unwrap();
let window = web_sys::window().unwrap();
match JsFuture::from(window.fetch_with_request(&request)).await {
Ok(resp_value) => {
let resp: Response = resp_value.dyn_into().unwrap();
if resp.ok() {
let buf = JsFuture::from(resp.array_buffer().unwrap()).await.unwrap();
let array = js_sys::Uint8Array::new(&buf);
tx.send(Ok(array.to_vec())).unwrap();
} else {
let status = resp.status();
let err = resp.status_text();
tx.send(Err(anyhow!("HTTP {}: {}", status, err))).unwrap();
}
}
Err(err) => {
tx.send(Err(anyhow!("{:?}", err))).unwrap();
}
}
});
Box::new(FileLoader {
response: rx,
on_load: Some(on_load),
panel: ctx.make_loading_screen(Text::from(format!("Loading {}...", url))),
started: Instant::now(),
url,
})
}
}
impl<A: AppLike + 'static, T: 'static + DeserializeOwned> State<A> for FileLoader<A, T> {
fn event(&mut self, ctx: &mut EventCtx, app: &mut A) -> Transition<A> {
if let Some(maybe_resp) = self.response.try_recv().unwrap() {
let mut timer = Timer::new(format!("Loading {}...", self.url));
let result = maybe_resp.and_then(|resp| {
if self.url.ends_with(".gz") {
let decoder = flate2::read::GzDecoder::new(&resp[..]);
if self.url.ends_with(".bin.gz") {
abstutil::from_binary_reader(decoder)
} else {
abstutil::from_json_reader(decoder)
}
} else if self.url.ends_with(".bin") {
abstutil::from_binary(&&resp)
} else {
abstutil::from_json(&&resp)
}
});
return (self.on_load.take().unwrap())(ctx, app, &mut timer, result);
}
self.panel = ctx.make_loading_screen(Text::from_multiline(vec![
Line(format!("Loading {}...", self.url)),
Line(format!(
"Time spent: {}",
Duration::realtime_elapsed(self.started)
)),
]));
ctx.request_update(UpdateType::Game);
Transition::Keep
}
fn draw(&self, g: &mut GfxCtx, _: &A) {
g.clear(Color::BLACK);
self.panel.draw(g);
}
}
pub struct RawFileLoader<A: AppLike> {
response: oneshot::Receiver<Result<Vec<u8>>>,
on_load: Option<Box<dyn FnOnce(&mut EventCtx, &mut A, Result<Vec<u8>>) -> Transition<A>>>,
panel: Panel,
started: Instant,
url: String,
}
impl<A: AppLike + 'static> RawFileLoader<A> {
pub fn new_state(
ctx: &mut EventCtx,
path: String,
on_load: Box<dyn FnOnce(&mut EventCtx, &mut A, Result<Vec<u8>>) -> Transition<A>>,
) -> Box<dyn State<A>> {
let base_url = ctx
.prerender
.assets_base_url()
.expect("assets_base_url must be specified for wasm builds via `Settings`");
let url = if ctx.prerender.assets_are_gzipped() {
format!("{}/{}.gz", base_url, path)
} else {
format!("{}/{}", base_url, path)
};
let (tx, rx) = oneshot::channel();
let url_copy = url.clone();
debug!("Loading {}", url_copy);
wasm_bindgen_futures::spawn_local(async move {
let mut opts = RequestInit::new();
opts.method("GET");
opts.mode(RequestMode::Cors);
let request = Request::new_with_str_and_init(&url_copy, &opts).unwrap();
let window = web_sys::window().unwrap();
match JsFuture::from(window.fetch_with_request(&request)).await {
Ok(resp_value) => {
let resp: Response = resp_value.dyn_into().unwrap();
if resp.ok() {
let buf = JsFuture::from(resp.array_buffer().unwrap()).await.unwrap();
let array = js_sys::Uint8Array::new(&buf);
tx.send(Ok(array.to_vec())).unwrap();
} else {
let status = resp.status();
let err = resp.status_text();
tx.send(Err(anyhow!("HTTP {}: {}", status, err))).unwrap();
}
}
Err(err) => {
tx.send(Err(anyhow!("{:?}", err))).unwrap();
}
}
});
Box::new(RawFileLoader {
response: rx,
on_load: Some(on_load),
panel: ctx.make_loading_screen(Text::from(format!("Loading {}...", url))),
started: Instant::now(),
url,
})
}
}
impl<A: AppLike + 'static> State<A> for RawFileLoader<A> {
fn event(&mut self, ctx: &mut EventCtx, app: &mut A) -> Transition<A> {
if let Some(maybe_resp) = self.response.try_recv().unwrap() {
let bytes = if self.url.ends_with(".gz") {
maybe_resp.and_then(|gzipped| {
let mut decoder = flate2::read::GzDecoder::new(&gzipped[..]);
let mut buffer: Vec<u8> = Vec::new();
decoder
.read_to_end(&mut buffer)
.map(|_| buffer)
.map_err(|err| err.into())
})
} else {
maybe_resp
};
return (self.on_load.take().unwrap())(ctx, app, bytes);
}
self.panel = ctx.make_loading_screen(Text::from_multiline(vec![
Line(format!("Loading {}...", self.url)),
Line(format!(
"Time spent: {}",
Duration::realtime_elapsed(self.started)
)),
]));
ctx.request_update(UpdateType::Game);
Transition::Keep
}
fn draw(&self, g: &mut GfxCtx, _: &A) {
g.clear(Color::BLACK);
self.panel.draw(g);
}
}
}
pub struct FutureLoader<A, T>
where
A: AppLike,
{
loading_title: String,
started: Instant,
panel: Panel,
receiver: oneshot::Receiver<Result<Box<dyn Send + FnOnce(&A) -> T>>>,
on_load: Option<Box<dyn FnOnce(&mut EventCtx, &mut A, Result<T>) -> Transition<A>>>,
outer_progress_receiver: Option<mpsc::Receiver<String>>,
inner_progress_receiver: Option<mpsc::Receiver<String>>,
last_outer_progress: String,
last_inner_progress: String,
#[cfg(not(target_arch = "wasm32"))]
#[allow(dead_code)]
runtime: Runtime,
}
impl<A, T> FutureLoader<A, T>
where
A: 'static + AppLike,
T: 'static,
{
#[cfg(target_arch = "wasm32")]
pub fn new_state(
ctx: &mut EventCtx,
future: Pin<Box<dyn Future<Output = Result<Box<dyn Send + FnOnce(&A) -> T>>>>>,
outer_progress_receiver: mpsc::Receiver<String>,
inner_progress_receiver: mpsc::Receiver<String>,
loading_title: &str,
on_load: Box<dyn FnOnce(&mut EventCtx, &mut A, Result<T>) -> Transition<A>>,
) -> Box<dyn State<A>> {
let (tx, receiver) = oneshot::channel();
wasm_bindgen_futures::spawn_local(async move {
tx.send(future.await).ok().unwrap();
});
Box::new(FutureLoader {
loading_title: loading_title.to_string(),
started: Instant::now(),
panel: ctx.make_loading_screen(Text::from(loading_title)),
receiver,
on_load: Some(on_load),
outer_progress_receiver: Some(outer_progress_receiver),
inner_progress_receiver: Some(inner_progress_receiver),
last_outer_progress: String::new(),
last_inner_progress: String::new(),
})
}
#[cfg(not(target_arch = "wasm32"))]
pub fn new_state(
ctx: &mut EventCtx,
future: Pin<Box<dyn Send + Future<Output = Result<Box<dyn Send + FnOnce(&A) -> T>>>>>,
outer_progress_receiver: mpsc::Receiver<String>,
inner_progress_receiver: mpsc::Receiver<String>,
loading_title: &str,
on_load: Box<dyn FnOnce(&mut EventCtx, &mut A, Result<T>) -> Transition<A>>,
) -> Box<dyn State<A>> {
let runtime = Runtime::new().unwrap();
let (tx, receiver) = oneshot::channel();
runtime.spawn(async move {
tx.send(future.await).ok().unwrap();
});
Box::new(FutureLoader {
loading_title: loading_title.to_string(),
started: Instant::now(),
panel: ctx.make_loading_screen(Text::from(loading_title)),
receiver,
on_load: Some(on_load),
runtime,
outer_progress_receiver: Some(outer_progress_receiver),
inner_progress_receiver: Some(inner_progress_receiver),
last_outer_progress: String::new(),
last_inner_progress: String::new(),
})
}
}
impl<A, T> State<A> for FutureLoader<A, T>
where
A: 'static + AppLike,
T: 'static,
{
fn event(&mut self, ctx: &mut EventCtx, app: &mut A) -> Transition<A> {
match self.receiver.try_recv() {
Err(e) => {
error!("channel failed: {:?}", e);
let on_load = self.on_load.take().unwrap();
on_load(ctx, app, Err(anyhow!("channel canceled")))
}
Ok(None) => {
if let Some(ref mut rx) = self.outer_progress_receiver {
loop {
match rx.try_next() {
Ok(Some(msg)) => {
self.last_outer_progress = msg;
}
Ok(None) => {
self.outer_progress_receiver = None;
break;
}
Err(_) => {
break;
}
}
}
}
if let Some(ref mut rx) = self.inner_progress_receiver {
loop {
match rx.try_next() {
Ok(Some(msg)) => {
self.last_inner_progress = msg;
}
Ok(None) => {
self.inner_progress_receiver = None;
break;
}
Err(_) => {
break;
}
}
}
}
self.panel = ctx.make_loading_screen(Text::from_multiline(vec![
Line(&self.loading_title),
Line(format!(
"Time spent: {}",
Duration::realtime_elapsed(self.started)
)),
Line(&self.last_outer_progress),
Line(&self.last_inner_progress),
]));
ctx.request_update(UpdateType::Game);
Transition::Keep
}
Ok(Some(Err(e))) => {
error!("error in fetching data");
let on_load = self.on_load.take().unwrap();
on_load(ctx, app, Err(e))
}
Ok(Some(Ok(builder))) => {
debug!("future complete");
let t = builder(app);
let on_load = self.on_load.take().unwrap();
on_load(ctx, app, Ok(t))
}
}
}
fn draw(&self, g: &mut GfxCtx, _: &A) {
g.clear(Color::BLACK);
self.panel.draw(g);
}
}