Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion builtin/mainmenu/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,22 @@ local function main_event_handler(tabview, event)
end

local function init_globals()
-- Permanent warning if on an unoptimized debug build

local old_set_topleft = core.set_topleft_text

core.set_topleft_text = function(s)
local my_ip = "Not available"
if core.get_local_ip then
my_ip = core.get_local_ip()
end

s = (s or "") .. "\nLocal IP: " .. my_ip

if old_set_topleft then
old_set_topleft(s)
end
end

if core.is_debug_build() then
local set_topleft_text = core.set_topleft_text
core.set_topleft_text = function(s)
Expand Down
33 changes: 32 additions & 1 deletion src/script/lua_api/l_mainmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include "gettext.h"
#include "log.h"
#include "util/string.h"
#include <ifaddrs.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <cassert>
#include <iostream>
Expand Down Expand Up @@ -1097,7 +1100,34 @@ int ModApiMainMenu::l_do_async_callback(lua_State *L)
lua_pushinteger(L, jobId);
return 1;
}

/******************************************************************************/
int ModApiMainMenu::l_get_local_ip(lua_State *L)
{
struct ifaddrs *list;
std::string final_ip = "IP Not Found";

if (getifaddrs(&list) == 0) {
struct ifaddrs *cur;
for (cur = list; cur != NULL; cur = cur->ifa_next) {
if (cur->ifa_addr && cur->ifa_addr->sa_family == AF_INET) {

struct sockaddr_in *sin = (struct sockaddr_in *)cur->ifa_addr;

if ((ntohl(sin->sin_addr.s_addr) & IN_CLASSA_NET) == (INADDR_LOOPBACK & IN_CLASSA_NET)) {
continue;
}

char *ip_string = inet_ntoa(sin->sin_addr);
final_ip = std::string(ip_string);
break;
}
}
freeifaddrs(list);
}

lua_pushstring(L, final_ip.c_str());
return 1;
}
/******************************************************************************/
void ModApiMainMenu::Initialize(lua_State *L, int top)
{
Expand Down Expand Up @@ -1156,6 +1186,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)

lua_pushboolean(L, g_first_run);
lua_setfield(L, top, "is_first_run");
API_FCT(get_local_ip);
}

/******************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions src/script/lua_api/l_mainmenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ class ModApiMainMenu: public ModApiBase
// async
static int l_do_async_callback(lua_State *L);

static int l_get_local_ip(lua_State *L);

public:

/**
Expand Down
Loading