JS globbing; remove glob b/c windows; pregame script hooks

This commit is contained in:
John Alanbrook 2023-09-13 06:08:32 +00:00
parent 009609458b
commit 94c2a3c143
9 changed files with 64 additions and 55 deletions

View file

@ -79,10 +79,10 @@ JSValue str2js(const char *c) {
return JS_NewString(js, c);
}
JSValue strarr2js(const char **c, int len)
JSValue strarr2js(const char **c)
{
JSValue arr = JS_NewArray(js);
for (int i = 0; i < len; i++)
for (int i = 0; i < arrlen(c); i++)
JS_SetPropertyUint32(js, arr, i, JS_NewString(js, c[i]));
return arr;
@ -421,19 +421,6 @@ JSValue duk_nuke(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
#endif
JSValue duk_win_make(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
/*
const char *title = JS_ToCString(js, argv[0]);
int w = js2int(argv[1]);
int h = js2int(argv[2]);
struct window *win = MakeSDLWindow(title, w, h, 0);
JS_FreeCString(js, title);
return JS_NewInt64(js, win->id);
*/
return JS_NULL;
}
JSValue duk_spline_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
static_assert(sizeof(tsReal) * 2 == sizeof(cpVect));
@ -575,25 +562,6 @@ int file_exists(char *path) {
return 0;
}
/*
static int duk2path(const char *path, const struct stat *sb, int typeflag) {
if (typeflag == FTW_F) {
char *ext = strrchr(path, '.');
if (ext && !strcmp(ext, dukext))
JS_SetPropertyUint32(js, dukarr, dukidx++, JS_NewString(js, &path[2]));
}
return 0;
}
*/
JSValue dukext2paths(char *ext) {
char *paths = NULL;
fill_extensions(paths, ".", ext);
// return dukarr;
return JS_NULL;
}
JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
int cmd = js2int(argv[0]);
const char *str = NULL;
@ -865,8 +833,7 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
break;
case 66:
str = JS_ToCString(js, argv[1]);
ret = dukext2paths(str);
ret = strarr2js(ls(","));
break;
case 67:
@ -1122,6 +1089,11 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
case 133:
ret = JS_NewFloat64(js, appTime);
break;
case 134:
str = JS_ToCString(js,argv[1]);
app_name(str);
break;
}
if (str)
@ -1687,7 +1659,6 @@ void ffi_load() {
DUK_FUNC(q_body, 2)
DUK_FUNC(sys_cmd, 1)
DUK_FUNC(win_make, 3)
DUK_FUNC(make_sprite, 3)
DUK_FUNC(make_anim2d, 3)

View file

@ -109,6 +109,29 @@ void fill_extensions(char *paths, const char *path, const char *ext) {
ext_paths = paths;
ftw(".", ext_check, 10);
}
static char **ls_paths = NULL;
static int ls_ftw(const char *path, const struct stat *sb, int typeflag)
{
if (typeflag == FTW_F && strlen(path) > 2)
arrpush(ls_paths, strdup(&path[2]));
return 0;
}
char **ls(char *path)
{
if (ls_paths) {
for (int i = 0; i < arrlen(ls_paths); i++)
free(ls_paths[i]);
arrfree(ls_paths);
}
ftw(".", ls_ftw, 10);
return ls_paths;
}
#else
void fill_extensions(char *paths, const char *path, const char *ext)
{};
@ -140,12 +163,6 @@ char *make_path(const char *file) {
return pathbuf;
}
char *strdup(const char *s) {
char *new = malloc(sizeof(char) * (strlen(s) + 1));
strcpy(new, s);
return new;
}
unsigned char *slurp_file(const char *filename, size_t *size)
{
if (cdb_find(&game_cdb, filename, strlen(filename))) {
@ -181,6 +198,7 @@ char *slurp_text(const char *filename, size_t *size)
{
size_t len;
char *str = slurp_file(filename, &len);
if (!str) return NULL;
char *retstr = malloc(len+1);
memcpy(retstr, str, len);
retstr[len] = '\0';

View file

@ -13,8 +13,7 @@ char *str_replace_ext(const char *s, const char *newext);
FILE *res_open(char *path, const char *tag);
FILE *path_open(const char *tag, const char *fmt, ...);
char *make_path(const char *file);
char *strdup(const char *s);
char **ls(char *path);
unsigned char *slurp_file(const char *filename, size_t *size);
char *slurp_text(const char *filename, size_t *size);

View file

@ -131,6 +131,7 @@ JSValue script_runfile(const char *file)
{
size_t len;
const char *script = slurp_text(file, &len);
if (!script) return JS_NULL;
JSValue obj = JS_Eval(js, script, len, file, JS_EVAL_FLAGS);
js_print_exception(obj);

View file

@ -310,6 +310,11 @@ static sapp_desc start_desc = {
.logger.func = sg_logging,
};
void app_name(char *name)
{
start_desc.window_title = strdup(name);
}
sapp_desc sokol_main(int argc, char **argv) {
#ifndef NDEBUG
#ifdef __linux__
@ -366,6 +371,8 @@ sapp_desc sokol_main(int argc, char **argv) {
sound_init();
input_init();
script_dofile("warmup.js");
start_desc.width = mainwin.width;
start_desc.height = mainwin.height;
start_desc.fullscreen = 0;

View file

@ -12,6 +12,7 @@ void set_timescale(float val);
void print_stacktrace();
const char *engine_info();
void app_name(char *name);
int frame_fps();
double get_timescale();

View file

@ -13,7 +13,7 @@ load("scripts/std.js");
function initialize()
{
if (Cmdline.play)
if (!Game.edit)
run("scripts/play.js");
else
run("scripts/editor.js");
@ -360,6 +360,7 @@ var Window = {
get width() { return cmd(48); },
get height() { return cmd(49); },
get dimensions() { return [this.width, this.height]; },
set name(str) { cmd(134, str); },
};
Window.icon = function(path) { cmd(90, path); };
@ -447,6 +448,7 @@ var Game = {
objects: [],
resolution: [1200,720],
name: "Untitled",
edit: true,
register_obj(obj) {
this.objects[obj.body] = obj;
},
@ -645,7 +647,6 @@ Game.view_camera = function(cam)
Game.view_camera(Primum.spawn(ur.camera2d));
win_make(Game.title, Game.resolution[0], Game.resolution[1]);
Window.name = "Primum Machinam (V0.1)";
Window.width = 1280;
Window.height = 720;

View file

@ -588,8 +588,7 @@ prototypes.from_obj("sprite", {
prototypes.generate_ur = function(path)
{
var ob = IO.glob("*.js");
ob = ob.concat(IO.glob("**/*.js"));
var ob = IO.glob("**.js");
ob = ob.filter(function(str) { return !str.startsWith("scripts"); });
ob.forEach(function(name) {

View file

@ -78,8 +78,22 @@ var IO = {
return cmd(38,file);
},
slurpwrite(str, file) { return cmd(39, str, file); },
extensions(ext) { return cmd(66, "." + ext); },
glob(pat) { return cmd(122, pat); },
extensions(ext) {
var paths = IO.ls();
paths = paths.filter(function(str) { return str.ext() === ext; });
return paths;
},
ls() { return cmd(66); },
glob(pat) {
var paths = IO.ls();
pat = pat.replaceAll(/([\[\]\(\)\^\$\.\|\+])/g, "\\$1");
pat = pat.replaceAll('**', '.*');
pat = pat.replaceAll(/[^\.]\*/g, '[^\\/]*');
pat = pat.replaceAll('?', '.');
var regex = new RegExp("^"+pat+"$", "");
return paths.filter(str => str.match(regex));
},
};
var Cmdline = {};
@ -98,8 +112,6 @@ function cmd_args(cmdargs)
var play = false;
var cmds = cmdargs.split(" ");
Cmdline.play = false;
for (var i = 1; i < cmds.length; i++) {
if (cmds[i][0] !== '-') {
Log.warn(`Command '${cmds[i]}' should start with a '-'.`);
@ -125,7 +137,7 @@ function cmd_args(cmdargs)
}
Cmdline.register_cmd("p", function() { Cmdline.play = true; }, "Launch engine in play mode.");
Cmdline.register_cmd("p", function() { Game.edit = false; }, "Launch engine in play mode.");
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) {