From 9deea0ee989db383dbbdadeba9309dc828538d83 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Thu, 27 Jul 2023 20:38:23 +0200 Subject: [PATCH] made peripherals a static class --- doc/Concepts.md | 4 +-- src/bin/Disk.hx | 8 ++--- src/bin/Net.hx | 2 +- src/bin/Perf.hx | 6 ++-- src/bin/Redstone.hx | 6 ++-- src/bin/exporter/Res.hx | 2 +- src/bin/exporter/ResManager.hx | 2 +- src/kernel/Init.hx | 1 - src/kernel/KernelEvents.hx | 2 +- src/kernel/net/Net.hx | 4 +-- src/kernel/net/Routing.hx | 2 +- src/kernel/peripherals/Peripherals.hx | 49 +++++++++++---------------- src/kernel/ui/WindowManager.hx | 4 +-- src/lib/HomeContext.hx | 2 +- 14 files changed, 42 insertions(+), 52 deletions(-) diff --git a/doc/Concepts.md b/doc/Concepts.md index 92eaf5a..b17c4e3 100644 --- a/doc/Concepts.md +++ b/doc/Concepts.md @@ -54,10 +54,10 @@ Also peripherals can be made accessible via the network. More on that later. ## Usage ```haxe -var back = Peripheral.instance.getRedstone("back"); +var back = Peripheral.getRedstone("back"); back.setOutput(true); -var drive Peripheral.instance.getDrive("drive_0"); +var drive Peripheral.getDrive("drive_0"); drive.eject(); ``` diff --git a/src/bin/Disk.hx b/src/bin/Disk.hx index f5d09c2..377d170 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.getAllDrives().foreach(drive -> { + Peripheral.getAllDrives().foreach(drive -> { var addr = drive.getAddr(); var label = drive.getDiskLabel(); var id = drive.getDiskID(); @@ -53,7 +53,7 @@ class Disk extends CLIAppBase { } var driveAddr = args[0]; - var drive = Peripheral.instance.getDrive(driveAddr); + var drive = Peripheral.getDrive(driveAddr); if (drive == null){ handle.writeLine("Drive not found: " + driveAddr); @@ -76,7 +76,7 @@ class Disk extends CLIAppBase { } var driveAddr = args[0]; - var drive = Peripheral.instance.getDrive(driveAddr); + var drive = Peripheral.getDrive(driveAddr); var label:String = args[1]; if (drive == null){ @@ -102,7 +102,7 @@ class Disk extends CLIAppBase { } private function audioDiskPlayPause(driveAddr: String, play: Bool): Bool { - var drive = Peripheral.instance.getDrive(driveAddr); + var drive = Peripheral.getDrive(driveAddr); if (drive == null){ handle.writeLine("Drive not found: " + driveAddr); diff --git a/src/bin/Net.hx b/src/bin/Net.hx index 38b90e9..0925dd7 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.getAllModems(); + var modems = Peripheral.getAllModems(); for (modem in modems) { handle.writeLine(modem.name()); diff --git a/src/bin/Perf.hx b/src/bin/Perf.hx index 60192cb..7a42ad6 100644 --- a/src/bin/Perf.hx +++ b/src/bin/Perf.hx @@ -8,7 +8,7 @@ class Perf extends CLIAppBase { registerSyncSubcommand("inspect",(args)->{ if (args.length < 1) return false; - var result = Peripheral.instance.inspect(args[0]); + var result = Peripheral.inspect(args[0]); if (result == null){ handle.writeLine("No peripheral found on side "+args[0]); @@ -29,8 +29,8 @@ class Perf extends CLIAppBase { },""); registerSyncSubcommand("list",(args)->{ - for (addr in Peripheral.instance.getAllAddresses()){ - handle.writeLine('$addr => ${Peripheral.instance.getTypes(addr).join(", ")}'); + for (addr in Peripheral.getAllAddresses()){ + handle.writeLine('$addr => ${Peripheral.getTypes(addr).join(", ")}'); } return true; }); diff --git a/src/bin/Redstone.hx b/src/bin/Redstone.hx index 14858d2..03cae64 100644 --- a/src/bin/Redstone.hx +++ b/src/bin/Redstone.hx @@ -8,17 +8,17 @@ using tink.CoreApi; class Redstone extends CLIAppBase{ public function new() { registerSyncSubcommand("on", (args)-> { - Peripheral.instance.getRedstone(args[0]).setOutput(true); + Peripheral.getRedstone(args[0]).setOutput(true); return true; },""); registerSyncSubcommand("off", (args)-> { - Peripheral.instance.getRedstone(args[0]).setOutput(false); + Peripheral.getRedstone(args[0]).setOutput(false); return true; },""); registerSyncSubcommand("get", (args)-> { - var value = Peripheral.instance.getRedstone(args[0]).getAnalogInput(); + var value = Peripheral.getRedstone(args[0]).getAnalogInput(); handle.write("Analog input: " + value); return true; },""); diff --git a/src/bin/exporter/Res.hx b/src/bin/exporter/Res.hx index 82a9107..e80f461 100644 --- a/src/bin/exporter/Res.hx +++ b/src/bin/exporter/Res.hx @@ -35,7 +35,7 @@ class Res extends CLIAppBase { return false; } - var perf: kernel.peripherals.Redstone = Peripheral.instance.getRedstone(addr); + var perf: kernel.peripherals.Redstone = Peripheral.getRedstone(addr); if (perf == null) { handle.writeLine("Error: peripheral not found"); diff --git a/src/bin/exporter/ResManager.hx b/src/bin/exporter/ResManager.hx index 3027b31..1855bf1 100644 --- a/src/bin/exporter/ResManager.hx +++ b/src/bin/exporter/ResManager.hx @@ -73,7 +73,7 @@ class ResManager implements Process { var savedExports: Array<{name: String, addr: String, type: String}> = store.get("exports",[]); for (export in savedExports){ - var perph = Peripheral.instance.getFromType(export.addr,export.type); + var perph = Peripheral.getFromType(export.addr,export.type); if (perph == null){ handle.writeLine('Could not load export: ${export.name} on ${export.addr}'); diff --git a/src/kernel/Init.hx b/src/kernel/Init.hx index 5ffaa4e..877f3e0 100644 --- a/src/kernel/Init.hx +++ b/src/kernel/Init.hx @@ -26,7 +26,6 @@ class Init { // Init singeltons here because haxe is confused about the order to create them. Log.init(); KernelEvents.init(); - Peripheral.instance = new Peripheral(); WindowManager.instance = new WindowManager(); MainTerm.instance = new MainTerm(); diff --git a/src/kernel/KernelEvents.hx b/src/kernel/KernelEvents.hx index 1051d50..ac83e83 100644 --- a/src/kernel/KernelEvents.hx +++ b/src/kernel/KernelEvents.hx @@ -149,7 +149,7 @@ class KernelEvents { public static function shutdown() { // clearing screens - for (screen in Peripheral.instance.getAllScreens()) { + for (screen in Peripheral.getAllScreens()) { screen.reset(); } diff --git a/src/kernel/net/Net.hx b/src/kernel/net/Net.hx index 6abbf8b..f723a47 100644 --- a/src/kernel/net/Net.hx +++ b/src/kernel/net/Net.hx @@ -32,7 +32,7 @@ class Net { @:allow(kernel.Init) private function new() { - this.interfaces = [for (e in Peripheral.instance.getAllModems()) e ]; // TODO: is this the way to do it? + this.interfaces = [for (e in Peripheral.getAllModems()) e ]; // TODO: is this the way to do it? this.interfaces.push(Loopback.instance); for (interf in interfaces){ @@ -280,7 +280,7 @@ class Net { data: null, }; - for (modem in Peripheral.instance.getAllModems()) { + for (modem in Peripheral.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 bc033a3..72042fa 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.getAllModems()) { + for (modem in Peripheral.getAllModems()) { modem.send(Net.BRODCAST_PORT, Net.instance.networkID, pack); } } diff --git a/src/kernel/peripherals/Peripherals.hx b/src/kernel/peripherals/Peripherals.hx index d384ee5..2a4c5f9 100644 --- a/src/kernel/peripherals/Peripherals.hx +++ b/src/kernel/peripherals/Peripherals.hx @@ -11,24 +11,15 @@ using tink.CoreApi; Class responseable for retrieving peripherals. **/ class Peripheral { - /** - Depends on: KernelEvents - **/ - public static var instance:Peripheral; - - @:allow(kernel.Init) - private function new() { - } - - public function getAllAddresses(): Array { + public static function getAllAddresses(): Array { return cc.Peripheral.getNames().toArray(); } - public function isPresent(addr: String): Bool { + public static function isPresent(addr: String): Bool { return cc.Peripheral.isPresent(addr); } - public function getTypes(addr: String): Array { + public static function getTypes(addr: String): Array { if (!cc.Peripheral.isPresent(addr)) { return []; } @@ -36,11 +27,11 @@ class Peripheral { return cc.Peripheral.getType(addr).toArray(); } - public function findAddrByType(type: String): Array { + public static function findAddrByType(type: String): Array { return getAllAddresses().filter(addr -> getTypes(addr).contains(type)); } - private function safeGetAddr(addr: String, type: String): Null { + private static function safeGetAddr(addr: String, type: String): Null { if (!isPresent(addr)) { return null; } @@ -53,7 +44,7 @@ class Peripheral { return addr; } - public function inspect(addr: String): Null<{ types: Array, methods: Array}> { + public static function inspect(addr: String): Null<{ types: Array, methods: Array}> { if (!isPresent(addr)) { return null; } @@ -76,7 +67,7 @@ class Peripheral { Cast peripheral to a specific type. This is a temporary solution, maybe forever. **/ - public function getFromType(addr: String, type: String): Null { + public static function getFromType(addr: String, type: String): Null { switch (type){ case Computer.TYPE_NAME: return getComputer(addr); @@ -97,69 +88,69 @@ class Peripheral { return null; } - public function getScreen(addr: String): Null { + public static 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 { + public static function getAllScreens(): Array { return [ for (addr in findAddrByType(Screen.TYPE_NAME)) new Screen(addr)]; } - public function getModem(addr: String): Null { + public static function getModem(addr: String): Null { var addr = safeGetAddr(addr, Modem.TYPE_NAME); if (addr == null) return null; return new Modem(addr); } - public function getAllModems(): Array { + public static function getAllModems(): Array { return [ for (addr in findAddrByType(Modem.TYPE_NAME)) new Modem(addr)]; } - public function getDrive(addr: String): Null { + public static function getDrive(addr: String): Null { var addr = safeGetAddr(addr, Drive.TYPE_NAME); if (addr == null) return null; return new Drive(addr); } - public function getAllDrives(): Array { + public static function getAllDrives(): Array { return [ for (addr in findAddrByType(Drive.TYPE_NAME)) new Drive(addr)]; } - public function getRedstone(side: String): Redstone { + public static function getRedstone(side: String): Redstone { // TODO: maybe handle restone differently to not duplicate event listeners return new Redstone(side); } - public function getPrinter(addr: String):Null { + public static function getPrinter(addr: String):Null { var addr = safeGetAddr(addr, Printer.TYPE_NAME); if (addr == null) return null; return new Printer(addr); } - public function getAllPrinters(): Array { + public static function getAllPrinters(): Array { return [ for (addr in findAddrByType(Printer.TYPE_NAME)) new Printer(addr)]; } - public function getEnergyStorage(addr: String): Null { + public static 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 { + public static function getAllEnergyStorages(): Array { return [ for (addr in findAddrByType(EnergyStorage.TYPE_NAME)) new EnergyStorage(addr)]; } - public function getComputer(addr: String): Null { + public static 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 { + public static function getAllComputers(): Array { return [ for (addr in findAddrByType(Computer.TYPE_NAME)) new Computer(addr)]; } } diff --git a/src/kernel/ui/WindowManager.hx b/src/kernel/ui/WindowManager.hx index fdeeb34..f049a4c 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.getAllScreens().map(screen -> return screen.getAddr()); + var arr = Peripheral.getAllScreens().map(screen -> return screen.getAddr()); arr.push("main"); return arr; } @@ -148,7 +148,7 @@ class WindowManager { target = MainTerm.instance; currentMainContext = context; } else { - target = Peripheral.instance.getScreen(output); + target = Peripheral.getScreen(output); if (target == null) { // output target not found diff --git a/src/lib/HomeContext.hx b/src/lib/HomeContext.hx index c5d8f52..bc95404 100644 --- a/src/lib/HomeContext.hx +++ b/src/lib/HomeContext.hx @@ -131,7 +131,7 @@ class HomeContext { } private function cycleOutput() { - var screenAddr = Peripheral.instance.findAddrByType(Screen.TYPE_NAME); + var screenAddr = Peripheral.findAddrByType(Screen.TYPE_NAME); if (selectedOutputIndex == -1) { selectedOutputIndex = 0;