2021-11-30 21:29:18 -06:00
|
|
|
#include "script.h"
|
|
|
|
|
2022-08-01 13:32:58 -05:00
|
|
|
#include "stdio.h"
|
2022-08-03 17:00:00 -05:00
|
|
|
#include "log.h"
|
2022-06-21 12:48:19 -05:00
|
|
|
|
2023-01-10 14:02:24 -06:00
|
|
|
#include "ffi.h"
|
|
|
|
#include "font.h"
|
2022-06-21 12:48:19 -05:00
|
|
|
|
2023-01-05 15:34:15 -06:00
|
|
|
#include "ftw.h"
|
|
|
|
|
2022-12-19 09:12:34 -06:00
|
|
|
#include "stb_ds.h"
|
|
|
|
|
2023-01-13 13:07:44 -06:00
|
|
|
#include "time.h"
|
|
|
|
#include "sys/stat.h"
|
|
|
|
#include "sys/types.h"
|
|
|
|
|
2023-01-10 07:13:00 -06:00
|
|
|
duk_context *duk = NULL;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-01-10 14:02:24 -06:00
|
|
|
duk_idx_t vect2duk(cpVect v) {
|
|
|
|
duk_idx_t arr = duk_push_array(duk);
|
|
|
|
duk_push_number(duk, v.x);
|
|
|
|
duk_put_prop_index(duk, arr, 0);
|
|
|
|
duk_push_number(duk, v.y);
|
|
|
|
duk_put_prop_index(duk, arr, 1);
|
2022-12-20 19:34:22 -06:00
|
|
|
|
2023-01-10 14:02:24 -06:00
|
|
|
return arr;
|
2022-12-28 16:50:54 -06:00
|
|
|
}
|
2022-12-19 09:12:34 -06:00
|
|
|
|
2022-12-15 17:30:22 -06:00
|
|
|
|
2023-01-05 15:34:15 -06:00
|
|
|
static int load_prefab(const char *fpath, const struct stat *sb, int typeflag) {
|
|
|
|
if (typeflag != FTW_F)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!strcmp(".prefab", strrchr(fpath, '.')))
|
2023-01-10 14:02:24 -06:00
|
|
|
script_dofile(fpath);
|
2023-01-05 15:34:15 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:32:58 -05:00
|
|
|
void script_init() {
|
2023-01-10 07:13:00 -06:00
|
|
|
duk = duk_create_heap_default();
|
2022-08-03 17:00:00 -05:00
|
|
|
ffi_load();
|
2023-01-05 15:34:15 -06:00
|
|
|
|
|
|
|
/* Load all prefabs into memory */
|
2023-01-10 14:02:24 -06:00
|
|
|
script_dofile("scripts/engine.js");
|
|
|
|
script_dofile("config.js");
|
|
|
|
//ftw(".", load_prefab, 10);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-08-01 13:32:58 -05:00
|
|
|
void script_run(const char *script) {
|
2023-01-10 14:02:24 -06:00
|
|
|
duk_eval_string(duk, script);
|
2022-02-06 10:14:57 -06:00
|
|
|
}
|
2022-08-01 13:32:58 -05:00
|
|
|
|
2023-01-13 13:07:44 -06:00
|
|
|
time_t file_mod_secs(const char *file) {
|
|
|
|
struct stat attr;
|
|
|
|
stat(file, &attr);
|
|
|
|
return attr.st_mtime;
|
|
|
|
}
|
|
|
|
|
2023-01-13 22:08:39 -06:00
|
|
|
void duk_run_err() {
|
|
|
|
duk_get_prop_string(duk, -1, "lineNumber");
|
|
|
|
duk_get_prop_string(duk, -2, "fileName");
|
|
|
|
duk_get_prop_string(duk, -3, "stack");
|
|
|
|
mYughLog(1, 2, duk_get_int(duk, -3), duk_safe_to_string(duk, -2), "%s\n%s", duk_safe_to_string(duk, -4), duk_safe_to_string(duk, -1));
|
|
|
|
|
|
|
|
duk_pop_3(duk);
|
|
|
|
}
|
|
|
|
|
2022-11-20 15:50:14 -06:00
|
|
|
int script_dofile(const char *file) {
|
2023-01-10 14:02:24 -06:00
|
|
|
const char *script = slurp_text(file);
|
|
|
|
if (!script) {
|
2022-12-15 17:30:22 -06:00
|
|
|
YughError("Can't find file %s.", file);
|
|
|
|
return 1;
|
|
|
|
}
|
2023-01-10 14:02:24 -06:00
|
|
|
duk_push_string(duk, script);
|
|
|
|
free(script);
|
2023-01-13 22:08:39 -06:00
|
|
|
duk_push_string(duk, file);
|
|
|
|
|
|
|
|
if (duk_pcompile(duk, 0) != 0) {
|
|
|
|
duk_run_err();
|
2023-01-10 14:02:24 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-01-13 22:08:39 -06:00
|
|
|
if (duk_pcall(duk, 0))
|
|
|
|
duk_run_err();
|
2023-01-10 14:02:24 -06:00
|
|
|
|
2023-01-13 22:08:39 -06:00
|
|
|
duk_pop(duk);
|
2023-01-13 13:07:44 -06:00
|
|
|
|
|
|
|
return file_mod_secs(file);
|
2022-08-03 17:00:00 -05:00
|
|
|
}
|
|
|
|
|
2022-12-12 10:48:21 -06:00
|
|
|
/* Call the "update" function in the master game script */
|
2022-08-07 01:43:45 -05:00
|
|
|
void script_update(double dt) {
|
2023-01-10 14:02:24 -06:00
|
|
|
|
2022-08-03 17:00:00 -05:00
|
|
|
}
|
|
|
|
|
2022-12-12 10:48:21 -06:00
|
|
|
/* Call the "draw" function in master game script */
|
2022-08-03 17:00:00 -05:00
|
|
|
void script_draw() {
|
2023-01-10 14:02:24 -06:00
|
|
|
|
2022-08-03 20:49:56 -05:00
|
|
|
}
|
|
|
|
|
2022-12-12 10:48:21 -06:00
|
|
|
/* Call "editor" function in master game script */
|
2022-08-03 20:49:56 -05:00
|
|
|
void script_editor() {
|
2023-01-10 14:02:24 -06:00
|
|
|
|
2022-08-05 14:23:39 -05:00
|
|
|
}
|
|
|
|
|
2023-01-10 14:02:24 -06:00
|
|
|
/* Call the given function name */
|
2022-08-05 14:23:39 -05:00
|
|
|
void script_call(const char *f) {
|
2023-01-10 14:02:24 -06:00
|
|
|
//s7_call(s7, s7_name_to_value(s7, f), s7_nil(s7));
|
2022-08-07 01:43:45 -05:00
|
|
|
}
|
|
|
|
|
2023-01-10 14:02:24 -06:00
|
|
|
void script_eval_w_env(const char *s, void *env) {
|
|
|
|
duk_push_heapptr(duk, env);
|
|
|
|
duk_push_string(duk, s);
|
2023-01-09 15:35:05 -06:00
|
|
|
|
2023-01-10 14:02:24 -06:00
|
|
|
if (!duk_has_prop(duk, -2)) {
|
|
|
|
duk_pop(duk);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
duk_push_string(duk, s);
|
2023-01-13 22:08:39 -06:00
|
|
|
if (duk_pcall_prop(duk, -2, 0))
|
|
|
|
duk_run_err();
|
|
|
|
|
|
|
|
duk_pop_2(duk);
|
2022-12-19 18:15:38 -06:00
|
|
|
}
|
|
|
|
|
2023-01-10 14:02:24 -06:00
|
|
|
void script_call_sym(void *sym)
|
2022-08-07 01:43:45 -05:00
|
|
|
{
|
2023-01-10 14:02:24 -06:00
|
|
|
duk_push_heapptr(duk, sym);
|
2023-01-13 22:08:39 -06:00
|
|
|
if (duk_pcall(duk, 0))
|
|
|
|
duk_run_err();
|
|
|
|
|
2023-01-10 14:02:24 -06:00
|
|
|
duk_pop(duk);
|
2022-08-07 01:43:45 -05:00
|
|
|
}
|
|
|
|
|
2023-01-10 14:02:24 -06:00
|
|
|
void script_call_sym_args(void *sym, void *args)
|
2022-12-26 20:57:45 -06:00
|
|
|
{
|
2023-01-10 14:02:24 -06:00
|
|
|
//s7_call(s7, sym, s7_cons(s7, args, s7_nil(s7)));
|
2022-11-25 07:12:31 -06:00
|
|
|
}
|
2022-12-19 09:12:34 -06:00
|
|
|
|
2023-01-09 07:21:45 -06:00
|
|
|
|
2022-12-19 09:12:34 -06:00
|
|
|
|
2023-01-11 16:57:34 -06:00
|
|
|
struct callee *updates = NULL;
|
|
|
|
struct callee *physics = NULL;
|
|
|
|
struct callee *guis = NULL;
|
|
|
|
|
|
|
|
void register_update(struct callee c) {
|
|
|
|
arrput(updates, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void register_gui(struct callee c) {
|
|
|
|
arrput(guis, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void register_physics(struct callee c) {
|
|
|
|
arrput(physics, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void call_callee(struct callee c) {
|
|
|
|
duk_push_heapptr(duk, c.fn);
|
|
|
|
duk_push_heapptr(duk, c.obj);
|
2023-01-13 22:08:39 -06:00
|
|
|
|
|
|
|
if (duk_pcall_method(duk, 0))
|
|
|
|
duk_run_err();
|
|
|
|
|
2023-01-11 16:57:34 -06:00
|
|
|
duk_pop(duk);
|
|
|
|
}
|
|
|
|
|
|
|
|
void callee_dbl(struct callee c, double d) {
|
|
|
|
duk_push_heapptr(duk, c.fn);
|
|
|
|
duk_push_heapptr(duk, c.obj);
|
|
|
|
duk_push_number(duk, d);
|
2023-01-13 22:08:39 -06:00
|
|
|
|
|
|
|
if (duk_pcall_method(duk, 1))
|
|
|
|
duk_run_err();
|
|
|
|
|
2023-01-11 16:57:34 -06:00
|
|
|
duk_pop(duk);
|
2023-01-09 07:21:45 -06:00
|
|
|
}
|
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
void call_updates(double dt) {
|
2023-01-10 14:02:24 -06:00
|
|
|
for (int i = 0; i < arrlen(updates); i++) {
|
2023-01-11 16:57:34 -06:00
|
|
|
callee_dbl(updates[i], dt);
|
2023-01-09 07:21:45 -06:00
|
|
|
}
|
2022-12-20 19:34:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void call_gui() {
|
|
|
|
for (int i = 0; i < arrlen(guis); i++)
|
2023-01-11 16:57:34 -06:00
|
|
|
call_callee(guis[i]);
|
2022-12-20 19:34:22 -06:00
|
|
|
}
|
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
void call_physics(double dt) {
|
2023-01-10 14:02:24 -06:00
|
|
|
for (int i = 0; i < arrlen(physics); i++) {
|
2023-01-11 16:57:34 -06:00
|
|
|
callee_dbl(updates[i], dt);
|
2023-01-10 14:02:24 -06:00
|
|
|
}
|
2023-01-10 15:41:43 -06:00
|
|
|
}
|