feat(runtime): implement shared transactional memcard with async saves
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
This commit is contained in:
@@ -15,6 +15,10 @@ use prometeu_system::windows::WindowOwner;
|
||||
pub struct LoadCartridgeStep {
|
||||
pub cartridge: Cartridge,
|
||||
init_error: Option<CrashReport>,
|
||||
awaiting_memcard: bool,
|
||||
preparing_memcard: bool,
|
||||
input_released: bool,
|
||||
memcard_message: String,
|
||||
loaded_task_id: Option<prometeu_system::task::TaskId>,
|
||||
loaded_app_mode: Option<AppMode>,
|
||||
failure_recovery: LoadCartridgeFailureRecovery,
|
||||
@@ -31,12 +35,20 @@ impl LoadCartridgeStep {
|
||||
Self {
|
||||
cartridge,
|
||||
init_error: None,
|
||||
awaiting_memcard: false,
|
||||
preparing_memcard: false,
|
||||
input_released: false,
|
||||
memcard_message: String::new(),
|
||||
loaded_task_id: None,
|
||||
loaded_app_mode: None,
|
||||
failure_recovery: LoadCartridgeFailureRecovery::AppCrash,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn needs_memcard_choice(&self) -> bool {
|
||||
self.awaiting_memcard && !self.preparing_memcard
|
||||
}
|
||||
|
||||
pub fn new_game_switch(cartridge: Cartridge) -> Self {
|
||||
Self { failure_recovery: LoadCartridgeFailureRecovery::ReturnToHub, ..Self::new(cartridge) }
|
||||
}
|
||||
@@ -51,6 +63,15 @@ impl LoadCartridgeStep {
|
||||
|
||||
ctx.platform.game2d_frame_composer().unbind_scene();
|
||||
|
||||
if self.cartridge.app_mode == AppMode::Game && self.cartridge.memcard_slots.is_some() {
|
||||
self.awaiting_memcard = true;
|
||||
ctx.os.vm().transition_render_owner(AppMode::Shell, 0);
|
||||
return;
|
||||
}
|
||||
self.load(ctx);
|
||||
}
|
||||
|
||||
fn load(&mut self, ctx: &mut PrometeuContext) {
|
||||
// Initialize Asset Manager
|
||||
ctx.platform.assets_mut().initialize_for_cartridge(
|
||||
self.cartridge.asset_table.clone(),
|
||||
@@ -70,6 +91,58 @@ impl LoadCartridgeStep {
|
||||
}
|
||||
|
||||
pub fn on_update(&mut self, ctx: &mut PrometeuContext) -> Option<FirmwareState> {
|
||||
if self.awaiting_memcard {
|
||||
use prometeu_system::memcard::admission::{Choice, PreparationState};
|
||||
if self.preparing_memcard {
|
||||
match ctx.os.memcard().admission.state {
|
||||
PreparationState::Ready => {
|
||||
self.awaiting_memcard = false;
|
||||
self.preparing_memcard = false;
|
||||
self.load(ctx);
|
||||
}
|
||||
PreparationState::Failed(error) => {
|
||||
self.memcard_message = Self::preparation_message(error);
|
||||
self.preparing_memcard = false;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if self.awaiting_memcard {
|
||||
let pressed = ctx.signals.a_signal
|
||||
|| ctx.signals.b_signal
|
||||
|| ctx.signals.x_signal
|
||||
|| ctx.signals.y_signal;
|
||||
if !pressed {
|
||||
self.input_released = true;
|
||||
}
|
||||
if self.input_released && pressed && !self.preparing_memcard {
|
||||
self.input_released = false;
|
||||
if ctx.signals.b_signal {
|
||||
return Some(FirmwareState::HubHome(HubHomeStep));
|
||||
}
|
||||
let choice = if ctx.signals.a_signal {
|
||||
Choice::Persistent
|
||||
} else if ctx.signals.x_signal {
|
||||
Choice::MemoryFresh
|
||||
} else {
|
||||
Choice::MemoryExisting
|
||||
};
|
||||
let created_at = ctx.platform.wall_clock().utc_unix_seconds();
|
||||
match ctx.os.memcard().choose_for_game(
|
||||
self.cartridge.app_id,
|
||||
self.cartridge.memcard_slots.unwrap() as usize,
|
||||
choice,
|
||||
created_at,
|
||||
) {
|
||||
Ok(()) => self.preparing_memcard = true,
|
||||
Err(error) => self.memcard_message = Self::preparation_message(error),
|
||||
}
|
||||
}
|
||||
self.draw_memcard(ctx);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(report) = self.init_error.take() {
|
||||
if self.failure_recovery == LoadCartridgeFailureRecovery::ReturnToHub {
|
||||
if self.cartridge.app_mode == AppMode::Game
|
||||
@@ -106,6 +179,18 @@ impl LoadCartridgeStep {
|
||||
return Some(FirmwareState::AppCrashes(AppCrashesStep { report }));
|
||||
}
|
||||
|
||||
match ctx.platform.assets().preloads_ready() {
|
||||
Ok(false) => return None,
|
||||
Err(error) => {
|
||||
return Some(FirmwareState::AppCrashes(AppCrashesStep {
|
||||
report: CrashReport::VmPanic {
|
||||
message: format!("Cartridge preload failed: {error:?}"),
|
||||
pc: None,
|
||||
},
|
||||
}));
|
||||
}
|
||||
Ok(true) => {}
|
||||
}
|
||||
let Some(task_id) = self.loaded_task_id.take() else {
|
||||
let report = CrashReport::VmPanic {
|
||||
message: format!(
|
||||
@@ -131,5 +216,67 @@ impl LoadCartridgeStep {
|
||||
Some(FirmwareState::GameRunning(GameRunningStep::new(task_id)))
|
||||
}
|
||||
|
||||
fn preparation_message(error: prometeu_system::memcard::image::Error) -> String {
|
||||
use prometeu_system::memcard::image::Error;
|
||||
match error {
|
||||
Error::NoSpace => "This memory card has no room for this game.".into(),
|
||||
Error::Corrupt => "The memory card failed its integrity checks.".into(),
|
||||
Error::ReadOnly => "This memory card cannot accept saves.".into(),
|
||||
Error::OutOfMemory => "There is not enough memory to start this game.".into(),
|
||||
Error::Busy => "The memory card is still busy. Please wait.".into(),
|
||||
Error::InvalidState => "No usable memory card or existing save is available.".into(),
|
||||
_ => format!("Memory card unavailable: {error:?}"),
|
||||
}
|
||||
}
|
||||
fn draw_memcard(&self, ctx: &mut PrometeuContext) {
|
||||
use prometeu_hal::{FrameId, GfxUiCommand, RenderSubmission, ShellUiFramePacket};
|
||||
let installed = ctx.os.memcard().card_installed();
|
||||
let existing = ctx.os.memcard().card_has_game(self.cartridge.app_id);
|
||||
let mut lines = vec!["Save memory".to_string(), self.cartridge.title.clone()];
|
||||
if let Some((recovered, degraded, blocked)) = ctx.os.memcard().installed_health() {
|
||||
if recovered {
|
||||
lines.push("Recovered an older save after an integrity failure.".into());
|
||||
}
|
||||
if degraded {
|
||||
lines.push("Memory card degraded: some blocks are excluded.".into());
|
||||
}
|
||||
if blocked {
|
||||
lines.push("Disk saving is unavailable on this card.".into());
|
||||
}
|
||||
}
|
||||
if let prometeu_system::memcard::admission::PreparationState::Failed(error) =
|
||||
ctx.os.memcard().admission.state
|
||||
{
|
||||
lines.push(Self::preparation_message(error));
|
||||
}
|
||||
|
||||
if self.preparing_memcard {
|
||||
lines.push("Preparing save memory...".into());
|
||||
} else {
|
||||
lines.push(
|
||||
if installed { "A: Use installed memory card" } else { "No memory card installed" }
|
||||
.into(),
|
||||
);
|
||||
lines.push("X: Start fresh without saving to disk".into());
|
||||
if existing {
|
||||
lines.push("Y: Load saves, then play without disk saving".into());
|
||||
}
|
||||
lines.push("B: Return to Home".into());
|
||||
}
|
||||
if !self.memcard_message.is_empty() {
|
||||
lines.push(self.memcard_message.clone());
|
||||
}
|
||||
let mut commands = vec![GfxUiCommand::Clear { color: Color::BLACK }];
|
||||
commands.extend(lines.into_iter().enumerate().map(|(i, text)| GfxUiCommand::DrawText {
|
||||
x: 8,
|
||||
y: 8 + i as i32 * 20,
|
||||
text,
|
||||
color: Color::WHITE,
|
||||
}));
|
||||
let _ = ctx.platform.render_submission_sink().submit_render_submission(
|
||||
RenderSubmission::shell_ui(FrameId::ZERO, ShellUiFramePacket::new(commands)),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn on_exit(&mut self, _ctx: &mut PrometeuContext) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user