made peripherals a static class
This commit is contained in:
parent
a93ee1cddf
commit
9deea0ee98
@ -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();
|
||||
```
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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());
|
||||
|
@ -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 {
|
||||
},"<side>");
|
||||
|
||||
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;
|
||||
});
|
||||
|
@ -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;
|
||||
},"<side>");
|
||||
|
||||
registerSyncSubcommand("off", (args)-> {
|
||||
Peripheral.instance.getRedstone(args[0]).setOutput(false);
|
||||
Peripheral.getRedstone(args[0]).setOutput(false);
|
||||
return true;
|
||||
},"<side>");
|
||||
|
||||
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;
|
||||
},"<side>");
|
||||
|
@ -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");
|
||||
|
@ -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}');
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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<String> {
|
||||
public static function getAllAddresses(): Array<String> {
|
||||
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<String> {
|
||||
public static function getTypes(addr: String): Array<String> {
|
||||
if (!cc.Peripheral.isPresent(addr)) {
|
||||
return [];
|
||||
}
|
||||
@ -36,11 +27,11 @@ class Peripheral {
|
||||
return cc.Peripheral.getType(addr).toArray();
|
||||
}
|
||||
|
||||
public function findAddrByType(type: String): Array<String> {
|
||||
public static function findAddrByType(type: String): Array<String> {
|
||||
return getAllAddresses().filter(addr -> getTypes(addr).contains(type));
|
||||
}
|
||||
|
||||
private function safeGetAddr(addr: String, type: String): Null<String> {
|
||||
private static function safeGetAddr(addr: String, type: String): Null<String> {
|
||||
if (!isPresent(addr)) {
|
||||
return null;
|
||||
}
|
||||
@ -53,7 +44,7 @@ class Peripheral {
|
||||
return addr;
|
||||
}
|
||||
|
||||
public function inspect(addr: String): Null<{ types: Array<String>, methods: Array<String>}> {
|
||||
public static function inspect(addr: String): Null<{ types: Array<String>, methods: Array<String>}> {
|
||||
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<IPeripheral> {
|
||||
public static function getFromType(addr: String, type: String): Null<IPeripheral> {
|
||||
switch (type){
|
||||
case Computer.TYPE_NAME:
|
||||
return getComputer(addr);
|
||||
@ -97,69 +88,69 @@ class Peripheral {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getScreen(addr: String): Null<Screen> {
|
||||
public static 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> {
|
||||
public static function getAllScreens(): Array<Screen> {
|
||||
return [ for (addr in findAddrByType(Screen.TYPE_NAME)) new Screen(addr)];
|
||||
}
|
||||
|
||||
public function getModem(addr: String): Null<Modem> {
|
||||
public static function getModem(addr: String): Null<Modem> {
|
||||
var addr = safeGetAddr(addr, Modem.TYPE_NAME);
|
||||
if (addr == null) return null;
|
||||
return new Modem(addr);
|
||||
}
|
||||
|
||||
public function getAllModems(): Array<Modem> {
|
||||
public static function getAllModems(): Array<Modem> {
|
||||
return [ for (addr in findAddrByType(Modem.TYPE_NAME)) new Modem(addr)];
|
||||
}
|
||||
|
||||
public function getDrive(addr: String): Null<Drive> {
|
||||
public static function getDrive(addr: String): Null<Drive> {
|
||||
var addr = safeGetAddr(addr, Drive.TYPE_NAME);
|
||||
if (addr == null) return null;
|
||||
return new Drive(addr);
|
||||
}
|
||||
|
||||
public function getAllDrives(): Array<Drive> {
|
||||
public static function getAllDrives(): Array<Drive> {
|
||||
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<Printer> {
|
||||
public static function getPrinter(addr: String):Null<Printer> {
|
||||
var addr = safeGetAddr(addr, Printer.TYPE_NAME);
|
||||
if (addr == null) return null;
|
||||
return new Printer(addr);
|
||||
}
|
||||
|
||||
public function getAllPrinters(): Array<Printer> {
|
||||
public static function getAllPrinters(): Array<Printer> {
|
||||
return [ for (addr in findAddrByType(Printer.TYPE_NAME)) new Printer(addr)];
|
||||
}
|
||||
|
||||
public function getEnergyStorage(addr: String): Null<EnergyStorage> {
|
||||
public static 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> {
|
||||
public static function getAllEnergyStorages(): Array<EnergyStorage> {
|
||||
return [ for (addr in findAddrByType(EnergyStorage.TYPE_NAME)) new EnergyStorage(addr)];
|
||||
}
|
||||
|
||||
public function getComputer(addr: String): Null<Computer> {
|
||||
public static function getComputer(addr: String): Null<Computer> {
|
||||
var addr = safeGetAddr(addr, Computer.TYPE_NAME);
|
||||
if (addr == null) return null;
|
||||
return new Computer(addr);
|
||||
}
|
||||
|
||||
public function getAllComputers(): Array<Computer> {
|
||||
public static function getAllComputers(): Array<Computer> {
|
||||
return [ for (addr in findAddrByType(Computer.TYPE_NAME)) new Computer(addr)];
|
||||
}
|
||||
}
|
||||
|
@ -134,7 +134,7 @@ class WindowManager {
|
||||
}
|
||||
|
||||
public function getOutputs():ReadOnlyArray<String> {
|
||||
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
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user