no message

This commit is contained in:
BADIM-PC\Vadim 2017-11-06 17:27:42 +03:00
parent a4826af052
commit 0e146aee0c
3 changed files with 61 additions and 75 deletions

View File

@ -79,7 +79,7 @@ static const char* getPMemName(Run* run)
{ {
static char buffer[FILENAME_MAX]; 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)); char* md5 = data2md5(data, (s32)strlen(data));
strcpy(buffer, TIC_LOCAL); strcpy(buffer, TIC_LOCAL);
strcat(buffer, md5); strcat(buffer, md5);
@ -89,6 +89,8 @@ static const char* getPMemName(Run* run)
static void tick(Run* run) static void tick(Run* run)
{ {
tic_mem* tic = run->tic;
while(pollEvent()); while(pollEvent());
if (getStudioMode() != TIC_RUN_MODE) if (getStudioMode() != TIC_RUN_MODE)
@ -96,6 +98,53 @@ static void tick(Run* run)
if(!run->init) 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->tickData.start = run->tickData.counter(),
run->init = true; run->init = true;
} }

View File

@ -1343,15 +1343,15 @@ static bool isJavascript(const char* code)
static tic_script_lang api_get_script(tic_mem* memory) static tic_script_lang api_get_script(tic_mem* memory)
{ {
if(isMoonscript(memory->cart.code.data)) return tic_script_moon; if(isMoonscript(memory->code.data)) return tic_script_moon;
if(isJavascript(memory->cart.code.data)) return tic_script_js; if(isJavascript(memory->code.data)) return tic_script_js;
return tic_script_lua; return tic_script_lua;
} }
static void updateSaveid(tic_mem* memory) static void updateSaveid(tic_mem* memory)
{ {
memset(memory->saveid, 0, sizeof memory->saveid); 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) if(saveid)
{ {
strcpy(memory->saveid, saveid); strcpy(memory->saveid, saveid);
@ -1359,7 +1359,7 @@ static void updateSaveid(tic_mem* memory)
} }
else else
{ {
const char* saveid = readMetatag(memory->cart.code.data, "saveid", TagFormatJS); const char* saveid = readMetatag(memory->code.data, "saveid", TagFormatJS);
if(saveid) if(saveid)
{ {
strcpy(memory->saveid, saveid); strcpy(memory->saveid, saveid);
@ -1378,77 +1378,18 @@ static void api_tick(tic_mem* memory, tic_tick_data* data)
{ {
cart2ram(memory); cart2ram(memory);
char* code = machine->memory.cart.code.data; const char* code = machine->memory.code.data;
if(code && strlen(code))
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; memory->input = compareMetatag(code, "input", "mouse") ? tic_mouse_input : tic_gamepad_input;
if(memory->input == tic_mouse_input) if(memory->input == tic_mouse_input)
memory->ram.vram.vars.mask.data = 0; memory->ram.vram.vars.mask.data = 0;
//////////////////////////
memory->script = tic_script_lua; memory->script = tic_script_lua;
if (isMoonscript(code)) if (isMoonscript(code))
@ -1469,12 +1410,7 @@ static void api_tick(tic_mem* memory, tic_tick_data* data)
return; 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.scanline = memory->script == tic_script_js ? callJavascriptScanline : callLuaScanline;
machine->state.initialized = true; machine->state.initialized = true;
} }

View File

@ -119,6 +119,7 @@ struct tic_mem
tic_script_lang script; tic_script_lang script;
tic_font font; tic_font font;
tic_api api; tic_api api;
tic_code code;
char saveid[TIC_SAVEID_SIZE]; char saveid[TIC_SAVEID_SIZE];