From fdcb81b5659b55c8c913f35fdc21ac16139b509a Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Tue, 12 Mar 2024 21:47:49 +0100 Subject: [PATCH] removed export system --- src/bin/exporter/Res.hx | 58 ---------------- src/bin/exporter/ResManager.hx | 90 ------------------------- src/kernel/peripherals/EnergyStorage.hx | 14 +--- src/kernel/peripherals/Redstone.hx | 7 +- src/lib/exporter/Export.hx | 46 ------------- src/lib/exporter/ExportConfig.hx | 8 --- src/lib/exporter/IExportable.hx | 5 -- src/lib/exporter/Import.hx | 29 -------- src/lib/exporter/Operation.hx | 6 -- src/lib/exporter/Request.hx | 50 -------------- src/lib/exporter/Response.hx | 14 ---- src/macros/Exporter.hx | 79 ---------------------- 12 files changed, 2 insertions(+), 404 deletions(-) delete mode 100644 src/bin/exporter/Res.hx delete mode 100644 src/bin/exporter/ResManager.hx delete mode 100644 src/lib/exporter/Export.hx delete mode 100644 src/lib/exporter/ExportConfig.hx delete mode 100644 src/lib/exporter/IExportable.hx delete mode 100644 src/lib/exporter/Import.hx delete mode 100644 src/lib/exporter/Operation.hx delete mode 100644 src/lib/exporter/Request.hx delete mode 100644 src/lib/exporter/Response.hx delete mode 100644 src/macros/Exporter.hx diff --git a/src/bin/exporter/Res.hx b/src/bin/exporter/Res.hx deleted file mode 100644 index 459f946..0000000 --- a/src/bin/exporter/Res.hx +++ /dev/null @@ -1,58 +0,0 @@ -package bin.exporter; - -import lib.exporter.Export; -import lib.exporter.IExportable; -import kernel.peripherals.Peripherals.Peripheral; -import kernel.service.ServiceManager; -import lib.exporter.Import; -import lib.CLIAppBase; - -class Res extends CLIAppBase { - public function new() { - registerAsyncSubcommand("get", (args) -> { - var url = args[0]; - - return Import.get(url).map((res) -> { - switch (res) { - case Success(data): - handle.writeLine(Std.string(data)); - case Failure(err): - handle.writeLine("Error: "); - handle.writeLine(Std.string(err)); - } - return true; - }); - }, ""); - - registerAsyncSubcommand("register", (args) -> { - var srv:Null = ServiceManager.get("resmgr"); - - var addr = args[0]; - var name = args[1]; - - if (srv == null) { - handle.writeLine("Error: resmgr not found"); - return false; - } - - var perf:kernel.peripherals.Redstone = Peripheral.getRedstone(addr); - - if (perf == null) { - handle.writeLine("Error: peripheral not found"); - return false; - } - - return srv.register(name, new Export(perf)).map((res) -> { - switch (res) { - case Success(_): - handle.writeLine("Success"); - return true; - case Failure(err): - handle.writeLine("Error: "); - handle.writeLine(Std.string(err)); - return false; - } - }); - }, " "); - } -} diff --git a/src/bin/exporter/ResManager.hx b/src/bin/exporter/ResManager.hx deleted file mode 100644 index 7e4f51f..0000000 --- a/src/bin/exporter/ResManager.hx +++ /dev/null @@ -1,90 +0,0 @@ -package bin.exporter; - -import kernel.KernelSettings; -import kernel.peripherals.Peripherals.Peripheral; -import lib.KVStore; -import lib.exporter.Request; -import kernel.ps.ProcessHandle; -import lib.exporter.Export; -import kernel.ps.Process; -import kernel.net.Package; -import kernel.net.Net; -import kernel.net.Package.GenericPackage; - -using tink.CoreApi; - -class ResManager implements Process { - private var handle:ProcessHandle; - private var exports:Map = []; - - public function new() {} - - public function run(handle:ProcessHandle) { - this.handle = handle; - Net.registerProto("res", handlePackage); - load(); - } - - public function register(id:String, export:Export):Promise { - if (exports.exists(id)) { - return Promise.reject(new Error("Ressource already exists: " + id)); - } - - return registerName(id).next((success) -> { - exports.set(id, export); - persist(); - return null; - }); - } - - private function handlePackage(pack:GenericPackage) { - var requestPack:Package = cast pack; - var id = requestPack.data.id; - - if (!exports.exists(id)) { - requestPack.respond(lib.exporter.Response.NotFound); - return; - } - - var export = exports.get(id); - var response = export.handleRequest(requestPack.data); - - requestPack.respond(response); - } - - private function registerName(id:String) { - var rpc = new SiteRessourceControllerRPC(KernelSettings.siteController); - return rpc.register(id, Net.networkID); - } - - private function persist() { - var store = new KVStore("export"); - - var saveExports:Array<{name:String, addr:String, type:String}> = [for (k => v in this.exports) {name: k, addr: v.getAddr(), type: v.getType()}]; - - store.set("exports", saveExports); - - store.save(); - } - - private function load() { - var store = new KVStore("export"); - - var savedExports:Array<{name:String, addr:String, type:String}> = store.get("exports", []); - - for (export in savedExports) { - var perph = Peripheral.getFromType(export.addr, export.type); - - if (perph == null) { - handle.writeLine('Could not load export: ${export.name} on ${export.addr}'); - continue; - } - - // I dont know if cast is the best way to do this - // But since we know that this is a IExportable we can do this (I think) - exports.set(export.name, new Export(cast perph)); - - handle.writeLine('Loaded export: ${export.name} on ${export.addr}'); - } - } -} diff --git a/src/kernel/peripherals/EnergyStorage.hx b/src/kernel/peripherals/EnergyStorage.hx index 690f03c..90cbd39 100644 --- a/src/kernel/peripherals/EnergyStorage.hx +++ b/src/kernel/peripherals/EnergyStorage.hx @@ -1,9 +1,6 @@ package kernel.peripherals; -import lib.exporter.ExportConfig; -import lib.exporter.IExportable; - -class EnergyStorage implements IPeripheral implements IExportable { +class EnergyStorage implements IPeripheral { public static inline final TYPE_NAME:String = "energyCell"; private final addr:String; @@ -29,13 +26,4 @@ class EnergyStorage implements IPeripheral implements IExportable { 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/Redstone.hx b/src/kernel/peripherals/Redstone.hx index 3e0e04d..ce0a172 100644 --- a/src/kernel/peripherals/Redstone.hx +++ b/src/kernel/peripherals/Redstone.hx @@ -1,7 +1,5 @@ package kernel.peripherals; -import lib.exporter.ExportConfig; -import lib.exporter.IExportable; import haxe.ds.ReadOnlyArray; import lib.Color; @@ -43,8 +41,7 @@ abstract BundleMask(Int) from cc.Colors.Color to cc.Colors.Color { } } -@:build(macros.Exporter.buildExport()) -class Redstone implements IPeripheral implements IExportable { +class Redstone implements IPeripheral { public static inline final TYPE_NAME:String = "redstone"; // TODO: there is technically not a type for redstone. public final onChange:Signal; @@ -89,12 +86,10 @@ class Redstone implements IPeripheral implements IExportable { cc.Redstone.setOutput(this.addr, on); } - @export("output") public inline function getOutput():Bool { return cc.Redstone.getOutput(this.addr); } - @export("input") public inline function getInput():Bool { return cc.Redstone.getInput(this.addr); } diff --git a/src/lib/exporter/Export.hx b/src/lib/exporter/Export.hx deleted file mode 100644 index 0361edf..0000000 --- a/src/lib/exporter/Export.hx +++ /dev/null @@ -1,46 +0,0 @@ -package lib.exporter; - -import kernel.peripherals.IPeripheral; -import kernel.log.Log; - -using tink.CoreApi; - -class Export { - private final exportConfig:ExportConfig; - private final peripheral:IPeripheral; - - public function new(exportPerph:T) { - this.peripheral = exportPerph; - this.exportConfig = exportPerph.export(); - } - - public function handleRequest(req:Request):Response { - switch (req.operation) { - case Get: - return handleGet(req); - case Set(value): - // TODO: implement - return NotFound; - } - } - - private function handleGet(request:Request):Response { - if (!this.exportConfig.getDelegates.exists(request.field)) { - Log.warn('Requested get field ${request.field} does not exist in ??'); - return NotFound; - } - - var delegate = this.exportConfig.getDelegates.get(request.field); - var value = delegate(request.index); - - return Get(value); - } - - public function getType():String { - return this.peripheral.getType(); - } - - public function getAddr():String { - return this.peripheral.getAddr(); - } -} diff --git a/src/lib/exporter/ExportConfig.hx b/src/lib/exporter/ExportConfig.hx deleted file mode 100644 index b2b3506..0000000 --- a/src/lib/exporter/ExportConfig.hx +++ /dev/null @@ -1,8 +0,0 @@ -package lib.exporter; - -import lib.exporter.Response; - -typedef ExportConfig = { - getDelegates:Map->ValueType>, - // setDelegates: Map)->ValueType>, -} diff --git a/src/lib/exporter/IExportable.hx b/src/lib/exporter/IExportable.hx deleted file mode 100644 index 7659cc2..0000000 --- a/src/lib/exporter/IExportable.hx +++ /dev/null @@ -1,5 +0,0 @@ -package lib.exporter; - -interface IExportable { - public function export():ExportConfig; -} diff --git a/src/lib/exporter/Import.hx b/src/lib/exporter/Import.hx deleted file mode 100644 index e153736..0000000 --- a/src/lib/exporter/Import.hx +++ /dev/null @@ -1,29 +0,0 @@ -package lib.exporter; - -import kernel.KernelSettings; -import kernel.net.Net; -import kernel.net.Package.NetworkID; - -using tink.CoreApi; - -class Import { - public static function get(ressourceLocator:String):Promise { - var request = Request.fromString(ressourceLocator); - var rpc = new SiteRessourceControllerRPC(KernelSettings.siteController); - - return rpc.get(request.id).next((response) -> { - return performRequest(response, request); - }); - } - - private static function performRequest(netID:NetworkID, request:Request):Promise { - return Net.sendAndAwait(netID, "res", request).map((response) -> { - switch (response) { - case Success(data): - return Success(cast(data.data, Response)); - case Failure(error): - return Failure(error); - } - }); - } -} diff --git a/src/lib/exporter/Operation.hx b/src/lib/exporter/Operation.hx deleted file mode 100644 index e6d1d9e..0000000 --- a/src/lib/exporter/Operation.hx +++ /dev/null @@ -1,6 +0,0 @@ -package lib.exporter; - -enum Operation { - Get; - Set(value:Dynamic); -} diff --git a/src/lib/exporter/Request.hx b/src/lib/exporter/Request.hx deleted file mode 100644 index a0adeb0..0000000 --- a/src/lib/exporter/Request.hx +++ /dev/null @@ -1,50 +0,0 @@ -package lib.exporter; - -import lua.TableTools; -import lua.NativeStringTools; - -class Request { - public final id:String; - public final field:String; - public final index:Null; - public final operation:Operation; - - public function new(id:String, field:String, index:Null, operation:Operation) { - this.id = id; - this.field = field; - this.index = index; - this.operation = operation; - } - - /** - Example: - "myfield[2]@myid" - "myfield@myid" - **/ - public static function fromString(locator:String):Request { - if (StringTools.contains(locator, "[")) { - var f = TableTools.pack(NativeStringTools.gmatch(locator, "(%a+)%[([%d]+)%]@(%a+)")()); - - var field = f[1]; - var index = Std.parseInt(f[2]); - var id = f[3]; - - return new Request(id, field, index, Get); - } else { - var f = TableTools.pack(NativeStringTools.gmatch(locator, "(%a+)@(%a+)")()); - - var field = f[1]; - var id = f[2]; - - return new Request(id, field, null, Get); - } - } - - public function toString() { - if (index == null) { - return field + "@" + id; - } else { - return field + "[" + index + "]@" + id; - } - } -} diff --git a/src/lib/exporter/Response.hx b/src/lib/exporter/Response.hx deleted file mode 100644 index 6bd4709..0000000 --- a/src/lib/exporter/Response.hx +++ /dev/null @@ -1,14 +0,0 @@ -package lib.exporter; - -enum Response { - NotFound; - Set; - NotSet; - Get(value:ValueType); -} - -enum ValueType { - Number(value:Int); - String(value:String); - Bool(value:Bool); -} diff --git a/src/macros/Exporter.hx b/src/macros/Exporter.hx deleted file mode 100644 index 15dc37b..0000000 --- a/src/macros/Exporter.hx +++ /dev/null @@ -1,79 +0,0 @@ -package macros; - -import haxe.macro.Context; -import haxe.macro.Expr; - -using Lambda; - -class Exporter { - macro public static function buildExport():Array { - var fields = Context.getBuildFields(); - - var getExp = []; - - for (field in fields) { - if (field.meta == null) - continue; - - var s = ""; - for (meta in field.meta) { - if (meta.name == "export") { - switch (meta.params[0].expr) { - case EConst(CString(s1)): - s = s1; - default: - Context.error("Invalid export name", meta.pos); - } - } - } - - if (s == "") - continue; - - switch (field.kind) { - case FFun(f): - var funName = field.name; - switch (f.ret) { - case TPath(p): - switch (p.name) { - case "Int": - getExp.push(macro $v{s} => (i:Int) -> lib.exporter.Response.ValueType.Int(this.$funName())); - case "Float": - getExp.push(macro $v{s} => (i:Int) -> lib.exporter.Response.ValueType.Float(this.$funName())); - case "Bool": - getExp.push(macro $v{s} => (i:Int) -> lib.exporter.Response.ValueType.Bool(this.$funName())); - case "String": - getExp.push(macro $v{s} => (i:Int) -> lib.exporter.Response.ValueType.String(this.$funName())); - default: - Context.error("Only Int, Float, Bool and String can be exported", field.pos); - } - default: - Context.error("Only functions returning a type can be exported", field.pos); - } - default: - Context.error("Only functions can be exported", field.pos); - } - } - - var exportField:Field = { - name: "export", - pos: Context.currentPos(), - kind: FFun({ - args: [], - ret: TPath({name: "ExportConfig", pack: []}), - expr: macro { - return { - getDelegates: $a{getExp}, - }; - } - }), - access: [APublic], - doc: null, - meta: [], - }; - - fields.push(exportField); - - return fields; - } -}