Skip to content

Commit ae0d8f7

Browse files
red-001paramat
authored andcommittedMay 4, 2017
Add function to get server info.
1 parent 468eeb6 commit ae0d8f7

File tree

7 files changed

+53
-21
lines changed

7 files changed

+53
-21
lines changed
 

‎clientmods/preview/init.lua

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ end)
88

99
core.register_on_connect(function()
1010
print("[PREVIEW] Player connection completed")
11+
local server_info = core.get_server_info()
12+
print("Server version: " .. server_info.protocol_version)
13+
print("Server ip: " .. server_info.ip)
14+
print("Server address: " .. server_info.address)
15+
print("Server port: " .. server_info.port)
1116
end)
1217

1318
core.register_on_placenode(function(pointed_thing, node)
@@ -80,7 +85,6 @@ core.after(2, function()
8085
print("[PREVIEW] loaded " .. modname .. " mod")
8186
modstorage:set_string("current_mod", modname)
8287
print(modstorage:get_string("current_mod"))
83-
print("Server version:" .. core.get_protocol_version())
8488
preview_minimap()
8589
end)
8690

‎doc/client_lua_api.md

+13-5
Original file line numberDiff line numberDiff line change
@@ -702,11 +702,10 @@ Call these functions only at load time!
702702
* `minetest.disconnect()`
703703
* Disconnect from the server and exit to main menu.
704704
* Returns `false` if the client is already disconnecting otherwise returns `true`.
705-
* `minetest.get_protocol_version()`
706-
* Returns the protocol version of the server.
707-
* 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.
708705
* `minetest.take_screenshot()`
709706
* Take a screenshot.
707+
* `minetest.get_server_info()`
708+
* Returns [server info](#server-info).
710709

711710
### Misc.
712711
* `minetest.parse_json(string[, nullvalue])`: returns something
@@ -932,9 +931,18 @@ Can be obtained via `minetest.get_meta(pos)`.
932931
{
933932
params = "<name> <privilege>", -- Short parameter description
934933
description = "Remove privilege from player", -- Full description
935-
func = function(param), -- Called when command is run.
936-
-- Returns boolean success and text output.
934+
func = function(param), -- Called when command is run.
935+
-- Returns boolean success and text output.
937936
}
937+
### Server info
938+
```lua
939+
{
940+
address = "minetest.example.org", -- The domain name/IP address of a remote server or "" for a local server.
941+
ip = "203.0.113.156", -- The IP address of the server.
Has conversations. Original line has conversations.
942+
port = 30000, -- The port the client is connected to.
943+
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.
944+
}
945+
```
938946

939947
Escape sequences
940948
----------------

‎src/client.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Client::Client(
5858
IrrlichtDevice *device,
5959
const char *playername,
6060
const std::string &password,
61+
const std::string &address_name,
6162
MapDrawControl &control,
6263
IWritableTextureSource *tsrc,
6364
IWritableShaderSource *shsrc,
@@ -89,6 +90,7 @@ Client::Client(
8990
),
9091
m_particle_manager(&m_env),
9192
m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, ipv6, this),
93+
m_address_name(address_name),
9294
m_device(device),
9395
m_camera(NULL),
9496
m_minimap_disabled_by_server(false),
@@ -253,13 +255,11 @@ Client::~Client()
253255
delete m_minimap;
254256
}
255257

256-
void Client::connect(Address address,
257-
const std::string &address_name,
258-
bool is_local_server)
258+
void Client::connect(Address address, bool is_local_server)
259259
{
260260
DSTACK(FUNCTION_NAME);
261261

262-
initLocalMapSaving(address, address_name, is_local_server);
262+
initLocalMapSaving(address, m_address_name, is_local_server);
263263

264264
m_con.SetTimeoutMs(0);
265265
m_con.Connect(address);

‎src/client.h

+13-3
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
257257
IrrlichtDevice *device,
258258
const char *playername,
259259
const std::string &password,
260+
const std::string &address_name,
260261
MapDrawControl &control,
261262
IWritableTextureSource *tsrc,
262263
IWritableShaderSource *shsrc,
@@ -284,9 +285,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
284285
The name of the local player should already be set when
285286
calling this, as it is sent in the initialization.
286287
*/
287-
void connect(Address address,
288-
const std::string &address_name,
289-
bool is_local_server);
288+
void connect(Address address, bool is_local_server);
290289

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

526525
IrrlichtDevice *getDevice() const { return m_device; }
527526

527+
const Address getServerAddress()
528+
{
529+
return m_con.GetPeerAddress(PEER_ID_SERVER);
530+
}
531+
532+
const std::string &getAddressName() const
533+
{
534+
return m_address_name;
535+
}
536+
528537
private:
529538

530539
// Virtual methods from con::PeerHandler
@@ -576,6 +585,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
576585
ClientEnvironment m_env;
577586
ParticleManager m_particle_manager;
578587
con::Connection m_con;
588+
std::string m_address_name;
579589
IrrlichtDevice *m_device;
580590
Camera *m_camera;
581591
Minimap *m_minimap;

‎src/game.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,7 @@ bool Game::connectToServer(const std::string &playername,
20442044
}
20452045

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

2059-
client->connect(connect_address, *address,
2059+
client->connect(connect_address,
20602060
simple_singleplayer_mode || local_server_mode);
20612061

20622062
/*

‎src/script/lua_api/l_client.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,20 @@ int ModApiClient::l_sound_stop(lua_State *L)
234234
return 0;
235235
}
236236

237-
// get_protocol_version()
238-
int ModApiClient::l_get_protocol_version(lua_State *L)
237+
// get_server_info()
238+
int ModApiClient::l_get_server_info(lua_State *L)
239239
{
240-
lua_pushinteger(L, getClient(L)->getProtoVersion());
240+
Client *client = getClient(L);
241+
Address serverAddress = client->getServerAddress();
242+
lua_newtable(L);
243+
lua_pushstring(L, client->getAddressName().c_str());
244+
lua_setfield(L, -2, "address");
245+
lua_pushstring(L, serverAddress.serializeString().c_str());
246+
lua_setfield(L, -2, "ip");
247+
lua_pushinteger(L, serverAddress.getPort());
248+
lua_setfield(L, -2, "port");
249+
lua_pushinteger(L, client->getProtoVersion());
250+
lua_setfield(L, -2, "protocol_version");
241251
return 1;
242252
}
243253

@@ -265,6 +275,6 @@ void ModApiClient::Initialize(lua_State *L, int top)
265275
API_FCT(get_meta);
266276
API_FCT(sound_play);
267277
API_FCT(sound_stop);
268-
API_FCT(get_protocol_version);
278+
API_FCT(get_server_info);
269279
API_FCT(take_screenshot);
270280
}

‎src/script/lua_api/l_client.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ class ModApiClient : public ModApiBase
6969

7070
static int l_sound_stop(lua_State *L);
7171

72-
// get_protocol_version()
73-
static int l_get_protocol_version(lua_State *L);
72+
// get_server_info()
73+
static int l_get_server_info(lua_State *L);
7474

7575
static int l_take_screenshot(lua_State *L);
7676

0 commit comments

Comments
 (0)
Please sign in to comment.