Merge branch '#383'

This commit is contained in:
BADIM-PC\Vadim 2017-11-08 15:32:05 +03:00
commit 7d52c1da99
6 changed files with 170 additions and 144 deletions

View File

@ -905,13 +905,6 @@ static void onConsoleConfigCommand(Console* console, const char* param)
commandDone(console);
}
static s32 saveRom(tic_mem* tic, void* buffer)
{
s32 size = tic->api.save(&tic->cart, buffer);
return size;
}
static void onFileDownloaded(GetResult result, void* data)
{
Console* console = (Console*)data;
@ -1245,6 +1238,8 @@ static void writeMemoryString(MemoryBuffer* memory, const char* str)
static void onConsoleExportHtmlCommand(Console* console, const char* name)
{
tic_mem* tic = console->tic;
char cartName[FILENAME_MAX];
strcpy(cartName, name);
@ -1286,43 +1281,62 @@ static void onConsoleExportHtmlCommand(Console* console, const char* name)
{
writeMemoryString(&output, "var cartridge = [");
s32 size = saveRom(console->tic, buffer);
s32 size = 0;
// zip buffer
// create cart copy
{
unsigned long outSize = sizeof(tic_cartridge);
u8* output = (u8*)SDL_malloc(outSize);
tic_cartridge* dup = SDL_malloc(sizeof(tic_cartridge));
SDL_memcpy(dup, &tic->cart, sizeof(tic_cartridge));
if(processDoFile())
{
SDL_memcpy(dup->code.data, tic->code.data, sizeof(tic_code));
size = tic->api.save(dup, buffer);
}
SDL_free(dup);
}
if(size)
{
// zip buffer
{
unsigned long outSize = sizeof(tic_cartridge);
u8* output = (u8*)SDL_malloc(outSize);
compress2(output, &outSize, buffer, size, Z_BEST_COMPRESSION);
SDL_free(buffer);
buffer = output;
size = outSize;
}
{
u8* ptr = buffer;
u8* end = ptr + size;
char value[] = "999,";
while(ptr != end)
{
sprintf(value, "%i,", *ptr++);
writeMemoryString(&output, value);
}
}
compress2(output, &outSize, buffer, size, Z_BEST_COMPRESSION);
SDL_free(buffer);
buffer = output;
size = outSize;
writeMemoryString(&output, "];\n");
writeMemoryData(&output, EmbedTicJs, EmbedTicJsSize);
writeMemoryString(&output, "</script>\n");
ptr += sizeof(Placeholder)-1;
writeMemoryData(&output, ptr, EmbedIndexSize - (ptr - EmbedIndex));
fsGetFileData(onFileDownloaded, cartName, output.data, output.size, DEFAULT_CHMOD, console);
}
{
u8* ptr = buffer;
u8* end = ptr + size;
char value[] = "999,";
while(ptr != end)
{
sprintf(value, "%i,", *ptr++);
writeMemoryString(&output, value);
}
}
SDL_free(buffer);
writeMemoryString(&output, "];\n");
writeMemoryData(&output, EmbedTicJs, EmbedTicJsSize);
writeMemoryString(&output, "</script>\n");
ptr += sizeof(Placeholder)-1;
writeMemoryData(&output, ptr, EmbedIndexSize - (ptr - EmbedIndex));
fsGetFileData(onFileDownloaded, cartName, output.data, output.size, DEFAULT_CHMOD, console);
}
}
}
@ -1335,22 +1349,30 @@ static void onConsoleExportHtmlCommand(Console* console, const char* name)
static void* embedCart(Console* console, s32* size)
{
void* data = fsReadFile(console->appPath, size);
tic_mem* tic = console->tic;
if(data)
if(processDoFile())
{
void* start = memmem(data, *size, embed.prefix, sizeof(embed.prefix));
void* data = fsReadFile(console->appPath, size);
if(start)
if(data)
{
embed.yes = true;
memcpy(&embed.file, &console->tic->cart, sizeof(tic_cartridge));
memcpy(start, &embed, sizeof(embed));
embed.yes = false;
void* start = memmem(data, *size, embed.prefix, sizeof(embed.prefix));
if(start)
{
embed.yes = true;
SDL_memcpy(&embed.file, &tic->cart, sizeof(tic_cartridge));
SDL_memcpy(embed.file.code.data, tic->code.data, sizeof(tic_code));
SDL_memcpy(start, &embed, sizeof(embed));
embed.yes = false;
}
}
return data;
}
return data;
return NULL;
}
#if defined(__WINDOWS__)
@ -1526,6 +1548,8 @@ static void onConsoleExportCommand(Console* console, const char* param)
static CartSaveResult saveCartName(Console* console, const char* name)
{
tic_mem* tic = console->tic;
bool success = false;
if(name && strlen(name))
@ -1543,7 +1567,7 @@ static CartSaveResult saveCartName(Console* console, const char* name)
}
else
{
s32 size = saveRom(console->tic, buffer);
s32 size = tic->api.save(&tic->cart, buffer);
name = getRomName(name);

View File

@ -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);
@ -96,6 +96,9 @@ static void tick(Run* run)
if(!run->init)
{
if(!processDoFile())
return;
run->tickData.start = run->tickData.counter(),
run->init = true;
}

