From 7ddf807efd526984ac3c8ffdc900c341d1311278 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Tue, 5 Sep 2023 22:09:25 +0000 Subject: [PATCH] Command line arguments, err messages, help; remove Proxy functions from JS --- source/engine/ffi.c | 3 +- source/engine/resources.c | 6 +-- source/engine/resources.h | 2 +- source/engine/yugine.c | 2 + source/scripts/components.js | 12 ------ source/scripts/engine.js | 77 +++++++++++++++++++++++++----------- source/scripts/play.js | 1 - 7 files changed, 63 insertions(+), 40 deletions(-) diff --git a/source/engine/ffi.c b/source/engine/ffi.c index 754eb21..2f21db8 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -1059,7 +1059,8 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) break; case 124: - pack_engine(); + str = JS_ToCString(js, argv[1]); + pack_engine(str); break; case 125: diff --git a/source/engine/resources.c b/source/engine/resources.c index baad86d..e0e675a 100644 --- a/source/engine/resources.c +++ b/source/engine/resources.c @@ -254,18 +254,18 @@ static int ftw_pack(const char *path, const struct stat *sb, int flag) return 0; } -void pack_engine() +void pack_engine(const char *fname) { int fd; char *key, *va; unsigned klen, vlen; - fd = open("test.cdb", O_RDWR|O_CREAT); + fd = open(fname, O_RDWR|O_CREAT); cdb_make_start(&cdbm, fd); ftw(".", ftw_pack, 20); cdb_make_finish(&cdbm); } #else -void pack_engine(){ +void pack_engine(const char *fname){ YughError("Cannot pack engine on a web build."); } #endif diff --git a/source/engine/resources.h b/source/engine/resources.h index f4ba384..119f603 100644 --- a/source/engine/resources.h +++ b/source/engine/resources.h @@ -20,6 +20,6 @@ unsigned char *slurp_file(const char *filename, long *size); char *slurp_text(const char *filename); int slurp_write(const char *txt, const char *filename); -void pack_engine(); +void pack_engine(const char *fname); #endif diff --git a/source/engine/yugine.c b/source/engine/yugine.c index 81359c5..a28b209 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -146,6 +146,8 @@ static char **args; void c_init() { render_init(); + + script_evalf("initialize();"); } int frame_fps() { diff --git a/source/scripts/components.js b/source/scripts/components.js index db6dad7..f48c62a 100644 --- a/source/scripts/components.js +++ b/source/scripts/components.js @@ -65,7 +65,6 @@ var sprite = clone(component, { kill() { cmd(9,this.id); }, }); - sprite = new Proxy(sprite, sync_proxy); sprite.obscure('boundingbox'); return sprite; }, @@ -250,14 +249,6 @@ collider2d.inputs['M-s'].doc = "Toggle if this collider is a sensor."; collider2d.inputs['M-t'] = function() { this.enabled = !this.enabled; } collider2d.inputs['M-t'].doc = "Toggle if this collider is enabled."; -var sync_proxy = { - set(t,p,v) { - t[p] = v; - t.sync(); - return true; - } -}; - var polygon2d = clone(collider2d, { name: "polygon 2d", points: [], @@ -288,8 +279,6 @@ var polygon2d = clone(collider2d, { poly.defn('points', this.points.copy()); - poly = new Proxy(poly, sync_proxy); - Object.defineProperty(poly, 'id', {enumerable:false}); Object.defineProperty(poly, 'shape', {enumerable:false}); @@ -466,7 +455,6 @@ var bucket = clone(collider2d, { make(go) { var edge = Object.create(this); Object.assign(edge, make_edge2d(go, this.points, this.thickness)); - edge = new Proxy(edge, sync_proxy); complete_assign(edge, { set thickness(x) { cmd_edge2d(1,this.id,x); diff --git a/source/scripts/engine.js b/source/scripts/engine.js index f20be09..88ce9c4 100644 --- a/source/scripts/engine.js +++ b/source/scripts/engine.js @@ -16,11 +16,11 @@ function compile(file) { var Cmdline = {}; Cmdline.cmds = []; -Cmdline.register_cmd = function(flag, fn, desc) { +Cmdline.register_cmd = function(flag, fn, doc) { Cmdline.cmds.push({ flag: flag, fn: fn, - desc: desc + doc: doc }); }; @@ -31,18 +31,36 @@ function cmd_args(cmdargs) Cmdline.play = false; - for (var i = 0; i < cmds.length; i++) { - if (cmds[i][0] !== '-') + for (var i = 1; i < cmds.length; i++) { + if (cmds[i][0] !== '-') { + Log.warn(`Command '${cmds[i]}' should start with a '-'.`); continue; + } var c = Cmdline.cmds.find(function(cmd) { return cmd.flag === cmds[i].slice(1); }); - if (c && c.fn) { - if (cmds[i+1] && cmds[i+1][0] !== '-') - c.fn(cmds[i+1]); - else - c.fn(); + if (!c) { + Log.warn(`Command ${cmds[i]} not recognized.`); + continue; } - } + + var sendstr = []; + var j = i+1; + while (cmds[j] && cmds[j][0] !== '-') { + sendstr.push(cmds[j]); + j++; + } + + c.fn(sendstr); + i = j-1; + } +} + +function initialize() +{ + if (IO.exists("config.js")) + load("config.js"); + + prototypes.load_all(); if (Cmdline.play) run("scripts/play.js"); @@ -51,20 +69,40 @@ function cmd_args(cmdargs) } Cmdline.register_cmd("p", function() { Cmdline.play = true; }, "Launch engine in play mode."); -Cmdline.register_cmd("v", function() { Log.say(cmd(120)); }, "Display engine info."); -Cmdline.register_cmd("c", null, "Redirect logging to console."); -Cmdline.register_cmd("l", null, "Set logging file name."); -Cmdline.register_cmd("h", function() { +Cmdline.register_cmd("v", function() { Log.say(cmd(120)); Game.quit(); }, "Display engine info."); +Cmdline.register_cmd("c", function() {}, "Redirect logging to console."); +Cmdline.register_cmd("l", function(n) { + Log.level = n; +}, "Set log level."); +Cmdline.register_cmd("h", function(str) { + for (var cmd of Cmdline.cmds) { + Log.say(`-${cmd.flag}: ${cmd.doc}`); + } + Game.quit(); }, "Help."); -Cmdline.register_cmd("b", function() { cmd(124); Game.quit(); }, "Pack the game into the given name."); +Cmdline.register_cmd("b", function(str) { + var packname; + if (str.length === 0) + packname = "test.cdb"; + else if (str.length > 1) { + Log.warn("Give me a single filename for the pack."); + Game.quit(); + } else + packname = str[0]; + + Log.warn(`Packing into ${packname}`); + + cmd(124, packname); + Game.quit(); +}, "Pack the game into the given name."); Cmdline.register_cmd("e", function(pawn) { run("scripts/editor.js"); eval(`Log.write(Input.print_md_kbm(${pawn}));`); Game.quit(); -}, "Print input documentation for a given object." ); +}, "Print input documentation for a given object in a markdown table." ); function run(file) { @@ -141,6 +179,7 @@ var Log = { }; load("scripts/diff.js"); +Log.level = 1; var Physics = { dynamic: 0, @@ -2362,12 +2401,6 @@ gameobject.clone("sprite", { sprite: sprite.clone(), }); - -//if (IO.exists("config.js")) -// load("config.js"); - - - var prototypes = {}; prototypes.load_all = function() { diff --git a/source/scripts/play.js b/source/scripts/play.js index 8859d72..4b813f1 100644 --- a/source/scripts/play.js +++ b/source/scripts/play.js @@ -1,6 +1,5 @@ Game.play(); - if (!IO.exists("game.js")) load("scripts/nogame.js"); else