restored 'dofile' function

This commit is contained in:
BADIM-PC\Vadim 2017-11-27 19:38:10 +03:00
parent 430ca6fb13
commit 0f6132d137
3 changed files with 97 additions and 26 deletions

View File

@ -115,6 +115,57 @@ static void tick(Run* run)
setStudioMode(TIC_CONSOLE_MODE); 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) void initRun(Run* run, Console* console, tic_mem* tic)
{ {
*run = (Run) *run = (Run)
@ -133,6 +184,7 @@ void initRun(Run* run, Console* console, tic_mem* tic)
.start = 0, .start = 0,
.data = run, .data = run,
.exit = onExit, .exit = onExit,
.preprocessor = processDoFile,
}, },
}; };

View File

@ -1377,44 +1377,61 @@ static void api_tick(tic_mem* memory, tic_tick_data* data)
if(!machine->state.initialized) 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) if(data->preprocessor)
memory->ram.vram.vars.mask.data = 0; data->preprocessor(data->data, code);
memory->script = tic_script_lua; bool done = false;
if (isMoonscript(code)) if(strlen(code))
{ {
if(!initMoonscript(machine, code)) cart2ram(memory);
return; 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)) machine->data->error(machine->data->data, "the code is empty");
return;
memory->script = tic_script_js;
} }
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; free(code);
machine->state.initialized = true;
if(done)
{
machine->state.scanline = memory->script == tic_script_js ? callJavascriptScanline : callLuaScanline;
machine->state.initialized = true;
}
else return;
}
} }
memory->script == tic_script_js memory->script == tic_script_js

View File

@ -56,6 +56,8 @@ typedef struct
u64 (*freq)(); u64 (*freq)();
u64 start; u64 start;
void (*preprocessor)(void* data, char* dst);
void* data; void* data;
} tic_tick_data; } tic_tick_data;