big Peripheal refactor
This commit is contained in:
parent
2051f486d4
commit
fd2b53167e
@ -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();
|
||||
|
@ -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());
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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<Noise>;
|
||||
public final onDiskEject:Signal<Noise>;
|
||||
|
||||
@ -12,9 +15,9 @@ class Drive implements IPeripheral {
|
||||
private final onDiskEjectTrigger:SignalTrigger<Noise> = 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();
|
||||
|
@ -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<Float>}>;
|
||||
|
||||
@ -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 ->{
|
||||
|
@ -17,93 +17,96 @@ class Peripheral {
|
||||
**/
|
||||
public static var instance:Peripheral;
|
||||
|
||||
private var screens: Array<Screen> = [];
|
||||
private var modes: Array<Modem> = [];
|
||||
private var drives:Array<Drive> = [];
|
||||
private var redstone:Array<Redstone> = [];
|
||||
private var printers:Array<Printer> = [];
|
||||
|
||||
@: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<String> {
|
||||
return cc.Peripheral.getNames().toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
Get all connected screens.
|
||||
**/
|
||||
public function getScreens():ReadOnlyArray<Screen> {
|
||||
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<String> {
|
||||
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<String> {
|
||||
return getAllAddresses().filter(addr -> getTypes(addr).contains(type));
|
||||
}
|
||||
|
||||
private function safeGetAddr(addr: String, type: String): Null<String> {
|
||||
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<Modem> {
|
||||
return this.modes;
|
||||
public function getScreen(addr: String): Null<Screen> {
|
||||
var addr = safeGetAddr(addr, Screen.TYPE_NAME);
|
||||
if (addr == null) return null;
|
||||
return new Screen(addr);
|
||||
}
|
||||
|
||||
|
||||
public function getAllScreens(): Array<Screen> {
|
||||
return [ for (addr in findAddrByType(Screen.TYPE_NAME)) new Screen(addr)];
|
||||
}
|
||||
|
||||
public function getModem(addr: String): Null<Modem> {
|
||||
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<Drive> {
|
||||
return this.drives;
|
||||
public function getAllModems(): Array<Modem> {
|
||||
return [ for (addr in findAddrByType(Modem.TYPE_NAME)) new Modem(addr)];
|
||||
}
|
||||
|
||||
public function getDrive(addr: String): Null<Drive> {
|
||||
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<Drive> {
|
||||
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<Printer> {
|
||||
return this.printers;
|
||||
public function getPrinter(addr: String):Null<Printer> {
|
||||
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<Printer> {
|
||||
return [ for (addr in findAddrByType(Printer.TYPE_NAME)) new Printer(addr)];
|
||||
}
|
||||
|
||||
public function getEnergyStorage(addr: String): Null<EnergyStorage> {
|
||||
var addr = safeGetAddr(addr, EnergyStorage.TYPE_NAME);
|
||||
if (addr == null) return null;
|
||||
return new EnergyStorage(addr);
|
||||
}
|
||||
|
||||
public function getAllEnergyStorages(): Array<EnergyStorage> {
|
||||
return [ for (addr in findAddrByType(EnergyStorage.TYPE_NAME)) new EnergyStorage(addr)];
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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<Vec2<Int>>;
|
||||
|
||||
@: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 -> {
|
||||
|
@ -134,7 +134,7 @@ class WindowManager {
|
||||
}
|
||||
|
||||
public function getOutputs():ReadOnlyArray<String> {
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user