removed export system

This commit is contained in:
Niklas Kapelle 2024-03-12 21:47:49 +01:00
parent 72654b1036
commit fdcb81b565
Signed by: niklas
GPG Key ID: 4EB651B36D841D16
12 changed files with 2 additions and 404 deletions

View File

@ -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;
});
}, "<url>");
registerAsyncSubcommand("register", (args) -> {
var srv:Null<ResManager> = 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;
}
});
}, "<addr> <name>");
}
}

View File

@ -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<String, Export> = [];
public function new() {}
public function run(handle:ProcessHandle) {
this.handle = handle;
Net.registerProto("res", handlePackage);
load();
}
public function register(id:String, export:Export):Promise<Noise> {
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<Request> = 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}');
}
}
}

View File

@ -1,9 +1,6 @@
package kernel.peripherals; package kernel.peripherals;
import lib.exporter.ExportConfig; class EnergyStorage implements IPeripheral {
import lib.exporter.IExportable;
class EnergyStorage implements IPeripheral implements IExportable {
public static inline final TYPE_NAME:String = "energyCell"; public static inline final TYPE_NAME:String = "energyCell";
private final addr:String; private final addr:String;
@ -29,13 +26,4 @@ class EnergyStorage implements IPeripheral implements IExportable {
public function getType():String { public function getType():String {
return TYPE_NAME; return TYPE_NAME;
} }
public function export():ExportConfig {
return {
getDelegates: [
"energy" => _ -> Number(this.getEnergy()),
"capacity" => _ -> Number(this.getEnergyCapacity()),
],
}
}
} }

View File

@ -1,7 +1,5 @@
package kernel.peripherals; package kernel.peripherals;
import lib.exporter.ExportConfig;
import lib.exporter.IExportable;
import haxe.ds.ReadOnlyArray; import haxe.ds.ReadOnlyArray;
import lib.Color; 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 {
class Redstone implements IPeripheral implements IExportable {
public static inline final TYPE_NAME:String = "redstone"; // TODO: there is technically not a type for redstone. public static inline final TYPE_NAME:String = "redstone"; // TODO: there is technically not a type for redstone.
public final onChange:Signal<Noise>; public final onChange:Signal<Noise>;
@ -89,12 +86,10 @@ class Redstone implements IPeripheral implements IExportable {
cc.Redstone.setOutput(this.addr, on); cc.Redstone.setOutput(this.addr, on);
} }
@export("output")
public inline function getOutput():Bool { public inline function getOutput():Bool {
return cc.Redstone.getOutput(this.addr); return cc.Redstone.getOutput(this.addr);
} }
@export("input")
public inline function getInput():Bool { public inline function getInput():Bool {
return cc.Redstone.getInput(this.addr); return cc.Redstone.getInput(this.addr);
} }

View File

@ -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<T:IExportable & IPeripheral>(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();
}
}

View File

@ -1,8 +0,0 @@
package lib.exporter;
import lib.exporter.Response;
typedef ExportConfig = {
getDelegates:Map<String, Null<Int>->ValueType>,
// setDelegates: Map<String, (ValueType, Null<Int>)->ValueType>,
}

View File

@ -1,5 +0,0 @@
package lib.exporter;
interface IExportable {
public function export():ExportConfig;
}

View File

@ -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<Response> {
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<Response> {
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);
}
});
}
}

View File

@ -1,6 +0,0 @@
package lib.exporter;
enum Operation {
Get;
Set(value:Dynamic);
}

View File

@ -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<Int>;
public final operation:Operation;
public function new(id:String, field:String, index:Null<Int>, 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;
}
}
}

View File

@ -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);
}

View File

@ -1,79 +0,0 @@
package macros;
import haxe.macro.Context;
import haxe.macro.Expr;
using Lambda;
class Exporter {
macro public static function buildExport():Array<haxe.macro.Expr.Field> {
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;
}
}