From 6b65df0b8ea3556e7f05d2a0889ceeb37b738bb7 Mon Sep 17 00:00:00 2001 From: Firew0lf Date: Wed, 13 Apr 2016 16:19:25 +0200 Subject: [PATCH] Fixed some documentation, added some things to sockets, "Fixed" the ROMFS The romfs is still not working, but it works better. --- Makefile | 4 +- sdcard/3ds/ctruLua/examples/qtm/qtm.lua | 4 +- sdcard/3ds/ctruLua/main.lua | 2 +- source/cfgu.c | 8 +- source/ctr.c | 3 +- source/fs.c | 1 + source/gfx.c | 7 +- source/main.c | 28 +++--- source/qtm.c | 2 +- source/socket.c | 126 +++++++++++++++--------- 10 files changed, 115 insertions(+), 70 deletions(-) diff --git a/Makefile b/Makefile index 9ea0a1a..b4d10bb 100644 --- a/Makefile +++ b/Makefile @@ -51,6 +51,9 @@ CFLAGS := -g -Wall -O2 -mword-relocations -std=gnu11 \ $(ARCH) CFLAGS += $(INCLUDE) -DARM11 -D_3DS -DCTR_VERSION=\"$(APP_VERSION)\" -DCTR_BUILD=\"$(LASTCOMMIT)\" +ifneq ($(ROMFS),) + CFLAGS += -DROMFS +endif CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11 @@ -132,7 +135,6 @@ endif ifneq ($(ROMFS),) export _3DSXFLAGS += --romfs=$(CURDIR)/$(ROMFS) - CFLAGS += -DROMFS endif .PHONY: $(BUILD) clean all diff --git a/sdcard/3ds/ctruLua/examples/qtm/qtm.lua b/sdcard/3ds/ctruLua/examples/qtm/qtm.lua index 25533c2..f4948ff 100644 --- a/sdcard/3ds/ctruLua/examples/qtm/qtm.lua +++ b/sdcard/3ds/ctruLua/examples/qtm/qtm.lua @@ -25,12 +25,12 @@ while ctr.run() do local keys = hid.keys() if keys.down.start then break end - local infos = qtm.getHeadTrackingInfo() + local infos = qtm.getHeadtrackingInfo() gfx.start(gfx.TOP) if infos:checkHeadFullyDetected() then for i=1, 4 do - gfx.point(infos:convertCoordToScreen(1, 400, 240)) + gfx.point(infos:convertCoordToScreen(i, 400, 240)) end end gfx.stop() diff --git a/sdcard/3ds/ctruLua/main.lua b/sdcard/3ds/ctruLua/main.lua index bea7791..e867261 100644 --- a/sdcard/3ds/ctruLua/main.lua +++ b/sdcard/3ds/ctruLua/main.lua @@ -51,4 +51,4 @@ while ctr.run() do end end -error("Main process has exited.\nPlease reboot.\nPressing Start does not work yet.") \ No newline at end of file +error("Main process has exited.\nPlease reboot.\nPressing Start does not work yet.") diff --git a/source/cfgu.c b/source/cfgu.c index 5b2e23b..c31a8af 100644 --- a/source/cfgu.c +++ b/source/cfgu.c @@ -109,9 +109,13 @@ static int cfgu_getUsername(lua_State *L) { CFGU_GetConfigInfoBlk2(0x1C, 0xA0000, (u8*)block); u8 *name = malloc(0x14); - utf16_to_utf8(name, block, 0x14); + ssize_t len = utf16_to_utf8(name, block, 0x14); + if (len < 0) { + lua_pushstring(L, ""); + return 1; + } - lua_pushlstring(L, (const char *)name, 0x14); // The username is only 0x14 characters long. + lua_pushlstring(L, (const char *)name, len); // The username is only 0x14 characters long. return 1; } diff --git a/source/ctr.c b/source/ctr.c index 366fc10..c545a84 100644 --- a/source/ctr.c +++ b/source/ctr.c @@ -215,7 +215,8 @@ int luaopen_ctr_lib(lua_State *L) { @field root */ #ifdef ROMFS - char* buff = "romfs:"; + char* buff = "romfs:/"; + chdir(buff); #else char* buff = malloc(1024); getcwd(buff, 1024); diff --git a/source/fs.c b/source/fs.c index b3e2235..dd5faeb 100644 --- a/source/fs.c +++ b/source/fs.c @@ -239,3 +239,4 @@ void unload_fs_lib(lua_State *L) { fsExit(); } + diff --git a/source/gfx.c b/source/gfx.c index 82342bb..d172eca 100644 --- a/source/gfx.c +++ b/source/gfx.c @@ -525,12 +525,17 @@ static int gfx_target___index(lua_State *L) { return 1; } +/*** +Console +@section console +*/ + /*** Initialize the console. You can print on it using print(), or any function that normally outputs to stdout. Warning: you can't use a screen for both a console and drawing, you have to disable the console first. @function console @tparam[opt=gfx.TOP] number screen screen to draw the console on. -@tparam[opt=true] boolean debug enable stderr output on the console +@tparam[opt=false] boolean debug enable stderr output on the console */ u8 consoleScreen = GFX_TOP; static int gfx_console(lua_State *L) { diff --git a/source/main.c b/source/main.c index d106898..33febb3 100644 --- a/source/main.c +++ b/source/main.c @@ -36,7 +36,23 @@ void error(const char *error) { // Main loop int main(int argc, char** argv) { // Default arguments + #ifdef ROMFS + char* mainFile = "romfs:/main.lua"; + #else char* mainFile = "main.lua"; + #endif + + // Init Lua + lua_State *L = luaL_newstate(); + if (L == NULL) { + error("Memory allocation error while creating a new Lua state"); + return 0; + } + + // Load libs + luaL_openlibs(L); + load_ctr_lib(L); + isGfxInitialized = true; // Parse arguments for (int i=0;iaddr.sin_family = AF_INET; userdata->isSSL = false; + fcntl(userdata->socket, F_SETFL, fcntl(userdata->socket, F_GETFL, 0)|O_NONBLOCK); return 1; } @@ -140,9 +147,9 @@ static int socket_udp(lua_State *L) { lua_pushstring(L, strerror(errno)); return 2; } - fcntl(userdata->socket, F_SETFL, O_NONBLOCK); userdata->addr.sin_family = AF_INET; + fcntl(userdata->socket, F_SETFL, fcntl(userdata->socket, F_GETFL, 0)|O_NONBLOCK); return 1; } @@ -151,6 +158,9 @@ static int socket_udp(lua_State *L) { Add a trusted root CA to the certChain. @function addTrustedRootCA @tparam string cert DER cert +@treturn[1] boolean `true` if everything went fine +@treturn[2] nil in case of error +@treturn[2] number error code */ static int socket_addTrustedRootCA(lua_State *L) { size_t size = 0; @@ -181,7 +191,7 @@ static int socket_bind(lua_State *L) { socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket"); int port = luaL_checkinteger(L, 2); - userdata->addr.sin_addr.s_addr = htonl(INADDR_ANY); + userdata->addr.sin_addr.s_addr = gethostid(); userdata->addr.sin_port = htons(port); bind(userdata->socket, (struct sockaddr*)&userdata->addr, sizeof(userdata->addr)); @@ -205,6 +215,64 @@ static int socket_close(lua_State *L) { return 0; } +/*** +Get some informations from a socket. +@function :getpeername +@treturn string IP +@treturn number port +*/ +static int socket_getpeername(lua_State *L) { + socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket"); + + struct sockaddr_in addr; + socklen_t addrSize = sizeof(addr); + + getpeername(userdata->socket, (struct sockaddr*)&addr, &addrSize); + + lua_pushstring(L, inet_ntoa(addr.sin_addr)); + lua_pushinteger(L, ntohs(addr.sin_port)); + + return 2; +} + +/*** +Get some local informations from a socket. +@function :getsockname +@treturn string IP +@treturn number port +*/ +static int socket_getsockname(lua_State *L) { + socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket"); + + struct sockaddr_in addr; + socklen_t addrSize = sizeof(addr); + + getsockname(userdata->socket, (struct sockaddr*)&addr, &addrSize); + + lua_pushstring(L, inet_ntoa(addr.sin_addr)); + lua_pushinteger(L, ntohs(addr.sin_port)); + + return 2; +} + +/*** +Set if the socket should be blocking. +@function :setBlocking +@tparam[opt=true] boolean block if `false`, the socket won't block +*/ +static int socket_setBlocking(lua_State *L) { + socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket"); + bool block = true; + if (lua_isboolean(L, 2)) + block = lua_toboolean(L, 2); + + int flags = fcntl(userdata->socket, F_GETFL, 0); + flags = block?(flags&~O_NONBLOCK):(flags|O_NONBLOCK); + fcntl(userdata->socket, F_SETFL, flags); + + return 0; +} + /*** TCP Sockets @section TCP @@ -221,6 +289,7 @@ static int socket_accept(lua_State *L) { socket_userdata *client = lua_newuserdata(L, sizeof(*client)); luaL_getmetatable(L, "LSocket"); lua_setmetatable(L, -2); + client->isSSL = false; socklen_t addrSize = sizeof(client->addr); client->socket = accept(userdata->socket, (struct sockaddr*)&client->addr, &addrSize); @@ -228,7 +297,6 @@ static int socket_accept(lua_State *L) { lua_pushnil(L); return 1; } - fcntl(client->socket, F_SETFL, O_NONBLOCK); return 1; } @@ -276,7 +344,6 @@ static int socket_connect(lua_State *L) { } userdata->isSSL = true; } - fcntl(userdata->socket, F_SETFL, O_NONBLOCK); lua_pushboolean(L, 1); return 1; @@ -402,46 +469,6 @@ static int socket_send(lua_State *L) { return 1; } -/*** -Get some informations from a socket. -@function :getpeername -@treturn string IP -@treturn number port -*/ -static int socket_getpeername(lua_State *L) { - socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket"); - - struct sockaddr_in addr; - socklen_t addrSize = sizeof(addr); - - getpeername(userdata->socket, (struct sockaddr*)&addr, &addrSize); - - lua_pushstring(L, inet_ntoa(addr.sin_addr)); - lua_pushinteger(L, ntohs(addr.sin_port)); - - return 2; -} - -/*** -Get some local informations from a socket. -@function :getsockname -@treturn string IP -@treturn number port -*/ -static int socket_getsockname(lua_State *L) { - socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket"); - - struct sockaddr_in addr; - socklen_t addrSize = sizeof(addr); - - getsockname(userdata->socket, (struct sockaddr*)&addr, &addrSize); - - lua_pushstring(L, inet_ntoa(addr.sin_addr)); - lua_pushinteger(L, ntohs(addr.sin_port)); - - return 2; -} - /*** UDP sockets @section UDP @@ -533,6 +560,7 @@ static const struct luaL_Reg socket_methods[] = { {"accept", socket_accept }, {"bind", socket_bind }, {"close", socket_close }, + {"setBlocking", socket_setBlocking}, {"__gc", socket_close }, {"connect", socket_connect }, {"listen", socket_listen },