Skip to main content

gasm_host/
session.rs

1//! A game ready to start: the module, its assets, parameters, storage and load
2//! options. [`crate::headless::run`] and `window::run` (feature `window`) drive it.
3
4use std::collections::HashMap;
5use std::time::Instant;
6
7use crate::assets::Assets;
8use crate::audio::AudioOut;
9use crate::gfx::Gfx;
10use crate::host::{Game, Host, LoadOptions, Stop};
11use crate::net::Net;
12use crate::storage::Storage;
13
14pub struct Session {
15    /// shown in logs and the window title
16    pub name: String,
17    /// a .wasm module, or a .cwasm if `load.allow_precompiled`
18    pub wasm: Vec<u8>,
19    pub assets: Assets,
20    pub params: HashMap<String, String>,
21    pub allow_net: bool,
22    pub storage: Storage,
23    pub load: LoadOptions,
24}
25
26impl Session {
27    /// Instantiate the guest and run its init. `reproducible`: headless rules
28    /// (virtual time, fixed random sequence, hashing).
29    pub fn start(self, audio: Option<Box<dyn AudioOut>>, gfx: Gfx, reproducible: bool, hashing: bool) -> Result<Game, Stop> {
30        let mut host = Host::new(self.assets, self.params, audio, gfx, Net::new(self.allow_net), self.storage);
31        if reproducible {
32            host.set_reproducible();
33        }
34        host.hashing = hashing;
35        let t0 = Instant::now();
36        let game = Game::load(&self.wasm, host, self.load)?;
37        eprintln!("[gasm] loaded {} in {:.0} ms", self.name, t0.elapsed().as_secs_f64() * 1000.0);
38        Ok(game)
39    }
40}