diff --git a/src/console.c b/src/console.c index f13fdf7..33476b3 100644 --- a/src/console.c +++ b/src/console.c @@ -1081,6 +1081,26 @@ static void onConsoleLoadCommandConfirmed(Console* console, const char* param) commandDone(console); } +static void load(Console* console, const char* path, const char* hash) +{ + s32 size = 0; + const char* name = getCartName(path); + + void* data = fsLoadFileByHash(console->fs, hash, &size); + + if(data) + { + console->showGameMenu = true; + + loadRom(console->tic, data, size, true); + onCartLoaded(console, name); + + free(data); + } + + commandDone(console); +} + typedef void(*ConfirmCallback)(Console* console, const char* param); typedef struct @@ -3117,7 +3137,7 @@ void initConsole(Console* console, tic_mem* tic, FileSystem* fs, Config* config, { .tic = tic, .config = config, - .load = onConsoleLoadCommandConfirmed, + .load = load, #if defined(TIC80_PRO) .loadProject = loadProject, diff --git a/src/console.h b/src/console.h index 6dffa43..0a0933b 100644 --- a/src/console.h +++ b/src/console.h @@ -103,7 +103,7 @@ struct Console bool crtMonitor; }; - void(*load)(Console*, const char* name); + void(*load)(Console*, const char* path, const char* hash); bool(*loadProject)(Console*, const char* name, const char* data, s32 size, tic_cartridge* dst); void(*updateProject)(Console*); void(*error)(Console*, const char*); diff --git a/src/fs.c b/src/fs.c index 1dea232..f0fd1e9 100644 --- a/src/fs.c +++ b/src/fs.c @@ -899,6 +899,26 @@ static bool onLoadPublicCart(const char* name, const char* info, s32 id, void* d return true; } +void* fsLoadFileByHash(FileSystem* fs, const char* hash, s32* size) +{ + char cachePath[FILENAME_MAX] = {0}; + sprintf(cachePath, TIC_CACHE "%s.tic", hash); + + { + void* data = fsLoadRootFile(fs, cachePath, size); + if(data) return data; + } + + char path[FILENAME_MAX] = {0}; + sprintf(path, "/cart/%s/cart.tic", hash); + void* data = getSystem()->getUrlRequest(path, size); + + if(data) + fsSaveRootFile(fs, cachePath, data, *size, false); + + return data; +} + void* fsLoadFile(FileSystem* fs, const char* name, s32* size) { if(isPublic(fs)) @@ -912,24 +932,7 @@ void* fsLoadFile(FileSystem* fs, const char* name, s32* size) fsEnumFiles(fs, onLoadPublicCart, &loadPublicCartData); if(strlen(loadPublicCartData.hash)) - { - char cachePath[FILENAME_MAX] = {0}; - sprintf(cachePath, TIC_CACHE "%s.tic", loadPublicCartData.hash); - - { - void* data = fsLoadRootFile(fs, cachePath, size); - if(data) return data; - } - - char path[FILENAME_MAX] = {0}; - sprintf(path, "/cart/%s/cart.tic", loadPublicCartData.hash); - void* data = getSystem()->getUrlRequest(path, size); - - if(data) - fsSaveRootFile(fs, cachePath, data, *size, false); - - return data; - } + return fsLoadFileByHash(fs, loadPublicCartData.hash, size); } else { diff --git a/src/fs.h b/src/fs.h index b5636e1..d55e5a8 100644 --- a/src/fs.h +++ b/src/fs.h @@ -57,6 +57,7 @@ bool fsDeleteDir(FileSystem* fs, const char* name); bool fsSaveFile(FileSystem* fs, const char* name, const void* data, size_t size, bool overwrite); bool fsSaveRootFile(FileSystem* fs, const char* name, const void* data, size_t size, bool overwrite); void* fsLoadFile(FileSystem* fs, const char* name, s32* size); +void* fsLoadFileByHash(FileSystem* fs, const char* hash, s32* size); void* fsLoadRootFile(FileSystem* fs, const char* name, s32* size); void fsMakeDir(FileSystem* fs, const char* name); bool fsExistsFile(FileSystem* fs, const char* name); diff --git a/src/surf.c b/src/surf.c index 8d7e626..67400bd 100644 --- a/src/surf.c +++ b/src/surf.c @@ -670,7 +670,7 @@ static void onPlayCart(Surf* surf) } } else - surf->console->load(surf->console, item->name); + surf->console->load(surf->console, item->name, item->hash); runGameFromSurf(); }