ALL NEW improved Binstore

also automaticly inlcude everything in src/bin
This commit is contained in:
Niklas Kapelle 2024-01-22 03:34:19 +01:00
parent 92deb7177f
commit 295d284bc1
Signed by: niklas
GPG Key ID: 4EB651B36D841D16
13 changed files with 172 additions and 126 deletions

View File

@ -9,4 +9,6 @@
-D lua-vanilla
-D lua-ver 5.1
--macro include("bin")
--lua build/haxe.lua

74
src/bin/HelloWorldGUI.hx Normal file
View File

@ -0,0 +1,74 @@
package bin;
import kernel.log.Log;
import lib.ui.elements.TextElement;
import lib.Pos;
import lib.ui.elements.UIElement;
import lib.ui.elements.LayerdRootElement;
import kernel.ui.WindowContext;
import kernel.ps.ProcessHandle;
import kernel.ps.Process;
@:build(macros.Binstore.includeBin("HelloWorld-GUI", ["hello-gui"]))
class HelloWorldGUI implements Process {
private var handle:ProcessHandle;
private var ctx:WindowContext;
private var requestRender:Void->Void;
private var root:LayerdRootElement;
public function new() {}
public function run(handle:ProcessHandle) {
this.handle = handle;
var stateless = handle.createStatelessWindowContext();
this.ctx = stateless.ctx;
this.requestRender = stateless.requestRender;
stateless.setRenderFunc(this.render);
this.root = new LayerdRootElement();
this.ctx.delegateEvents(this.root);
this.requestRender();
}
private function render() {
var children:Array<{element:UIElement, offset:Pos}> = [
{
element: new TextElement("Hello World", {
uiEvents: {
onClick: () -> {
Log.debug("Hello World");
}
}
}),
offset: new Pos({x: 0, y: 0})
},
{
element: new TextElement("Holla Mundo", {
uiEvents: {
onClick: () -> {
Log.debug("Holla Mundo");
}
}
}),
offset: new Pos({x: 0, y: 1})
},
{
element: new TextElement("Ayyy", {
uiEvents: {
onClick: () -> {
Log.debug("Ayy");
}
}
}),
offset: new Pos({x: 4, y: 1})
}
];
this.root.setChildren(children);
this.root.render(ctx.getSize()).renderToContext(ctx);
}
}

View File

