lua client API: Fix crash in get_server_info()

When running on a local machine in Docker, calling
`minetest.get_server_info()` in lua can throw an exception, resulting
in client shutdown.

    ERROR[Main]: ModError: Failed to load and run mod "tchat":
    ERROR[Main]: No address for peer found!
    ERROR[Main]: stack traceback:
    ERROR[Main]: 	[C]: in function 'get_server_info'
    ERROR[Main]: 	tchat:init.lua:118: in main chunk
    ERROR[Main]: Check debug.txt for details.
This commit is contained in:
Schmappie Eldress 2021-08-20 01:14:35 -05:00
parent 1c1eac07f3
commit a4187aa951

View File

@ -322,8 +322,24 @@ int ModApiClient::l_sound_fade(lua_State *L)
int ModApiClient::l_get_server_info(lua_State *L) int ModApiClient::l_get_server_info(lua_State *L)
{ {
Client *client = getClient(L); Client *client = getClient(L);
Address serverAddress = client->getServerAddress();
lua_newtable(L); lua_newtable(L);
try {
Address serverAddress = client->getServerAddress();
} catch (const con::PeerNotFoundException &) {
// Local connection?
lua_pushstring(L, "unknown");
lua_setfield(L, -2, "address");
lua_pushstring(L, "unknown");
lua_setfield(L, -2, "ip");
lua_pushinteger(L, 0);
lua_setfield(L, -2, "port");
lua_pushinteger(L, 0);
lua_setfield(L, -2, "port");
lua_pushinteger(L, 0);
lua_setfield(L, -2, "protocol_version");
return 1;
}
lua_pushstring(L, client->getAddressName().c_str()); lua_pushstring(L, client->getAddressName().c_str());
lua_setfield(L, -2, "address"); lua_setfield(L, -2, "address");
lua_pushstring(L, serverAddress.serializeString().c_str()); lua_pushstring(L, serverAddress.serializeString().c_str());
@ -332,6 +348,7 @@ int ModApiClient::l_get_server_info(lua_State *L)
lua_setfield(L, -2, "port"); lua_setfield(L, -2, "port");
lua_pushinteger(L, client->getProtoVersion()); lua_pushinteger(L, client->getProtoVersion());
lua_setfield(L, -2, "protocol_version"); lua_setfield(L, -2, "protocol_version");
return 1; return 1;
} }