From dc85609a75804d24bcf487983f0d12ce6ab52c37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Mon, 31 Jan 2022 10:11:04 +0100 Subject: [PATCH] Wasm skeleton --- .gitignore | 2 + build_release.py | 44 ++++++++++++ build_wasm.py | 13 ++++ install_dev_dependencies.sh | 3 + js/main.js | 4 ++ rust/.gitignore | 1 + rust/Cargo.lock | 137 ++++++++++++++++++++++++++++++++++++ rust/Cargo.toml | 17 +++++ rust/rustfmt.toml | 1 + rust/src/lib.rs | 6 ++ 10 files changed, 228 insertions(+) create mode 100755 build_release.py create mode 100755 build_wasm.py create mode 100755 install_dev_dependencies.sh create mode 100644 rust/.gitignore create mode 100644 rust/Cargo.lock create mode 100644 rust/Cargo.toml create mode 100644 rust/rustfmt.toml create mode 100644 rust/src/lib.rs diff --git a/.gitignore b/.gitignore index 2809123..d606dd2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ foundry-*.js +artifact/ +wasm/ diff --git a/build_release.py b/build_release.py new file mode 100755 index 0000000..097bec3 --- /dev/null +++ b/build_release.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +import json +from pathlib import PurePath, Path +import subprocess +import tempfile +import zipfile + +wasm_pack = Path("~/.cargo/bin/wasm-pack").expanduser() + +root_files = ["module.json", "README.md", "CHANGELOG.md", "LICENSE"] +wasm_files = ["gridless_pathfinding_bg.wasm", "gridless_pathfinding.js"] +output_dir = Path("artifact") +copy_everything_directories = ["js", "lang", "templates"] +wasm_dir = Path("wasm") +root_dir = Path(".") +rust_dir = Path("rust") +build_dir_tmp = tempfile.TemporaryDirectory() +build_dir = Path(build_dir_tmp.name) + +with open("module.json", "r") as file: + manifest = json.load(file) + +zip_root = PurePath(f'{manifest["name"]}') + +filename = f'{manifest["name"]}-{manifest["version"]}.zip' + +result = subprocess.run([wasm_pack, "build", "--target", "web", "--out-dir", build_dir, root_dir / rust_dir]) +if result.returncode != 0: + raise Exception("Wasm build failed") + +output_dir.mkdir(parents=True, exist_ok=True) + +with zipfile.ZipFile(output_dir / filename, mode="w", compression=zipfile.ZIP_DEFLATED, compresslevel=9) as archive: + for f in root_files: + archive.write(root_dir / f, arcname=zip_root / f) + for d in copy_everything_directories: + for f in (root_dir / d).iterdir(): + assert(f.is_file()) + archive.write(f, arcname=zip_root / d / f.name) + for f in wasm_files: + archive.write(build_dir / f, arcname=zip_root / wasm_dir / f) + +print(f"Successfully built {output_dir / filename}") diff --git a/build_wasm.py b/build_wasm.py new file mode 100755 index 0000000..33ae107 --- /dev/null +++ b/build_wasm.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +import sys +import subprocess +from pathlib import Path + +root_dir = Path(".") +wasm_dir = root_dir / Path("wasm") +rust_dir = root_dir / Path("rust") + +debug = " --debug" if len(sys.argv) >= 2 and sys.argv[1] == "--debug" else "" + +result = subprocess.run(["cargo", "watch", "-C" , rust_dir, "-s", f"wasm-pack build --target web --out-dir {wasm_dir.resolve()}{debug}"]) diff --git a/install_dev_dependencies.sh b/install_dev_dependencies.sh new file mode 100755 index 0000000..d3534f1 --- /dev/null +++ b/install_dev_dependencies.sh @@ -0,0 +1,3 @@ +#!/bin/sh +cargo install cargo-watch +cargo install wasm-pack diff --git a/js/main.js b/js/main.js index 0a761aa..d3fd1f9 100644 --- a/js/main.js +++ b/js/main.js @@ -13,9 +13,13 @@ import {recalculate} from "./socket.js"; import {SpeedProvider} from "./speed_provider.js" import {setSnapParameterOnOptions} from "./util.js"; +import * as GridlessPathfinding from "../wasm/gridless_pathfinding.js" + CONFIG.debug.dragRuler = false; export let debugGraphics = undefined; +GridlessPathfinding.init(); + Hooks.once("init", () => { registerSettings() registerKeybindings() diff --git a/rust/.gitignore b/rust/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/rust/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/rust/Cargo.lock b/rust/Cargo.lock new file mode 100644 index 0000000..b5debf6 --- /dev/null +++ b/rust/Cargo.lock @@ -0,0 +1,137 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[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" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + +[[package]] +name = "gridless-pathfinding" +version = "0.1.0" +dependencies = [ + "console_error_panic_hook", + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "proc-macro2" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + +[[package]] +name = "wasm-bindgen" +version = "0.2.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25f1af7423d8588a3d840681122e72e6a24ddbcb3f0ec385cac0d12d24256c06" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b21c0df030f5a177f3cba22e9bc4322695ec43e7257d865302900290bcdedca" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f4203d69e40a52ee523b2529a773d5ffc1dc0071801c87b3d270b471b80ed01" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa8a30d46208db204854cadbb5d4baf5fcf8071ba5bf48190c3e59937962ebc" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d958d035c4438e28c70e4321a2911302f10135ce78a9c7834c0cab4123d06a2" diff --git a/rust/Cargo.toml b/rust/Cargo.toml new file mode 100644 index 0000000..5dc3ffa --- /dev/null +++ b/rust/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "gridless-pathfinding" +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"] + +[profile.release] +lto = true +#debug = true + +[dependencies] +console_error_panic_hook = "0.1.7" +wasm-bindgen = "0.2.79" diff --git a/rust/rustfmt.toml b/rust/rustfmt.toml new file mode 100644 index 0000000..218e203 --- /dev/null +++ b/rust/rustfmt.toml @@ -0,0 +1 @@ +hard_tabs = true diff --git a/rust/src/lib.rs b/rust/src/lib.rs new file mode 100644 index 0000000..b1dbf7e --- /dev/null +++ b/rust/src/lib.rs @@ -0,0 +1,6 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen(start)] +pub fn main() { + std::panic::set_hook(Box::new(console_error_panic_hook::hook)) +}