From d3ca4d036075ecd557a328c32dcff3a6d6bfe5f8 Mon Sep 17 00:00:00 2001 From: Firew0lf Date: Mon, 24 Aug 2015 20:24:15 +0200 Subject: [PATCH] "Fixed" the HTTPC lib with the crappiest code you've ever seen, Added a (very) small QTM lib Don't look at the HTTPC code !!! Also, the example only work a random number of times. --- sdcard/3ds/ctruLua/tests/httpc.lua | 37 +++++++++++++++ source/ctr.c | 19 ++++---- source/httpc.c | 76 +++++++++++++++++++++++------- source/qtm.c | 57 ++++++++++++++++++++++ 4 files changed, 165 insertions(+), 24 deletions(-) create mode 100644 sdcard/3ds/ctruLua/tests/httpc.lua create mode 100644 source/qtm.c diff --git a/sdcard/3ds/ctruLua/tests/httpc.lua b/sdcard/3ds/ctruLua/tests/httpc.lua new file mode 100644 index 0000000..f7d707b --- /dev/null +++ b/sdcard/3ds/ctruLua/tests/httpc.lua @@ -0,0 +1,37 @@ +local ctr = require("ctr") +local gfx = require("ctr.gfx") +local hid = require("ctr.hid") +local httpc = require("ctr.httpc") + +local err = 0 + +--assert(httpc.init()) + +local context = assert(httpc.context()) + +assert(context:open("http://firew0lf.github.io/")) +assert(context:beginRequest()) + +local data = assert(context:downloadData()) + +while ctr.run() do + hid.read() + keys = hid.keys() + if keys.held.start then break end + if keys.down.b then + assert(context:open("http://firew0lf.github.io/")) + assert(context:beginRequest()) + data = assert(context:downloadData()) + data = (data.."!") + end + + gfx.startFrame(gfx.GFX_TOP) + gfx.text(0, 0, data) + gfx.endFrame() + + gfx.render() +end + + +context:close() +--httpc.shutdown() diff --git a/source/ctr.c b/source/ctr.c index be415aa..0a226f1 100644 --- a/source/ctr.c +++ b/source/ctr.c @@ -12,10 +12,12 @@ void load_hid_lib(lua_State *L); void load_ir_lib(lua_State *L); void load_fs_lib(lua_State *L); void load_httpc_lib(lua_State *L); +void load_qtm_lib(lua_State *L); void unload_gfx_lib(lua_State *L); void unload_hid_lib(lua_State *L); void unload_fs_lib(lua_State *L); +void unload_httpc_lib(lua_State *L); static int ctr_run(lua_State *L) { lua_pushboolean(L, aptMainLoop()); @@ -38,13 +40,14 @@ static const struct luaL_Reg ctr_lib[] = { // Subtables struct { char *name; void (*load)(lua_State *L); void (*unload)(lua_State *L); } ctr_libs[] = { - { "gfx", load_gfx_lib, unload_gfx_lib }, - { "news", load_news_lib, NULL }, - { "ptm", load_ptm_lib, NULL }, - { "hid", load_hid_lib, unload_hid_lib }, - { "ir", load_ir_lib, NULL }, - { "fs", load_fs_lib, unload_fs_lib }, - { "httpc", load_httpc_lib, NULL }, + { "gfx", load_gfx_lib, unload_gfx_lib }, + { "news", load_news_lib, NULL }, + { "ptm", load_ptm_lib, NULL }, + { "hid", load_hid_lib, unload_hid_lib }, + { "ir", load_ir_lib, NULL }, + { "fs", load_fs_lib, unload_fs_lib }, + { "httpc", load_httpc_lib, unload_httpc_lib }, + { "qtm", load_qtm_lib, NULL }, { NULL, NULL } }; @@ -67,4 +70,4 @@ void unload_ctr_lib(lua_State *L) { for (int i = 0; ctr_libs[i].name; i++) { if (ctr_libs[i].unload) ctr_libs[i].unload(L); } -} \ No newline at end of file +} diff --git a/source/httpc.c b/source/httpc.c index ae2d6ce..7292985 100644 --- a/source/httpc.c +++ b/source/httpc.c @@ -1,4 +1,5 @@ #include +#include #include <3ds.h> #include <3ds/types.h> @@ -7,19 +8,31 @@ #include #include -static int httpc_init(lua_State *L) { - httpcInit(); - return 0; -} +/*static int httpc_init(lua_State *L) { + Result ret = httpcInit(); + if (ret != 0) { + lua_pushnil(L); + lua_pushinteger(L, ret); + return 2; + } + lua_pushboolean(L, true); + return 1; +}*/ -static int httpc_shutdown(lua_State *L) { +/*static int httpc_shutdown(lua_State *L) { httpcExit(); return 0; -} +}*/ static int httpc_context(lua_State *L) { - httpcContext *context; - context = (httpcContext*)lua_newuserdata(L, sizeof(*context)); + httpcContext context; + Result ret = httpcOpenContext(&context, "http://google.com/", 0); // Initialization only. + if (ret != 0) { + lua_pushnil(L); + lua_pushinteger(L, ret); + return 2; + } + lua_newuserdata(L, sizeof(&context)); luaL_getmetatable(L, "LHTTPC"); lua_setmetatable(L, -2); @@ -32,8 +45,12 @@ static int httpc_open(lua_State *L) { Result ret = 0; ret = httpcOpenContext(context, url, 0); - - lua_pushinteger(L, ret); + if (ret != 0) { + lua_pushnil(L); + lua_pushinteger(L, ret); + return 2; + } + lua_pushboolean(L, true); return 1; } @@ -42,8 +59,12 @@ static int httpc_beginRequest(lua_State *L) { Result ret = 0; ret = httpcBeginRequest(context); - - lua_pushinteger(L, ret); + if (ret != 0) { + lua_pushnil(L); + lua_pushinteger(L, ret); + return 2; + } + lua_pushboolean(L, true); return 1; } @@ -73,14 +94,31 @@ static int httpc_getDownloadSize(lua_State *L) { static int httpc_downloadData(lua_State *L) { httpcContext *context = lua_touserdata(L, 1); + u32 status = 0; + Result ret = httpcGetResponseStatusCode(context, &status, 0); + if (ret != 0) { + lua_pushnil(L); + lua_pushinteger(L, ret); + return 2; + } + u32 size = 0; httpcGetDownloadSizeState(context, NULL, &size); u8 *buff = (u8*)malloc(size); + //memset(buff, 0, size); - httpcDownloadData(context, buff, size, NULL); + ret = httpcDownloadData(context, buff, size, NULL); + if (ret != 0) { + lua_pushnil(L); + lua_pushinteger(L, ret); + return 2; + } + //strcpy(buff, "loltest"); lua_pushstring(L, (char*)buff); - return 1; + free(buff); + lua_pushinteger(L, size); // only for test purposes. + return 2; } static int httpc_close(lua_State *L) { @@ -104,8 +142,8 @@ static const struct luaL_Reg httpc_methods[] = { // module static const struct luaL_Reg httpc_functions[] = { - {"init", httpc_init }, - {"shutdown", httpc_shutdown}, +// {"init", httpc_init }, +// {"shutdown", httpc_shutdown}, {"context", httpc_context }, {NULL, NULL} }; @@ -122,6 +160,12 @@ int luaopen_httpc_lib(lua_State *L) { } void load_httpc_lib(lua_State *L) { + httpcInit(); + luaL_requiref(L, "ctr.httpc", luaopen_httpc_lib, false); } +void unload_httpc_lib(lua_State *L) { + httpcExit(); +} + diff --git a/source/qtm.c b/source/qtm.c new file mode 100644 index 0000000..3e0599b --- /dev/null +++ b/source/qtm.c @@ -0,0 +1,57 @@ +#include <3ds.h> +#include <3ds/types.h> +#include <3ds/services/qtm.h> + +#include +#include + +static int qtm_init(lua_State *L) { + qtmInit(); + + return 0; +} + +static int qtm_shutdown(lua_State *L) { + qtmExit(); + + return 0; +} + +static int qtm_checkInitialized(lua_State *L) { + bool isInit = qtmCheckInitialized(); + + lua_pushboolean(L, isInit); + return 1; +} + +static int qtm_getHeadtrackingInfo(lua_State *L) { + return 0; +} + +static int qtm_checkHeadFullyDetected(lua_State *L) { + return 0; +} + +static int qtm_convertCoordToScreen(lua_State *L) { + return 0; +} + +// module +static const struct luaL_Reg qtm_functions[] = { + {"init", qtm_init }, + {"shutdown", qtm_shutdown }, + {"checkInitialized", qtm_checkInitialized }, + {"getHeadtrackingInfo", qtm_getHeadtrackingInfo }, + {"checkHeadFullyDetected", qtm_checkHeadFullyDetected}, + {"convertCoordToScreen", qtm_convertCoordToScreen }, + {NULL, NULL} +}; + +int luaopen_qtm_lib(lua_State *L) { + luaL_newlib(L, qtm_functions); + return 1; +} + +void load_qtm_lib(lua_State *L) { + luaL_requiref(L, "ctr.qtm", luaopen_qtm_lib, false); +}