1use std::collections::HashMap;
5use std::time::Instant;
6
7use crate::assets::Assets;
8use crate::audio::AudioOut;
9use crate::angle::Angle;
10use crate::gfx::Gfx;
11use crate::host::{Game, Host, LoadOptions, Stop};
12use crate::net::Net;
13use crate::storage::Storage;
14
15pub struct Session {
16 pub name: String,
18 pub wasm: Vec<u8>,
20 pub assets: Assets,
21 pub params: HashMap<String, String>,
22 pub allow_net: bool,
23 pub allow_hosts: Vec<String>,
25 pub fetch: crate::fetch::FetchMode,
27 pub app_id: Option<String>,
29 pub save: crate::files::SaveTarget,
31 pub consent: Option<crate::consent::Consent>,
33 pub manifest: Option<crate::manifest::Manifest>,
35 pub mod_requests: Vec<(String, Vec<String>)>,
38 pub storage: Storage,
39 pub load: LoadOptions,
40 pub gl_lib: Option<std::path::PathBuf>,
42 pub gl_software: bool,
44 pub gl_stats: bool,
46 pub watch_assets: Vec<crate::assets::AssetWatch>,
49}
50
51impl Session {
52 pub fn uses_gl(&self) -> bool {
54 crate::host::imports_module(&self.wasm, "gasm:gl", self.load.allow_precompiled)
55 }
56
57 pub fn open_gl(&self, window: Option<crate::angle::NativeWindow>, size: (u32, u32)) -> Result<Angle, String> {
59 let dir = crate::angle::find(self.gl_lib.as_deref())?;
60 let open = |software| match window {
61 Some(w) => Angle::window(&dir, w, software),
62 None => Angle::offscreen(&dir, size.0, size.1, software),
63 };
64 match open(self.gl_software) {
65 Err(e) if !self.gl_software => {
67 eprintln!("[gasm] gl: {e}; trying SwiftShader (software)");
68 open(true)
69 }
70 r => r,
71 }
72 }
73
74 pub fn compile_in_background(&self) -> std::thread::JoinHandle<Result<wasmtime::Module, Stop>> {
80 let (wasm, allow) = (self.wasm.clone(), self.load.allow_precompiled);
81 std::thread::spawn(move || Game::compile(&wasm, allow))
82 }
83
84 pub fn start(self, audio: Option<Box<dyn AudioOut>>, gfx: Gfx, gl: Option<Angle>, reproducible: bool, hashing: bool) -> Result<Game, Stop> {
85 self.start_with(None, audio, gfx, gl, reproducible, hashing)
86 }
87
88 fn decide_mods(&mut self, policy: &crate::net::NetPolicy) {
91 for (file, hosts) in std::mem::take(&mut self.mod_requests) {
92 let covered = policy.allowed && hosts.iter().all(|h| policy.permits(h));
93 let said = || self.consent.as_ref().and_then(|c| c.lock().unwrap().check_mod(&file, &hosts));
94 if covered || said() == Some(true) {
95 eprintln!("[gasm] mods: {file} may connect to {}", hosts.join(", "));
96 continue;
97 }
98 let why = match &self.consent {
99 Some(_) => format!("it connects to {} and the player said no", hosts.join(", ")),
100 None => format!("it connects to {}, which --allow-net doesn't cover", hosts.join(", ")),
101 };
102 eprintln!("[gasm] mods: refused {file} ({why})");
103 self.assets.refuse_mod(&file, &why);
104 }
105 }
106
107 pub fn start_compiled(self, module: wasmtime::Module, audio: Option<Box<dyn AudioOut>>, gfx: Gfx, gl: Option<Angle>) -> Result<Game, Stop> {
109 self.start_with(Some(module), audio, gfx, gl, false, false)
110 }
111
112 fn start_with(mut self, module: Option<wasmtime::Module>, audio: Option<Box<dyn AudioOut>>, gfx: Gfx, gl: Option<Angle>, reproducible: bool, hashing: bool) -> Result<Game, Stop> {
113 let policy = crate::net::NetPolicy::new(self.allow_net, self.allow_hosts.clone()).with_consent(self.consent.clone());
114 self.decide_mods(&policy);
115 let mut host = Host::new(self.assets, self.params, audio, gfx, Net::with_policy(policy.clone()), self.storage);
116 host.requires = self.manifest.map(|m| m.requires).unwrap_or_default();
117 host.fetch = crate::fetch::Fetch::new(policy, self.fetch);
118 host.fetch.set_app_id(self.app_id.as_deref());
119 host.files = crate::files::Files::new(self.save).with_consent(self.consent.clone());
120 if self.gl_stats {
121 host.gl.stats = Some(Default::default());
122 }
123 if let Some(a) = gl {
124 eprintln!("[gasm] gl: {}", a.renderer);
125 host.gl.attach(a);
126 }
127 if reproducible {
128 host.set_reproducible();
129 }
130 host.hashing = hashing;
131 let t0 = Instant::now();
132 let module = match module {
133 Some(m) => m,
134 None => Game::compile(&self.wasm, self.load.allow_precompiled)?,
135 };
136 let mut game = Game::load_module(module, host, self.load)?;
137 for w in self.watch_assets {
138 game.add_watch(w);
139 }
140 eprintln!("[gasm] loaded {} in {:.0} ms", self.name, t0.elapsed().as_secs_f64() * 1000.0);
141 Ok(game)
142 }
143}