From bbfa4dfb5167e9fac32a6bc917dd76fa24505963 Mon Sep 17 00:00:00 2001 From: "BADIM-PC\\Vadim" Date: Sat, 11 Nov 2017 17:43:29 +0300 Subject: [PATCH 1/6] no message --- src/code.c | 8 ++++---- src/console.c | 6 ++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/code.c b/src/code.c index aeb31b8..2839cb4 100644 --- a/src/code.c +++ b/src/code.c @@ -143,9 +143,9 @@ static s32 getLinesCount(Code* code) static void removeInvalidChars(char* code) { - // remove \r symbol - char* s; char* d; - for(s = d = code; (*d = *s); d += (*s++ != '\r')); + // remove \r symbol + char* s; char* d; + for(s = d = code; (*d = *s); d += (*s++ != '\r')); } static void updateEditor(Code* code) @@ -647,7 +647,7 @@ static void pageDown(Code* code) s32 line = 0; getCursorPosition(code, &column, &line); s32 lines = getLinesCount(code); - setCursorPosition(code, column, line < lines - TEXT_BUFFER_HEIGHT ? line + TEXT_BUFFER_HEIGHT : lines); + setCursorPosition(code, column, line < lines - TEXT_BUFFER_HEIGHT ? line + TEXT_BUFFER_HEIGHT : lines); } static bool replaceSelection(Code* code) diff --git a/src/console.c b/src/console.c index fe42a1c..eb7b18f 100644 --- a/src/console.c +++ b/src/console.c @@ -1815,6 +1815,12 @@ static bool loadProject(Console* console, const char* data, s32 size) SDL_memcpy(project, data, size); project[size] = '\0'; + // remove all the '\r' chars + { + char *s, *d; + for(s = d = project; (*d = *s); d += (*s++ != '\r')); + } + tic_cartridge* cart = (tic_cartridge*)SDL_malloc(sizeof(tic_cartridge)); if(cart) From 44f9a0aca194bd6f0beea98e678993771f4b4bb1 Mon Sep 17 00:00:00 2001 From: "BADIM-PC\\Vadim" Date: Wed, 15 Nov 2017 15:46:01 +0300 Subject: [PATCH 2/6] no message --- src/tic.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tic.c b/src/tic.c index 99c1a11..533a9ba 100644 --- a/src/tic.c +++ b/src/tic.c @@ -153,6 +153,7 @@ static void setPixel(tic_machine* machine, s32 x, s32 y, u8 color) { if(x < machine->state.clip.l || y < machine->state.clip.t || x >= machine->state.clip.r || y >= machine->state.clip.b) return; + // TODO: check color bounds here tic_tool_poke4(machine->memory.ram.vram.screen.data, y * TIC80_WIDTH + x, tic_tool_peek4(machine->memory.ram.vram.mapping, color)); } From 46e62e4eb275c6abc9640aa44717e470d9d50380 Mon Sep 17 00:00:00 2001 From: "BADIM-PC\\Vadim" Date: Sun, 19 Nov 2017 16:58:54 +0300 Subject: [PATCH 3/6] .ticp project loads from command line --- src/console.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/console.c b/src/console.c index eb7b18f..f9b1a94 100644 --- a/src/console.c +++ b/src/console.c @@ -1564,9 +1564,11 @@ static void onConsoleExportCommand(Console* console, const char* param) #if defined(TIC80_PRO) +static const char ProjectExt[] = ".ticp"; + static const char* getProjectName(const char* name) { - return getName(name, ".ticp"); + return getName(name, ProjectExt); } static void buf2str(const void* data, s32 size, char* ptr, bool flip) @@ -1802,7 +1804,7 @@ static void loadBinarySection(const char* project, const char* tag, s32 count, v } } -static bool loadProject(Console* console, const char* data, s32 size) +static bool loadProject(Console* console, const char* data, s32 size, tic_cartridge* dst) { tic_mem* tic = console->tic; @@ -1838,7 +1840,7 @@ static bool loadProject(Console* console, const char* data, s32 size) loadBinarySection(project, "COVER", 1, &cart->cover, -1, true); - SDL_memcpy(&tic->cart, cart, sizeof(tic_cartridge)); + SDL_memcpy(dst, cart, sizeof(tic_cartridge)); SDL_free(cart); @@ -1853,6 +1855,8 @@ static bool loadProject(Console* console, const char* data, s32 size) static void onConsoleLoadProjectCommandConfirmed(Console* console, const char* param) { + tic_mem* tic = console->tic; + if(param) { s32 size = 0; @@ -1860,7 +1864,7 @@ static void onConsoleLoadProjectCommandConfirmed(Console* console, const char* p void* data = fsLoadFile(console->fs, name, &size); - if(data && loadProject(console, data, size)) + if(data && loadProject(console, data, size, &tic->cart)) { strcpy(console->romName, name); @@ -2666,7 +2670,17 @@ static void cmdLoadCart(Console* console, const char* name) if(data) { - loadCart(console->tic, &embed.file, data, size, true); +#if defined(TIC80_PRO) + if(strstr(name, ProjectExt) == name + strlen(name) - strlen(ProjectExt)) + { + loadProject(console, data, size, &embed.file); + } + else +#endif + { + loadCart(console->tic, &embed.file, data, size, true); + } + embed.yes = true; SDL_free(data); From 6970c9097922464d53b5cfb6ac919f0ce46cc68c Mon Sep 17 00:00:00 2001 From: "BADIM-PC\\Vadim" Date: Sun, 19 Nov 2017 17:44:01 +0300 Subject: [PATCH 4/6] project loads in surf --- src/console.c | 21 +++++++++++-------- src/console.h | 1 + src/studio.h | 3 +++ src/surf.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 68 insertions(+), 14 deletions(-) diff --git a/src/console.c b/src/console.c index f9b1a94..a53994d 100644 --- a/src/console.c +++ b/src/console.c @@ -62,8 +62,6 @@ static struct .fast = false, }; -static const char CartExt[] = ".tic"; - static const char DefaultLuaTicPath[] = TIC_LOCAL "default.tic"; static const char DefaultMoonTicPath[] = TIC_LOCAL "default_moon.tic"; static const char DefaultJSTicPath[] = TIC_LOCAL "default_js.tic"; @@ -85,7 +83,7 @@ static const char* getName(const char* name, const char* ext) static const char* getCartName(const char* name) { - return getName(name, CartExt); + return getName(name, CART_EXT); } static void scrollBuffer(char* buffer) @@ -342,12 +340,12 @@ static bool onConsoleLoadSectionCommand(Console* console, const char* param) for(s32 i = 0; i < COUNT_OF(Sections); i++) { - sprintf(buf, "%s %s", CartExt, Sections[i]); + sprintf(buf, "%s %s", CART_EXT, Sections[i]); char* pos = SDL_strstr(param, buf); if(pos) { - pos[sizeof(CartExt) - 1] = 0; + pos[sizeof(CART_EXT) - 1] = 0; const char* name = getCartName(param); s32 size = 0; void* data = fsLoadFile(console->fs, name, &size); @@ -1564,11 +1562,9 @@ static void onConsoleExportCommand(Console* console, const char* param) #if defined(TIC80_PRO) -static const char ProjectExt[] = ".ticp"; - static const char* getProjectName(const char* name) { - return getName(name, ProjectExt); + return getName(name, PROJECT_EXT); } static void buf2str(const void* data, s32 size, char* ptr, bool flip) @@ -2671,7 +2667,7 @@ static void cmdLoadCart(Console* console, const char* name) if(data) { #if defined(TIC80_PRO) - if(strstr(name, ProjectExt) == name + strlen(name) - strlen(ProjectExt)) + if(strstr(name, PROJECT_EXT) == name + strlen(name) - strlen(PROJECT_EXT)) { loadProject(console, data, size, &embed.file); } @@ -2820,6 +2816,13 @@ void initConsole(Console* console, tic_mem* tic, FileSystem* fs, Config* config, .tic = tic, .config = config, .load = onConsoleLoadCommandConfirmed, + +#if defined(TIC80_PRO) + .loadProject = loadProject, +#else + .loadProject = NULL, +#endif + .error = error, .trace = trace, .tick = tick, diff --git a/src/console.h b/src/console.h index ba3ff3e..4e33096 100644 --- a/src/console.h +++ b/src/console.h @@ -90,6 +90,7 @@ struct Console bool showGameMenu; void(*load)(Console*, const char* name); + bool(*loadProject)(Console*, const char* data, s32 size, tic_cartridge* dst); void(*error)(Console*, const char*); void(*trace)(Console*, const char*, u8 color); void(*tick)(Console*); diff --git a/src/studio.h b/src/studio.h index 2163eec..b3e0bf4 100644 --- a/src/studio.h +++ b/src/studio.h @@ -60,6 +60,9 @@ #define KEYMAP_DAT "keymap.dat" #define KEYMAP_DAT_PATH TIC_LOCAL KEYMAP_DAT +#define CART_EXT ".tic" +#define PROJECT_EXT ".ticp" + typedef struct { struct diff --git a/src/surf.c b/src/surf.c index 391cade..e9e738f 100644 --- a/src/surf.c +++ b/src/surf.c @@ -155,6 +155,7 @@ struct MenuItem s32 id; tic_screen* cover; bool dir; + bool project; }; typedef struct @@ -372,13 +373,29 @@ static void replace(char* src, const char* what, const char* with) } } +static bool hasExt(const char* name, const char* ext) +{ + return strstr(name, ext) == name + strlen(name) - strlen(ext); +} + +static void cutExt(char* name, const char* ext) +{ + name[strlen(name)-strlen(ext)] = '\0'; +} + static bool addMenuItem(const char* name, const char* info, s32 id, void* ptr, bool dir) { AddMenuItem* data = (AddMenuItem*)ptr; - static const char CartExt[] = ".tic"; + static const char CartExt[] = CART_EXT; + static const char ProjectExt[] = PROJECT_EXT; - if(dir || (strstr(name, CartExt) == name + strlen(name) - sizeof(CartExt)+1)) + if(dir + || hasExt(name, CartExt) +#if defined(TIC80_PRO) + || hasExt(name, ProjectExt) +#endif + ) { MenuItem* item = &data->items[data->count++]; @@ -394,7 +411,15 @@ static bool addMenuItem(const char* name, const char* info, s32 id, void* ptr, b { item->label = SDL_strdup(name); - item->label[strlen(item->label)-sizeof(CartExt)+1] = '\0'; + + if(hasExt(name, CartExt)) + cutExt(item->label, CartExt); + else + { + cutExt(item->label, ProjectExt); + item->project = true; + } + replace(item->label, "&", "&"); replace(item->label, "'", "'"); @@ -509,7 +534,10 @@ static void loadCover(Surf* surf) if(cart) { - tic->api.load(cart, data, size, true); + if(hasExt(item->name, PROJECT_EXT)) + surf->console->loadProject(surf->console, data, size, cart); + else + tic->api.load(cart, data, size, true); if(cart->cover.size) updateMenuItemCover(surf, cart->cover.data, cart->cover.size); @@ -617,7 +645,26 @@ static void onPlayCart(Surf* surf) { MenuItem* item = &surf->menu.items[surf->menu.pos]; - surf->console->load(surf->console, item->name); + if(item->project) + { + tic_cartridge* cart = SDL_malloc(sizeof(tic_cartridge)); + + if(cart) + { + s32 size = 0; + void* data = fsLoadFile(surf->fs, item->name, &size); + + surf->console->loadProject(surf->console, data, size, cart); + + SDL_memcpy(&surf->tic->cart, cart, sizeof(tic_cartridge)); + + studioRomLoaded(); + + SDL_free(cart); + } + } + else + surf->console->load(surf->console, item->name); runGameFromSurf(); } From 4e3d148c06fe1b5f71ae627b39ae51eaad86c929 Mon Sep 17 00:00:00 2001 From: "BADIM-PC\\Vadim" Date: Sun, 19 Nov 2017 18:41:39 +0300 Subject: [PATCH 5/6] removed loadp/savep --- src/console.c | 619 +++++++++++++++++++++----------------------------- 1 file changed, 263 insertions(+), 356 deletions(-) diff --git a/src/console.c b/src/console.c index a53994d..0bdcbd2 100644 --- a/src/console.c +++ b/src/console.c @@ -507,6 +507,237 @@ static void onCartLoaded(Console* console, const char* name) } +#if defined(TIC80_PRO) + +static const char* getProjectName(const char* name) +{ + return getName(name, PROJECT_EXT); +} + +static void buf2str(const void* data, s32 size, char* ptr, bool flip) +{ + enum {Len = 2}; + + for(s32 i = 0; i < size; i++, ptr+=Len) + { + sprintf(ptr, "%02x", ((u8*)data)[i]); + + if(flip) + { + char tmp = ptr[0]; + ptr[0] = ptr[1]; + ptr[1] = tmp; + } + } +} + +static bool bufferEmpty(const u8* data, s32 size) +{ + for(s32 i = 0; i < size; i++) + if(*data++) + return false; + + return true; +} + +static char* saveTextSection(char* ptr, const char* tag, const char* data) +{ + if(strlen(data) == 0) + return ptr; + + sprintf(ptr, "-- <%s>\n%s\n-- \n", tag, data, tag); + ptr += strlen(ptr); + + return ptr; +} + +static char* saveBinaryBuffer(char* ptr, const void* data, s32 size, s32 row, bool flip) +{ + if(bufferEmpty(data, size)) + return ptr; + + sprintf(ptr, "-- %03i:", row); + ptr += strlen(ptr); + + buf2str(data, size, ptr, flip); + ptr += strlen(ptr); + + sprintf(ptr, "\n"); + ptr += strlen(ptr); + + return ptr; +} + +static char* saveBinarySection(char* ptr, const char* tag, s32 count, const void* data, s32 size, bool flip) +{ + if(bufferEmpty(data, size * count)) + return ptr; + + sprintf(ptr, "\n-- <%s>\n", tag); + ptr += strlen(ptr); + + for(s32 i = 0; i < count; i++, data = (u8*)data + size) + ptr = saveBinaryBuffer(ptr, data, size, i, flip); + + sprintf(ptr, "-- \n", tag); + ptr += strlen(ptr); + + return ptr; +} + +typedef struct {char* tag; s32 count; s32 offset; s32 size; bool flip;} BinarySection; +static const BinarySection BinarySections[] = +{ + {"PALETTE", 1, offsetof(tic_cartridge, palette.data), sizeof(tic_palette), false}, + {"TILES", TIC_BANK_SPRITES, offsetof(tic_cartridge, gfx.tiles), sizeof(tic_tile), true}, + {"SPRITES", TIC_BANK_SPRITES, offsetof(tic_cartridge, gfx.sprites), sizeof(tic_tile), true}, + {"MAP", TIC_MAP_HEIGHT, offsetof(tic_cartridge, gfx.map), TIC_MAP_WIDTH, true}, + {"WAVES", ENVELOPES_COUNT, offsetof(tic_cartridge,sound.sfx.waveform.envelopes), sizeof(tic_waveform), true}, + {"SFX", SFX_COUNT, offsetof(tic_cartridge, sound.sfx.data), sizeof(tic_sound_effect), true}, + {"PATTERNS", MUSIC_PATTERNS, offsetof(tic_cartridge, sound.music.patterns), sizeof(tic_track_pattern), true}, + {"TRACKS", MUSIC_TRACKS, offsetof(tic_cartridge, sound.music.tracks), sizeof(tic_track), true}, +}; + +static s32 saveProject(Console* console, void* buffer) +{ + tic_mem* tic = console->tic; + + char* stream = buffer; + char* ptr = saveTextSection(stream, "CODE", tic->cart.code.data); + + for(s32 i = 0; i < COUNT_OF(BinarySections); i++) + { + const BinarySection* section = &BinarySections[i]; + ptr = saveBinarySection(ptr, section->tag, section->count, (u8*)&tic->cart + section->offset, section->size, section->flip); + } + + saveBinarySection(ptr, "COVER", 1, &tic->cart.cover, tic->cart.cover.size + sizeof(s32), true); + + return strlen(stream); +} + +static void loadTextSection(const char* project, const char* tag, void* dst, s32 size) +{ + char tagbuf[64]; + sprintf(tagbuf, "-- <%s>\n", tag); + + const char* start = SDL_strstr(project, tagbuf); + + if(start) + { + start += strlen(tagbuf); + + if(start < project + strlen(project)) + { + sprintf(tagbuf, "\n-- ", tag); + const char* end = SDL_strstr(start, tagbuf); + + if(end > start) + SDL_memcpy(dst, start, SDL_min(size, end - start)); + } + } +} + +static void loadBinarySection(const char* project, const char* tag, s32 count, void* dst, s32 size, bool flip) +{ + char tagbuf[64]; + sprintf(tagbuf, "-- <%s>\n", tag); + + const char* start = SDL_strstr(project, tagbuf); + + if(start) + { + start += strlen(tagbuf); + + sprintf(tagbuf, "\n-- ", tag); + const char* end = SDL_strstr(start, tagbuf); + + if(end > start) + { + const char* ptr = start; + + if(size > 0) + { + while(ptr < end) + { + static char lineStr[] = "999"; + memcpy(lineStr, ptr + sizeof("-- ") - 1, sizeof lineStr - 1); + + s32 index = SDL_atoi(lineStr); + + if(index < count) + { + ptr += sizeof("-- 999:") - 1; + str2buf(ptr, size*2, (u8*)dst + size*index, flip); + ptr += size*2 + 1; + } + } + } + else + { + ptr += sizeof("-- 999:") - 1; + str2buf(ptr, end - ptr, (u8*)dst, flip); + } + } + } +} + +static bool loadProject(Console* console, const char* data, s32 size, tic_cartridge* dst) +{ + tic_mem* tic = console->tic; + + char* project = (char*)SDL_malloc(size+1); + + bool done = false; + + if(project) + { + SDL_memcpy(project, data, size); + project[size] = '\0'; + + // remove all the '\r' chars + { + char *s, *d; + for(s = d = project; (*d = *s); d += (*s++ != '\r')); + } + + tic_cartridge* cart = (tic_cartridge*)SDL_malloc(sizeof(tic_cartridge)); + + if(cart) + { + SDL_memset(cart, 0, sizeof(tic_cartridge)); + SDL_memcpy(&cart->palette, &tic->config.palette.data, sizeof(tic_palette)); + + loadTextSection(project, "CODE", cart->code.data, sizeof(tic_code)); + + for(s32 i = 0; i < COUNT_OF(BinarySections); i++) + { + const BinarySection* section = &BinarySections[i]; + loadBinarySection(project, section->tag, section->count, (u8*)cart + section->offset, section->size, section->flip); + } + + loadBinarySection(project, "COVER", 1, &cart->cover, -1, true); + + SDL_memcpy(dst, cart, sizeof(tic_cartridge)); + + SDL_free(cart); + + done = true; + } + + SDL_free(project); + } + + return done; +} + +static bool hasExt(const char* name, const char* ext) +{ + return strstr(name, ext) == name + strlen(name) - strlen(ext); +} + +#endif + static void onConsoleLoadCommandConfirmed(Console* console, const char* param) { if(onConsoleLoadSectionCommand(console, param)) return; @@ -530,9 +761,27 @@ static void onConsoleLoadCommandConfirmed(Console* console, const char* param) SDL_free(data); } - else + else { +#if defined(TIC80_PRO) + const char* name = getProjectName(param); + + void* data = fsLoadFile(console->fs, name, &size); + + if(data) + { + loadProject(console, data, size, &console->tic->cart); + onCartLoaded(console, name); + + SDL_free(data); + } + else + { + printBack(console, "\ncart loading error"); + } +#else printBack(console, "\ncart loading error"); +#endif } } else printBack(console, "\ncart name is missing"); @@ -1560,353 +1809,6 @@ static void onConsoleExportCommand(Console* console, const char* param) #endif -#if defined(TIC80_PRO) - -static const char* getProjectName(const char* name) -{ - return getName(name, PROJECT_EXT); -} - -static void buf2str(const void* data, s32 size, char* ptr, bool flip) -{ - enum {Len = 2}; - - for(s32 i = 0; i < size; i++, ptr+=Len) - { - sprintf(ptr, "%02x", ((u8*)data)[i]); - - if(flip) - { - char tmp = ptr[0]; - ptr[0] = ptr[1]; - ptr[1] = tmp; - } - } -} - -static bool bufferEmpty(const u8* data, s32 size) -{ - for(s32 i = 0; i < size; i++) - if(*data++) - return false; - - return true; -} - -static char* saveTextSection(char* ptr, const char* tag, const char* data) -{ - if(strlen(data) == 0) - return ptr; - - sprintf(ptr, "-- <%s>\n%s\n-- \n", tag, data, tag); - ptr += strlen(ptr); - - return ptr; -} - -static char* saveBinaryBuffer(char* ptr, const void* data, s32 size, s32 row, bool flip) -{ - if(bufferEmpty(data, size)) - return ptr; - - sprintf(ptr, "-- %03i:", row); - ptr += strlen(ptr); - - buf2str(data, size, ptr, flip); - ptr += strlen(ptr); - - sprintf(ptr, "\n"); - ptr += strlen(ptr); - - return ptr; -} - -static char* saveBinarySection(char* ptr, const char* tag, s32 count, const void* data, s32 size, bool flip) -{ - if(bufferEmpty(data, size * count)) - return ptr; - - sprintf(ptr, "\n-- <%s>\n", tag); - ptr += strlen(ptr); - - for(s32 i = 0; i < count; i++, data = (u8*)data + size) - ptr = saveBinaryBuffer(ptr, data, size, i, flip); - - sprintf(ptr, "-- \n", tag); - ptr += strlen(ptr); - - return ptr; -} - -typedef struct {char* tag; s32 count; s32 offset; s32 size; bool flip;} BinarySection; -static const BinarySection BinarySections[] = -{ - {"PALETTE", 1, offsetof(tic_cartridge, palette.data), sizeof(tic_palette), false}, - {"TILES", TIC_BANK_SPRITES, offsetof(tic_cartridge, gfx.tiles), sizeof(tic_tile), true}, - {"SPRITES", TIC_BANK_SPRITES, offsetof(tic_cartridge, gfx.sprites), sizeof(tic_tile), true}, - {"MAP", TIC_MAP_HEIGHT, offsetof(tic_cartridge, gfx.map), TIC_MAP_WIDTH, true}, - {"WAVES", ENVELOPES_COUNT, offsetof(tic_cartridge,sound.sfx.waveform.envelopes), sizeof(tic_waveform), true}, - {"SFX", SFX_COUNT, offsetof(tic_cartridge, sound.sfx.data), sizeof(tic_sound_effect), true}, - {"PATTERNS", MUSIC_PATTERNS, offsetof(tic_cartridge, sound.music.patterns), sizeof(tic_track_pattern), true}, - {"TRACKS", MUSIC_TRACKS, offsetof(tic_cartridge, sound.music.tracks), sizeof(tic_track), true}, -}; - -static CartSaveResult saveProject(Console* console, const char* name) -{ - tic_mem* tic = console->tic; - - bool success = false; - - if(name && strlen(name)) - { - char* stream = (char*)SDL_malloc(sizeof(tic_cartridge) * 3); - - if(stream) - { - char* ptr = saveTextSection(stream, "CODE", tic->cart.code.data); - - for(s32 i = 0; i < COUNT_OF(BinarySections); i++) - { - const BinarySection* section = &BinarySections[i]; - ptr = saveBinarySection(ptr, section->tag, section->count, (u8*)&tic->cart + section->offset, section->size, section->flip); - } - - saveBinarySection(ptr, "COVER", 1, &tic->cart.cover, tic->cart.cover.size + sizeof(s32), true); - - name = getProjectName(name); - - s32 size = strlen(stream); - if(size && fsSaveFile(console->fs, name, stream, size, true)) - { - strcpy(console->romName, name); - success = true; - studioRomSaved(); - } - - SDL_free(stream); - } - } - else if (strlen(console->romName)) - { - return saveProject(console, console->romName); - } - else return CART_SAVE_MISSING_NAME; - - return success ? CART_SAVE_OK : CART_SAVE_ERROR; -} - -static void onConsoleSaveProjectCommandConfirmed(Console* console, const char* param) -{ - CartSaveResult rom = saveProject(console, param); - - if(rom == CART_SAVE_OK) - { - printBack(console, "\nproject "); - printFront(console, console->romName); - printBack(console, " saved!\n"); - } - else if(rom == CART_SAVE_MISSING_NAME) - printBack(console, "\nproject name is missing\n"); - else - printBack(console, "\nproject saving error"); - - commandDone(console); -} - -static void onConsoleSaveProjectCommand(Console* console, const char* param) -{ - if(param && strlen(param) && fsExistsFile(console->fs, getProjectName(param))) - { - static const char* Rows[] = - { - "THE PROJECT", - "ALREADY EXISTS", - "", - "DO YOU WANT TO", - "OVERWRITE IT?", - }; - - confirmCommand(console, Rows, COUNT_OF(Rows), param, onConsoleSaveProjectCommandConfirmed); - } - else - { - onConsoleSaveProjectCommandConfirmed(console, param); - } -} - -static void loadTextSection(const char* project, const char* tag, void* dst, s32 size) -{ - char tagbuf[64]; - sprintf(tagbuf, "-- <%s>\n", tag); - - const char* start = SDL_strstr(project, tagbuf); - - if(start) - { - start += strlen(tagbuf); - - if(start < project + strlen(project)) - { - sprintf(tagbuf, "\n-- ", tag); - const char* end = SDL_strstr(start, tagbuf); - - if(end > start) - SDL_memcpy(dst, start, SDL_min(size, end - start)); - } - } -} - -static void loadBinarySection(const char* project, const char* tag, s32 count, void* dst, s32 size, bool flip) -{ - char tagbuf[64]; - sprintf(tagbuf, "-- <%s>\n", tag); - - const char* start = SDL_strstr(project, tagbuf); - - if(start) - { - start += strlen(tagbuf); - - sprintf(tagbuf, "\n-- ", tag); - const char* end = SDL_strstr(start, tagbuf); - - if(end > start) - { - const char* ptr = start; - - if(size > 0) - { - while(ptr < end) - { - static char lineStr[] = "999"; - memcpy(lineStr, ptr + sizeof("-- ") - 1, sizeof lineStr - 1); - - s32 index = SDL_atoi(lineStr); - - if(index < count) - { - ptr += sizeof("-- 999:") - 1; - str2buf(ptr, size*2, (u8*)dst + size*index, flip); - ptr += size*2 + 1; - } - } - } - else - { - ptr += sizeof("-- 999:") - 1; - str2buf(ptr, end - ptr, (u8*)dst, flip); - } - } - } -} - -static bool loadProject(Console* console, const char* data, s32 size, tic_cartridge* dst) -{ - tic_mem* tic = console->tic; - - char* project = (char*)SDL_malloc(size+1); - - bool done = false; - - if(project) - { - SDL_memcpy(project, data, size); - project[size] = '\0'; - - // remove all the '\r' chars - { - char *s, *d; - for(s = d = project; (*d = *s); d += (*s++ != '\r')); - } - - tic_cartridge* cart = (tic_cartridge*)SDL_malloc(sizeof(tic_cartridge)); - - if(cart) - { - SDL_memset(cart, 0, sizeof(tic_cartridge)); - SDL_memcpy(&cart->palette, &tic->config.palette.data, sizeof(tic_palette)); - - loadTextSection(project, "CODE", cart->code.data, sizeof(tic_code)); - - for(s32 i = 0; i < COUNT_OF(BinarySections); i++) - { - const BinarySection* section = &BinarySections[i]; - loadBinarySection(project, section->tag, section->count, (u8*)cart + section->offset, section->size, section->flip); - } - - loadBinarySection(project, "COVER", 1, &cart->cover, -1, true); - - SDL_memcpy(dst, cart, sizeof(tic_cartridge)); - - SDL_free(cart); - - done = true; - } - - SDL_free(project); - } - - return done; -} - -static void onConsoleLoadProjectCommandConfirmed(Console* console, const char* param) -{ - tic_mem* tic = console->tic; - - if(param) - { - s32 size = 0; - const char* name = getProjectName(param); - - void* data = fsLoadFile(console->fs, name, &size); - - if(data && loadProject(console, data, size, &tic->cart)) - { - strcpy(console->romName, name); - - studioRomLoaded(); - - printBack(console, "\nproject "); - printFront(console, console->romName); - printBack(console, " loaded!\nuse "); - printFront(console, "RUN"); - printBack(console, " command to run it\n"); - - SDL_free(data); - } - else - { - printBack(console, "\nproject loading error"); - } - } - else printBack(console, "\nproject name is missing"); - - commandDone(console); -} - -static void onConsoleLoadProjectCommand(Console* console, const char* param) -{ - if(studioCartChanged()) - { - static const char* Rows[] = - { - "YOU HAVE", - "UNSAVED CHANGES", - "", - "DO YOU REALLY WANT", - "TO LOAD PROJECT?", - }; - - confirmCommand(console, Rows, COUNT_OF(Rows), param, onConsoleLoadProjectCommandConfirmed); - } - else - { - onConsoleLoadProjectCommandConfirmed(console, param); - } -} - -#endif - static CartSaveResult saveCartName(Console* console, const char* name) { tic_mem* tic = console->tic; @@ -1915,7 +1817,7 @@ static CartSaveResult saveCartName(Console* console, const char* name) if(name && strlen(name)) { - u8* buffer = (u8*)SDL_malloc(sizeof(tic_cartridge)); + u8* buffer = (u8*)SDL_malloc(sizeof(tic_cartridge) * 3); if(buffer) { @@ -1928,16 +1830,25 @@ static CartSaveResult saveCartName(Console* console, const char* name) } else { - s32 size = tic->api.save(&tic->cart, buffer); + s32 size = 0; - name = getCartName(name); +#if defined(TIC80_PRO) + if(hasExt(name, PROJECT_EXT)) + { + size = saveProject(console, buffer); + } + else +#endif + { + name = getCartName(name); + size = tic->api.save(&tic->cart, buffer); + } if(size && fsSaveFile(console->fs, name, buffer, size, true)) { strcpy(console->romName, name); success = true; studioRomSaved(); - } } @@ -2210,10 +2121,6 @@ static const struct {"new", NULL, "create new cart", onConsoleNewCommand}, {"load", NULL, "load cart", onConsoleLoadCommand}, {"save", NULL, "save cart", onConsoleSaveCommand}, -#if defined(TIC80_PRO) - {"loadp", NULL, "load project", onConsoleLoadProjectCommand}, - {"savep", NULL, "save project", onConsoleSaveProjectCommand}, -#endif {"run", NULL, "run loaded cart", onConsoleRunCommand}, {"resume", NULL, "resume run cart", onConsoleResumeCommand}, {"dir", "ls", "show list of files", onConsoleDirCommand}, From 768c60f779dc1467b11e4c5c9306fcdf1d749a37 Mon Sep 17 00:00:00 2001 From: "BADIM-PC\\Vadim" Date: Sun, 19 Nov 2017 18:46:07 +0300 Subject: [PATCH 6/6] no message --- src/console.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/console.c b/src/console.c index 0bdcbd2..a366925 100644 --- a/src/console.c +++ b/src/console.c @@ -2574,7 +2574,7 @@ static void cmdLoadCart(Console* console, const char* name) if(data) { #if defined(TIC80_PRO) - if(strstr(name, PROJECT_EXT) == name + strlen(name) - strlen(PROJECT_EXT)) + if(hasExt(name, PROJECT_EXT)) { loadProject(console, data, size, &embed.file); }