main knows nothing about lua and etc.

This commit is contained in:
BADIM-PC\Vadim 2018-02-08 15:29:14 +03:00
parent 2de319ce5a
commit 3c40e171fc
10 changed files with 181 additions and 197 deletions

View File

@ -29,10 +29,6 @@ MINGW_LINKER_FLAGS= \
-lmingw32 \ -lmingw32 \
-lSDL2main \ -lSDL2main \
-lSDL2 \ -lSDL2 \
-lz \
-lgif \
-llua \
-lwren \
-lcomdlg32 \ -lcomdlg32 \
-lws2_32 \ -lws2_32 \
-mwindows -mwindows

View File

@ -41,9 +41,6 @@
<ProjectReference Include="..\..\..\3rd-party\SDL2-2.0.7\VisualC\SDL\SDL.vcxproj"> <ProjectReference Include="..\..\..\3rd-party\SDL2-2.0.7\VisualC\SDL\SDL.vcxproj">
<Project>{81ce8daf-ebb2-4761-8e45-b71abcca8c68}</Project> <Project>{81ce8daf-ebb2-4761-8e45-b71abcca8c68}</Project>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\lua\lua.vcxproj">
<Project>{57d2471b-3138-495e-af18-6e290d098ffc}</Project>
</ProjectReference>
<ProjectReference Include="..\studio\studio.vcxproj"> <ProjectReference Include="..\studio\studio.vcxproj">
<Project>{6181f6a6-aa1b-4cd2-b306-e242cfde9b20}</Project> <Project>{6181f6a6-aa1b-4cd2-b306-e242cfde9b20}</Project>
</ProjectReference> </ProjectReference>

View File

