diff --git a/src/console.c b/src/console.c index 4f9c66e..34fcc56 100644 --- a/src/console.c +++ b/src/console.c @@ -23,7 +23,6 @@ #include "console.h" #include "fs.h" #include "config.h" -#include "net.h" #include "ext/gif.h" #include "ext/file_dialog.h" @@ -32,6 +31,10 @@ #include #include +#include +#include +#include + #define CONSOLE_CURSOR_COLOR ((tic_color_red)) #define CONSOLE_BACK_TEXT_COLOR ((tic_color_dark_gray)) #define CONSOLE_FRONT_TEXT_COLOR ((tic_color_white)) @@ -2535,23 +2538,80 @@ static void processGesture(Console* console) else console->scroll.active = false; } +typedef struct +{ + s32 major; + s32 minor; + s32 patch; +} NetVersion; + +static lua_State* netLuaInit(u8* buffer, s32 size) +{ + if (buffer && size) + { + lua_State* lua = luaL_newstate(); + + if(lua) + { + if(luaL_loadstring(lua, (char*)buffer) == LUA_OK && lua_pcall(lua, 0, LUA_MULTRET, 0) == LUA_OK) + return lua; + + else lua_close(lua); + } + } + + return NULL; +} + +static NetVersion netVersionRequest() +{ + NetVersion version = + { + .major = TIC_VERSION_MAJOR, + .minor = TIC_VERSION_MINOR, + .patch = TIC_VERSION_PATCH, + }; + + s32 size = 0; + void* buffer = getUrlRequest("/api?fn=version", &size); + + if(buffer && size) + { + lua_State* lua = netLuaInit(buffer, size); + + if(lua) + { + static const char* Fields[] = {"major", "minor", "patch"}; + + for(s32 i = 0; i < COUNT_OF(Fields); i++) + { + lua_getglobal(lua, Fields[i]); + + if(lua_isinteger(lua, -1)) + ((s32*)&version)[i] = (s32)lua_tointeger(lua, -1); + + lua_pop(lua, 1); + } + + lua_close(lua); + } + } + + return version; +} + static void checkNewVersion(Console* console) { - Net* net = _createNet(); - if(net) + NetVersion version = netVersionRequest(); + + if((version.major > TIC_VERSION_MAJOR) || + (version.major == TIC_VERSION_MAJOR && version.minor > TIC_VERSION_MINOR) || + (version.major == TIC_VERSION_MAJOR && version.minor == TIC_VERSION_MINOR && version.patch > TIC_VERSION_PATCH)) { - NetVersion version = netVersionRequest(net); - free(net); - - if((version.major > TIC_VERSION_MAJOR) || - (version.major == TIC_VERSION_MAJOR && version.minor > TIC_VERSION_MINOR) || - (version.major == TIC_VERSION_MAJOR && version.minor == TIC_VERSION_MINOR && version.patch > TIC_VERSION_PATCH)) - { - char msg[FILENAME_MAX] = {0}; - sprintf(msg, "\n A new version %i.%i.%i is available.\n", version.major, version.minor, version.patch); - consolePrint(console, msg, (tic_color_light_green)); - } + char msg[FILENAME_MAX] = {0}; + sprintf(msg, "\n A new version %i.%i.%i is available.\n", version.major, version.minor, version.patch); + consolePrint(console, msg, (tic_color_light_green)); } } diff --git a/src/fs.c b/src/fs.c index ac3e4d9..9a98fe3 100644 --- a/src/fs.c +++ b/src/fs.c @@ -22,13 +22,16 @@ #include "studio.h" #include "fs.h" -#include "net.h" #include "ext/file_dialog.h" #include #include #include +#include +#include +#include + #if defined(__WINRT__) || defined(__TIC_WINDOWS__) #include #include @@ -49,8 +52,6 @@ struct FileSystem { char dir[FILENAME_MAX]; char work[FILENAME_MAX]; - - Net* net; }; static const char* getFilePath(FileSystem* fs, const char* name) @@ -164,6 +165,127 @@ int _wremove(const wchar_t *); #endif +typedef struct +{ + ListCallback callback; + void* data; +} NetDirData; + +static lua_State* netLuaInit(u8* buffer, s32 size) +{ + if (buffer && size) + { + lua_State* lua = luaL_newstate(); + + if(lua) + { + if(luaL_loadstring(lua, (char*)buffer) == LUA_OK && lua_pcall(lua, 0, LUA_MULTRET, 0) == LUA_OK) + return lua; + + else lua_close(lua); + } + } + + return NULL; +} + +static void onDirResponse(u8* buffer, s32 size, void* data) +{ + NetDirData* netDirData = (NetDirData*)data; + + lua_State* lua = netLuaInit(buffer, size); + + if(lua) + { + { + lua_getglobal(lua, "folders"); + + if(lua_type(lua, -1) == LUA_TTABLE) + { + s32 count = (s32)lua_rawlen(lua, -1); + + for(s32 i = 1; i <= count; i++) + { + lua_geti(lua, -1, i); + + { + lua_getfield(lua, -1, "name"); + if(lua_isstring(lua, -1)) + netDirData->callback(lua_tostring(lua, -1), NULL, 0, netDirData->data, true); + + lua_pop(lua, 1); + } + + lua_pop(lua, 1); + } + } + + lua_pop(lua, 1); + } + + { + lua_getglobal(lua, "files"); + + if(lua_type(lua, -1) == LUA_TTABLE) + { + s32 count = (s32)lua_rawlen(lua, -1); + + for(s32 i = 1; i <= count; i++) + { + lua_geti(lua, -1, i); + + char hash[FILENAME_MAX] = {0}; + char name[FILENAME_MAX] = {0}; + + { + lua_getfield(lua, -1, "hash"); + if(lua_isstring(lua, -1)) + strcpy(hash, lua_tostring(lua, -1)); + + lua_pop(lua, 1); + } + + { + lua_getfield(lua, -1, "name"); + + if(lua_isstring(lua, -1)) + strcpy(name, lua_tostring(lua, -1)); + + lua_pop(lua, 1); + } + + { + lua_getfield(lua, -1, "id"); + + if(lua_isinteger(lua, -1)) + netDirData->callback(name, hash, lua_tointeger(lua, -1), netDirData->data, false); + + lua_pop(lua, 1); + } + + lua_pop(lua, 1); + } + } + + lua_pop(lua, 1); + } + + lua_close(lua); + } +} + +static void netDirRequest(const char* path, ListCallback callback, void* data) +{ + char request[FILENAME_MAX] = {'\0'}; + sprintf(request, "/api?fn=dir&path=%s", path); + + s32 size = 0; + void* buffer = getUrlRequest(request, &size); + + NetDirData netDirData = {callback, data}; + onDirResponse(buffer, size, &netDirData); +} + void fsEnumFiles(FileSystem* fs, ListCallback callback, void* data) { #if !defined(__EMSCRIPTEN__) @@ -172,7 +294,7 @@ void fsEnumFiles(FileSystem* fs, ListCallback callback, void* data) if(isPublic(fs)) { - netDirRequest(fs->net, fs->work + sizeof(TIC_HOST), callback, data); + netDirRequest(fs->work + sizeof(TIC_HOST), callback, data); return; } @@ -721,7 +843,7 @@ void* fsLoadFile(FileSystem* fs, const char* name, s32* size) char path[FILENAME_MAX] = {0}; sprintf(path, "/cart/%s/cart.tic", loadPublicCartData.hash); - void* data = _netGetRequest(fs->net, path, size); + void* data = getUrlRequest(path, size); if(data) fsSaveRootFile(fs, cachePath, data, *size, false); @@ -786,8 +908,6 @@ FileSystem* createFileSystem(const char* path) FileSystem* fs = (FileSystem*)malloc(sizeof(FileSystem)); memset(fs, 0, sizeof(FileSystem)); - fs->net = _createNet(); - strcpy(fs->dir, path); return fs; diff --git a/src/main.c b/src/main.c index 4ad13f1..4bc1597 100644 --- a/src/main.c +++ b/src/main.c @@ -54,6 +54,8 @@ static struct const u8* src; } mouse; + Net* net; + bool missedFrame; bool fullscreen; } platform; @@ -957,6 +959,11 @@ void _openSystemPath(const char* path) {} #endif +static void* _getUrlRequest(const char* url, s32* size) +{ + return netGetRequest(platform.net, url, size); +} + static System sysHandlers = { .setClipboardText = _setClipboardText, @@ -965,9 +972,7 @@ static System sysHandlers = .getPerformanceCounter = _getPerformanceCounter, .getPerformanceFrequency = _getPerformanceFrequency, - .netGetRequest = netGetRequest, - .createNet = createNet, - .closeNet = closeNet, + .getUrlRequest = _getUrlRequest, .file_dialog_load = file_dialog_load, .file_dialog_save = file_dialog_save, @@ -1063,6 +1068,8 @@ static s32 start(s32 argc, char **argv, const char* folder) initSound(); + platform.net = createNet(); + platform.window = SDL_CreateWindow( TIC_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (TIC80_FULLWIDTH) * STUDIO_UI_SCALE, (TIC80_FULLHEIGHT) * STUDIO_UI_SCALE, @@ -1123,6 +1130,8 @@ static s32 start(s32 argc, char **argv, const char* folder) studioClose(); + closeNet(platform.net); + if(platform.audio.cvt.buf) SDL_free(platform.audio.cvt.buf); diff --git a/src/studio.c b/src/studio.c index b84e2dd..91ebbea 100644 --- a/src/studio.c +++ b/src/studio.c @@ -39,7 +39,6 @@ #include "fs.h" -#include "net.h" #include "ext/gif.h" #include "ext/md5.h" @@ -1912,8 +1911,6 @@ void studioTick(void* pixels) void studioClose() { - _closeNet(studioImpl.surf->net); - { for(s32 i = 0; i < TIC_EDITOR_BANKS; i++) { @@ -1963,21 +1960,6 @@ u64 getPerformanceFrequency() return studioImpl.system->getPerformanceFrequency(); } -void* _netGetRequest(Net* net, const char* path, s32* size) -{ - return studioImpl.system->netGetRequest(net, path, size); -} - -Net* _createNet() -{ - return studioImpl.system->createNet(); -} - -void _closeNet(Net* net) -{ - return studioImpl.system->closeNet(net); -} - void _file_dialog_load(file_dialog_load_callback callback, void* data) { studioImpl.system->file_dialog_load(callback, data); @@ -1988,164 +1970,6 @@ void _file_dialog_save(file_dialog_save_callback callback, const char* name, con studioImpl.system->file_dialog_save(callback, name, buffer, size, data, mode); } -static lua_State* netLuaInit(u8* buffer, s32 size) -{ - if (buffer && size) - { - lua_State* lua = luaL_newstate(); - - if(lua) - { - if(luaL_loadstring(lua, (char*)buffer) == LUA_OK && lua_pcall(lua, 0, LUA_MULTRET, 0) == LUA_OK) - return lua; - - else lua_close(lua); - } - } - - return NULL; -} - -NetVersion netVersionRequest(Net* net) -{ - NetVersion version = - { - .major = TIC_VERSION_MAJOR, - .minor = TIC_VERSION_MINOR, - .patch = TIC_VERSION_PATCH, - }; - - s32 size = 0; - void* buffer = _netGetRequest(net, "/api?fn=version", &size); - - if(buffer && size) - { - lua_State* lua = netLuaInit(buffer, size); - - if(lua) - { - static const char* Fields[] = {"major", "minor", "patch"}; - - for(s32 i = 0; i < COUNT_OF(Fields); i++) - { - lua_getglobal(lua, Fields[i]); - - if(lua_isinteger(lua, -1)) - ((s32*)&version)[i] = (s32)lua_tointeger(lua, -1); - - lua_pop(lua, 1); - } - - lua_close(lua); - } - } - - return version; -} - -typedef struct -{ - ListCallback callback; - void* data; -} NetDirData; - -static void onDirResponse(u8* buffer, s32 size, void* data) -{ - NetDirData* netDirData = (NetDirData*)data; - - lua_State* lua = netLuaInit(buffer, size); - - if(lua) - { - { - lua_getglobal(lua, "folders"); - - if(lua_type(lua, -1) == LUA_TTABLE) - { - s32 count = (s32)lua_rawlen(lua, -1); - - for(s32 i = 1; i <= count; i++) - { - lua_geti(lua, -1, i); - - { - lua_getfield(lua, -1, "name"); - if(lua_isstring(lua, -1)) - netDirData->callback(lua_tostring(lua, -1), NULL, 0, netDirData->data, true); - - lua_pop(lua, 1); - } - - lua_pop(lua, 1); - } - } - - lua_pop(lua, 1); - } - - { - lua_getglobal(lua, "files"); - - if(lua_type(lua, -1) == LUA_TTABLE) - { - s32 count = (s32)lua_rawlen(lua, -1); - - for(s32 i = 1; i <= count; i++) - { - lua_geti(lua, -1, i); - - char hash[FILENAME_MAX] = {0}; - char name[FILENAME_MAX] = {0}; - - { - lua_getfield(lua, -1, "hash"); - if(lua_isstring(lua, -1)) - strcpy(hash, lua_tostring(lua, -1)); - - lua_pop(lua, 1); - } - - { - lua_getfield(lua, -1, "name"); - - if(lua_isstring(lua, -1)) - strcpy(name, lua_tostring(lua, -1)); - - lua_pop(lua, 1); - } - - { - lua_getfield(lua, -1, "id"); - - if(lua_isinteger(lua, -1)) - netDirData->callback(name, hash, lua_tointeger(lua, -1), netDirData->data, false); - - lua_pop(lua, 1); - } - - lua_pop(lua, 1); - } - } - - lua_pop(lua, 1); - } - - lua_close(lua); - } -} - -void netDirRequest(Net* net, const char* path, ListCallback callback, void* data) -{ - char request[FILENAME_MAX] = {'\0'}; - sprintf(request, "/api?fn=dir&path=%s", path); - - s32 size = 0; - void* buffer = _netGetRequest(net, request, &size); - - NetDirData netDirData = {callback, data}; - onDirResponse(buffer, size, &netDirData); -} - void showMessageBox(const char* title, const char* message) { studioImpl.system->showMessageBox(title, message); @@ -2154,4 +1978,9 @@ void showMessageBox(const char* title, const char* message) void openSystemPath(const char* path) { studioImpl.system->openSystemPath(path); +} + +void* getUrlRequest(const char* url, s32* size) +{ + return studioImpl.system->getUrlRequest(url, size); } \ No newline at end of file diff --git a/src/studio.h b/src/studio.h index 5554547..5451f63 100644 --- a/src/studio.h +++ b/src/studio.h @@ -37,7 +37,6 @@ #include "ticapi.h" #include "defines.h" #include "tools.h" -#include "net.h" #include "ext/file_dialog.h" #define TIC_LOCAL ".local/" @@ -227,9 +226,7 @@ typedef struct u64 (*getPerformanceCounter)(); u64 (*getPerformanceFrequency)(); - void* (*netGetRequest)(Net* net, const char* path, s32* size); - Net* (*createNet)(); - void (*closeNet)(Net* net); + void* (*getUrlRequest)(const char* url, s32* size); void (*file_dialog_load)(file_dialog_load_callback callback, void* data); void (*file_dialog_save)(file_dialog_save_callback callback, const char* name, const u8* buffer, size_t size, void* data, u32 mode); @@ -257,23 +254,11 @@ char* getClipboardText(); u64 getPerformanceCounter(); u64 getPerformanceFrequency(); -void* _netGetRequest(Net* net, const char* path, s32* size); -Net* _createNet(); -void _closeNet(Net* net); - void _file_dialog_load(file_dialog_load_callback callback, void* data); void _file_dialog_save(file_dialog_save_callback callback, const char* name, const u8* buffer, size_t size, void* data, u32 mode); -typedef struct -{ - s32 major; - s32 minor; - s32 patch; -} NetVersion; - -NetVersion netVersionRequest(Net* net); -void netDirRequest(Net* net, const char* path, ListCallback callback, void* data); void showMessageBox(const char* title, const char* message); TIC80_API void updateStudioProject(); -void openSystemPath(const char* path); \ No newline at end of file +void openSystemPath(const char* path); +void* getUrlRequest(const char* url, s32* size); diff --git a/src/surf.c b/src/surf.c index 4f41d06..20f5110 100644 --- a/src/surf.c +++ b/src/surf.c @@ -22,7 +22,6 @@ #include "surf.h" #include "fs.h" -#include "net.h" #include "console.h" #include "ext/gif.h" @@ -478,7 +477,7 @@ static void* requestCover(Surf* surf, const char* hash, s32* size) char path[FILENAME_MAX] = {0}; sprintf(path, "/cart/%s/cover.gif", hash); - void* data = _netGetRequest(surf->net, path, size); + void* data = getUrlRequest(path, size); if(data) { @@ -861,7 +860,6 @@ void initSurf(Surf* surf, tic_mem* tic, struct Console* console) .items = NULL, .count = 0, }, - .net = _createNet(), }; fsMakeDir(surf->fs, TIC_CACHE); diff --git a/src/surf.h b/src/surf.h index 057339f..ca13915 100644 --- a/src/surf.h +++ b/src/surf.h @@ -31,7 +31,6 @@ struct Surf tic_mem* tic; struct FileSystem* fs; struct Console* console; - struct Net* net; struct Movie* state; bool init;