From 4ca1bfe2c84594c3e8d88c0dc4db01b76d27d8eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Wed, 18 Sep 2024 22:02:18 +0100 Subject: [PATCH] [wasm/kgm] refactor: simplify kgm interface --- um_wasm/src/exports/kgm.rs | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/um_wasm/src/exports/kgm.rs b/um_wasm/src/exports/kgm.rs index 18407c6..975e5ed 100644 --- a/um_wasm/src/exports/kgm.rs +++ b/um_wasm/src/exports/kgm.rs @@ -2,32 +2,18 @@ use umc_kgm::{header::Header, Decipher}; use wasm_bindgen::prelude::wasm_bindgen; use wasm_bindgen::JsError; -/// KuGou KGM file header. -#[wasm_bindgen(js_name=KuGouHeader)] -pub struct JsKuGouHeader(Header); - -#[wasm_bindgen(js_class = KuGouHeader)] -impl JsKuGouHeader { - /// Parse the KuGou header (0x400 bytes) - pub fn parse(header: &[u8]) -> Result { - Ok(JsKuGouHeader( - Header::from_buffer(header).map_err(JsError::from)?, - )) - } -} - /// KuGou KGM file decipher. -#[wasm_bindgen(js_name=KuGouDecipher)] -pub struct JsKuGouDecipher(Decipher); +#[wasm_bindgen(js_name=KuGou)] +pub struct JsKuGou(Decipher); -#[wasm_bindgen(js_class=KuGouDecipher)] -impl JsKuGouDecipher { - /// Create an instance of cipher (decipher) for decryption - #[wasm_bindgen(constructor)] - pub fn new(header: &JsKuGouHeader) -> Result { - Ok(JsKuGouDecipher( - Decipher::new(&header.0).map_err(JsError::from)?, - )) +#[wasm_bindgen(js_class = KuGou)] +impl JsKuGou { + /// Parse the KuGou header (0x400 bytes) + pub fn from_header(header: &[u8]) -> Result { + let header = Header::from_buffer(header).map_err(JsError::from)?; + let decipher = Decipher::new(&header).map_err(JsError::from)?; + + Ok(JsKuGou(decipher)) } /// Decrypt a buffer.