Skip to content

Commit b0260b5

Browse files
committedNov 11, 2019
Refactor CSM restriction code a bit
This also fixes find_node_near restrictions being ineffective.
1 parent 4d668f3 commit b0260b5

File tree

4 files changed

+27
-32
lines changed

4 files changed

+27
-32
lines changed
 

Diff for: ‎src/client/client.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,7 @@ void Client::removeNode(v3s16 p)
13131313
* @param is_valid_position
13141314
* @return
13151315
*/
1316-
MapNode Client::getNode(v3s16 p, bool *is_valid_position)
1316+
MapNode Client::CSMGetNode(v3s16 p, bool *is_valid_position)
13171317
{
13181318
if (checkCSMRestrictionFlag(CSMRestrictionFlags::CSM_RF_LOOKUP_NODES)) {
13191319
v3s16 ppos = floatToInt(m_env.getLocalPlayer()->getPosition(), BS);
@@ -1325,6 +1325,18 @@ MapNode Client::getNode(v3s16 p, bool *is_valid_position)
13251325
return m_env.getMap().getNode(p, is_valid_position);
13261326
}
13271327

1328+
int Client::CSMClampRadius(v3s16 pos, int radius)
1329+
{
1330+
if (!checkCSMRestrictionFlag(CSMRestrictionFlags::CSM_RF_LOOKUP_NODES))
1331+
return radius;
1332+
// This is approximate and will cause some allowed nodes to be excluded
1333+
v3s16 ppos = floatToInt(m_env.getLocalPlayer()->getPosition(), BS);
1334+
u32 distance = ppos.getDistanceFrom(pos);
1335+
if (distance >= m_csm_restriction_noderange)
1336+
return 0;
1337+
return std::min<int>(radius, m_csm_restriction_noderange - distance);
1338+
}
1339+
13281340
void Client::addNode(v3s16 p, MapNode n, bool remove_metadata)
13291341
{
13301342
//TimeTaker timer1("Client::addNode()");

Diff for: ‎src/client/client.h

+4-8
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,10 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
261261
// Causes urgent mesh updates (unlike Map::add/removeNodeWithEvent)
262262
void removeNode(v3s16 p);
263263

264-
/**
265-
* Helper function for Client Side Modding
266-
* CSM restrictions are applied there, this should not be used for core engine
267-
* @param p
268-
* @param is_valid_position
269-
* @return
270-
*/
271-
MapNode getNode(v3s16 p, bool *is_valid_position);
264+
// helpers to enforce CSM restrictions
265+
MapNode CSMGetNode(v3s16 p, bool *is_valid_position);
266+
int CSMClampRadius(v3s16 pos, int radius);
267+
272268
void addNode(v3s16 p, MapNode n, bool remove_metadata = true);
273269

274270
void setPlayerControl(PlayerControl &control);

Diff for: ‎src/script/lua_api/l_client.cpp

+8-18
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3636
#include "util/string.h"
3737
#include "nodedef.h"
3838

39+
#define checkCSMRestrictionFlag(flag) \
40+
( getClient(L)->checkCSMRestrictionFlag(CSMRestrictionFlags::flag) )
41+
3942
// get_current_modname()
4043
int ModApiClient::l_get_current_modname(lua_State *L)
4144
{
@@ -106,11 +109,8 @@ int ModApiClient::l_send_chat_message(lua_State *L)
106109

107110
// If server disabled this API, discard
108111

109-
// clang-format off
110-
if (getClient(L)->checkCSMRestrictionFlag(
111-
CSMRestrictionFlags::CSM_RF_CHAT_MESSAGES))
112+
if (checkCSMRestrictionFlag(CSM_RF_CHAT_MESSAGES))
112113
return 0;
113-
// clang-format on
114114

115115
std::string message = luaL_checkstring(L, 1);
116116
getClient(L)->sendChatMessage(utf8_to_wide(message));
@@ -127,12 +127,8 @@ int ModApiClient::l_clear_out_chat_queue(lua_State *L)
127127
// get_player_names()
128128
int ModApiClient::l_get_player_names(lua_State *L)
129129
{
130-
// clang-format off
131-
if (getClient(L)->checkCSMRestrictionFlag(
132-
CSMRestrictionFlags::CSM_RF_READ_PLAYERINFO)) {
130+
if (checkCSMRestrictionFlag(CSM_RF_READ_PLAYERINFO))
133131
return 0;
134-
}
135-
// clang-format on
136132

137133
const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
138134
lua_createtable(L, plist.size(), 0);
@@ -201,7 +197,7 @@ int ModApiClient::l_get_node_or_nil(lua_State *L)
201197

202198
// Do it
203199
bool pos_ok;
204-
MapNode n = getClient(L)->getNode(pos, &pos_ok);
200+
MapNode n = getClient(L)->CSMGetNode(pos, &pos_ok);
205201
if (pos_ok) {
206202
// Return node
207203
pushnode(L, n, getClient(L)->ndef());
@@ -308,11 +304,8 @@ int ModApiClient::l_get_item_def(lua_State *L)
308304
IItemDefManager *idef = gdef->idef();
309305
assert(idef);
310306

311-
// clang-format off
312-
if (getClient(L)->checkCSMRestrictionFlag(
313-
CSMRestrictionFlags::CSM_RF_READ_ITEMDEFS))
307+
if (checkCSMRestrictionFlag(CSM_RF_READ_ITEMDEFS))
314308
return 0;
315-
// clang-format on
316309

317310
if (!lua_isstring(L, 1))
318311
return 0;
@@ -339,11 +332,8 @@ int ModApiClient::l_get_node_def(lua_State *L)
339332
if (!lua_isstring(L, 1))
340333
return 0;
341334

342-
// clang-format off
343-
if (getClient(L)->checkCSMRestrictionFlag(
344-
CSMRestrictionFlags::CSM_RF_READ_NODEDEFS))
335+
if (checkCSMRestrictionFlag(CSM_RF_READ_NODEDEFS))
345336
return 0;
346-
// clang-format on
347337

348338
std::string name = readParam<std::string>(L, 1);
349339
const ContentFeatures &cf = ndef->get(ndef->getId(name));

Diff for: ‎src/script/lua_api/l_env.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -769,11 +769,8 @@ int ModApiEnvMod::l_find_node_near(lua_State *L)
769769

770770
#ifndef SERVER
771771
// Client API limitations
772-
if (getClient(L) &&
773-
getClient(L)->checkCSMRestrictionFlag(
774-
CSMRestrictionFlags::CSM_RF_LOOKUP_NODES)) {
775-
radius = std::max<int>(radius, getClient(L)->getCSMNodeRangeLimit());
776-
}
772+
if (getClient(L))
773+
radius = getClient(L)->CSMClampRadius(pos, radius);
777774
#endif
778775

779776
for (int d = start_radius; d <= radius; d++) {

0 commit comments

Comments
 (0)
Please sign in to comment.