From 0f6132d1375ac5b21267e287e8312addabed06e9 Mon Sep 17 00:00:00 2001 From: "BADIM-PC\\Vadim" Date: Mon, 27 Nov 2017 19:38:10 +0300 Subject: [PATCH] restored 'dofile' function --- src/run.c | 52 +++++++++++++++++++++++++++++++++++++++ src/tic.c | 69 ++++++++++++++++++++++++++++++++-------------------- src/ticapi.h | 2 ++ 3 files changed, 97 insertions(+), 26 deletions(-) diff --git a/src/run.c b/src/run.c index 9e9c8ca..9f5cf18 100644 --- a/src/run.c +++ b/src/run.c @@ -115,6 +115,57 @@ static void tick(Run* run) setStudioMode(TIC_CONSOLE_MODE); } +static void processDoFile(void* data, char* dst) +{ + Run* run = (Run*)data; + tic_mem* tic = run->tic; + + static const char DoFileTag[] = "dofile("; + enum {Size = sizeof DoFileTag - 1}; + + if (memcmp(tic->cart.code.data, DoFileTag, Size) == 0) + { + const char* start = tic->cart.code.data + Size; + const char* end = strchr(start, ')'); + + if(end && *start == *(end-1) && (*start == '"' || *start == '\'')) + { + char filename[FILENAME_MAX] = {0}; + memcpy(filename, start + 1, end - start - 2); + memset(dst, 0, sizeof(tic_code)); + + s32 size = 0; + void* buffer = fsReadFile(filename, &size); + + if(buffer) + { + if(size > 0) + { + if(size > TIC_CODE_SIZE) + { + char buffer[256]; + sprintf(buffer, "code is larger than %i symbols", TIC_CODE_SIZE); + onError(run, buffer); + + return; + } + else SDL_memcpy(dst, buffer, size); + } + } + else + { + char buffer[256]; + sprintf(buffer, "dofile: file '%s' not found", filename); + onError(run, buffer); + + return; + } + } + } + + return; +} + void initRun(Run* run, Console* console, tic_mem* tic) { *run = (Run) @@ -133,6 +184,7 @@ void initRun(Run* run, Console* console, tic_mem* tic) .start = 0, .data = run, .exit = onExit, + .preprocessor = processDoFile, }, }; diff --git a/src/tic.c b/src/tic.c index 2031b3b..b207679 100644 --- a/src/tic.c +++ b/src/tic.c @@ -1377,44 +1377,61 @@ static void api_tick(tic_mem* memory, tic_tick_data* data) if(!machine->state.initialized) { - cart2ram(memory); - const char* code = machine->memory.cart.code.data; + char* code = malloc(sizeof(tic_code)); - if(strlen(code)) + if(code) { - memory->input = compareMetatag(code, "input", "mouse") ? tic_mouse_input : tic_gamepad_input; + memcpy(code, machine->memory.cart.code.data, sizeof(tic_code)); - if(memory->input == tic_mouse_input) - memory->ram.vram.vars.mask.data = 0; + if(data->preprocessor) + data->preprocessor(data->data, code); - memory->script = tic_script_lua; + bool done = false; - if (isMoonscript(code)) + if(strlen(code)) { - if(!initMoonscript(machine, code)) - return; + cart2ram(memory); + memory->input = compareMetatag(code, "input", "mouse") ? tic_mouse_input : tic_gamepad_input; - memory->script = tic_script_moon; + if(memory->input == tic_mouse_input) + memory->ram.vram.vars.mask.data = 0; + + memory->script = tic_script_lua; + + if (isMoonscript(code)) + { + if(initMoonscript(machine, code)) + { + memory->script = tic_script_moon; + done = true; + } + } + else if(isJavascript(code)) + { + if(initJavascript(machine, code)) + { + memory->script = tic_script_js; + done = true; + } + } + else if(initLua(machine, code)) + done = true; } - else if(isJavascript(code)) + else { - if(!initJavascript(machine, code)) - return; - - memory->script = tic_script_js; + machine->data->error(machine->data->data, "the code is empty"); } - else if(!initLua(machine, code)) - return; - } - else - { - machine->data->error(machine->data->data, "the code is empty"); - return; - } - machine->state.scanline = memory->script == tic_script_js ? callJavascriptScanline : callLuaScanline; - machine->state.initialized = true; + free(code); + + if(done) + { + machine->state.scanline = memory->script == tic_script_js ? callJavascriptScanline : callLuaScanline; + machine->state.initialized = true; + } + else return; + } } memory->script == tic_script_js diff --git a/src/ticapi.h b/src/ticapi.h index 2d113d1..674885c 100644 --- a/src/ticapi.h +++ b/src/ticapi.h @@ -56,6 +56,8 @@ typedef struct u64 (*freq)(); u64 start; + void (*preprocessor)(void* data, char* dst); + void* data; } tic_tick_data;