use anyhow::{Error, Result};
use wasmer::{Engine, Module};
use super::SM64Game;
use super::memory::MemoryLayout;
use super::util::{check_hash, unzip_bytes, xor_bytes};
use super::wasm_module::{self, BuildId, DataSegments};
const XOR_BYTES: &[u8] = include_bytes!("../../pkg/sm64_headless.us.wasm.zip.xor");
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Backend {
#[default]
Auto,
Cranelift,
Precompiled,
}
#[derive(Debug, Clone)]
pub struct SM64GameGenerator {
source: GameSource,
}
impl SM64GameGenerator {
pub fn new(rom_bytes: Vec<u8>) -> Result<Self, Error> {
if !check_hash(&rom_bytes) {
return Err(Error::msg(
"Invalid ROM. Should be a US copy, in z64 format (8.00MB)",
));
};
Self::from_wasm(Self::rom_to_wasm_bytes(rom_bytes))
}
#[cfg(feature = "fs")]
pub fn from_file(rom_path: &str) -> Result<Self, Error> {
let rom_bytes = std::fs::read(rom_path)?;
Self::new(rom_bytes)
}
pub fn from_wasm(wasm_bytes: impl AsRef<[u8]>) -> Result<Self, Error> {
Self::from_wasm_with(wasm_bytes, Backend::Auto)
}
pub fn from_wasm_with(wasm_bytes: impl AsRef<[u8]>, backend: Backend) -> Result<Self, Error> {
let wasm = wasm_bytes.as_ref();
let source = match backend {
Backend::Auto => GameSource::for_wasm(wasm)?,
Backend::Cranelift => GameSource::cranelift(wasm)?,
Backend::Precompiled => GameSource::precompiled(wasm)?.ok_or_else(|| {
Error::msg("the precompiled module is not for this platform or this WASM")
})?,
};
Ok(SM64GameGenerator { source })
}
pub fn create_game(&self) -> Result<SM64Game, Error> {
SM64Game::from_source(&self.source)
}
pub fn rom_to_wasm_bytes(rom_bytes: Vec<u8>) -> Vec<u8> {
unzip_bytes(&xor_bytes(XOR_BYTES, &rom_bytes))
}
pub fn copy_from(&self, game: &SM64Game) -> Result<SM64Game, Error> {
let mut copy = self.create_game()?;
copy.copy_from(game)?;
Ok(copy)
}
}
#[derive(Debug, Clone)]
pub(super) struct GameSource {
pub(super) engine: Engine,
pub(super) module: Module,
pub(super) segments: Option<std::sync::Arc<DataSegments>>,
pub(super) build_id: BuildId,
pub(super) layout: MemoryLayout,
}
impl GameSource {
fn for_wasm(wasm: &[u8]) -> Result<GameSource> {
let wanted = std::env::var("SM64_BACKEND").ok();
match wanted.as_deref() {
None | Some("cranelift") | Some("precompiled") => {}
Some(other) => {
return Err(Error::msg(format!(
"SM64_BACKEND={other}: expected cranelift or precompiled"
)));
}
}
if wanted.as_deref() != Some("cranelift") {
if let Some(source) = Self::precompiled(wasm)? {
return Ok(source);
}
if wanted.as_deref() == Some("precompiled") {
return Err(Error::msg(
"SM64_BACKEND=precompiled, but the precompiled module is not for this \
platform or was made from a different WASM",
));
}
}
Self::cranelift(wasm)
}
fn cranelift(wasm: &[u8]) -> Result<GameSource> {
let (code, data) = wasm_module::separate_data(wasm)?;
let engine = Engine::default();
let module = Module::new(&engine, wasm)?;
Ok(GameSource {
engine,
module,
segments: None,
build_id: wasm_module::build_id(&code),
layout: MemoryLayout::from_data(&data),
})
}
fn precompiled(wasm: &[u8]) -> Result<Option<GameSource>> {
#[cfg(all(
feature = "precompiled",
target_arch = "x86_64",
target_os = "linux",
target_env = "gnu"
))]
{
let (code, segments) = wasm_module::separate_data(wasm)?;
let build_id = wasm_module::build_id(&code);
Ok(
super::precompiled::load(&build_id)?.map(|(engine, module)| GameSource {
engine,
module,
layout: MemoryLayout::from_data(&segments),
segments: Some(std::sync::Arc::new(segments)),
build_id,
}),
)
}
#[cfg(not(all(
feature = "precompiled",
target_arch = "x86_64",
target_os = "linux",
target_env = "gnu"
)))]
{
let _ = wasm;
Ok(None)
}
}
}