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
|
|
|
|
2022-08-03 17:00:00 -05:00
|
|
|
#include "mrbffi.h"
|
2022-06-21 12:48:19 -05:00
|
|
|
|
2022-12-19 09:12:34 -06:00
|
|
|
#include "stb_ds.h"
|
|
|
|
|
2022-12-12 10:48:21 -06:00
|
|
|
s7_scheme *s7 = NULL;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-12-19 09:12:34 -06:00
|
|
|
s7_pointer *updates;
|
|
|
|
|
2022-12-15 17:30:22 -06:00
|
|
|
static void my_print(s7_scheme *sc, uint8_t c, s7_pointer port) {
|
|
|
|
static char buffer[1024];
|
|
|
|
static char *p = buffer;
|
|
|
|
if (c != '\0' && p != &buffer[1023]) {
|
|
|
|
*p = c;
|
|
|
|
p++;
|
|
|
|
} else {
|
|
|
|
YughInfo(buffer);
|
|
|
|
p = buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:32:58 -05:00
|
|
|
void script_init() {
|
2022-12-12 10:48:21 -06:00
|
|
|
s7 = s7_init();
|
2022-12-15 17:30:22 -06:00
|
|
|
s7_set_current_error_port(s7, s7_open_output_string(s7));
|
|
|
|
s7_set_current_output_port(s7, s7_open_output_function(s7, my_print));
|
2022-08-03 17:00:00 -05:00
|
|
|
ffi_load();
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-08-01 13:32:58 -05:00
|
|
|
void script_run(const char *script) {
|
2022-12-19 12:16:51 -06:00
|
|
|
s7_pointer old_port = s7_set_current_error_port(s7, s7_open_output_string(s7));
|
|
|
|
int gc_loc = -1;
|
|
|
|
if (old_port != s7_nil(s7)) gc_loc = s7_gc_protect(s7, old_port);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-12-12 10:48:21 -06:00
|
|
|
s7_eval_c_string(s7, script);
|
2022-12-19 09:12:34 -06:00
|
|
|
|
|
|
|
const char *errmsg = s7_get_output_string(s7, s7_current_error_port(s7));
|
|
|
|
|
|
|
|
if (errmsg && (*errmsg))
|
|
|
|
mYughLog(1, 2, 0, "script", "Scripting error: %s", errmsg);
|
|
|
|
|
2022-12-19 12:16:51 -06:00
|
|
|
s7_close_output_port(s7, s7_current_error_port(s7));
|
|
|
|
s7_set_current_error_port(s7, old_port);
|
|
|
|
if (gc_loc != -1) s7_gc_unprotect_at(s7, gc_loc);
|
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
}
|
2022-08-01 13:32:58 -05:00
|
|
|
|
2022-11-20 15:50:14 -06:00
|
|
|
int script_dofile(const char *file) {
|
2022-12-15 17:30:22 -06:00
|
|
|
/* static char fload[512];
|
|
|
|
snprintf(fload, 512, "(write (load \"%s\"))", file);
|
|
|
|
s7_eval_c_string(s7, fload);
|
|
|
|
*/
|
|
|
|
if (!s7_load(s7, file)) {
|
|
|
|
YughError("Can't find file %s.", file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *errmsg = s7_get_output_string(s7, s7_current_error_port(s7));
|
|
|
|
|
|
|
|
if (errmsg && (*errmsg))
|
|
|
|
mYughLog(1, 2, 0, "script", "Scripting error: %s", errmsg);
|
|
|
|
|
|
|
|
|
2022-11-20 15:50:14 -06:00
|
|
|
return 0;
|
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) {
|
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() {
|
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() {
|
2022-08-05 14:23:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void script_call(const char *f) {
|
2022-12-12 10:48:21 -06:00
|
|
|
s7_call(s7, s7_name_to_value(s7, f), s7_nil(s7));
|
2022-08-07 01:43:45 -05:00
|
|
|
}
|
|
|
|
|
2022-12-12 10:48:21 -06:00
|
|
|
void script_call_sym(s7_pointer sym)
|
2022-08-07 01:43:45 -05:00
|
|
|
{
|
2022-12-12 10:48:21 -06:00
|
|
|
s7_call(s7, sym, s7_nil(s7));
|
2022-08-07 01:43:45 -05:00
|
|
|
}
|
|
|
|
|
2022-12-12 10:48:21 -06:00
|
|
|
int script_has_sym(s7_pointer sym) {
|
|
|
|
return 1;
|
2022-11-25 07:12:31 -06:00
|
|
|
}
|
2022-12-19 09:12:34 -06:00
|
|
|
|
|
|
|
void register_update(s7_pointer sym) {
|
|
|
|
arrput(updates, sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
void call_updates() {
|
|
|
|
for (int i = 0; i < arrlen(updates); i++)
|
|
|
|
script_call_sym(updates[i]);
|
|
|
|
}
|