@ -2540,7 +2540,7 @@ static void checkNewVersion(Console* console)
if(net) if(net)
{ {
NetVersion version = _netVersionRequest(net); NetVersion version = netVersionRequest(net);
free(net); free(net);
if((version.major > TIC_VERSION_MAJOR) || if((version.major > TIC_VERSION_MAJOR) ||

View File

@ -174,7 +174,7 @@ void fsEnumFiles(FileSystem* fs, ListCallback callback, void* data)
if(isPublic(fs)) if(isPublic(fs))
{ {
_netDirRequest(fs->net, fs->work + sizeof(TIC_HOST), callback, data); netDirRequest(fs->net, fs->work + sizeof(TIC_HOST), callback, data);
return; return;
} }

View File

@ -899,8 +899,6 @@ static System sysHandlers =
.getPerformanceCounter = getPerformanceCounter, .getPerformanceCounter = getPerformanceCounter,
.getPerformanceFrequency = getPerformanceFrequency, .getPerformanceFrequency = getPerformanceFrequency,
.netVersionRequest = netVersionRequest,
.netDirRequest = netDirRequest,
.netGetRequest = netGetRequest, .netGetRequest = netGetRequest,
.createNet = createNet, .createNet = createNet,
.closeNet = closeNet, .closeNet = closeNet,

160
src/net.c
View File

@ -24,9 +24,8 @@
#include "tic.h" #include "tic.h"
#include "SDL_net.h" #include "SDL_net.h"
#include <lua.h> #include <stdlib.h>
#include <lauxlib.h> #include <stdio.h>
#include <lualib.h>
struct Net struct Net
{ {
@ -178,124 +177,6 @@ static void getRequest(Net* net, const char* path, NetResponse callback, void* d
#endif #endif
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;
}
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);
NetDirData netDirData = {callback, data};
getRequest(net, request, onDirResponse, &netDirData);
}
typedef struct typedef struct
{ {
void* buffer; void* buffer;
@ -319,43 +200,6 @@ void* netGetRequest(Net* net, const char* path, s32* size)
return netGetData.buffer; return netGetData.buffer;
} }
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;
}
Net* createNet() Net* createNet()
{ {
SDLNet_Init(); SDLNet_Init();

View File

@ -26,16 +26,6 @@
typedef struct Net Net; typedef struct Net Net;
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* netGetRequest(Net* net, const char* path, s32* size);
Net* createNet(); Net* createNet();
void* netGetRequest(Net* net, const char* path, s32* size);
void closeNet(Net* net); void closeNet(Net* net);

View File

@ -39,12 +39,18 @@
#include "fs.h" #include "fs.h"
#include <zlib.h>
#include <ctype.h>
#include "net.h" #include "net.h"
#include "ext/gif.h" #include "ext/gif.h"
#include "ext/md5.h" #include "ext/md5.h"
#include <zlib.h>
#include <ctype.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
// #define TEXTURE_SIZE (TIC80_FULLWIDTH) // #define TEXTURE_SIZE (TIC80_FULLWIDTH)
// #define STUDIO_PIXEL_FORMAT SDL_PIXELFORMAT_ARGB8888 // #define STUDIO_PIXEL_FORMAT SDL_PIXELFORMAT_ARGB8888
#define FRAME_SIZE (TIC80_FULLWIDTH * TIC80_FULLHEIGHT * sizeof(u32)) #define FRAME_SIZE (TIC80_FULLWIDTH * TIC80_FULLHEIGHT * sizeof(u32))
@ -3105,16 +3111,6 @@ u64 getPerformanceFrequency()
return studioImpl.system->getPerformanceFrequency(); return studioImpl.system->getPerformanceFrequency();
} }
NetVersion _netVersionRequest(Net* net)
{
return studioImpl.system->netVersionRequest(net);
}
void _netDirRequest(Net* net, const char* path, ListCallback callback, void* data)
{
return studioImpl.system->netDirRequest(net, path, callback, data);
}
void* _netGetRequest(Net* net, const char* path, s32* size) void* _netGetRequest(Net* net, const char* path, s32* size)
{ {
return studioImpl.system->netGetRequest(net, path, size); return studioImpl.system->netGetRequest(net, path, size);
@ -3139,3 +3135,161 @@ void _file_dialog_save(file_dialog_save_callback callback, const char* name, con
{ {
studioImpl.system->file_dialog_save(callback, name, buffer, size, data, mode); 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);
}

View File

@ -227,8 +227,6 @@ typedef struct
u64 (*getPerformanceCounter)(); u64 (*getPerformanceCounter)();
u64 (*getPerformanceFrequency)(); u64 (*getPerformanceFrequency)();
NetVersion (*netVersionRequest)(Net* net);
void (*netDirRequest)(Net* net, const char* path, ListCallback callback, void* data);
void* (*netGetRequest)(Net* net, const char* path, s32* size); void* (*netGetRequest)(Net* net, const char* path, s32* size);
Net* (*createNet)(); Net* (*createNet)();
void (*closeNet)(Net* net); void (*closeNet)(Net* net);
@ -253,11 +251,19 @@ char* getClipboardText();
u64 getPerformanceCounter(); u64 getPerformanceCounter();
u64 getPerformanceFrequency(); u64 getPerformanceFrequency();
NetVersion _netVersionRequest(Net* net);
void _netDirRequest(Net* net, const char* path, ListCallback callback, void* data);
void* _netGetRequest(Net* net, const char* path, s32* size); void* _netGetRequest(Net* net, const char* path, s32* size);
Net* _createNet(); Net* _createNet();
void _closeNet(Net* net); void _closeNet(Net* net);
void _file_dialog_load(file_dialog_load_callback callback, void* data); 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); 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);

View File

@ -494,7 +494,6 @@ static void updateMenuItemCover(Surf* surf, const u8* cover, s32 size)
MenuItem* item = &surf->menu.items[surf->menu.pos]; MenuItem* item = &surf->menu.items[surf->menu.pos];
item->cover = malloc(sizeof(tic_screen)); item->cover = malloc(sizeof(tic_screen));
gif_image* image = gif_read_data(cover, size); gif_image* image = gif_read_data(cover, size);