gmfwasm: init

main
q3k 2022-04-15 16:51:22 +00:00
parent 8318a1d48b
commit 3af511b650
7 changed files with 287 additions and 0 deletions

133
Cargo.lock generated
View File

@ -22,6 +22,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "bumpalo"
version = "3.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899"
[[package]]
name = "cfg-if"
version = "1.0.0"
@ -46,6 +52,7 @@ name = "gmflib"
version = "0.1.0"
dependencies = [
"gmfmacros",
"serde",
]
[[package]]
@ -64,6 +71,18 @@ dependencies = [
"env_logger",
"gmflib",
"log",
"serde_json",
]
[[package]]
name = "gmfwasm"
version = "0.1.0"
dependencies = [
"gmflib",
"js-sys",
"serde",
"serde_json",
"wasm-bindgen",
]
[[package]]
@ -81,6 +100,27 @@ version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "itoa"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
[[package]]
name = "js-sys"
version = "0.3.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "671a26f820db17c2a2750743f1dd03bafd15b98c9f30c7c2628c024c05d73397"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.123"
@ -137,6 +177,43 @@ version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "ryu"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f"
[[package]]
name = "serde"
version = "1.0.136"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.136"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "1.0.90"
@ -163,6 +240,62 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "wasm-bindgen"
version = "0.2.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27370197c907c55e3f1a9fbe26f44e937fe6451368324e009cba39e139dc08ad"
dependencies = [
"cfg-if",
"serde",
"serde_json",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53e04185bfa3a779273da532f5025e33398409573f348985af9a1cbf3774d3f4"
dependencies = [
"bumpalo",
"lazy_static",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17cae7ff784d7e83a2fe7611cfe766ecf034111b49deb850a3dc7699c08251f5"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99ec0dc7a4756fffc231aab1b9f2f578d23cd391390ab27f952ae0c9b3ece20b"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d554b7f530dee5964d9a9468d95c1f8b8acae4f282807e7d27d4b03099a46744"
[[package]]
name = "winapi"
version = "0.3.9"

View File

@ -4,6 +4,7 @@ members = [
"gmflib",
"gmftool",
"gmfmacros",
"gmfwasm",
]
[profile.release]

1
gmfwasm/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
pkg

16
gmfwasm/Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "gmfwasm"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = { version = "0", features = ["serde-serialize"] }
js-sys = "0"
gmflib = { path = "../gmflib" }
serde = "1"
serde_json = "1"

5
gmfwasm/README.md Normal file
View File

@ -0,0 +1,5 @@
Building:
cargo install wasm-pack
wasm-pack build --target=web
rsync -ayv ./ foo.com:/var/www/foo/

73
gmfwasm/index.html Normal file
View File

@ -0,0 +1,73 @@
<html>
<head>
<title>GMF web tooling demo</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
</head>
<body style="max-width: 1000px; margin-left: auto; margin-right: auto;">
<h2>GMF web tooling demo</h2>
<p>
Experimental. Fully client-side.
</p>
<input type="file" id="picker" style="margin-bottom: 1em;" />
<pre id="log" style="background-color: #333; color: #eee; display: block; padding: 1em; overflow-x: scroll;"></pre>
<!-- Note the usage of `type=module` here as this is an ES6 module -->
<script type="module">
// Use ES module import syntax to import functionality from the module
// that we have compiled.
//
// Note that the `default` import is an initialization function which
// will "boot" the module and make it ready to use. Currently browsers
// don't support natively imported WebAssembly as an ES module, but
// eventually the manual initialization won't be required!
import init, { foo, GMF } from './pkg/gmfwasm.js';
function log(text) {
document.querySelector("#log").innerText += text;
document.querySelector("#log").innerText += '\n';
}
function logClear(text) {
document.querySelector("#log").innerText = '';
}
async function run() {
log("Initializing...");
await init();
log("Ready. Pick a binary GMF file from your system.");
document.querySelector('#picker').addEventListener("change", function(e) {
let file = e.target.files[0];
(async () => {
const buffer = await file.arrayBuffer();
const array = new Uint8Array(buffer);
logClear();
log("Got file, parsing...");
let gmf = null;
try {
gmf = GMF.parse_binary(array);
} catch (e) {
log("Failed to parse: " + e.msg());
for (const frame of e.stack()) {
log(" " + frame);
}
return;
}
log("GMF parsed.");
let j = gmf.to_json();
log(JSON.stringify(j.scene, null, 2));
log("");
log("Decompiled:");
log("");
try {
let gma = gmf.to_gma();
log(gma);
} catch (e) {
log("Failed to decompile: " + e);
}
})();
})
}
run();
</script>
</body>
</html>

58
gmfwasm/src/lib.rs Normal file
View File

@ -0,0 +1,58 @@
use wasm_bindgen::prelude::*;
use serde::Serialize;
#[wasm_bindgen]
pub fn foo(name: &str) -> String {
return name.to_uppercase()
}
#[wasm_bindgen]
pub struct GMF {
inner: gmflib::types::GMF,
}
//#[derive(Serialize)]
#[wasm_bindgen(getter_with_clone)]
pub struct ParseError {
inner: gmflib::GMIReadError,
}
#[wasm_bindgen]
impl ParseError {
pub fn msg(&self) -> String {
return self.inner.msg.clone()
}
pub fn stack(&self) -> JsValue {
let v: Vec<String> = self.inner.stack.iter().map(|f| {
format!("{} at 0x{:x}", f.name, f.loc)
}).collect();
JsValue::from_serde(&v).unwrap()
}
}
#[wasm_bindgen]
impl GMF {
pub fn parse_binary(array: js_sys::Uint8Array) -> Result<GMF,ParseError> {
let data = array.to_vec();
gmflib::GMF::read_gmi(data.as_slice()).map_err(|inner| {
ParseError {
inner
}
}).map(|inner| {
GMF {
inner,
}
})
}
pub fn to_json(&self) -> JsValue {
JsValue::from_serde(&self.inner).unwrap()
}
pub fn to_gma(&self) -> Result<String,String> {
let mut buf: Vec<u8> = vec![];
self.inner.write_gma(&mut buf).map_err(|e| {
e.msg.to_string()
}).map(|_| std::str::from_utf8(&buf).unwrap().to_string())
}
}