diff --git a/ic10emu/src/network.rs b/ic10emu/src/network.rs index 0a29748..9e36a4e 100644 --- a/ic10emu/src/network.rs +++ b/ic10emu/src/network.rs @@ -1,6 +1,7 @@ use std::collections::HashSet; use serde::{Deserialize, Serialize}; +use strum_macros::{AsRefStr, EnumIter}; use thiserror::Error; use itertools::Itertools; @@ -23,7 +24,9 @@ pub enum Connection { Other, } -#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[derive( + Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, EnumIter, AsRefStr, +)] pub enum ConnectionType { Pipe, Power, @@ -39,7 +42,9 @@ pub enum ConnectionType { None, } -#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[derive( + Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, EnumIter, AsRefStr, +)] pub enum ConnectionRole { Input, Input2, diff --git a/ic10emu_wasm/build.rs b/ic10emu_wasm/build.rs index 7a1ea33..c452c1a 100644 --- a/ic10emu_wasm/build.rs +++ b/ic10emu_wasm/build.rs @@ -48,6 +48,38 @@ fn main() { let rm_tstype = format!("\nexport type ReagentMode = {};", rm_tsunion); ts_types.push_str(&rm_tstype); + let sc_tsunion: String = Itertools::intersperse( + ic10emu::device::SortingClass::iter().map(|rm| format!("\"{}\"", rm.as_ref())), + "\n | ".to_owned(), + ) + .collect(); + let sc_tstype = format!("\nexport type SlotType = {};", sc_tsunion); + ts_types.push_str(&sc_tstype); + + let st_tsunion: String = Itertools::intersperse( + ic10emu::device::SlotType::iter().map(|rm| format!("\"{}\"", rm.as_ref())), + "\n | ".to_owned(), + ) + .collect(); + let st_tstype = format!("\nexport type SortingClass = {};", st_tsunion); + ts_types.push_str(&st_tstype); + + let ct_tsunion: String = Itertools::intersperse( + ic10emu::network::ConnectionType::iter().map(|rm| format!("\"{}\"", rm.as_ref())), + "\n | ".to_owned(), + ) + .collect(); + let ct_tstype = format!("\nexport type ConnectionType = {};", ct_tsunion); + ts_types.push_str(&ct_tstype); + + let cr_tsunion: String = Itertools::intersperse( + ic10emu::network::ConnectionRole::iter().map(|rm| format!("\"{}\"", rm.as_ref())), + "\n | ".to_owned(), + ) + .collect(); + let cr_tstype = format!("\nexport type ConnectionRole = {};", cr_tsunion); + ts_types.push_str(&cr_tstype); + let infile = Path::new("src/types.ts"); let contents = fs::read_to_string(infile).unwrap(); diff --git a/ic10emu_wasm/src/lib.rs b/ic10emu_wasm/src/lib.rs index 405d3be..65c35b5 100644 --- a/ic10emu_wasm/src/lib.rs +++ b/ic10emu_wasm/src/lib.rs @@ -288,7 +288,7 @@ impl DeviceRef { self.device.borrow_mut().set_name(name) } - #[wasm_bindgen(js_name = "setField")] + #[wasm_bindgen(js_name = "setField", skip_typescript)] pub fn set_field(&self, field: &str, value: f64, force: bool) -> Result<(), JsError> { let logic_typ = LogicType::from_str(field)?; let mut device_ref = self.device.borrow_mut(); @@ -296,7 +296,7 @@ impl DeviceRef { Ok(()) } - #[wasm_bindgen(js_name = "setSlotField")] + #[wasm_bindgen(js_name = "setSlotField", skip_typescript)] pub fn set_slot_field(&self, slot: f64, field: &str, value: f64, force: bool) -> Result<(), JsError> { let logic_typ = SlotLogicType::from_str(field)?; let mut device_ref = self.device.borrow_mut(); @@ -304,7 +304,7 @@ impl DeviceRef { Ok(()) } - #[wasm_bindgen(js_name = "getSlotField")] + #[wasm_bindgen(js_name = "getSlotField", skip_typescript)] pub fn get_slot_field(&self, slot: f64, field: &str) -> Result { let logic_typ = SlotLogicType::from_str(field)?; let device_ref = self.device.borrow_mut(); diff --git a/ic10emu_wasm/src/types.ts b/ic10emu_wasm/src/types.ts index b630cc9..01120ac 100644 --- a/ic10emu_wasm/src/types.ts +++ b/ic10emu_wasm/src/types.ts @@ -7,40 +7,6 @@ export interface LogicField { export type LogicFields = Map; export type SlotLogicFields = Map; -export type SlotType = - | "AccessCard" - | "Appliance" - | "Back" - | "Battery" - | "Blocked" - | "Bottle" - | "Cartridge" - | "Circuitboard" - | "CreditCard" - | "DataDisk" - | "DrillHead" - | "Egg" - | "Entity" - | "Flare" - | "GasCanister" - | "GasFilter" - | "Helmet" - | "Ingot" - | "LiquidBottle" - | "LiquidCanister" - | "Magazine" - | "Ore" - | "Organ" - | "Plant" - | "ProgramableChip" - | "ScanningHead" - | "SensorProcessingUnit" - | "SoundCartridge" - | "Suit" - | "Tool" - | "Torpedo" - | "None"; - export interface SlotOccupant { readonly id: number; readonly prefab_hash: number; @@ -156,6 +122,9 @@ export interface DeviceRef { readonly pins?: Pins; readonly program?: Program; getSlotFields(slot: number): SlotLogicFields; + setField(field: LogicType, value: number, force: boolean): void; + setSlotField(slot: number, field: SlotLogicType, value: number, force: boolean): void; + getSlotField(slot: number, field: SlotLogicType): number; } export interface SlotOccupantTemplate { @@ -178,6 +147,6 @@ export interface DeviceTemplate { fields: { [key in LogicType]?: LogicField }; } -export interface VM { +export interface VMRef { addDeviceFromTemplate(template: DeviceTemplate): number; } diff --git a/www/src/ts/virtual_machine/device_db.ts b/www/src/ts/virtual_machine/device_db.ts index 7136793..2bc0269 100644 --- a/www/src/ts/virtual_machine/device_db.ts +++ b/www/src/ts/virtual_machine/device_db.ts @@ -1,85 +1,6 @@ -import { LogicType, SlotLogicType } from "ic10emu_wasm"; - -export type SortingClass = - | "Default" - | "Kits" - | "Tools" - | "Resources" - | "Food" - | "Clothing" - | "Appliances" - | "Atmospherics" - | "Storage" - | "Ores" - | "Ices"; -export type SlotClass = - | "None" - | "Helmet" - | "Suit" - | "Back" - | "GasFilter" - | "GasCanister" - | "Motherboard" - | "Circuitboard" - | "DataDisk" - | "Organ" - | "Ore" - | "Plant" - | "Uniform" - | "Entity" - | "Battery" - | "Egg" - | "Belt" - | "Tool" - | "Appliance" - | "Ingot" - | "Torpedo" - | "Cartridge" - | "AccessCard" - | "Magazine" - | "Circuit" - | "Bottle" - | "ProgrammableChip" - | "Glasses" - | "CreditCard" - | "DirtCanister" - | "SensorProcessingUnit" - | "LiquidCanister" - | "LiquidBottle" - | "Wreckage" - | "SoundCartridge" - | "DrillHead" - | "ScanningHead" - | "Flare" - | "Blocked"; -export type NetworkType = - | "None" - | "Pipe" - | "Power" - | "Data" - | "Chute" - | "Elevator" - | "PipeLiquid" - | "LandingPad" - | "LaunchPad" - | "PowerAndData" - | "All"; -export type ConnectionRole = - | "None" - | "Input" - | "Input2" - | "Output" - | "Output2" - | "Waste"; - -export type FieldType = "Read" | "Write" | "ReadWrite"; - -export type ReagentMode = "Contents" | "Recipe" | "Required" | "TotalContents"; - -export type BatchMode = "Average" | "Maximum" | "Minimum" | "Sum"; - +import { LogicType, SlotLogicType, SortingClass, SlotType, FieldType, ReagentMode, BatchMode, ConnectionType, ConnectionRole } from "ic10emu_wasm"; export interface DeviceDBItem { - slotclass: SlotClass; + slotclass: SlotType; sorting: SortingClass; maxquantity?: number; filtertype?: string; @@ -96,7 +17,7 @@ export interface DeviceDBDevice { } export interface DeviceDBConnection { - typ: NetworkType; + typ: ConnectionType; role: ConnectionRole; name: string; } @@ -106,7 +27,7 @@ export interface DeviceDBEntry { hash: number; title: string; desc: string; - slots?: { name: string; typ: SlotClass }[]; + slots?: { name: string; typ: SlotType }[]; logic?: { [key in LogicType]?: FieldType }; slotlogic?: { [key in SlotLogicType]?: number[] }; modes?: { [key: number]: string }; diff --git a/www/src/ts/virtual_machine/index.ts b/www/src/ts/virtual_machine/index.ts index b81e8f9..66edb59 100644 --- a/www/src/ts/virtual_machine/index.ts +++ b/www/src/ts/virtual_machine/index.ts @@ -1,4 +1,4 @@ -import { DeviceRef, DeviceTemplate, LogicType, SlotLogicType, VM, init } from "ic10emu_wasm"; +import { DeviceRef, DeviceTemplate, LogicType, SlotLogicType, VMRef, init } from "ic10emu_wasm"; import { DeviceDB } from "./device_db"; import "./base_device"; @@ -17,7 +17,7 @@ export interface ToastMessage { } class VirtualMachine extends EventTarget { - ic10vm: VM; + ic10vm: VMRef; _devices: Map; _ics: Map;