Skip to content

Commit

Permalink
Add function to get server info.
Browse files Browse the repository at this point in the history
  • Loading branch information
red-001 authored and paramat committed May 4, 2017
1 parent 468eeb6 commit ae0d8f7
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 21 deletions.
6 changes: 5 additions & 1 deletion clientmods/preview/init.lua
Expand Up @@ -8,6 +8,11 @@ end)

core.register_on_connect(function()
print("[PREVIEW] Player connection completed")
local server_info = core.get_server_info()
print("Server version: " .. server_info.protocol_version)
print("Server ip: " .. server_info.ip)
print("Server address: " .. server_info.address)
print("Server port: " .. server_info.port)
end)

core.register_on_placenode(function(pointed_thing, node)
Expand Down Expand Up @@ -80,7 +85,6 @@ core.after(2, function()
print("[PREVIEW] loaded " .. modname .. " mod")
modstorage:set_string("current_mod", modname)
print(modstorage:get_string("current_mod"))
print("Server version:" .. core.get_protocol_version())
preview_minimap()
end)

Expand Down
18 changes: 13 additions & 5 deletions doc/client_lua_api.md
Expand Up @@ -702,11 +702,10 @@ Call these functions only at load time!
* `minetest.disconnect()`
* Disconnect from the server and exit to main menu.
* Returns `false` if the client is already disconnecting otherwise returns `true`.
* `minetest.get_protocol_version()`
* Returns the protocol version of the server.
* Might not be accurate at start up as the client might not be connected to the server yet, in that case it will return 0.
* `minetest.take_screenshot()`
* Take a screenshot.
* `minetest.get_server_info()`
* Returns [server info](#server-info).

### Misc.
* `minetest.parse_json(string[, nullvalue])`: returns something
Expand Down Expand Up @@ -932,9 +931,18 @@ Can be obtained via `minetest.get_meta(pos)`.
{
params = "<name> <privilege>", -- Short parameter description
description = "Remove privilege from player", -- Full description
func = function(param), -- Called when command is run.
-- Returns boolean success and text output.
func = function(param), -- Called when command is run.
-- Returns boolean success and text output.
}
### Server info
```lua
{
address = "minetest.example.org", -- The domain name/IP address of a remote server or "" for a local server.
ip = "203.0.113.156", -- The IP address of the server.

This comment has been minimized.

Copy link
@sofar

sofar May 4, 2017

Contributor

well, this isn't a valid IP?!

what would have been wrong with putting 127.0.0.1 here? and hostname localhost ?!

This comment has been minimized.

Copy link
@kilbith

kilbith May 4, 2017

Contributor

Agreed, please fix this shit.

This comment has been minimized.

Copy link
@bigfoot547

bigfoot547 May 4, 2017

Contributor

@red-001 um… cough… Who's ip is this?

This comment has been minimized.

Copy link
@nerzhul

nerzhul May 4, 2017

Member

It's a valid IP for documentation, sorry guys, please be polite.

According to whois:

inetnum:        203.0.113.0 - 203.0.113.255
netname:        TEST-NET-3
descr:          IANA
descr:          RFC5737 Documentation Address Block
country:        AU
admin-c:        HM20-AP
tech-c:         HM20-AP
mnt-by:         APNIC-HM
mnt-routes:     APNIC-HM
status:         ASSIGNED PORTABLE
remarks:        -+-+-+-+-+-+-+-+-+-+-+-++-+-+-+-+-+-+-+-+-+-+-+-+-+-+
remarks:        This block is reserved for use in documentation and
remarks:        should not be used in any real networks.
remarks:        Please see more details at
remarks:        http://www.iana.org/go/rfc5737
remarks:        -+-+-+-+-+-+-+-+-+-+-+-++-+-+-+-+-+-+-+-+-+-+-+-+-+-+
source:         APNIC
mnt-irt:        IRT-APNICRANDNET-AU
changed:        hm-changed@apnic.net 20100617

This comment has been minimized.

Copy link
@ShadowNinja

ShadowNinja May 4, 2017

Member

As nrz mentioned, this is from one of the example ranges. I think this is a reasonable value to have in the documentation.

This comment has been minimized.

Copy link
@red-001

red-001 May 4, 2017

Author Contributor

it's an IP from one of the ranges reserved for use in documentation (rfc 5737).

port = 30000, -- The port the client is connected to.
protocol_version = 30 -- Will not be accurate at start up as the client might not be connected to the server yet, in that case it will be 0.
}
```

Escape sequences
----------------
Expand Down
8 changes: 4 additions & 4 deletions src/client.cpp
Expand Up @@ -58,6 +58,7 @@ Client::Client(
IrrlichtDevice *device,
const char *playername,
const std::string &password,
const std::string &address_name,
MapDrawControl &control,
IWritableTextureSource *tsrc,
IWritableShaderSource *shsrc,
Expand Down Expand Up @@ -89,6 +90,7 @@ Client::Client(
),
m_particle_manager(&m_env),
m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, ipv6, this),
m_address_name(address_name),
m_device(device),
m_camera(NULL),
m_minimap_disabled_by_server(false),
Expand Down Expand Up @@ -253,13 +255,11 @@ Client::~Client()
delete m_minimap;
}

void Client::connect(Address address,
const std::string &address_name,
bool is_local_server)
void Client::connect(Address address, bool is_local_server)
{
DSTACK(FUNCTION_NAME);

initLocalMapSaving(address, address_name, is_local_server);
initLocalMapSaving(address, m_address_name, is_local_server);

m_con.SetTimeoutMs(0);
m_con.Connect(address);
Expand Down
16 changes: 13 additions & 3 deletions src/client.h
Expand Up @@ -257,6 +257,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
IrrlichtDevice *device,
const char *playername,
const std::string &password,
const std::string &address_name,
MapDrawControl &control,
IWritableTextureSource *tsrc,
IWritableShaderSource *shsrc,
Expand Down Expand Up @@ -284,9 +285,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
The name of the local player should already be set when
calling this, as it is sent in the initialization.
*/
void connect(Address address,
const std::string &address_name,
bool is_local_server);
void connect(Address address, bool is_local_server);

/*
Stuff that references the environment is valid only as
Expand Down Expand Up @@ -525,6 +524,16 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef

IrrlichtDevice *getDevice() const { return m_device; }

const Address getServerAddress()
{
return m_con.GetPeerAddress(PEER_ID_SERVER);
}

const std::string &getAddressName() const
{
return m_address_name;
}

private:

// Virtual methods from con::PeerHandler
Expand Down Expand Up @@ -576,6 +585,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
ClientEnvironment m_env;
ParticleManager m_particle_manager;
con::Connection m_con;
std::string m_address_name;
IrrlichtDevice *m_device;
Camera *m_camera;
Minimap *m_minimap;
Expand Down
4 changes: 2 additions & 2 deletions src/game.cpp
Expand Up @@ -2044,7 +2044,7 @@ bool Game::connectToServer(const std::string &playername,
}

client = new Client(device,
playername.c_str(), password,
playername.c_str(), password, *address,
*draw_control, texture_src, shader_src,
itemdef_manager, nodedef_manager, sound, eventmgr,
connect_address.isIPv6(), &flags);
Expand All @@ -2056,7 +2056,7 @@ bool Game::connectToServer(const std::string &playername,
connect_address.print(&infostream);
infostream << std::endl;

client->connect(connect_address, *address,
client->connect(connect_address,
simple_singleplayer_mode || local_server_mode);

/*
Expand Down
18 changes: 14 additions & 4 deletions src/script/lua_api/l_client.cpp
Expand Up @@ -234,10 +234,20 @@ int ModApiClient::l_sound_stop(lua_State *L)
return 0;
}

// get_protocol_version()
int ModApiClient::l_get_protocol_version(lua_State *L)
// get_server_info()
int ModApiClient::l_get_server_info(lua_State *L)
{
lua_pushinteger(L, getClient(L)->getProtoVersion());
Client *client = getClient(L);
Address serverAddress = client->getServerAddress();
lua_newtable(L);
lua_pushstring(L, client->getAddressName().c_str());
lua_setfield(L, -2, "address");
lua_pushstring(L, serverAddress.serializeString().c_str());
lua_setfield(L, -2, "ip");
lua_pushinteger(L, serverAddress.getPort());
lua_setfield(L, -2, "port");
lua_pushinteger(L, client->getProtoVersion());
lua_setfield(L, -2, "protocol_version");
return 1;
}

Expand Down Expand Up @@ -265,6 +275,6 @@ void ModApiClient::Initialize(lua_State *L, int top)
API_FCT(get_meta);
API_FCT(sound_play);
API_FCT(sound_stop);
API_FCT(get_protocol_version);
API_FCT(get_server_info);
API_FCT(take_screenshot);
}
4 changes: 2 additions & 2 deletions src/script/lua_api/l_client.h
Expand Up @@ -69,8 +69,8 @@ class ModApiClient : public ModApiBase

static int l_sound_stop(lua_State *L);

// get_protocol_version()
static int l_get_protocol_version(lua_State *L);
// get_server_info()
static int l_get_server_info(lua_State *L);

static int l_take_screenshot(lua_State *L);

Expand Down

0 comments on commit ae0d8f7

Please sign in to comment.