From 0e146aee0c003fced9e9c308ed28bb7dcfac3b29 Mon Sep 17 00:00:00 2001 From: "BADIM-PC\\Vadim" Date: Mon, 6 Nov 2017 17:27:42 +0300 Subject: [PATCH] no message --- src/run.c | 51 ++++++++++++++++++++++++++++++- src/tic.c | 84 +++++++--------------------------------------------- src/ticapi.h | 1 + 3 files changed, 61 insertions(+), 75 deletions(-) diff --git a/src/run.c b/src/run.c index fd79ee5..a8147c4 100644 --- a/src/run.c +++ b/src/run.c @@ -79,7 +79,7 @@ static const char* getPMemName(Run* run) { static char buffer[FILENAME_MAX]; - const char* data = strlen(run->tic->saveid) ? run->tic->saveid : run->tic->cart.code.data; + const char* data = strlen(run->tic->saveid) ? run->tic->saveid : run->tic->code.data; char* md5 = data2md5(data, (s32)strlen(data)); strcpy(buffer, TIC_LOCAL); strcat(buffer, md5); @@ -89,6 +89,8 @@ static const char* getPMemName(Run* run) static void tick(Run* run) { + tic_mem* tic = run->tic; + while(pollEvent()); if (getStudioMode() != TIC_RUN_MODE) @@ -96,6 +98,53 @@ static void tick(Run* run) if(!run->init) { + // process 'dofile' + { + memset(tic->code.data, 0, sizeof(tic_code)); + + 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); + + 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(tic->code.data, buffer, size); + } + } + else + { + char buffer[256]; + sprintf(buffer, "dofile: file '%s' not found", filename); + onError(run, buffer); + + return; + } + } + } + } + run->tickData.start = run->tickData.counter(), run->init = true; } diff --git a/src/tic.c b/src/tic.c index 53c1889..7a8131b 100644 --- a/src/tic.c +++ b/src/tic.c @@ -1343,15 +1343,15 @@ static bool isJavascript(const char* code) static tic_script_lang api_get_script(tic_mem* memory) { - if(isMoonscript(memory->cart.code.data)) return tic_script_moon; - if(isJavascript(memory->cart.code.data)) return tic_script_js; + if(isMoonscript(memory->code.data)) return tic_script_moon; + if(isJavascript(memory->code.data)) return tic_script_js; return tic_script_lua; } static void updateSaveid(tic_mem* memory) { memset(memory->saveid, 0, sizeof memory->saveid); - const char* saveid = readMetatag(memory->cart.code.data, "saveid", TagFormatLua); + const char* saveid = readMetatag(memory->code.data, "saveid", TagFormatLua); if(saveid) { strcpy(memory->saveid, saveid); @@ -1359,7 +1359,7 @@ static void updateSaveid(tic_mem* memory) } else { - const char* saveid = readMetatag(memory->cart.code.data, "saveid", TagFormatJS); + const char* saveid = readMetatag(memory->code.data, "saveid", TagFormatJS); if(saveid) { strcpy(memory->saveid, saveid); @@ -1378,77 +1378,18 @@ static void api_tick(tic_mem* memory, tic_tick_data* data) { cart2ram(memory); - char* code = machine->memory.cart.code.data; - if(code && strlen(code)) + const char* code = machine->memory.code.data; + + if(!strlen(code)) + memcpy(memory->code.data, memory->cart.code.data, sizeof(tic_code)); + + if(strlen(code)) { - static const char DoFileTag[] = "dofile("; - enum {Size = sizeof DoFileTag - 1}; - - if (memcmp(code, DoFileTag, Size) == 0) - { - const char* start = code + 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); - - FILE* file = fopen(filename, "rb"); - - if(file) - { - fseek(file, 0, SEEK_END); - s32 size = ftell(file); - fseek(file, 0, SEEK_SET); - - if(size > 0) - { - if(size > TIC_CODE_SIZE) - { - char buffer[256]; - sprintf(buffer, "code is larger than %i symbols", TIC_CODE_SIZE); - machine->data->error(machine->data->data, buffer); - - fclose(file); - - return; - } - else - { - void* buffer = malloc(size+1); - - if(buffer) - { - memset(buffer, 0, size+1); - - if(fread(buffer, size, 1, file)) {} - - code = buffer; - } - } - } - - fclose(file); - } - else - { - char buffer[256]; - sprintf(buffer, "dofile: file '%s' not found", filename); - machine->data->error(machine->data->data, buffer); - return; - } - } - } - memory->input = compareMetatag(code, "input", "mouse") ? tic_mouse_input : tic_gamepad_input; - if(memory->input == tic_mouse_input) memory->ram.vram.vars.mask.data = 0; - ////////////////////////// - memory->script = tic_script_lua; if (isMoonscript(code)) @@ -1469,12 +1410,7 @@ static void api_tick(tic_mem* memory, tic_tick_data* data) return; } - // TODO: possible memory leak if script not initialozed - if(code != machine->memory.cart.code.data) - free(code); - machine->state.scanline = memory->script == tic_script_js ? callJavascriptScanline : callLuaScanline; - machine->state.initialized = true; } diff --git a/src/ticapi.h b/src/ticapi.h index 2d113d1..7bf7aa9 100644 --- a/src/ticapi.h +++ b/src/ticapi.h @@ -119,6 +119,7 @@ struct tic_mem tic_script_lang script; tic_font font; tic_api api; + tic_code code; char saveid[TIC_SAVEID_SIZE];