Commit 15ca4dff authored by Chunchi Che's avatar Chunchi Che

remove rust-src and wasm

parent 98850a94
Pipeline #40766 passed with stages
in 2 minutes and 34 seconds
stages:
- install
- rs_check_lint
- wasm
- ts_check_lint
- test
- build
......@@ -22,42 +20,9 @@ npm_ci:
dependencies:
- npm_ci
rs_check_lint:
stage: rs_check_lint
image: rust:latest
tags:
- linux
# before_script:
# - curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
# - source "$HOME/.cargo/env"
script:
- cd rust-src
- cargo check
- rustup component add clippy
- cargo clippy -- -Dwarnings
wasm_build:
tags:
- linux
stage: wasm
image: rust:latest
extends:
- .build_base
before_script:
# - curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
# - source "$HOME/.cargo/env"
- curl --proto '=https' --tlsv1.2 -sSf https://rustwasm.github.io/wasm-pack/installer/init.sh | sh
script:
- wasm-pack build ./rust-src --target web
artifacts:
paths:
- rust-src/pkg
- node_modules
ts_check_lint:
dependencies:
- npm_ci
- wasm_build
stage: ts_check_lint
tags:
- linux
......@@ -66,8 +31,6 @@ ts_check_lint:
- npm run lint
npm_build:
dependencies:
- wasm_build
stage: build
image: git-registry.mycard.moe/mycard/docker-runner-base:master-x86
tags:
......
......@@ -40,13 +40,12 @@
"ygopro-deck-encode": "^1.0.3"
},
"scripts": {
"wasm": "wasm-pack build ./rust-src --target web",
"dev": "npm run wasm && vite",
"dev": "vite",
"build": "vite build && cp -r neos-assets dist/",
"build:prod": "vite build --base=https://cdn02.moecube.com:444/neos/",
"preview": "vite preview",
"lint": "eslint --ext .ts --ext .tsx src",
"fmt": "npx tsc && eslint --ext .ts --ext .tsx src --fix && cd rust-src && cargo fmt"
"fmt": "npx tsc && eslint --ext .ts --ext .tsx src --fix"
},
"eslintConfig": {
"extends": [
......@@ -88,7 +87,6 @@
"vite": "^4.4.9",
"vite-plugin-arraybuffer": "^0.0.6",
"vite-plugin-sass-dts": "^1.3.9",
"vite-plugin-wasm-pack": "^0.1.12",
"vite-tsconfig-paths": "^4.2.0",
"vite-ydk-loader": "^0.0.2"
}
......
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
# replace-with = 'tuna'
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "bumpalo"
version = "3.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43"
[[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.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9"
[[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.3",
"wasm-bindgen",
]
[[package]]
name = "js-sys"
version = "0.3.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.176"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58f929b4d672ea937a23a1ab494143d968337a5f47e56d0815df1e0890ddf174"
[[package]]
name = "log"
version = "0.4.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432"
[[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.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "proc-macro2"
version = "1.0.101"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
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 = "2.0.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d"
[[package]]
name = "wasm-bindgen"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8"
dependencies = [
"cfg-if 1.0.3",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0"
dependencies = [
"cfg-if 1.0.3",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
[[package]]
name = "wasm-bindgen-test"
version = "0.3.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9bf62a58e0780af3e852044583deee40983e5886da43a271dd772379987667b"
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.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7f89739351a2e03cb94beb799d47fb2cac01759b40ec441f7de39b00cbf7ef0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "web-sys"
version = "0.3.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef"
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.92" # not "0.2.92" nor "0.2"
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.
use std::convert::TryInto;
use wasm_bindgen::prelude::wasm_bindgen;
#[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,
}
}
//! 一些`ocg raw buffer`到`neos-protobuf message`的转换逻辑
//!
//! TODO: neos与ygopro交互设计介绍
mod damage;
pub use damage::*;
//! Raw Buffer 解析工具
//!
//! 在Javascript/Typescript中数字类型只有`numbuer`,
//! 要做二进制数组的解析需要用[`DataView`],比较复杂,且性能不佳。
//! 因此这里用WASM实现两个基础的二进制数据解析模块[`BufferReader`]和[`BufferWriter`]。
use wasm_bindgen::prelude::wasm_bindgen;
const OFFSET_UINT8: usize = 1;
const OFFSET_INT8: usize = 1;
const OFFSET_UINT16: usize = 2;
const OFFSET_UINT32: usize = 4;
const OFFSET_INT32: usize = 4;
#[wasm_bindgen]
pub struct BufferReader {
array: Vec<u8>,
offset: usize,
}
#[wasm_bindgen]
impl BufferReader {
#[wasm_bindgen(constructor)]
pub fn new(array: js_sys::Uint8Array) -> Self {
Self {
array: array.to_vec(),
offset: 0,
}
}
pub fn readUint8(&mut self) -> u8 {
let ret = self.array[self.offset];
self.offset += OFFSET_UINT8;
ret
}
pub fn readInt8(&mut self) -> i8 {
let ret = self.array[self.offset] as i8;
self.offset += OFFSET_INT8;
ret
}
pub fn readUint16(&mut self) -> u16 {
let ret = self.array[self.offset..self.offset + OFFSET_UINT16]
.try_into()
.map(u16::from_le_bytes)
.unwrap();
self.offset += OFFSET_UINT16;
ret
}
pub fn readUint32(&mut self) -> u32 {
let ret = self.array[self.offset..self.offset + OFFSET_UINT32]
.try_into()
.map(u32::from_le_bytes)
.unwrap();
self.offset += OFFSET_UINT32;
ret
}
pub fn readInt32(&mut self) -> i32 {
let ret = self.array[self.offset..self.offset + OFFSET_INT32]
.try_into()
.map(i32::from_le_bytes)
.unwrap();
self.offset += OFFSET_INT32;
ret
}
pub fn offset(&self) -> usize {
self.offset
}
pub fn setOffset(&mut self, offset: usize) {
self.offset = offset;
}
}
#[wasm_bindgen]
pub struct BufferWriter {
array: Vec<u8>,
}
#[wasm_bindgen]
impl BufferWriter {
#[wasm_bindgen(constructor)]
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self { array: Vec::new() }
}
pub fn writeUint8(&mut self, value: u8) {
self.array.push(value);
}
pub fn writeInt8(&mut self, value: i8) {
self.array.extend(value.to_le_bytes());
}
pub fn writeUint16(&mut self, value: u16) {
self.array.extend(value.to_le_bytes());
}
pub fn writeInt16(&mut self, value: i16) {
self.array.extend(value.to_le_bytes());
}
pub fn writeUint32(&mut self, value: u32) {
self.array.extend(value.to_le_bytes());
}
pub fn writeInt32(&mut self, value: i32) {
self.array.extend(value.to_le_bytes());
}
pub fn toArray(self) -> Vec<u8> {
self.array
}
}
#![allow(non_snake_case)]
mod adapters;
mod buffer;
mod utils;
pub use adapters::*;
pub use buffer::{BufferReader, BufferWriter};
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;
// 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 { BufferReader } from "rust-src";
import { BufferReader } from "@/infra";
import { ygopro } from "../idl/ocgcore";
import { numberToCardPosition, numberToCardZone } from "./util";
......
import { BufferWriter } from "rust-src";
import { BufferWriter } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
......
import { BufferWriter } from "rust-src";
import { BufferWriter } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
......
import { BufferWriter } from "rust-src";
import { BufferWriter } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
......
import { BufferWriter } from "rust-src";
import { BufferWriter } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
......
import { BufferWriter } from "rust-src";
import { BufferWriter } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
......
import { BufferWriter } from "rust-src";
import { BufferWriter } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
......
import { BufferWriter } from "rust-src";
import { BufferWriter } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
import { cardZoneToNumber } from "../../util";
......
import { BufferWriter } from "rust-src";
import { BufferWriter } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
......
import { BufferWriter } from "rust-src";
import { BufferWriter } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
......
import { BufferWriter } from "rust-src";
import { BufferWriter } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
......
import { BufferReader } from "rust-src";
import { BufferReader } from "@/infra";
import { ygopro } from "../../idl/ocgcore";
import { StocAdapter, YgoProPacket } from "../packet";
......
import { BufferReader } from "rust-src";
import { ygopro } from "@/api/ocgcore/idl/ocgcore";
import { BufferReader } from "@/infra";
import MsgAnnounce = ygopro.StocGameMessage.MsgAnnounce;
/*
......
import { BufferReader } from "rust-src";
import { ygopro } from "@/api/ocgcore/idl/ocgcore";
import { BufferReader } from "@/infra";
import MsgAnnounce = ygopro.StocGameMessage.MsgAnnounce;
/*
......
import { BufferReader } from "rust-src";
import { ygopro } from "@/api/ocgcore/idl/ocgcore";
import { BufferReader } from "@/infra";
import MsgAnnounce = ygopro.StocGameMessage.MsgAnnounce;
/*
......
import { BufferReader } from "rust-src";
import { ygopro } from "@/api/ocgcore/idl/ocgcore";
import { BufferReader } from "@/infra";
import MsgAnnounce = ygopro.StocGameMessage.MsgAnnounce;
/*
......
import { ocgDamageAdapter } from "rust-src";
import { BufferReader } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
import MsgUpdateHp = ygopro.StocGameMessage.MsgUpdateHp;
import ActionType = ygopro.StocGameMessage.MsgUpdateHp.ActionType;
/*
* Msg Damage
*
......@@ -9,7 +12,14 @@ import { ygopro } from "../../../idl/ocgcore";
* @param value - 减少的Hp数值
* */
export default (data: Uint8Array) => {
const damage = ocgDamageAdapter(data);
const reader = new BufferReader(data);
const player = reader.readInt8();
const value = reader.readInt32();
return new ygopro.StocGameMessage.MsgUpdateHp(damage);
return new MsgUpdateHp({
player,
type_: ActionType.DAMAGE,
value,
});
};
import { BufferReader } from "rust-src";
import { BufferReader } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
......
import { ygopro } from "@/api/ocgcore/idl/ocgcore";
import { BufferReader } from "../../../../../../rust-src/pkg/rust_src";
import { BufferReader } from "@/infra";
import MsgFieldDisabled = ygopro.StocGameMessage.MsgFieldDisabled;
import CardZone = ygopro.CardZone;
......
import { ygopro } from "@/api/ocgcore/idl/ocgcore";
import { BufferReader } from "../../../../../../rust-src/pkg/rust_src";
import { BufferReader } from "@/infra";
/*
* Msg Hand Result
......
import { BufferReader } from "rust-src";
import { BufferReader } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
......
import { BufferReader } from "rust-src";
import { BufferReader } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
......
import { BufferReader } from "rust-src";
import { BufferReader } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
......
import { BufferReader } from "rust-src";
import { BufferReader } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
......
import { BufferReader } from "rust-src";
import { BufferReader } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
import MsgSelectOption = ygopro.StocGameMessage.MsgSelectOption;
......
import { BufferReader } from "rust-src";
import { BufferReader } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
import MsgSelectPlace = ygopro.StocGameMessage.MsgSelectPlace;
......
import { BufferReader } from "rust-src";
import { BufferReader } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
import MsgSelectPosition = ygopro.StocGameMessage.MsgSelectPosition;
......
import { ygopro } from "@/api/ocgcore/idl/ocgcore";
import { BufferReader } from "../../../../../../rust-src/pkg/rust_src";
import { BufferReader } from "@/infra";
import MsgShuffleHandExtra = ygopro.StocGameMessage.MsgShuffleHandExtra;
/*
......
import { BufferReader } from "../../../../../../rust-src/pkg/rust_src";
import { BufferReader } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
import MsgToss = ygopro.StocGameMessage.MsgToss;
......
import { BufferReader } from "rust-src";
import { BufferReader } from "@/infra";
import { ygopro } from "../../../idl/ocgcore";
import MsgWin = ygopro.StocGameMessage.MsgWin;
......
import { BufferReader } from "../../../../../rust-src/pkg/rust_src";
import { BufferReader } from "@/infra";
import { ygopro } from "../../idl/ocgcore";
import { StocAdapter, YgoProPacket } from "../packet";
......
import { BufferReader } from "rust-src";
import { BufferReader } from "@/infra";
import { ygopro } from "../../idl/ocgcore";
import { StocAdapter, YgoProPacket } from "../packet";
......
export class BufferReader {
private dataView: DataView;
private _offset: number;
/**
* Constructor that takes a Uint8Array as the data source
* @param array The binary array to read from
*/
constructor(array: Uint8Array) {
this.dataView = new DataView(
array.buffer,
array.byteOffset,
array.byteLength,
);
this._offset = 0;
}
/**
* Reads an unsigned 8-bit integer
* @returns The read value
*/
readUint8(): number {
const value = this.dataView.getUint8(this._offset);
this._offset += 1;
return value;
}
/**
* Reads a signed 8-bit integer
* @returns The read value
*/
readInt8(): number {
const value = this.dataView.getInt8(this._offset);
this._offset += 1;
return value;
}
/**
* Reads an unsigned 16-bit integer (little-endian)
* @returns The read value
*/
readUint16(): number {
const value = this.dataView.getUint16(this._offset, true);
this._offset += 2;
return value;
}
/**
* Reads an unsigned 32-bit integer (little-endian)
* @returns The read value
*/
readUint32(): number {
const value = this.dataView.getUint32(this._offset, true);
this._offset += 4;
return value;
}
/**
* Reads a signed 32-bit integer (little-endian)
* @returns The read value
*/
readInt32(): number {
const value = this.dataView.getInt32(this._offset, true);
this._offset += 4;
return value;
}
/**
* Gets the current offset position
* @returns The current offset
*/
offset(): number {
return this._offset;
}
/**
* Sets the offset position
* @param offset The new offset value
*/
setOffset(offset: number): void {
// Ensure offset is within valid range
if (offset < 0 || offset > this.dataView.byteLength) {
throw new Error(`Invalid offset: ${offset}`);
}
this._offset = offset;
}
}
export class BufferWriter {
private buffer: ArrayBuffer;
private dataView: DataView;
private length: number;
private capacity: number;
/**
* Constructor that creates a new BufferWriter instance
*/
constructor() {
// Initial capacity set to 1024 bytes, will auto-expand
this.capacity = 1024;
this.buffer = new ArrayBuffer(this.capacity);
this.dataView = new DataView(this.buffer);
this.length = 0;
}
/**
* Ensures the buffer has enough capacity
* @param additional Number of additional bytes needed
*/
private ensureCapacity(additional: number): void {
while (this.length + additional > this.capacity) {
this.capacity *= 2;
const newBuffer = new ArrayBuffer(this.capacity);
const newDataView = new DataView(newBuffer);
// Copy existing data
for (let i = 0; i < this.length; i++) {
newDataView.setUint8(i, this.dataView.getUint8(i));
}
this.buffer = newBuffer;
this.dataView = newDataView;
}
}
/**
* Writes an unsigned 8-bit integer
* @param value The value to write
*/
writeUint8(value: number): void {
this.ensureCapacity(1);
this.dataView.setUint8(this.length, value);
this.length += 1;
}
/**
* Writes a signed 8-bit integer
* @param value The value to write
*/
writeInt8(value: number): void {
this.ensureCapacity(1);
this.dataView.setInt8(this.length, value);
this.length += 1;
}
/**
* Writes an unsigned 16-bit integer (little-endian)
* @param value The value to write
*/
writeUint16(value: number): void {
this.ensureCapacity(2);
this.dataView.setUint16(this.length, value, true);
this.length += 2;
}
/**
* Writes a signed 16-bit integer (little-endian)
* @param value The value to write
*/
writeInt16(value: number): void {
this.ensureCapacity(2);
this.dataView.setInt16(this.length, value, true);
this.length += 2;
}
/**
* Writes an unsigned 32-bit integer (little-endian)
* @param value The value to write
*/
writeUint32(value: number): void {
this.ensureCapacity(4);
this.dataView.setUint32(this.length, value, true);
this.length += 4;
}
/**
* Writes a signed 32-bit integer (little-endian)
* @param value The value to write
*/
writeInt32(value: number): void {
this.ensureCapacity(4);
this.dataView.setInt32(this.length, value, true);
this.length += 4;
}
/**
* Converts the written data to a Uint8Array
* @returns Uint8Array containing the written data
*/
toArray(): Uint8Array {
return new Uint8Array(this.buffer, 0, this.length);
}
}
// Some implementation of infrastructure
import "./console";
export * from "./buffer";
export * from "./eventbus";
export * from "./pfetch";
export * from "./sleep";
......
......@@ -8,7 +8,6 @@ export const initStore = proxy({
}, // ygodb
decks: false,
i18n: false,
wasm: false,
forbidden: false, // 禁卡表
superprerelease: false, // 超先行
// ...
......
......@@ -40,7 +40,6 @@ import {
initI18N,
initSqlite,
initSuper,
initWASM,
} from "./utils";
const NeosConfig = useConfig();
......@@ -49,7 +48,6 @@ export const loader: LoaderFunction = async () => {
getLoginStatus();
initDeck();
initSqlite();
initWASM();
initForbidden();
initI18N();
initSuper();
......
import rustInit from "rust-src";
import {
CookieKeys,
forbidden,
......@@ -10,11 +8,10 @@ import {
setCookie,
} from "@/api";
import { useConfig } from "@/config";
import { useEnv } from "@/hook";
import sqliteMiddleWare, { sqliteCmd } from "@/middleware/sqlite";
import { accountStore, deckStore, initStore, type User } from "@/stores";
const { releaseResource, preReleaseResource, env408Resource } = useConfig();
const { BASE_URL } = useEnv();
/** 加载ygodb */
export const initSqlite = async () => {
......@@ -43,16 +40,6 @@ export const initDeck = async () => {
}
};
/** 加载WASM */
export const initWASM = async () => {
const url =
BASE_URL === "/"
? undefined
: new URL("rust_src_bg.wasm", `${BASE_URL}assets/`);
await rustInit(url);
initStore.wasm = true;
};
/** 加载禁限卡表 */
export const initForbidden = async () => {
if (!initStore.forbidden) {
......
import rustInit from "rust-src";
import { initStrings, initSuperPrerelease } from "@/api";
import { useConfig } from "@/config";
import { getUIContainer, initUIContainer } from "@/container/compat";
......@@ -24,13 +22,6 @@ export const connectSrvpro = async (params: {
replayData?: ArrayBuffer;
customOnConnected?: (conn: WebSocketStream) => void;
}) => {
// 初始化wasm
const url =
import.meta.env.BASE_URL === "/"
? undefined
: new URL("rust_src_bg.wasm", `${import.meta.env.BASE_URL}assets/`);
await rustInit(url);
// 初始化sqlite
await initSqlite();
......
......@@ -26,9 +26,6 @@
"@/*": [
"./src/*"
],
"rust-src": [
"./rust-src/pkg"
],
"neos-assets/*": [
"./neos-assets/*"
]
......@@ -36,7 +33,6 @@
},
"include": [
"src",
"rust-src/pkg",
"neos-assets"
]
}
......@@ -3,7 +3,6 @@ import react from "@vitejs/plugin-react";
import svgr from "vite-plugin-svgr";
import ydkLoader from "vite-ydk-loader";
import tsconfigPaths from "vite-tsconfig-paths";
import wasmPack from "vite-plugin-wasm-pack";
import sassDts from "vite-plugin-sass-dts";
import path from "path";
import arraybuffer from "vite-plugin-arraybuffer";
......@@ -16,7 +15,6 @@ export default defineConfig({
ydkLoader(),
arraybuffer(),
tsconfigPaths(),
wasmPack("./rust-src"),
sassDts({
enabledMode: ["development"],
sourceDir: path.resolve(__dirname, "./src"),
......
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