device templates exist now

This commit is contained in:
Rachel Powers
2024-04-15 21:30:05 -07:00
parent 10d5db967c
commit 4e6ac09c5e
9 changed files with 108 additions and 29 deletions

View File

@@ -21,6 +21,11 @@ serde_with = "3.7.0"
tsify = { version = "0.4.5", default-features = false, features = ["js", "wasm-bindgen"] }
thiserror = "1.0.58"
[build-dependencies]
ic10emu = { path = "../ic10emu" }
strum = { version = "0.26.2"}
itertools = "0.12.1"
[features]
default = ["console_error_panic_hook"]
console_error_panic_hook = ["dep:console_error_panic_hook"]

View File

@@ -5,18 +5,59 @@ use std::{
path::Path,
};
use itertools::Itertools;
use strum::IntoEnumIterator;
fn main() {
let out_dir = env::var_os("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("ts_types.rs");
let output_file = File::create(dest_path).unwrap();
let mut writer = BufWriter::new(&output_file);
let mut ts_types: String = String::new();
let lt_tsunion: String = Itertools::intersperse(
ic10emu::grammar::generated::LogicType::iter().map(|lt| format!("\"{}\"", lt.as_ref())),
"\n | ".to_owned(),
)
.collect();
let lt_tstype = format!("\nexport type LogicType = {};", lt_tsunion);
ts_types.push_str(&lt_tstype);
let slt_tsunion: String = Itertools::intersperse(
ic10emu::grammar::generated::SlotLogicType::iter().map(|slt| format!("\"{}\"", slt.as_ref())),
"\n | ".to_owned(),
)
.collect();
let slt_tstype = format!("\nexport type SlotLogicType = {};", slt_tsunion);
ts_types.push_str(&slt_tstype);
let bm_tsunion: String = Itertools::intersperse(
ic10emu::grammar::generated::BatchMode::iter().map(|bm| format!("\"{}\"", bm.as_ref())),
"\n | ".to_owned(),
)
.collect();
let bm_tstype = format!("\nexport type BatchMode = {};", bm_tsunion);
ts_types.push_str(&bm_tstype);
let rm_tsunion: String = Itertools::intersperse(
ic10emu::grammar::generated::ReagentMode::iter().map(|rm| format!("\"{}\"", rm.as_ref())),
"\n | ".to_owned(),
)
.collect();
let rm_tstype = format!("\nexport type ReagentMode = {};", rm_tsunion);
ts_types.push_str(&rm_tstype);
let infile = Path::new("src/types.ts");
let contents = fs::read_to_string(infile).unwrap();
ts_types.push('\n');
ts_types.push_str(&contents);
write!(
&mut writer,
"#[wasm_bindgen(typescript_custom_section)]\n\
const TYPES: &'static str = r#\"{contents}\"#;
const TYPES: &'static str = r#\"{ts_types}\"#;
"
)
.unwrap();

View File

@@ -2,7 +2,7 @@
mod utils;
mod types;
use ic10emu::grammar::{LogicType, SlotLogicType};
use ic10emu::{grammar::{LogicType, SlotLogicType}, DeviceTemplate};
use serde::{Deserialize, Serialize};
use types::{Registers, Stack};
@@ -55,18 +55,18 @@ impl DeviceRef {
}
#[wasm_bindgen(getter, js_name = "nameHash")]
pub fn name_hash(&self) -> Option<f64> {
pub fn name_hash(&self) -> Option<i32> {
self.device.borrow().name_hash
}
#[wasm_bindgen(getter, js_name = "prefabName")]
pub fn prefab_name(&self) -> Option<String> {
self.device.borrow().prefab_name.clone()
self.device.borrow().prefab.as_ref().map(|prefab| prefab.name.clone())
}
#[wasm_bindgen(getter, js_name = "prefabHash")]
pub fn prefab_hash(&self) -> Option<i32> {
self.device.borrow().prefab_hash
self.device.borrow().prefab.as_ref().map(|prefab| prefab.hash)
}
#[wasm_bindgen(getter, skip_typescript)]
@@ -351,6 +351,12 @@ impl VM {
Ok(self.vm.borrow_mut().add_device(network)?)
}
#[wasm_bindgen(js_name = "addDeviceFromTemplate", skip_typescript)]
pub fn add_device_from_template(&self, template: JsValue) -> Result<u32, JsError> {
let template: DeviceTemplate = serde_wasm_bindgen::from_value(template)?;
Ok(self.vm.borrow_mut().add_device_from_template(template)?)
}
#[wasm_bindgen(js_name = "getDevice")]
pub fn get_device(&self, id: u32) -> Option<DeviceRef> {
let device = self.vm.borrow().get_device(id);

View File

@@ -4,7 +4,8 @@ export interface LogicField {
field_type: FieldType;
value: number;
}
export type Fields = Map<string, LogicField>;
export type LogicFields = Map<LogicType, LogicField>;
export type SlotLogicFields = Map<SlotLogicType, LogicField>;
export type SlotType =
| "AccessCard"
@@ -46,12 +47,12 @@ export interface SlotOccupant {
readonly quantity: number;
readonly max_quantity: number;
readonly damage: number;
readonly fields: Fields;
readonly fields: SlotLogicFields;
}
export interface Slot {
readonly typ: SlotType;
readonly occupant: SlotOccupant | undefined;
readonly fields: Fields;
readonly fields: SlotLogicFields;
}
export type Reagents = Map<string, Map<number, number>>;
@@ -80,10 +81,10 @@ export type DeviceSpec = {
};
readonly connection: number | undefined;
};
export type LogicType = { readonly LogicType: string };
export type SlotLogicType = { readonly SlotLogicType: string };
export type BatchMode = { readonly BatchMode: string };
export type ReagentMode = { readonly ReagentMode: string };
export type OperandLogicType = { readonly LogicType: string };
export type OperandSlotLogicType = { readonly SlotLogicType: string };
export type OperandBatchMode = { readonly BatchMode: string };
export type OperandReagentMode = { readonly ReagentMode: string };
export type Identifier = { readonly Identifier: { name: string } };
export type NumberFloat = { readonly Float: number };
@@ -106,10 +107,10 @@ export type Operand =
| RegisterSpec
| DeviceSpec
| NumberOperand
| LogicType
| SlotLogicType
| BatchMode
| ReagentMode
| OperandLogicType
| OperandSlotLogicType
| OperandBatchMode
| OperandReagentMode
| Identifier;
export type Alias = RegisterSpec | DeviceSpec;
@@ -141,7 +142,7 @@ export interface Program {
}
export interface DeviceRef {
readonly fields: Fields;
readonly fields: LogicFields;
readonly slots: Slot[];
readonly reagents: Reagents;
readonly connections: Connection[];
@@ -149,5 +150,29 @@ export interface DeviceRef {
readonly defines?: Defines | undefined;
readonly pins?: Pins;
readonly program?: Program;
getSlotFields(slot: number): Fields;
getSlotFields(slot: number): SlotLogicFields;
}
export interface SlotOccupantTemplate {
id?: number;
fields: { [key in SlotLogicType]?: LogicField };
}
export interface SlotTemplate {
typ: SlotType;
occupant?: SlotOccupantTemplate;
}
export interface DeviceTemplate {
id?: number;
name?: string;
prefab_name?: string;
slots: SlotTemplate[];
// reagents: { [key: string]: float}
connections: Connection[];
fields: { [key in LogicType]?: LogicField };
}
export interface VM {
addDeviceFromTemplate(template: DeviceTemplate): number
}