diff --git a/src/bin/Disk.hx b/src/bin/Disk.hx index 6532167..611edbc 100644 --- a/src/bin/Disk.hx +++ b/src/bin/Disk.hx @@ -9,7 +9,7 @@ using Lambda; class Disk extends CLIAppBase { public function new() { registerSyncSubcommand("ls", (args)->{ - Peripheral.instance.getDrives().foreach(drive -> { + Peripheral.instance.getAllDrives().foreach(drive -> { var addr = drive.getAddr(); var label = drive.getDiskLabel(); var id = drive.getDiskID(); diff --git a/src/bin/Net.hx b/src/bin/Net.hx index 9cfa223..38b90e9 100644 --- a/src/bin/Net.hx +++ b/src/bin/Net.hx @@ -19,7 +19,7 @@ class Net extends CLIAppBase { }); registerSyncSubcommand("iface", (args)->{ - var modems = Peripheral.instance.getModems(); + var modems = Peripheral.instance.getAllModems(); for (modem in modems) { handle.writeLine(modem.name()); diff --git a/src/kernel/net/Net.hx b/src/kernel/net/Net.hx index 6d98729..a3bde4b 100644 --- a/src/kernel/net/Net.hx +++ b/src/kernel/net/Net.hx @@ -31,7 +31,7 @@ class Net { @:allow(kernel.Init) private function new() { - this.interfaces = [for (e in Peripheral.instance.getModems()) e ]; // TODO: is this the way to do it? + this.interfaces = [for (e in Peripheral.instance.getAllModems()) e ]; // TODO: is this the way to do it? this.interfaces.push(Loopback.instance); for (interf in interfaces){ @@ -279,7 +279,7 @@ class Net { data: null, }; - for (modem in Peripheral.instance.getModems()) { + for (modem in Peripheral.instance.getAllModems()) { if (!modem.isWireless()) continue; modem.send(Net.BRODCAST_PORT, Net.instance.networkID, pack); } diff --git a/src/kernel/net/Routing.hx b/src/kernel/net/Routing.hx index 89ca0e8..22bb403 100644 --- a/src/kernel/net/Routing.hx +++ b/src/kernel/net/Routing.hx @@ -64,7 +64,7 @@ class Routing { private function brodcastRoutingTable() { var pack = newRoutDiscoverPackage(); - for (modem in Peripheral.instance.getModems()) { + for (modem in Peripheral.instance.getAllModems()) { modem.send(Net.BRODCAST_PORT, Net.instance.networkID, pack); } } diff --git a/src/kernel/peripherals/Drive.hx b/src/kernel/peripherals/Drive.hx index e8eae50..2013b56 100644 --- a/src/kernel/peripherals/Drive.hx +++ b/src/kernel/peripherals/Drive.hx @@ -1,8 +1,11 @@ package kernel.peripherals; +import cc.Peripheral; using tink.CoreApi; class Drive implements IPeripheral { + public static inline final TYPE_NAME:String = "drive"; + public final onDiskInsert:Signal; public final onDiskEject:Signal; @@ -12,9 +15,9 @@ class Drive implements IPeripheral { private final onDiskEjectTrigger:SignalTrigger = Signal.trigger(); @:allow(kernel.peripherals) - private function new(native: cc.periphs.Disk,addr: String) { + private function new(addr: String) { this.addr = addr; - this.native = native; + this.native = Peripheral.wrap(addr); this.onDiskInsert = this.onDiskInsertTrigger.asSignal(); this.onDiskEject = this.onDiskEjectTrigger.asSignal(); diff --git a/src/kernel/peripherals/Modem.hx b/src/kernel/peripherals/Modem.hx index ef9e5e2..bd7b233 100644 --- a/src/kernel/peripherals/Modem.hx +++ b/src/kernel/peripherals/Modem.hx @@ -1,5 +1,6 @@ package kernel.peripherals; +import cc.Peripheral; import kernel.log.Log; import kernel.net.Package; import kernel.net.INetworkInterface; @@ -7,6 +8,8 @@ import kernel.net.INetworkInterface; using tink.CoreApi; class Modem implements INetworkInterface implements IPeripheral { + public static inline final TYPE_NAME:String = "modem"; + public final addr:String; public var onMessage(default, null):Signal<{pack:Package,dist:Null}>; @@ -14,9 +17,9 @@ class Modem implements INetworkInterface implements IPeripheral { private final native:cc.periphs.Modem.Modem; @:allow(kernel.peripherals) - private function new(nativePeripherals:cc.periphs.Modem.Modem, addr:String) { + private function new(addr:String) { this.onMessage = onMessageTrigger.asSignal(); - this.native = nativePeripherals; + this.native = Peripheral.wrap(addr); this.addr = addr; KernelEvents.instance.onModemMessage.handle(params ->{ diff --git a/src/kernel/peripherals/Peripherals.hx b/src/kernel/peripherals/Peripherals.hx index 7b330c3..0582b83 100644 --- a/src/kernel/peripherals/Peripherals.hx +++ b/src/kernel/peripherals/Peripherals.hx @@ -17,93 +17,96 @@ class Peripheral { **/ public static var instance:Peripheral; - private var screens: Array = []; - private var modes: Array = []; - private var drives:Array = []; - private var redstone:Array = []; - private var printers:Array = []; - @:allow(kernel.Init) private function new() { - KernelEvents.instance.onPeripheral.handle(this.updatePeripherals); - KernelEvents.instance.onPeripheralDetach.handle(this.updatePeripherals); - updatePeripherals(); - - redstone = [ for(side in [Side.Top,Side.Right,Side.Left,Side.Front,Side.Bottom,Side.Back]) new Redstone(side) ]; } - private function updatePeripherals() { - findScreens(); - findModems(); - findDrives(); + public function getAllAddresses(): Array { + return cc.Peripheral.getNames().toArray(); } - /** - Get all connected screens. - **/ - public function getScreens():ReadOnlyArray { - return this.screens; + public function isPresent(addr: String): Bool { + return cc.Peripheral.isPresent(addr); } - private function findScreens():Void { - var allScreens = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "monitor"); - this.screens = allScreens.map(s -> return new Screen((cc.Peripheral.wrap(s) : Dynamic), s)); + public function getTypes(addr: String): Array { + if (!cc.Peripheral.isPresent(addr)) { + return []; + } + + return cc.Peripheral.getType(addr).toArray(); } - public function getScreen(addr: String): Screen { - return this.screens.find(item -> item.getAddr() == addr); + private function findAddrByType(type: String): Array { + return getAllAddresses().filter(addr -> getTypes(addr).contains(type)); + } + + private function safeGetAddr(addr: String, type: String): Null { + if (!isPresent(addr)) { + return null; + } + + var types = getTypes(addr); + if (!types.contains(type)) { + return null; + } + + return addr; } - /** - Get all connected modems. - **/ - public function getModems():ReadOnlyArray { - return this.modes; + public function getScreen(addr: String): Null { + var addr = safeGetAddr(addr, Screen.TYPE_NAME); + if (addr == null) return null; + return new Screen(addr); + } + + + public function getAllScreens(): Array { + return [ for (addr in findAddrByType(Screen.TYPE_NAME)) new Screen(addr)]; } public function getModem(addr: String): Null { - return this.modes.find(item -> item.getAddr() == addr); + var addr = safeGetAddr(addr, Modem.TYPE_NAME); + if (addr == null) return null; + return new Modem(addr); } - private function findModems():Void { - var allModems = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "modem"); - this.modes = allModems.map(s -> return new Modem((cc.Peripheral.wrap(s) : Dynamic), s)); - } - - /** - Get all connected drives. - **/ - public function getDrives(): ReadOnlyArray { - return this.drives; + public function getAllModems(): Array { + return [ for (addr in findAddrByType(Modem.TYPE_NAME)) new Modem(addr)]; } public function getDrive(addr: String): Null { - return this.drives.find(item -> item.getAddr() == addr); + var addr = safeGetAddr(addr, Drive.TYPE_NAME); + if (addr == null) return null; + return new Drive(addr); } - private function findDrives() { - var allDrives = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "drive"); - this.drives = allDrives.map(s -> return new Drive((cc.Peripheral.wrap(s) : Dynamic), s)); + public function getAllDrives(): Array { + return [ for (addr in findAddrByType(Drive.TYPE_NAME)) new Drive(addr)]; } - /** - Get redstone peripheral. - **/ public function getRedstone(side: String): Redstone { - return this.redstone.find(item -> item.getAddr() == side); + // TODO: maybe handle restone differently to not duplicate event listeners + return new Redstone(side); } - /** - Get all connected printers. - **/ - - public function getPrinters():ReadOnlyArray { - return this.printers; + public function getPrinter(addr: String):Null { + var addr = safeGetAddr(addr, Printer.TYPE_NAME); + if (addr == null) return null; + return new Printer(addr); } - private function findPrinters() { - var allPrinters = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "printer"); - this.printers = allPrinters.map(s -> return new Printer((cc.Peripheral.wrap(s) : Dynamic), s)); + public function getAllPrinters(): Array { + return [ for (addr in findAddrByType(Printer.TYPE_NAME)) new Printer(addr)]; } + public function getEnergyStorage(addr: String): Null { + var addr = safeGetAddr(addr, EnergyStorage.TYPE_NAME); + if (addr == null) return null; + return new EnergyStorage(addr); + } + + public function getAllEnergyStorages(): Array { + return [ for (addr in findAddrByType(EnergyStorage.TYPE_NAME)) new EnergyStorage(addr)]; + } } diff --git a/src/kernel/peripherals/Printer.hx b/src/kernel/peripherals/Printer.hx index 7958d34..ead785d 100644 --- a/src/kernel/peripherals/Printer.hx +++ b/src/kernel/peripherals/Printer.hx @@ -1,15 +1,17 @@ package kernel.peripherals; +import cc.Peripheral; import lib.Rect; import lib.Pos; class Printer implements IPeripheral { + public static inline final TYPE_NAME:String = "printer"; private final native:cc.periphs.Printer.Printer; private final addr:String; - public function new(native: cc.periphs.Printer.Printer, addr: String) { - this.native = native; + public function new(addr: String) { + this.native = Peripheral.wrap(addr); this.addr = addr; } diff --git a/src/kernel/peripherals/Screen.hx b/src/kernel/peripherals/Screen.hx index a487d1b..d31eba9 100644 --- a/src/kernel/peripherals/Screen.hx +++ b/src/kernel/peripherals/Screen.hx @@ -1,5 +1,6 @@ package kernel.peripherals; +import cc.Peripheral; import lib.Pos; import cc.Term.TerminalSize; import kernel.ui.TermWriteable; @@ -9,6 +10,8 @@ import lib.Color; using tink.CoreApi; class Screen implements TermWriteable implements IPeripheral { + public static inline final TYPE_NAME:String = "monitor"; + private final nativ:cc.periphs.Monitor.Monitor; private final addr:String; @@ -17,10 +20,10 @@ class Screen implements TermWriteable implements IPeripheral { public final onResize:Signal>; @:allow(kernel.peripherals) - private function new(nativePeripherals:cc.periphs.Monitor.Monitor, addr:String) { + private function new(addr:String) { this.onResize = onResizeTrigger.asSignal(); - this.nativ = nativePeripherals; + this.nativ = Peripheral.wrap(addr); this.addr = addr; KernelEvents.instance.onMonitorResize.handle(addr -> { diff --git a/src/kernel/ui/WindowManager.hx b/src/kernel/ui/WindowManager.hx index 8f13fb6..f78a9ac 100644 --- a/src/kernel/ui/WindowManager.hx +++ b/src/kernel/ui/WindowManager.hx @@ -134,7 +134,7 @@ class WindowManager { } public function getOutputs():ReadOnlyArray { - var arr = Peripheral.instance.getScreens().map(screen -> return screen.getAddr()); + var arr = Peripheral.instance.getAllScreens().map(screen -> return screen.getAddr()); arr.push("main"); return arr; }