View File

@ -1357,6 +1357,60 @@ static void onFullscreen()
SDL_SetWindowFullscreen(studio.window, studio.fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
}
bool processDoFile()
{
tic_mem* tic = studio.tic;
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);
setStudioMode(TIC_CONSOLE_MODE);
studio.console.error(&studio.console, buffer);
return false;
}
else SDL_memcpy(tic->code.data, buffer, size);
}
}
else
{
char buffer[256];
sprintf(buffer, "dofile: file '%s' not found", filename);
setStudioMode(TIC_CONSOLE_MODE);
studio.console.error(&studio.console, buffer);
return false;
}
}
}
else SDL_memcpy(tic->code.data, tic->cart.code.data, sizeof(tic_code));
return true;
}
void runProject()
{
studio.tic->api.reset(studio.tic);

View File

@ -193,3 +193,4 @@ void gotoCode();
void gotoSurf();
void exitFromGameMenu();
void runProject();
bool processDoFile();

133
src/tic.c
View File

@ -431,6 +431,8 @@ static void api_reset(tic_mem* memory)
machine->state.scanline = NULL;
updateSaveid(memory);
memset(memory->code.data, 0, sizeof(tic_code));
}
static void api_pause(tic_mem* memory)
@ -1343,15 +1345,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 +1361,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 +1380,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))
@ -1468,13 +1411,13 @@ static void api_tick(tic_mem* memory, tic_tick_data* data)
else if(!initLua(machine, code))
return;
}
// TODO: possible memory leak if script not initialozed
if(code != machine->memory.cart.code.data)
free(code);
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;
}
@ -1644,28 +1587,28 @@ static s32 api_save(const tic_cartridge* cart, u8* buffer)
inline void memset4(void *dst, u32 val, u32 dwords)
{
#if defined(__GNUC__) && defined(i386)
s32 u0, u1, u2;
__asm__ __volatile__ (
"cld \n\t"
"rep ; stosl \n\t"
: "=&D" (u0), "=&a" (u1), "=&c" (u2)
: "0" (dst), "1" (val), "2" (dwords)
: "memory"
);
s32 u0, u1, u2;
__asm__ __volatile__ (
"cld \n\t"
"rep ; stosl \n\t"
: "=&D" (u0), "=&a" (u1), "=&c" (u2)
: "0" (dst), "1" (val), "2" (dwords)
: "memory"
);
#else
u32 _n = (dwords + 3) / 4;
u32 *_p = (u32*)dst;
u32 _val = (val);
if (dwords == 0)
return;
switch (dwords % 4)
{
case 0: do { *_p++ = _val;
case 3: *_p++ = _val;
case 2: *_p++ = _val;
case 1: *_p++ = _val;
} while ( --_n );
}
u32 _n = (dwords + 3) / 4;
u32 *_p = (u32*)dst;
u32 _val = (val);
if (dwords == 0)
return;
switch (dwords % 4)
{
case 0: do { *_p++ = _val;
case 3: *_p++ = _val;
case 2: *_p++ = _val;
case 1: *_p++ = _val;
} while ( --_n );
}
#endif
}

View File

@ -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];