1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Loading large resources requires different strategies on native and web. Both cases are wrapped
//! up as a State that runs a callback when done.

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 abstutil::Timer;
use geom::Duration;

use crate::{Color, EventCtx, GfxCtx, Line, Panel, State, Text, Transition, UpdateType};

#[cfg(not(target_arch = "wasm32"))]
pub use native_loader::FileLoader;

#[cfg(target_arch = "wasm32")]
pub use wasm_loader::FileLoader;

/// Use this with FileLoader to just read raw bytes without any deserialization.
pub struct RawBytes(pub Vec<u8>);

#[cfg(not(target_arch = "wasm32"))]
mod native_loader {
    use super::*;

    pub trait Readable {
        fn read_file(path: String, timer: &mut Timer) -> Result<Self>
        where
            Self: Sized;
    }

    /// Loads a JSON, bincoded, or raw file, then deserializes it
    pub struct FileLoader<A, T> {
        path: String,
        // Wrapped in an Option just to make calling from event() work. Technically this is unsafe
        // if a caller fails to pop the FileLoader state in their transitions!
        on_load:
            Option<Box<dyn FnOnce(&mut EventCtx, &mut A, &mut Timer, Result<T>) -> Transition<A>>>,
    }

    impl<A: 'static, T: 'static + Readable> 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: 'static, T: 'static + Readable> 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 = T::read_file(self.path.clone(), timer);
                (self.on_load.take().unwrap())(ctx, app, timer, file)
            })
        }

        fn draw(&self, g: &mut GfxCtx, _: &A) {
            g.clear(Color::BLACK);
        }
    }

    // Two implementations for reading the file, using serde or just raw bytes
    impl<T: 'static + DeserializeOwned> Readable for T {
        fn read_file(path: String, timer: &mut Timer) -> Result<T> {
            // TODO We'll eventually want to remove the dependency on abstio
            abstio::read_object(path, timer)
        }
    }
    impl Readable for RawBytes {
        fn read_file(path: String, _: &mut Timer) -> Result<RawBytes> {
            abstio::slurp_file(path).map(RawBytes)
        }
    }
}

#[cfg(target_arch = "wasm32")]
mod wasm_loader {
    use std::io::Read;

    use futures::StreamExt;
    use wasm_bindgen::{JsCast, UnwrapThrowExt};
    use wasm_bindgen_futures::JsFuture;
    use wasm_streams::ReadableStream;
    use web_sys::{Request, RequestInit, RequestMode, Response};

    use abstutil::prettyprint_usize;

    use super::*;

    pub trait Readable {
        fn read_url(url: String, resp: Vec<u8>) -> Result<Self>
        where
            Self: Sized;
    }

    /// Loads a JSON, bincoded, or raw file, then deserializes it
    ///
    /// Instead of blockingly reading a file within ctx.loading_screen, on the web have to
    /// asynchronously make an HTTP request and keep "polling" for completion in a way that's
    /// compatible with winit's event loop.
    pub struct FileLoader<A, 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,

