Skip to content

Commit

Permalink
added jsmn json parser
Browse files Browse the repository at this point in the history
  • Loading branch information
nesbox committed May 2, 2024
1 parent 39bc8a1 commit 2a1ae54
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 113 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,6 @@
path = vendor/dlfcn
url = https://github.com/dlfcn-win32/dlfcn-win32.git
shallow = true
[submodule "vendor/jsmn"]
path = vendor/jsmn
url = https://github.com/zserge/jsmn.git
5 changes: 4 additions & 1 deletion cmake/studio.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ add_library(tic80studio STATIC
${DEMO_CARTS_OUT}
${CMAKE_SOURCE_DIR}/build/assets/cart.png.dat)

target_include_directories(tic80studio PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
target_include_directories(tic80studio
PRIVATE ${THIRDPARTY_DIR}/jsmn
PUBLIC ${CMAKE_CURRENT_BINARY_DIR}
)

target_link_libraries(tic80studio PUBLIC tic80core PRIVATE zip wave_writer argparse giflib png)

Expand Down
201 changes: 102 additions & 99 deletions src/studio/screens/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
#include <emscripten.h>
#endif

// #define JSMN_HEADER
#include <jsmn.h>

#define CONSOLE_CURSOR_COLOR tic_color_red
#define CONSOLE_INPUT_COLOR tic_color_white
#define CONSOLE_BACK_TEXT_COLOR tic_color_grey
Expand Down Expand Up @@ -3712,101 +3715,103 @@ static void setScroll(Console* console, s32 val)
}
}

// !TODO: get JSON with version info from the server

// #if defined (TIC_BUILD_WITH_LUA)

// static lua_State* netLuaInit(u8* buffer, s32 size)
// {
// if (buffer && size)
// {
// char* script = calloc(1, size + 1);
// memcpy(script, buffer, size);
// lua_State* lua = luaL_newstate();

// if(lua)
// {
// if(luaL_loadstring(lua, (char*)script) == LUA_OK && lua_pcall(lua, 0, LUA_MULTRET, 0) == LUA_OK)
// {
// free(script);
// return lua;
// }
// else lua_close(lua);
// }

// free(script);
// }

// return NULL;
// }

// static void onHttpVersionGet(const net_get_data* data)
// {
// Console* console = (Console*)data->calldata;

// switch(data->type)
// {
// case net_get_done:
// {
// lua_State* lua = netLuaInit(data->done.data, data->done.size);

// union
// {
// struct
// {
// s32 major;
// s32 minor;
// s32 patch;
// };

// s32 data[3];
// } version =
// {
// {
// .major = TIC_VERSION_MAJOR,
// .minor = TIC_VERSION_MINOR,
// .patch = TIC_VERSION_REVISION,
// },
// };

// 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))
// version.data[i] = (s32)lua_tointeger(lua, -1);

// lua_pop(lua, 1);
// }

// lua_close(lua);
// }

// 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_REVISION))
// {
// char msg[TICNAME_MAX];
// sprintf(msg, " new version %i.%i.%i available", version.major, version.minor, version.patch);

// enum{Offset = (2 * STUDIO_TEXT_BUFFER_WIDTH)};

// memset(console->text + Offset, ' ', STUDIO_TEXT_BUFFER_WIDTH);
// strcpy(console->text + Offset, msg);
// memset(console->color + Offset, tic_color_red, strlen(msg));
// }
// }
// break;
// default:
// break;
// }
// }

// #endif
static s32 jsoneq(const char *json, const jsmntok_t *tok, const char *s)
{
if (tok->type == JSMN_STRING
&& strlen(s) == tok->end - tok->start
&& strncmp(json + tok->start, s, tok->end - tok->start) == 0)
{
return 0;
}

return -1;
}

static s32 jsonint(const char *json, const jsmntok_t *tok)
{
char buf[TICNAME_MAX];
memcpy(buf, json + tok->start, tok->end - tok->start);
buf[tok->end - tok->start] = '\0';

return atoi(buf);
}

