Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add wasm and watch the whole project burn #4

Closed
wants to merge 1 commit into from

Conversation

SaintMercury
Copy link

@SaintMercury SaintMercury commented May 18, 2024

Adds wasm-bindgen and also doesn't work :)

@SaintMercury SaintMercury changed the title add wasm and watch the whole project burn, as you now have to program… add wasm and watch the whole project burn May 18, 2024
@SaintMercury SaintMercury marked this pull request as draft May 18, 2024 08:01
@@ -10,12 +10,16 @@ license = "MIT OR Apache-2.0"
keywords = ["TES3", "Morrowind"]
categories = ["Game development"]

[lib]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might be unnecessary. Can't remember adding it, must've done so in the other repo


[features]
default = []
default = ["wasm"]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

temp

@SaintMercury
Copy link
Author

rustwasm/wasm-bindgen#2631
rustwasm/wasm-bindgen#2407

Relevant issues and PR for possible solutions to errors

@Greatness7
Copy link
Owner

Greatness7 commented May 20, 2024

I recommend working off the dev branch if possible. It's got a lot of changes and improvements. I plan to merge it into main eventually.

Besides that, since serde support is already implemented there, I don't think you actually need to decorate every struct with #[wasm_bindgen] and can instead just let serde_wasm_bindgen do it for you.

Example:

[dependencies]
wasm-bindgen = "^0.2"
serde = "^1.0"
serde-wasm-bindgen = "^0.6"
js-sys = "^0.3"
web-sys = "^0.3"

[dependencies.tes3]
git = "https://github.com/Greatness7/tes3"
branch = "dev"
default-features = false
features = ["esp", "nightly", "serde"]
use js_sys::Uint8Array;
use serde_wasm_bindgen::{from_value, to_value};
use tes3::esp::Plugin;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn load_objects(array: Uint8Array) -> Result<JsValue, JsValue> {
    let mut plugin = Plugin::new();
    plugin
        .load_bytes(array.to_vec().as_ref())
        .map_err(|e| JsValue::from(e.to_string()))?;

    let value = to_value(&plugin.objects)?;

    Ok(value)
}

#[wasm_bindgen]
pub fn save_objects(value: JsValue) -> Result<Uint8Array, JsValue> {
    let mut plugin = Plugin {
        objects: from_value(value)?,
    };

    let bytes = plugin
        .save_bytes()
        .map_err(|e| JsValue::from(e.to_string()))?;

    let length = u32::try_from(bytes.len()) //
        .map_err(|e| JsValue::from(e.to_string()))?;

    let array = Uint8Array::new_with_length(length);
    array.copy_from(&bytes);

    Ok(array)
}

@Greatness7 Greatness7 closed this Sep 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants