feat: implement cartridge v1 loader and contract
This commit is contained in:
@@ -1,29 +1,40 @@
|
||||
use crate::virtual_machine::Program;
|
||||
use std::path::PathBuf;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
|
||||
pub enum AppMode {
|
||||
Game,
|
||||
System,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AppHeader {
|
||||
pub mode: AppMode,
|
||||
pub app_id: String,
|
||||
pub magic: u32,
|
||||
pub version: u16,
|
||||
pub title: String,
|
||||
pub entrypoint: u32,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Cartridge {
|
||||
pub header: AppHeader,
|
||||
pub program: Program,
|
||||
pub app_id: u32,
|
||||
pub title: String,
|
||||
pub app_version: String,
|
||||
pub app_mode: AppMode,
|
||||
pub entrypoint: String,
|
||||
pub program: Vec<u8>,
|
||||
pub assets_path: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl Cartridge {
|
||||
pub fn new(header: AppHeader, program: Program) -> Self {
|
||||
Self { header, program }
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub enum CartridgeError {
|
||||
NotFound,
|
||||
InvalidFormat,
|
||||
InvalidManifest,
|
||||
UnsupportedVersion,
|
||||
MissingProgram,
|
||||
IoError,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CartridgeManifest {
|
||||
pub magic: String,
|
||||
pub cartridge_version: u32,
|
||||
pub app_id: u32,
|
||||
pub title: String,
|
||||
pub app_version: String,
|
||||
pub app_mode: AppMode,
|
||||
pub entrypoint: String,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use crate::model::cartridge::{Cartridge, CartridgeError, CartridgeManifest};
|
||||
|
||||
pub struct CartridgeLoader;
|
||||
|
||||
impl CartridgeLoader {
|
||||
pub fn load(path: impl AsRef<Path>) -> Result<Cartridge, CartridgeError> {
|
||||
let path = path.as_ref();
|
||||
if !path.exists() {
|
||||
return Err(CartridgeError::NotFound);
|
||||
}
|
||||
|
||||
if path.is_dir() {
|
||||
DirectoryCartridgeLoader::load(path)
|
||||
} else if path.extension().is_some_and(|ext| ext == "pmc") {
|
||||
PackedCartridgeLoader::load(path)
|
||||
} else {
|
||||
Err(CartridgeError::InvalidFormat)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DirectoryCartridgeLoader;
|
||||
|
||||
impl DirectoryCartridgeLoader {
|
||||
pub fn load(path: &Path) -> Result<Cartridge, CartridgeError> {
|
||||
let manifest_path = path.join("manifest.json");
|
||||
if !manifest_path.exists() {
|
||||
return Err(CartridgeError::InvalidManifest);
|
||||
}
|
||||
|
||||
let manifest_content = fs::read_to_string(manifest_path).map_err(|_| CartridgeError::IoError)?;
|
||||
let manifest: CartridgeManifest = serde_json::from_str(&manifest_content).map_err(|_| CartridgeError::InvalidManifest)?;
|
||||
|
||||
// Validação adicional conforme requisitos
|
||||
if manifest.magic != "PMTU" {
|
||||
return Err(CartridgeError::InvalidManifest);
|
||||
}
|
||||
if manifest.cartridge_version != 1 {
|
||||
return Err(CartridgeError::UnsupportedVersion);
|
||||
}
|
||||
|
||||
let program_path = path.join("program.pbc");
|
||||
if !program_path.exists() {
|
||||
return Err(CartridgeError::MissingProgram);
|
||||
}
|
||||
|
||||
let program = fs::read(program_path).map_err(|_| CartridgeError::IoError)?;
|
||||
|
||||
let assets_path = path.join("assets");
|
||||
let assets_path = if assets_path.exists() && assets_path.is_dir() {
|
||||
Some(assets_path)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Ok(Cartridge {
|
||||
app_id: manifest.app_id,
|
||||
title: manifest.title,
|
||||
app_version: manifest.app_version,
|
||||
app_mode: manifest.app_mode,
|
||||
entrypoint: manifest.entrypoint,
|
||||
program,
|
||||
assets_path,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PackedCartridgeLoader;
|
||||
|
||||
impl PackedCartridgeLoader {
|
||||
pub fn load(_path: &Path) -> Result<Cartridge, CartridgeError> {
|
||||
// Stub inicialmente, como solicitado
|
||||
Err(CartridgeError::InvalidFormat)
|
||||
}
|
||||
}
|
||||
@@ -6,10 +6,12 @@ mod tile_bank;
|
||||
mod sprite;
|
||||
mod sample;
|
||||
mod cartridge;
|
||||
mod cartridge_loader;
|
||||
mod window;
|
||||
|
||||
pub use button::Button;
|
||||
pub use cartridge::{AppHeader, AppMode, Cartridge};
|
||||
pub use cartridge::{AppMode, Cartridge, CartridgeError};
|
||||
pub use cartridge_loader::{CartridgeLoader, DirectoryCartridgeLoader, PackedCartridgeLoader};
|
||||
pub use color::Color;
|
||||
pub use sample::Sample;
|
||||
pub use sprite::Sprite;
|
||||
|
||||
Reference in New Issue
Block a user