From 4dcc060e9a5d0982c4055c1e07d17cff9ba239a3 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Mon, 26 Jun 2023 19:05:48 +0200 Subject: [PATCH] added type to IPerph --- src/kernel/peripherals/Drive.hx | 4 +++ src/kernel/peripherals/EnergyStorage.hx | 18 ++++++++++++- src/kernel/peripherals/IPeripheral.hx | 1 + src/kernel/peripherals/Modem.hx | 4 +++ src/kernel/peripherals/Peripherals.hx | 35 +++++++++++++++++++++++++ src/kernel/peripherals/Printer.hx | 4 +++ src/kernel/peripherals/Redstone.hx | 6 ++++- src/kernel/peripherals/Screen.hx | 4 +++ 8 files changed, 74 insertions(+), 2 deletions(-) diff --git a/src/kernel/peripherals/Drive.hx b/src/kernel/peripherals/Drive.hx index 2013b56..5af591a 100644 --- a/src/kernel/peripherals/Drive.hx +++ b/src/kernel/peripherals/Drive.hx @@ -39,6 +39,10 @@ class Drive implements IPeripheral { return this.addr; } + public function getType():String { + return TYPE_NAME; + } + public inline function isDiskPresent(): Bool { return this.native.isDiskPresent(); } diff --git a/src/kernel/peripherals/EnergyStorage.hx b/src/kernel/peripherals/EnergyStorage.hx index cf3da2d..0fb322b 100644 --- a/src/kernel/peripherals/EnergyStorage.hx +++ b/src/kernel/peripherals/EnergyStorage.hx @@ -1,6 +1,9 @@ package kernel.peripherals; -class EnergyStorage implements IPeripheral{ +import lib.exporter.ExportConfig; +import lib.exporter.IExportable; + +class EnergyStorage implements IPeripheral implements IExportable{ public static inline final TYPE_NAME:String = "energyCell"; private final addr:String; @@ -22,4 +25,17 @@ class EnergyStorage implements IPeripheral{ public function getAddr():String { return this.addr; } + + public function getType():String { + return TYPE_NAME; + } + + public function export():ExportConfig { + return { + getDelegates: [ + "energy" => _ -> Number(this.getEnergy()), + "capacity" => _ -> Number(this.getEnergyCapacity()), + ], + } + } } diff --git a/src/kernel/peripherals/IPeripheral.hx b/src/kernel/peripherals/IPeripheral.hx index 6412d95..d1f55bd 100644 --- a/src/kernel/peripherals/IPeripheral.hx +++ b/src/kernel/peripherals/IPeripheral.hx @@ -2,4 +2,5 @@ package kernel.peripherals; interface IPeripheral { public function getAddr(): String; + public function getType(): String; } diff --git a/src/kernel/peripherals/Modem.hx b/src/kernel/peripherals/Modem.hx index 8d7d540..e53d046 100644 --- a/src/kernel/peripherals/Modem.hx +++ b/src/kernel/peripherals/Modem.hx @@ -74,6 +74,10 @@ class Modem implements INetworkInterface implements IPeripheral { return this.addr; } + public function getType():String { + return TYPE_NAME; + } + public function getBaseRoutingCost():Int { if (this.native.isWireless()){ return 2; // Prefere messages over cable diff --git a/src/kernel/peripherals/Peripherals.hx b/src/kernel/peripherals/Peripherals.hx index 9c45ede..30d1824 100644 --- a/src/kernel/peripherals/Peripherals.hx +++ b/src/kernel/peripherals/Peripherals.hx @@ -72,6 +72,31 @@ class Peripheral { }; } + /** + Cast peripheral to a specific type. + This is a temporary solution, maybe forever. + **/ + public function getFromType(addr: String, type: String): Null { + switch (type){ + case Computer.TYPE_NAME: + return getComputer(addr); + case Screen.TYPE_NAME: + return getScreen(addr); + case Drive.TYPE_NAME: + return getDrive(addr); + case EnergyStorage.TYPE_NAME: + return getEnergyStorage(addr); + case Modem.TYPE_NAME: + return getModem(addr); + case Printer.TYPE_NAME: + return getPrinter(addr); + case "redstone": + return getRedstone(addr); + } + + return null; + } + public function getScreen(addr: String): Null { var addr = safeGetAddr(addr, Screen.TYPE_NAME); if (addr == null) return null; @@ -127,4 +152,14 @@ class Peripheral { public function getAllEnergyStorages(): Array { return [ for (addr in findAddrByType(EnergyStorage.TYPE_NAME)) new EnergyStorage(addr)]; } + + public function getComputer(addr: String): Null { + var addr = safeGetAddr(addr, Computer.TYPE_NAME); + if (addr == null) return null; + return new Computer(addr); + } + + public function getAllComputers(): Array { + return [ for (addr in findAddrByType(Computer.TYPE_NAME)) new Computer(addr)]; + } } diff --git a/src/kernel/peripherals/Printer.hx b/src/kernel/peripherals/Printer.hx index ead785d..7cc1868 100644 --- a/src/kernel/peripherals/Printer.hx +++ b/src/kernel/peripherals/Printer.hx @@ -19,6 +19,10 @@ class Printer implements IPeripheral { return addr; } + public function getType():String { + return TYPE_NAME; + } + public function write(text: String){ } diff --git a/src/kernel/peripherals/Redstone.hx b/src/kernel/peripherals/Redstone.hx index da42530..b6d2bc1 100644 --- a/src/kernel/peripherals/Redstone.hx +++ b/src/kernel/peripherals/Redstone.hx @@ -44,6 +44,7 @@ abstract BundleMask(Int) from cc.Colors.Color to cc.Colors.Color { } class Redstone implements IPeripheral implements IExportable { + public static inline final TYPE_NAME:String = "redstone"; // TODO: there is technically not a type for redstone. public final onChange:Signal; @@ -74,6 +75,10 @@ class Redstone implements IPeripheral implements IExportable { return this.addr; } + public function getType():String { + return TYPE_NAME; + } + private function updateState() { this.analogInputState = cc.Redstone.getAnalogInput(this.addr); this.bundleInputState = cc.Redstone.getBundledInput(this.addr); @@ -125,7 +130,6 @@ class Redstone implements IPeripheral implements IExportable { public function export():ExportConfig { return { - type: "redstone", getDelegates: [ "input" => (_) -> {return Bool(this.getInput());}, "analog" => (_) -> {return Number(this.getAnalogInput());} diff --git a/src/kernel/peripherals/Screen.hx b/src/kernel/peripherals/Screen.hx index d31eba9..db33221 100644 --- a/src/kernel/peripherals/Screen.hx +++ b/src/kernel/peripherals/Screen.hx @@ -39,6 +39,10 @@ class Screen implements TermWriteable implements IPeripheral { return this.addr; } + public function getType():String { + return TYPE_NAME; + } + public function getTextScale():Float { return nativ.getTextScale(); }