use crate::CrashReport; use crate::os::LifecycleError; use crate::os::SystemOS; use crate::process::ProcessId; use crate::task::TaskId; use crate::vm_session::{VmSession, VmSessionError}; use prometeu_hal::app_mode::AppMode; use prometeu_hal::cartridge::Cartridge; use std::sync::Arc; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct LoadedVmCartridge { pub task_id: TaskId, pub app_mode: AppMode, pub reused_existing_session: bool, } #[derive(Debug, Clone, PartialEq, Eq)] pub enum SessionError { Lifecycle(LifecycleError), VmSession(VmSessionError), TaskNotFound(TaskId), ProcessNotFound(ProcessId), MissingVmSession(TaskId), Initialization(CrashReport), } impl SessionError { pub fn into_crash_report(self, operation: &str) -> CrashReport { match self { Self::Initialization(report) => report, error => { CrashReport::VmPanic { message: format!("{operation} failed: {error:?}"), pc: None } } } } } impl From for SessionError { fn from(error: LifecycleError) -> Self { Self::Lifecycle(error) } } impl From for SessionError { fn from(error: VmSessionError) -> Self { Self::VmSession(error) } } pub struct SessionsFacade<'a> { pub(in crate::os) os: &'a mut SystemOS, } impl<'a> SessionsFacade<'a> { #[cfg(test)] pub fn create_vm_game_task(&mut self, app_id: u32, title: impl Into) -> TaskId { self.try_create_vm_game_task(app_id, title) .expect("test/setup VM game creation should succeed") } pub fn try_create_vm_game_task( &mut self, app_id: u32, title: impl Into, ) -> Result { if let Some(session) = self.os.vm_sessions.resident_game_for_app(app_id) { return Ok(session.task_id); } let title = title.into(); let process_id = self.os.process_manager.spawn_vm_game(app_id, title.clone()); let task_id = self.os.task_manager.create_game_task(process_id, app_id, title); self.os.lifecycle().set_game_foreground_task(task_id)?; self.try_create_vm_session_for_task(task_id)?; Ok(task_id) } #[cfg(test)] pub fn create_vm_shell_task(&mut self, app_id: u32, title: impl Into) -> TaskId { self.try_create_vm_shell_task(app_id, title) .expect("test/setup VM shell creation should succeed") } pub fn try_create_vm_shell_task( &mut self, app_id: u32, title: impl Into, ) -> Result { let title = title.into(); let process_id = self.os.process_manager.spawn_vm_shell(app_id, title.clone()); let task_id = self.os.task_manager.create_shell_task(process_id, app_id, title); self.os.lifecycle().set_shell_foreground_task(task_id)?; self.try_create_vm_session_for_task(task_id)?; Ok(task_id) } #[cfg(test)] pub fn create_native_shell_task(&mut self, app_id: u32, title: impl Into) -> TaskId { self.try_create_native_shell_task(app_id, title) .expect("test/setup native shell creation should succeed") } pub fn try_create_native_shell_task( &mut self, app_id: u32, title: impl Into, ) -> Result { let title = title.into(); let process_id = self.os.process_manager.spawn_native_shell(app_id, title.clone()); let task_id = self.os.task_manager.create_shell_task(process_id, app_id, title); self.os.lifecycle().set_shell_foreground_task(task_id)?; Ok(task_id) } pub fn load_vm_cartridge( &mut self, cartridge: &Cartridge, ) -> Result { let existing_game_task = if cartridge.app_mode == AppMode::Game { self.os .vm_sessions .resident_game_for_app(cartridge.app_id) .map(|session| session.task_id) } else { None }; let (task_id, reused_existing_session) = if let Some(task_id) = existing_game_task { (task_id, true) } else if cartridge.app_mode == AppMode::Shell { (self.try_create_vm_shell_task(cartridge.app_id, cartridge.title.clone())?, false) } else { let task_id = self.try_create_vm_game_task(cartridge.app_id, cartridge.title.clone())?; (task_id, false) }; if !reused_existing_session { self.try_initialize_session_cartridge(task_id, cartridge)?; } Ok(LoadedVmCartridge { task_id, app_mode: cartridge.app_mode, reused_existing_session }) } pub fn vm_session_for_task(&self, task_id: TaskId) -> Option<&VmSession> { self.os.vm_sessions.for_task(task_id) } pub fn vm_session_for_task_mut(&mut self, task_id: TaskId) -> Option<&mut VmSession> { self.os.vm_sessions.for_task_mut(task_id) } pub fn resident_game_session_for_app(&self, app_id: u32) -> Option<&VmSession> { self.os.vm_sessions.resident_game_for_app(app_id) } pub fn vm_session_count(&self) -> usize { self.os.vm_sessions.len() } pub fn clear_vm_sessions(&mut self) { self.os.vm_sessions.clear(); } pub fn try_create_vm_session_for_task(&mut self, task_id: TaskId) -> Result<(), SessionError> { let task = self.os.task_manager.get(task_id).ok_or(SessionError::TaskNotFound(task_id))?.clone(); let process = self .os .process_manager .get(task.process_id) .ok_or(SessionError::ProcessNotFound(task.process_id))? .clone(); let cap_config = self.os.vm_runtime.certifier.config; let logs_count = Arc::clone(&self.os.log_service.logs_count); self.os .vm_sessions .create_for_task(&task, &process, "", Some(cap_config), logs_count) .map(|_| ()) .map_err(SessionError::from) } pub fn try_initialize_session_cartridge( &mut self, task_id: TaskId, cartridge: &Cartridge, ) -> Result<(), SessionError> { let session = self .os .vm_sessions .for_task_mut(task_id) .ok_or(SessionError::MissingVmSession(task_id))?; session.app_id = cartridge.app_id; session.title = cartridge.title.clone(); session.app_version = cartridge.app_version.clone(); session.app_mode = cartridge.app_mode; session.clear_cartridge_service_state(); session .runtime .initialize_vm(&mut self.os.log_service, &mut session.vm, cartridge) .map_err(SessionError::Initialization) } }