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

873
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -15,3 +15,6 @@ curlywas = { git = "https://github.com/exoticorn/curlywas.git", rev = "196719b"
wat = "1"
uw8-tool = { path = "uw8-tool" }
same-file = "1"
warp = "0.3.2"
tokio = { version = "1.17.0", features = ["sync", "rt"] }
webbrowser = "0.6.0"

View File

@@ -11,8 +11,10 @@ use wasmtime::{
};
mod filewatcher;
mod run_web;
pub use filewatcher::FileWatcher;
pub use run_web::RunWebServer;
static GAMEPAD_KEYS: &'static [Key] = &[
Key::Up,

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,8 +66,11 @@ 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()))?;
if !run_browser {
let mut uw8 = MicroW8::new()?;
if let Some(timeout) = timeout {
@@ -102,6 +106,22 @@ fn run(mut args: Arguments) -> Result<()> {
}
}
}
} 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)?;

34
src/run_web.rs Normal file
View File

@@ -0,0 +1,34 @@
use anyhow::Result;
use std::thread;
use warp::{http::Response, Filter};
pub struct RunWebServer {}
impl RunWebServer {
pub fn new() -> RunWebServer {
thread::spawn(move || {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_io()
.build()
.expect("Failed to create tokio runtime");
rt.block_on(async {
let html = warp::path::end().map(|| {
Response::builder()
.header("Content-Type", "text/html")
.body(include_str!("run-web.html"))
});
let server_future = warp::serve(html).bind(([127, 0, 0, 1], 3030));
println!("Point browser at 127.0.0.1:3030");
let _ = webbrowser::open("http://127.0.0.1:3030");
server_future.await
});
});
RunWebServer {}
}
pub fn load_module(&mut self, module_data: &[u8]) -> Result<()> {
Ok(())
}
}

View File

@@ -1,7 +1,7 @@
import loaderUrl from "data-url:../../platform/bin/loader.wasm";
import platformUrl from "data-url:../../platform/bin/platform.uw8";
export default function MicroW8(screen, config) {
export default function MicroW8(screen, config = {}) {
let canvasCtx = screen.getContext('2d');
let imageData = canvasCtx.createImageData(320, 240);
@@ -310,5 +310,3 @@ export default function MicroW8(screen, config) {
setDevkitMode: (m) => devkitMode = m,
};
}
this.uw8 = MicroW8;

14
web/src/run-web.html Normal file
View File

@@ -0,0 +1,14 @@
<!doctype html>
<html>
<head>
<meta charset="utf8" />
<title>uw8-run</title>
</head>
<body>
<canvas id="screen" width="320" height="240">
</canvas>
</body>
<script type="module">
import "./run-web.js";
</script>
</html>

10
web/src/run-web.js Normal file
View File

@@ -0,0 +1,10 @@
import MicroW8 from './microw8.js';
let uw8 = MicroW8(document.getElementById('screen'));
let events = new EventSource('events');
events.onmessage = event => {
console.log(event.data);
if(event.data == 'L') {
uw8.runModuleFromURL('cart');
}
};