1
0
Fork 0
mirror of https://github.com/ctruLua/ctruLua.git synced 2025-10-27 16:39:29 +00:00

Fixed some documentation, added some things to sockets, "Fixed" the ROMFS

The romfs is still not working, but it works better.
This commit is contained in:
Firew0lf 2016-04-13 16:19:25 +02:00
parent e7ff54d58c
commit 6b65df0b8e
10 changed files with 115 additions and 70 deletions

View file

@ -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;
}

View file

@ -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);

View file

@ -239,3 +239,4 @@ void unload_fs_lib(lua_State *L) {
fsExit();
}

View file

@ -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) {

View file

@ -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;i<argc;i++) {
@ -60,18 +76,6 @@ int main(int argc, char** argv) {
}
}
// 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;
// Do the actual thing
if (luaL_dofile(L, mainFile)) error(luaL_checkstring(L, -1));

View file

@ -59,7 +59,7 @@ static int qtm_checkInitialized(lua_State *L) {
/***
Return informations about the headtracking
@function getHeadTrackingInfo
@function getHeadtrackingInfo
@treturn qtmInfos QTM informations
*/
static int qtm_getHeadtrackingInfo(lua_State *L) {

View file

@ -1,6 +1,7 @@
/***
The `socket` module. Almost like luasocket, but for the TCP part only.
The UDP part is only without connection.
All sockets are not blocking by default.
@module ctr.socket
@usage local socket = require("ctr.socket")
*/
@ -41,6 +42,9 @@ u32 rootCertChain = 0;
Initialize the socket module
@function init
@tparam[opt=0x100000] number buffer size (in bytes), must be a multiple of 0x1000
@treturn[1] boolean `true` if everything went fine
@treturn[2] boolean `false` in case of error
@treturn[2] number/string error code/message
*/
static int socket_init(lua_State *L) {
if (!initStateSocket) {
@ -60,7 +64,7 @@ static int socket_init(lua_State *L) {
Result ret = socInit(mem, size);
if (ret) {
if (R_FAILED(ret)) {
lua_pushboolean(L, false);
lua_pushinteger(L, ret);
return 2;
@ -92,11 +96,13 @@ Disable the socket module. Must be called before exiting ctrµLua.
@function shutdown
*/
static int socket_shutdown(lua_State *L) {
sslcDestroyRootCertChain(rootCertChain);
sslcExit();
socExit();
if (initStateSocket) {
sslcDestroyRootCertChain(rootCertChain);
sslcExit();
socExit();
initStateSocket = false;
}
initStateSocket = false;
return 0;
}
@ -120,6 +126,7 @@ static int socket_tcp(lua_State *L) {
userdata->addr.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 },