static void onHttpVersionGet(const net_get_data* data)
{
Console* console = (Console*)data->calldata;

switch(data->type)
{
case net_get_done:
{
union
{
struct
{
s32 major;
s32 minor;
s32 patch;
};

s32 data[3];
} version =
{{
.major = TIC_VERSION_MAJOR,
.minor = TIC_VERSION_MINOR,
.patch = TIC_VERSION_REVISION,
}};

const char* json = data->done.data;

jsmn_parser p;
jsmntok_t t[16];

jsmn_init(&p);

s32 r = jsmn_parse(&p, json, data->done.size, t, COUNT_OF(t));

if(r > 0 && t[0].type == JSMN_OBJECT)
{
char buf[TICNAME_MAX];

for(s32 i = 0; i < r; i++)
{
if(jsoneq(json, &t[i], "version") == 0)
{
++i;
}
else if(jsoneq(json, &t[i], "major") == 0)
{
version.major = jsonint(json, &t[++i]);
}
else if(jsoneq(json, &t[i], "minor") == 0)
{
version.minor = jsonint(json, &t[++i]);
}
else if(jsoneq(json, &t[i], "patch") == 0)
{
version.patch = jsonint(json, &t[++i]);
}
}
}

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_REVISION))
{
char msg[TICNAME_MAX];
sprintf(msg, " new version %i.%i.%i available", version.major, version.minor, version.patch);

enum{Offset = (2 * STUDIO_TEXT_BUFFER_WIDTH)};

memset(console->text + Offset, ' ', STUDIO_TEXT_BUFFER_WIDTH);
strcpy(console->text + Offset, msg);
memset(console->color + Offset, tic_color_red, strlen(msg));
}
}
break;
}
}

static char* getSelectionText(Console* console)
{
Expand Down Expand Up @@ -4133,10 +4138,8 @@ static void tick(Console* console)
printFront(console, "help");
printBack(console, " for help\n");

// #if defined (TIC_BUILD_WITH_LUA)
// if(getConfig(console->studio)->checkNewVersion)
// tic_net_get(console->net, "/api?fn=version", onHttpVersionGet, console);
// #endif
if(getConfig(console->studio)->checkNewVersion)
tic_net_get(console->net, "/json?fn=version", onHttpVersionGet, console);
}

commandDone(console);
Expand Down
22 changes: 9 additions & 13 deletions tic80.sublime-project
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@
[
{
"name": "fresh",
"shell_cmd": "cd ${project_path}/build && cmake -G Ninja -DCMAKE_BUILD_TYPE=MinSizeRel .. --fresh"
"shell_cmd": "cd ${project_path}/build && cmake .. --fresh"
},
{
"name": "shared disable",
"shell_cmd": "cd ${project_path}/build && cmake -G Ninja -DCMAKE_BUILD_TYPE=MinSizeRel -DBUILD_STATIC=ON .."
"name": "shared [on]",
"shell_cmd": "cd ${project_path}/build && cmake -DBUILD_STATIC=ON .."
},
{
"name": "sokol enable",
"shell_cmd": "cd ${project_path}/build && cmake -G Ninja -DCMAKE_BUILD_TYPE=MinSizeRel -DBUILD_SOKOL=ON .."
"name": "shared [off]",
"shell_cmd": "cd ${project_path}/build && cmake -DBUILD_STATIC=OFF .."
},
{
"name": "pro enable",
"shell_cmd": "cd ${project_path}/build && cmake -G Ninja -DCMAKE_BUILD_TYPE=MinSizeRel -DBUILD_PRO=ON .."
"name": "sokol [on]",
"shell_cmd": "cd ${project_path}/build && cmake -DBUILD_SOKOL=ON .."
},
{
"name": "shared enable",
"shell_cmd": "cd ${project_path}/build && cmake -G Ninja -DCMAKE_BUILD_TYPE=MinSizeRel -DBUILD_STATIC=OFF .."
"name": "pro [on]",
"shell_cmd": "cd ${project_path}/build && cmake -DBUILD_PRO=ON .."
},
{
"name": "make",
Expand All @@ -40,10 +40,6 @@
"name": "clean",
"shell_cmd": "cd ${project_path}/build && ninja -t clean"
},
{
"name": "clean rebuild",
"shell_cmd": "cd ${project_path}/build && ninja -t clean && ninja"
}
]
}
],
Expand Down
1 change: 1 addition & 0 deletions vendor/jsmn
Submodule jsmn added at 25647e

0 comments on commit 2a1ae54

Please sign in to comment.