use wasm_bindgen::prelude::*;
use js_sys::{self, Array, Object, Reflect};
use web_sys::console;
use crate::{
UniversalSchematic,
BlockState,
formats::{litematic, schematic},
print_utils::{format_schematic, format_json_schematic},
};
use std::collections::HashMap;
#[wasm_bindgen(start)]
pub fn start() {
console::log_1(&"Initializing nucleation".into());
}
#[wasm_bindgen]
pub struct BlockStateWrapper { inner: BlockState }
#[wasm_bindgen]
impl BlockStateWrapper {
#[wasm_bindgen(constructor)]
pub fn new(name: String) -> Self {
Self { inner: BlockState::new(name) }
}
pub fn with_property(&mut self, key: String, value: String) -> BlockStateWrapper {
Self { inner: self.inner.clone().with_property(key.to_string(), value.to_string()) }
}
#[wasm_bindgen(getter)]
pub fn name(&self) -> String {
self.inner.name.clone()
}
#[wasm_bindgen(getter)]
pub fn properties(&self) -> JsValue {
let obj = Object::new();
for (key, value) in &self.inner.properties {
Reflect::set(&obj, &JsValue::from_str(key), &JsValue::from_str(value)).unwrap();
}
obj.into()
}
}
#[wasm_bindgen]
pub struct SchematicWrapper { inner: UniversalSchematic }
#[wasm_bindgen]
impl SchematicWrapper {
#[wasm_bindgen(constructor)]
pub fn new(name: Option<String>) -> Self {
let name = name.unwrap_or_else(|| "Default".to_string());
Self { inner: UniversalSchematic::new(name) }
}
pub fn from_data(&mut self, data: &[u8]) -> Result<(), String> {
if crate::formats::litematic::is_litematic(data) {
self.inner = crate::formats::litematic::from_litematic(data)
.map_err(|e| JsValue::from_str(&format!("Litematic error: {}", e)))?;
} else if crate::formats::schematic::is_schematic(data) {
self.inner = crate::formats::schematic::from_schematic(data)
.map_err(|e| JsValue::from_str(&format!("Schematic error: {}", e)))?;
} else {
return Err(JsValue::from_str("Unknown format"));
}
Ok(())
}
pub fn from_litematic(&mut self, data: &[u8]) -> Result<(), String> {
self.inner = crate::formats::litematic::from_litematic(data)
.map_err(|e| JsValue::from_str(&format!("Litematic error: {}", e)))?;
Ok(())
}
pub fn to_litematic(&mut self) -> Result<&[u8], String> {
crate::formats::litematic::to_litematic(&self.inner)
.map_err(|e| JsValue::from_str(&format!("Litematic error: {}", e)))
}
pub fn from_schematic(&mut self, data: &[u8]) -> Result<(), String> {
self.inner = crate::formats::schematic::from_schematic(data)
.map_err(|e| JsValue::from_str(&format!("Schematic error: {}", e)))?;
Ok(())
}
pub fn to_schematic(&mut self) -> Result<&[u8], String> {
crate::formats::schematic::to_schematic(&self.inner)
.map_err(|e| JsValue::from_str(&format!("Schematic error: {}", e)))
}
pub fn set_block(&mut self, x: i32, y: i32, z: i32, block_name: String) {
self.inner.set_block(x, y, z, BlockState::new(block_name.to_string()));
}
pub fn set_block_with_properties(&mut self, x: i32, y: i32, z: i32, block_name: String, properties: JsValue) {
let mut props = HashMap::new();
let block_state = BlockState { name: block_name.to_string(), properties: props };
self.inner.set_block(x, y, z, block_state);
}
pub fn get_block(&mut self, x: i32, y: i32, z: i32) -> Option<BlockStateWrapper> {
self.inner.get_block(x, y, z).cloned().map(|bs| BlockStateWrapper { inner: bs })
}
#[wasm_bindgen(getter)]
pub fn dimensions(&self) -> Vec<i32> {
let (x, y, z) = self.inner.get_dimensions();
vec![x, y, z]
}
#[wasm_bindgen(getter)]
pub fn block_count(&self) -> i32 {
self.inner.total_blocks()
}
#[wasm_bindgen(getter)]
pub fn volume(&self) -> i32 {
self.inner.total_volume()
}
#[wasm_bindgen(getter)]
pub fn region_names(&self) -> Vec<String> {
self.inner.get_region_names()
}
}
#[wasm_bindgen]
pub fn load_schematic(path: String) -> Result<SchematicWrapper, String> {
Err(JsValue::from_str("Not implemented"))
}
#[wasm_bindgen]
pub fn save_schematic(schematic: SchematicWrapper, path: String, format: String) -> Result<(), String> {
Err(JsValue::from_str("Not implemented"))
}
#[wasm_bindgen]
pub fn debug_schematic(schematic: SchematicWrapper) -> String {
format!("{}\n{}", schematic.debug_info(), crate::print_utils::format_schematic(&schematic.inner))
}
#[wasm_bindgen]
pub fn debug_json_schematic(schematic: SchematicWrapper) -> String {
format!("{}\n{}", schematic.debug_info(), crate::print_utils::format_json_schematic(&schematic.inner))
}