        total_bytes: Option<usize>,
        read_bytes: usize,
        got_total_bytes: oneshot::Receiver<usize>,
        got_read_bytes: mpsc::Receiver<usize>,
    }

    impl<A: 'static, T: 'static + Readable> 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`");

            // 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 ctx.prerender.assets_are_gzipped() {
                format!("{}/{}.gz", base_url, path)
            } else {
                format!("{}/{}", base_url, path)
            };

            // Make the HTTP request nonblockingly. When the response is received, send it through
            // the channel.
            let (tx, rx) = oneshot::channel();
            let (tx_total_bytes, got_total_bytes) = oneshot::channel();
            let (mut tx_read_bytes, got_read_bytes) = mpsc::channel(10);
            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 total_bytes = resp
                                .headers()
                                .get("Content-Length")
                                .unwrap()
                                .unwrap()
                                .parse::<usize>()
                                .unwrap();
                            tx_total_bytes.send(total_bytes).unwrap();

                            let raw_body = resp.body().unwrap_throw();
                            let body = ReadableStream::from_raw(raw_body.dyn_into().unwrap_throw());
                            let mut stream = body.into_stream();
                            let mut buffer = Vec::new();
                            while let Some(Ok(chunk)) = stream.next().await {
                                let array = js_sys::Uint8Array::new(&chunk);
                                if let Err(err) =
                                    tx_read_bytes.try_send(array.byte_length() as usize)
                                {
                                    warn!("Couldn't send update on bytes: {}", err);
                                }
                                // TODO Can we avoid this clone?
                                buffer.extend(array.to_vec());
                            }
                            tx.send(Ok(buffer)).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,
                total_bytes: None,
                read_bytes: 0,
                got_total_bytes,
                got_read_bytes,
            })
        }
    }

    impl<A: 'static, T: 'static + Readable> State<A> for FileLoader<A, T> {
        fn event(&mut self, ctx: &mut EventCtx, app: &mut A) -> Transition<A> {
            if self.total_bytes.is_none() {
                if let Ok(Some(total)) = self.got_total_bytes.try_recv() {
                    self.total_bytes = Some(total);
                }
            }
            if let Some(read) = self.got_read_bytes.try_next().ok().and_then(|value| value) {
                self.read_bytes += read;
            }

            if let Some(maybe_resp) = self.response.try_recv().unwrap() {
                // TODO We stop drawing and start blocking at this point. It can take a
                // while. Any way to make it still be nonblockingish? Maybe put some of the work
                // inside that spawn_local?
                let mut timer = Timer::new(format!("Loading {}...", self.url));
                let result = maybe_resp.and_then(|resp| T::read_url(self.url.clone(), resp));
                return (self.on_load.take().unwrap())(ctx, app, &mut timer, result);
            }

            let mut lines = vec![
                Line(format!("Loading {}...", self.url)),
                Line(format!(
                    "Time spent: {}",
                    Duration::realtime_elapsed(self.started)
                )),
            ];
            if let Some(total) = self.total_bytes {
                lines.push(Line(format!(
                    "Read {} / {} bytes",
                    prettyprint_usize(self.read_bytes),
                    prettyprint_usize(total)
                )));
            }
            self.panel = ctx.make_loading_screen(Text::from_multiline(lines));

            // Until the response is received, just ask winit to regularly call event(), so we can
            // keep polling the channel.
            ctx.request_update(UpdateType::Game);
            Transition::Keep
        }

        fn draw(&self, g: &mut GfxCtx, _: &A) {
            // TODO Progress bar for bytes received
            g.clear(Color::BLACK);
            self.panel.draw(g);
        }
    }

    // Two implementations for reading the file, using serde or just raw bytes
    impl<T: 'static + DeserializeOwned> Readable for T {
        fn read_url(url: String, resp: Vec<u8>) -> Result<T> {
            if url.ends_with(".gz") {
                let decoder = flate2::read::GzDecoder::new(&resp[..]);
                if url.ends_with(".bin.gz") {
                    abstutil::from_binary_reader(decoder)
                } else {
                    abstutil::from_json_reader(decoder)
                }
            } else if url.ends_with(".bin") {
                abstutil::from_binary(&&resp)
            } else {
                abstutil::from_json(&&resp)
            }
        }
    }
    impl Readable for RawBytes {
        fn read_url(url: String, resp: Vec<u8>) -> Result<RawBytes> {
            if url.ends_with(".gz") {
                let mut decoder = flate2::read::GzDecoder::new(&resp[..]);
                let mut buffer: Vec<u8> = Vec::new();
                decoder
                    .read_to_end(&mut buffer)
                    .map(|_| RawBytes(buffer))
                    .map_err(|err| err.into())
            } else {
                Ok(RawBytes(resp))
            }
        }
    }
}

pub struct FutureLoader<A, T> {
    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>>>,
    // These're just two different types of progress updates that callers can provide
    outer_progress_receiver: Option<mpsc::Receiver<String>>,
    inner_progress_receiver: Option<mpsc::Receiver<String>>,
    last_outer_progress: String,
    last_inner_progress: String,

    // If Runtime is dropped, any active tasks will be canceled, so we retain it here even
    // though we never access it. It might make more sense for Runtime to live on App if we're
    // going to be doing more background spawning.
    #[cfg(not(target_arch = "wasm32"))]
    #[allow(dead_code)]
    runtime: Runtime,
}

impl<A, T> FutureLoader<A, T>
where
    A: 'static,
    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,
    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 {
                    // Read all of the progress that's happened
                    loop {
                        match rx.try_next() {
                            Ok(Some(msg)) => {
                                self.last_outer_progress = msg;
                            }
                            Ok(None) => {
                                self.outer_progress_receiver = None;
                                break;
                            }
                            Err(_) => {
                                // No messages
                                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(_) => {
                                // No messages
                                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),
                ]));

                // Until the response is received, just ask winit to regularly call event(), so we
                // can keep polling the channel.
                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);
    }
}