use std::collections::HashMap;
use std::time::Instant;
use crate::assets::Assets;
use crate::audio::AudioOut;
use crate::angle::Angle;
use crate::gfx::Gfx;
use crate::host::{Game, Host, LoadOptions, Stop};
use crate::net::Net;
use crate::storage::Storage;
pub struct Session {
pub name: String,
pub wasm: Vec<u8>,
pub assets: Assets,
pub params: HashMap<String, String>,
pub allow_net: bool,
pub allow_hosts: Vec<String>,
pub fetch: crate::fetch::FetchMode,
pub app_id: Option<String>,
pub save: crate::files::SaveTarget,
pub consent: Option<crate::consent::Consent>,
pub storage: Storage,
pub load: LoadOptions,
pub gl_lib: Option<std::path::PathBuf>,
pub gl_software: bool,
pub gl_stats: bool,
pub watch_assets: Vec<crate::assets::AssetWatch>,
}
impl Session {
pub fn uses_gl(&self) -> bool {
crate::host::imports_module(&self.wasm, "gasm:gl", self.load.allow_precompiled)
}
pub fn open_gl(&self, window: Option<crate::angle::NativeWindow>, size: (u32, u32)) -> Result<Angle, String> {
let dir = crate::angle::find(self.gl_lib.as_deref())?;
let open = |software| match window {
Some(w) => Angle::window(&dir, w, software),
None => Angle::offscreen(&dir, size.0, size.1, software),
};
match open(self.gl_software) {
Err(e) if !self.gl_software => {
eprintln!("[gasm] gl: {e}; trying SwiftShader (software)");
open(true)
}
r => r,
}
}
pub fn compile_in_background(&self) -> std::thread::JoinHandle<Result<wasmtime::Module, Stop>> {
let (wasm, allow) = (self.wasm.clone(), self.load.allow_precompiled);
std::thread::spawn(move || Game::compile(&wasm, allow))
}
pub fn start(self, audio: Option<Box<dyn AudioOut>>, gfx: Gfx, gl: Option<Angle>, reproducible: bool, hashing: bool) -> Result<Game, Stop> {
self.start_with(None, audio, gfx, gl, reproducible, hashing)
}
pub fn start_compiled(self, module: wasmtime::Module, audio: Option<Box<dyn AudioOut>>, gfx: Gfx, gl: Option<Angle>) -> Result<Game, Stop> {
self.start_with(Some(module), audio, gfx, gl, false, false)
}
fn start_with(self, module: Option<wasmtime::Module>, audio: Option<Box<dyn AudioOut>>, gfx: Gfx, gl: Option<Angle>, reproducible: bool, hashing: bool) -> Result<Game, Stop> {
let policy = crate::net::NetPolicy::new(self.allow_net, self.allow_hosts).with_consent(self.consent.clone());
let mut host = Host::new(self.assets, self.params, audio, gfx, Net::with_policy(policy.clone()), self.storage);
host.fetch = crate::fetch::Fetch::new(policy, self.fetch);
host.fetch.set_app_id(self.app_id.as_deref());
host.files = crate::files::Files::new(self.save).with_consent(self.consent.clone());
if self.gl_stats {
host.gl.stats = Some(Default::default());
}
if let Some(a) = gl {
eprintln!("[gasm] gl: {}", a.renderer);
host.gl.attach(a);
}
if reproducible {
host.set_reproducible();
}
host.hashing = hashing;
let t0 = Instant::now();
let module = match module {
Some(m) => m,
None => Game::compile(&self.wasm, self.load.allow_precompiled)?,
};
let mut game = Game::load_module(module, host, self.load)?;
for w in self.watch_assets {
game.add_watch(w);
}
eprintln!("[gasm] loaded {} in {:.0} ms", self.name, t0.elapsed().as_secs_f64() * 1000.0);
Ok(game)
}
}