make both native and browser runtime optional

This commit is contained in:
2022-02-21 22:23:52 +01:00
parent 90467f7c5b
commit 44b8656f29
7 changed files with 374 additions and 330 deletions

View File

@@ -1,24 +1,26 @@
use std::fs::File;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use std::process;
use std::time::Duration;
use std::{
path::{Path, PathBuf},
process::exit,
};
use anyhow::Result;
use pico_args::Arguments;
use uw8::{FileWatcher, MicroW8, RunWebServer};
#[cfg(feature = "native")]
use uw8::MicroW8;
#[cfg(feature = "browser")]
use uw8::RunWebServer;
#[cfg(any(feature = "native", feature = "browser"))]
use uw8::Runtime;
fn main() -> Result<()> {
let mut args = Arguments::from_env();
match args.subcommand()?.as_ref().map(|s| s.as_str()) {
match args.subcommand()?.as_deref() {
Some("version") => {
println!("{}", env!("CARGO_PKG_VERSION"));
Ok(())
}
#[cfg(any(feature = "native", feature = "browser"))]
Some("run") => run(args),
Some("pack") => pack(args),
Some("unpack") => unpack(args),
@@ -28,7 +30,8 @@ fn main() -> Result<()> {
println!("uw8 {}", env!("CARGO_PKG_VERSION"));
println!();
println!("Usage:");
println!(" uw8 run [-t/--timeout <frames>] [-w/--watch] [-p/--pack] [-u/--uncompressed] [-l/--level] [-o/--output <out-file>] <file>");
#[cfg(any(feature = "native", feature = "browser"))]
println!(" uw8 run [-t/--timeout <frames>] [--b/--browser] [-w/--watch] [-p/--pack] [-u/--uncompressed] [-l/--level] [-o/--output <out-file>] <file>");
println!(" uw8 pack [-u/--uncompressed] [-l/--level] <in-file> <out-file>");
println!(" uw8 unpack <in-file> <out-file>");
println!(" uw8 compile [-d/--debug] <in-file> <out-file>");
@@ -42,6 +45,7 @@ fn main() -> Result<()> {
}
}
#[cfg(any(feature = "native", feature = "browser"))]
fn run(mut args: Arguments) -> Result<()> {
let watch_mode = args.contains(["-w", "--watch"]);
let timeout: Option<u32> = args.opt_value_from_str(["-t", "--timeout"])?;
@@ -66,11 +70,14 @@ fn run(mut args: Arguments) -> Result<()> {
config.output_path = Some(path);
}
#[cfg(feature = "native")]
let run_browser = args.contains(["-b", "--browser"]);
#[cfg(not(feature = "native"))]
let run_browser = args.contains(["-b", "--browser"]) || true;
let filename = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
let mut watcher = FileWatcher::builder();
let mut watcher = uw8::FileWatcher::builder();
if watch_mode {
watcher.add_file(&filename);
@@ -78,57 +85,44 @@ fn run(mut args: Arguments) -> Result<()> {
let watcher = watcher.build()?;
if !run_browser {
let mut uw8 = MicroW8::new()?;
use std::process::exit;
if let Some(timeout) = timeout {
uw8.set_timeout(timeout);
let mut runtime: Box<dyn Runtime> = if !run_browser {
#[cfg(not(feature = "native"))]
unimplemented!();
#[cfg(feature = "native")]
Box::new(MicroW8::new()?)
} else {
#[cfg(not(feature = "browser"))]
unimplemented!();
#[cfg(feature = "browser")]
Box::new(RunWebServer::new())
};
if let Some(timeout) = timeout {
runtime.set_timeout(timeout);
}
if let Err(err) = start_cart(&filename, &mut *runtime, &config) {
eprintln!("Load error: {}", err);
if !watch_mode {
exit(1);
}
}
while runtime.is_open() {
if watcher.poll_changed_file()?.is_some() {
if let Err(err) = start_cart(&filename, &mut *runtime, &config) {
eprintln!("Load error: {}", err);
}
}
if let Err(err) = start_cart(&filename, &mut uw8, &config) {
eprintln!("Load error: {}", err);
if let Err(err) = runtime.run_frame() {
eprintln!("Runtime error: {}", err);
if !watch_mode {
exit(1);
}
}
while uw8.is_open() {
if watcher.poll_changed_file()?.is_some() {
if let Err(err) = start_cart(&filename, &mut uw8, &config) {
eprintln!("Load error: {}", err);
}
}
if let Err(err) = uw8.run_frame() {
eprintln!("Runtime error: {}", err);
if !watch_mode {
exit(1);
}
}
}
} else {
let mut server = RunWebServer::new();
match load_cart(&filename, &config) {
Ok(cart) => server.load_module(&cart)?,
Err(err) => {
eprintln!("Load error: {}", err);
if !watch_mode {
exit(1);
}
}
}
loop {
if watcher.poll_changed_file()?.is_some() {
match load_cart(&filename, &config) {
Ok(cart) => server.load_module(&cart)?,
Err(err) => {
eprintln!("Load error: {}", err);
}
}
}
std::thread::sleep(Duration::from_millis(100));
}
}
Ok(())
@@ -165,10 +159,11 @@ fn load_cart(filename: &Path, config: &Config) -> Result<Vec<u8>> {
Ok(cart)
}
fn start_cart(filename: &Path, uw8: &mut MicroW8, config: &Config) -> Result<()> {
#[cfg(any(feature = "native", feature = "browser"))]
fn start_cart(filename: &Path, runtime: &mut dyn Runtime, config: &Config) -> Result<()> {
let cart = load_cart(filename, config)?;
if let Err(err) = uw8.load_from_memory(&cart) {
if let Err(err) = runtime.load(&cart) {
eprintln!("Load error: {}", err);
Err(err)
} else {
@@ -208,7 +203,7 @@ fn unpack(mut args: Arguments) -> Result<()> {
let in_file = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
let out_file = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
uw8_tool::unpack_file(&in_file, &out_file).into()
uw8_tool::unpack_file(&in_file, &out_file)
}
fn compile(mut args: Arguments) -> Result<()> {