start implementing run --browser

This commit is contained in:
2022-02-19 16:21:02 +01:00
parent f1493ebded
commit 4c75ba2e44
8 changed files with 993 additions and 49 deletions

View File

@@ -1,6 +1,7 @@
use std::fs::File;
use std::io::prelude::*;
use std::process;
use std::time::Duration;
use std::{
path::{Path, PathBuf},
process::exit,
@@ -8,7 +9,7 @@ use std::{
use anyhow::Result;
use pico_args::Arguments;
use uw8::{FileWatcher, MicroW8};
use uw8::{FileWatcher, MicroW8, RunWebServer};
fn main() -> Result<()> {
let mut args = Arguments::from_env();
@@ -65,42 +66,61 @@ fn run(mut args: Arguments) -> Result<()> {
config.output_path = Some(path);
}
let run_browser = args.contains(["-b", "--browser"]);
let filename = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
let mut uw8 = MicroW8::new()?;
if !run_browser {
let mut uw8 = MicroW8::new()?;
if let Some(timeout) = timeout {
uw8.set_timeout(timeout);
}
let mut watcher = FileWatcher::builder();
if watch_mode {
watcher.add_file(&filename);
}
let watcher = watcher.build()?;
if let Err(err) = start_cart(&filename, &mut uw8, &config) {
eprintln!("Load 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 Some(timeout) = timeout {
uw8.set_timeout(timeout);
}
if let Err(err) = uw8.run_frame() {
eprintln!("Runtime error: {}", err);
let mut watcher = FileWatcher::builder();
if watch_mode {
watcher.add_file(&filename);
}
let watcher = watcher.build()?;
if let Err(err) = start_cart(&filename, &mut uw8, &config) {
eprintln!("Load 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 {
std::thread::sleep(Duration::from_millis(100));
}
}
Ok(())
@@ -112,7 +132,7 @@ struct Config {
output_path: Option<PathBuf>,
}
fn load_cart(filename: &Path, pack: &Option<uw8_tool::PackConfig>) -> Result<Vec<u8>> {
fn load_cart(filename: &Path, config: &Config) -> Result<Vec<u8>> {
let mut cart = vec![];
File::open(filename)?.read_to_end(&mut cart)?;
@@ -125,20 +145,20 @@ fn load_cart(filename: &Path, pack: &Option<uw8_tool::PackConfig>) -> Result<Vec
};
}
if let Some(pack_config) = pack {
if let Some(ref pack_config) = config.pack {
cart = uw8_tool::pack(&cart, pack_config)?;
println!("packed size: {} bytes", cart.len());
}
if let Some(ref path) = config.output_path {
File::create(path)?.write_all(&cart)?;
}
Ok(cart)
}
fn start_cart(filename: &Path, uw8: &mut MicroW8, config: &Config) -> Result<()> {
let cart = load_cart(filename, &config.pack)?;
if let Some(ref path) = config.output_path {
File::create(path)?.write_all(&cart)?;
}
let cart = load_cart(filename, config)?;
if let Err(err) = uw8.load_from_memory(&cart) {
eprintln!("Load error: {}", err);
@@ -163,7 +183,13 @@ fn pack(mut args: Arguments) -> Result<()> {
let out_file = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
let cart = load_cart(&in_file, &Some(pack_config))?;
let cart = load_cart(
&in_file,
&Config {
pack: Some(pack_config),
output_path: None,
},
)?;
File::create(out_file)?.write_all(&cart)?;
@@ -173,7 +199,7 @@ fn pack(mut args: Arguments) -> Result<()> {
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()
}