@ -141,7 +141,7 @@ class Terminal implements Process {
var commandArgs:Array<String> = args.slice(1);
var ps = getProgByName(commandName);
var ps = BinStore.instantiate(commandName);
if (ps == null) {
this.backlog.push("Unknown command: " + commandName);
this.requestRender();
@ -204,15 +204,6 @@ class Terminal implements Process {
this.requestRender();
}
private function getProgByName(name:String):Process {
var bin = BinStore.getBinByAlias(name);
if (bin == null) {
return null;
}
return Type.createInstance(bin.c, []);
}
private function moveCursorToInput() {
var size = this.ctx.getSize();
this.ctx.setCursorPos(this.input.length + 2, size.y - 1);

View File

@ -1,9 +0,0 @@
package kernel;
@:keep
class DCEHack {
// Dont actually call this
public static function load():Array<kernel.ps.Process> {
macros.DCEHack.dceGenerateCreate();
}
}

View File

@ -8,7 +8,6 @@ import kernel.net.Routing;
import lib.Debug;
import kernel.ui.WindowManager;
import kernel.net.Net;
import kernel.DCEHack; // Important for DCE hack
class Init {
@:allow(kernel.KernelEvents)

View File

@ -6,7 +6,7 @@ import kernel.ps.Process;
Represents a callable program.
**/
typedef Bin = {
c:Class<Process>,
c:Void->Process,
name:String,
aliases:Array<String>,
}

View File

@ -1,64 +1,31 @@
package kernel.binstore;
import bin.turtle.Excavate;
import bin.turtle.Patrol;
import bin.pathfinder.PFClient;
import bin.ID;
import bin.exporter.Res;
import bin.exporter.ResManager;
import bin.KSettings;
import bin.Perf;
import bin.srsc.CLI;
import bin.srsc.SiteRessourceController;
import bin.HelloWorldService;
import bin.Service;
import bin.LSPS;
import bin.Turtle;
import bin.Terminal;
import bin.Redstone;
import bin.Net;
import bin.KernelLog;
import bin.HelloWorld;
import bin.GPS;
import bin.Disk;
import haxe.ds.ReadOnlyArray;
import kernel.ps.Process;
import macros.Binstore;
class BinStore {
private static final store:ReadOnlyArray<Bin> = [
{c: Disk, name: "Disk", aliases: ["disk"]},
{c: GPS, name: "GPS", aliases: ["gps"]},
{c: HelloWorld, name: "HelloWorld", aliases: ["hello"]},
{c: KernelLog, name: "KernelLog", aliases: ["log"]},
{c: Net, name: "Net", aliases: ["net"]},
{c: Redstone, name: "Redstone", aliases: ["redstone", "rs"]},
{c: Terminal, name: "Terminal", aliases: ["terminal", "term"]},
{c: Turtle, name: "Turtle", aliases: ["turtle", "t"]},
{c: LSPS, name: "PM", aliases: ["lsps"]},
{c: Service, name: "Service", aliases: ["service", "srv"]},
{c: HelloWorldService, name: "HelloWorldService", aliases: ["hello-service"]},
{c: SiteRessourceController, name: "SiteRessourceController", aliases: ["srsc"]},
{c: CLI, name: "SRSC CLI", aliases: ["srsc-cli"]},
{c: Perf, name: "Perf", aliases: ["perf"]},
{c: KSettings, name: "KSettings", aliases: ["ksettings", "ks"]},
{c: ResManager, name: "ResManager", aliases: ["resmanager", "resmgr"]},
{c: Res, name: "Res", aliases: ["res"]},
{c: ID, name: "ID", aliases: ["id"]},
{c: PFClient, name: "PFClient", aliases: ["pfclient"]},
{c: Patrol, name: "Patrol", aliases: ["patrol"]},
{c: Excavate, name: "Excavate", aliases: ["excavate"]}
];
private static final bins:Array<Bin> = populateStore();
public static function getBinByName(name:String):Null<Bin> {
for (bin in store) {
if (bin.name == name) {
return bin;
private static function populateStore()
return {
var bins:Array<Bin> = [];
Binstore.generateBinStore();
return bins;
}
public static function instantiate(alias:String):Null<Process> {
for (bin in bins) {
for (a in bin.aliases) {
if (a == alias) {
return bin.c();
}
}
}
return null;
}
public static function getBinByAlias(alias:String):Null<Bin> {
for (bin in store) {
private static function getBinByAlias(alias:String):Null<Bin> {
for (bin in bins) {
for (a in bin.aliases) {
if (a == alias) {
return bin;
@ -68,7 +35,7 @@ class BinStore {
return null;
}
public static function getNameByAlias(alias:String):String {
public static function getNameByAlias(alias:String):Null<String> {
var bin = getBinByAlias(alias);
if (bin == null) {
return null;

View File

@ -3,7 +3,6 @@ package kernel.ps;
/**
Defines an independent process that can be run by the kernel.
**/
@:autoBuild(macros.DCEHack.DCEHack.dceInclude())
interface Process {
public function run(handle:ProcessHandle):Void;
}

View File

@ -21,13 +21,13 @@ class Service {
}
public function start() {
var bin = BinStore.getBinByAlias(this.binName);
var ps = BinStore.instantiate(this.binName);
if (bin == null) {
if (ps == null) {
throw new Error('Bin ${this.binName} not found');
}
this.ps = Type.createInstance(bin.c, this.args);
this.ps = ps;
this.pid = ProcessManager.run(this.ps, {});
}

View File

@ -65,7 +65,7 @@ class ServiceManager {
}
public static function register(name:String, binName:String, args:Array<String>):Outcome<Noise, String> {
if (BinStore.getBinByAlias(binName) == null) {
if (BinStore.getNameByAlias(binName) == null) {
return Failure("bin not found");
}

View File

@ -31,7 +31,7 @@ class HomeContext {
private var selectedOutput:String = "main";
private var selectedOutputIndex:Int = -1;
private final listedApps:Array<String> = ["terminal", "log", "pfclient"];
private final listedApps:Array<String> = ["terminal", "log", "tprint", "hello-gui"];
public function new() {}
@ -101,14 +101,13 @@ class HomeContext {
}
private function spawnPs(binName:String) {
var bin = BinStore.getBinByAlias(binName);
var ps = BinStore.instantiate(binName);
if (bin == null) {
if (ps == null) {
Log.error('Could not find bin: ${binName}');
return;
}
var ps = Type.createInstance(bin.c, []);
var pid = ProcessManager.run(ps, {});
var lastContextID = -1;

68
src/macros/Binstore.hx Normal file
View File

@ -0,0 +1,68 @@
package macros;
import haxe.macro.TypeTools;
import haxe.macro.Context;
import haxe.macro.Expr;
using Lambda;
class Binstore {
static var bins:Array<{c:TypePath, name:String, alias:Array<String>}> = [];
macro static function includeBin(name:String, alias:Array<String>):Array<Field> {
var fields = Context.getBuildFields();
var localClass = Context.getLocalClass().get();
// FIXME: Right now we are trusting the dev that the class implements Process. It will fail later
// anyway but better tell ihm now.
var constructor = fields.find((field) -> field.name == "new");
if (!constructor.access.contains(APublic)) {
Context.error("Constructor needs to be public", constructor.pos);
}
switch constructor.kind {
case FFun(f):
if (f.args.length != 0) {
Context.error("Process constructor are not allowed to have args", constructor.pos);
}
default:
Context.error("Somethings wrong with your constructor", constructor.pos);
}
switch TypeTools.toComplexType(Context.getLocalType()) {
case TPath(p):
bins.push({
c: p,
name: name,
alias: alias
});
default:
Context.error("IDFK what went wrong", localClass.pos);
}
return fields;
}
public macro static function generateBinStore() {
var exprs:Array<Expr> = [];
for (bin in bins) {
var c = bin.c;
exprs.push(macro {
bins.push({
c: () -> {
return new $c();
},
name: $v{bin.name},
aliases: $v{bin.alias}
});
});
}
return macro {
$a{exprs}
};
}
}

View File

@ -1,44 +0,0 @@
package macros;
import haxe.macro.Context;
import haxe.macro.Expr;
using Lambda;
class DCEHack {
public static final classes:Array<haxe.macro.Type> = [];
macro static public function dceInclude():Array<Field> {
#if !display
var localClass = Context.getLocalClass();
if (localClass == null) {
return Context.getBuildFields();
}
// Ignore abstract classes
if (localClass.get().isAbstract) {
return Context.getBuildFields();
}
classes.push(Context.getLocalType());
#end
return Context.getBuildFields();
}
macro static public function dceGenerateCreate() {
var exprs = [];
for (c in classes) {
switch (c) {
case TInst(_.get() => t, _):
var path:TypePath = {pack: t.pack, name: t.name};
exprs.push(macro new $path());
default:
Context.error("Unknown type: " + c, Context.currentPos());
}
}
return macro return $a{exprs};
}
}