Commit 7d3ca9c8 authored by Chunchi Che's avatar Chunchi Che

Merge branch 'feat/wasm' into 'main'

Feat/wasm

See merge request mycard/Neos!130
parents 12102999 c7709641
Pipeline #20664 passed with stages
in 12 minutes and 45 seconds
......@@ -12,6 +12,7 @@
/build
/builds
/dist
/rust-src/target
# misc
.DS_Store
......
......@@ -2,6 +2,7 @@ stages:
- install
- lint
- test
- wasm
- build
- deploy
......@@ -20,16 +21,43 @@ npm_ci:
dependencies:
- npm_ci
npm_lint:
ts_lint:
extends: .build_base
stage: lint
tags:
- linux
script: npm run lint
rs_lint:
stage: lint
image: rust:latest
tags:
- linux
script:
- rustup component add clippy
- cd rust-src && cargo clippy -- -Dwarnings
wasm_build:
tags:
- linux
image: rust:latest
stage: wasm
extends:
- .build_base
before_script:
- curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
script:
- cd rust-src && wasm-pack build
artifacts:
paths:
- rust-src/pkg
- node_modules
npm_build:
extends: .build_base
dependencies:
- wasm_build
stage: build
image: git-registry.mycard.moe/mycard/docker-runner-base:master-x86
tags:
- linux
script:
......
......@@ -31,6 +31,7 @@
"react-redux": "^8.0.4",
"react-router-dom": "^6.4.0",
"react-scripts": "^2.1.3",
"rust-src": "file:rust-src/pkg",
"socket.io-client": "^4.5.1",
"sql.js": "^1.8.0",
"vite-plugin-svgr": "^2.4.0",
......@@ -50,7 +51,8 @@
"prettier": "^2.7.1",
"sass": "^1.58.0",
"typescript": "^4.8.4",
"vite": "^3.1.0"
"vite": "^3.1.0",
"vite-plugin-wasm": "^3.2.2"
}
},
"node_modules/@adobe/css-tools": {
......@@ -22769,6 +22771,10 @@
"aproba": "^1.1.1"
}
},
"node_modules/rust-src": {
"resolved": "rust-src/pkg",
"link": true
},
"node_modules/rxjs": {
"version": "6.6.7",
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.7.tgz",
......@@ -25909,6 +25915,15 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/vite-plugin-wasm": {
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/vite-plugin-wasm/-/vite-plugin-wasm-3.2.2.tgz",
"integrity": "sha512-cdbBUNR850AEoMd5nvLmnyeq63CSfoP1ctD/L2vLk/5+wsgAPlAVAzUK5nGKWO/jtehNlrSSHLteN+gFQw7VOA==",
"dev": true,
"peerDependencies": {
"vite": "^2 || ^3 || ^4"
}
},
"node_modules/vm-browserify": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vm-browserify/-/vm-browserify-1.1.2.tgz",
......@@ -27123,6 +27138,9 @@
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"rust-src/pkg": {
"version": "0.1.0"
}
},
"dependencies": {
......@@ -44501,6 +44519,9 @@
"aproba": "^1.1.1"
}
},
"rust-src": {
"version": "file:rust-src/pkg"
},
"rxjs": {
"version": "6.6.7",
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.7.tgz",
......@@ -46983,6 +47004,13 @@
}
}
},
"vite-plugin-wasm": {
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/vite-plugin-wasm/-/vite-plugin-wasm-3.2.2.tgz",
"integrity": "sha512-cdbBUNR850AEoMd5nvLmnyeq63CSfoP1ctD/L2vLk/5+wsgAPlAVAzUK5nGKWO/jtehNlrSSHLteN+gFQw7VOA==",
"dev": true,
"requires": {}
},
"vm-browserify": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vm-browserify/-/vm-browserify-1.1.2.tgz",
......@@ -30,15 +30,17 @@
"sql.js": "^1.8.0",
"vite-plugin-svgr": "^2.4.0",
"web-vitals": "^2.1.4",
"ygopro-deck-encode": "^1.0.3"
"ygopro-deck-encode": "^1.0.3",
"rust-src": "file:rust-src/pkg"
},
"scripts": {
"dev": "vite",
"wasm": "cd rust-src && wasm-pack build",
"dev": "npm run wasm && vite",
"build": "tsc && vite build && cp -r neos-assets dist/",
"build:prod": "tsc && vite build --base=https://cdn02.moecube.com:444/neos/",
"preview": "vite preview",
"lint": "eslint --ext .ts --ext .tsx src",
"fmt": "eslint --ext .ts --ext .tsx src --fix",
"fmt": "eslint --ext .ts --ext .tsx src --fix && cd rust-src && cargo fmt",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
......@@ -73,6 +75,7 @@
"prettier": "^2.7.1",
"sass": "^1.58.0",
"typescript": "^4.8.4",
"vite": "^3.1.0"
"vite": "^3.1.0",
"vite-plugin-wasm": "^3.2.2"
}
}
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "bumpalo"
version = "3.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535"
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[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 1.0.0",
"wasm-bindgen",
]
[[package]]
name = "js-sys"
version = "0.3.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "445dde2150c55e483f3d8416706b97ec8e8237c307e5b7b4b8dd15e6af2a0730"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.140"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"
[[package]]
name = "log"
version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [
"cfg-if 1.0.0",
]
[[package]]
name = "memory_units"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3"
[[package]]
name = "once_cell"
version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
[[package]]
name = "proc-macro2"
version = "1.0.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d0e1ae9e836cc3beddd63db0df682593d7e2d3d891ae8c9083d2113e1744224"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50686e0021c4136d1d453b2dfe059902278681512a34d4248435dc34b6b5c8ec"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rust-src"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"js-sys",
"wasm-bindgen",
"wasm-bindgen-test",
"wee_alloc",
]
[[package]]
name = "scoped-tls"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294"
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4"
[[package]]
name = "wasm-bindgen"
version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31f8dcbc21f30d9b8f2ea926ecb58f6b91192c17e9d33594b3df58b2007ca53b"
dependencies = [
"cfg-if 1.0.0",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95ce90fd5bcc06af55a641a86428ee4229e44e07033963a2290a8e241607ccb9"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f219e0d211ba40266969f6dbdd90636da12f75bee4fc9d6c23d1260dadb51454"
dependencies = [
"cfg-if 1.0.0",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c21f77c0bedc37fd5dc21f897894a5ca01e7bb159884559461862ae90c0b4c5"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d"
[[package]]
name = "wasm-bindgen-test"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db36fc0f9fb209e88fb3642590ae0205bb5a56216dabd963ba15879fe53a30b"
dependencies = [
"console_error_panic_hook",
"js-sys",
"scoped-tls",
"wasm-bindgen",
"wasm-bindgen-futures",
"wasm-bindgen-test-macro",
]
[[package]]
name = "wasm-bindgen-test-macro"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0734759ae6b3b1717d661fe4f016efcfb9828f5edb4520c18eaee05af3b43be9"
dependencies = [
"proc-macro2",
"quote",
]
[[package]]
name = "web-sys"
version = "0.3.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e33b99f4b23ba3eec1a53ac264e35a755f00e966e0065077d6027c0f575b0b97"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "wee_alloc"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e"
dependencies = [
"cfg-if 0.1.10",
"libc",
"memory_units",
"winapi",
]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[package]
name = "rust-src"
version = "0.1.0"
authors = ["SKTT1Ryze <linuxgnulover@gmail.com>"]
edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["console_error_panic_hook", "wee_alloc"]
[dependencies]
wasm-bindgen = "0.2.63"
js-sys = "0.3.61"
console_error_panic_hook = { version = "0.1.6", optional = true }
wee_alloc = { version = "0.4.5", optional = true }
[dev-dependencies]
wasm-bindgen-test = "0.3.13"
[profile.release]
opt-level = "s"
# Rust Code for Neos
`rust-src` is a WebAssembly crate for `neos-ts` project.
#![allow(non_snake_case)]
mod buffer;
mod utils;
use std::convert::TryInto;
use wasm_bindgen::prelude::wasm_bindgen;
pub use utils::set_panic_hook;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
pub struct MsgUpdateHp {
pub player: Option<u8>,
pub type_: Option<u8>,
pub value: Option<i32>,
}
#[repr(u8)]
enum ActionType {
_Unknown = 0,
Damage = 1,
_Recover = 2,
}
#[wasm_bindgen]
pub fn ocgDamageAdapter(data: js_sys::Uint8Array) -> MsgUpdateHp {
let data = data.to_vec();
let player = data[0];
let value = data[1..5].try_into().map(i32::from_le_bytes).ok();
MsgUpdateHp {
player: Some(player),
type_: Some(ActionType::Damage as u8),
value,
}
}
// ref: https://github.com/rustwasm/wasm-pack-template/blob/master/src/utils.rs
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
import { ygopro } from "../../../idl/ocgcore";
import { BufferReader } from "../../bufferIO";
import { ocgDamageAdapter } from "rust-src";
/*
* Msg Damage
......@@ -8,14 +8,7 @@ import { BufferReader } from "../../bufferIO";
* @param value - 减少的Hp数值
* */
export default (data: Uint8Array) => {
const reader = new BufferReader(data, true);
const damage = ocgDamageAdapter(data);
const player = reader.readUint8();
const value = reader.readInt32();
return new ygopro.StocGameMessage.MsgUpdateHp({
player,
type_: ygopro.StocGameMessage.MsgUpdateHp.ActionType.DAMAGE,
value,
});
return new ygopro.StocGameMessage.MsgUpdateHp(damage);
};
......@@ -16,6 +16,12 @@ export const store = configureStore({
mora: moraReducer,
duel: duelReducer,
},
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: {
ignoredActions: ["duel/updateHp"],
},
}),
});
// Ref: https://github.com/reduxjs/redux/issues/303
......
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import svgr from 'vite-plugin-svgr'
import wasm from "vite-plugin-wasm";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react(), svgr()]
plugins: [react(), svgr(), wasm()]
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment