Skip to content

Commit 2c4cf50

Browse files
committedNov 11, 2019
[CSM] Implement minetest.get_csm_restrictions()
fixes #8068
1 parent 3f27156 commit 2c4cf50

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed
 

Diff for: ‎clientmods/preview/init.lua

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ print("Server version: " .. server_info.protocol_version)
1414
print("Server ip: " .. server_info.ip)
1515
print("Server address: " .. server_info.address)
1616
print("Server port: " .. server_info.port)
17+
18+
print("CSM restrictions: " .. dump(core.get_csm_restrictions()))
19+
1720
mod_channel = core.mod_channel_join("experimental_preview")
1821

1922
core.after(4, function()

Diff for: ‎doc/client_lua_api.txt

+5
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,11 @@ Minetest namespace reference
649649
* `minetest.sha1(data, [raw])`: returns the sha1 hash of data
650650
* `data`: string of data to hash
651651
* `raw`: return raw bytes instead of hex digits, default: false
652+
* `minetest.get_csm_restrictions()`: returns a table of `Flags` indicating the
653+
restrictions applied to the current mod.
654+
* If a flag in this table is set to true, the feature is RESTRICTED.
655+
* Possible flags: `load_client_mods`, `chat_messages`, `read_itemdefs`,
656+
`read_nodedefs`, `lookup_nodes`, `read_playerinfo`
652657

653658
### Logging
654659
* `minetest.debug(...)`

Diff for: ‎src/client/client.h

+5
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,11 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
410410
return m_address_name;
411411
}
412412

413+
inline u64 getCSMRestrictionFlags() const
414+
{
415+
return m_csm_restriction_flags;
416+
}
417+
413418
inline bool checkCSMRestrictionFlag(CSMRestrictionFlags flag) const
414419
{
415420
return m_csm_restriction_flags & flag;

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

+35
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3939
#define checkCSMRestrictionFlag(flag) \
4040
( getClient(L)->checkCSMRestrictionFlag(CSMRestrictionFlags::flag) )
4141

42+
// Not the same as FlagDesc, which contains an `u32 flag`
43+
struct CSMFlagDesc {
44+
const char *name;
45+
u64 flag;
46+
};
47+
48+
/*
49+
FIXME: This should eventually be moved somewhere else
50+
It also needs to be kept in sync with the definition of CSMRestrictionFlags
51+
in network/networkprotocol.h
52+
*/
53+
const static CSMFlagDesc flagdesc_csm_restriction[] = {
54+
{"load_client_mods", CSM_RF_LOAD_CLIENT_MODS},
55+
{"chat_messages", CSM_RF_CHAT_MESSAGES},
56+
{"read_itemdefs", CSM_RF_READ_ITEMDEFS},
57+
{"read_nodedefs", CSM_RF_READ_NODEDEFS},
58+
{"lookup_nodes", CSM_RF_LOOKUP_NODES},
59+
{"read_playerinfo", CSM_RF_READ_PLAYERINFO},
60+
{NULL, 0}
61+
};
62+
4263
// get_current_modname()
4364
int ModApiClient::l_get_current_modname(lua_State *L)
4465
{
@@ -363,6 +384,19 @@ int ModApiClient::l_get_builtin_path(lua_State *L)
363384
return 1;
364385
}
365386

387+
// get_csm_restrictions()
388+
int ModApiClient::l_get_csm_restrictions(lua_State *L)
389+
{
390+
u64 flags = getClient(L)->getCSMRestrictionFlags();
391+
const CSMFlagDesc *flagdesc = flagdesc_csm_restriction;
392+
393+
lua_newtable(L);
394+
for (int i = 0; flagdesc[i].name; i++) {
395+
setboolfield(L, -1, flagdesc[i].name, !!(flags & flagdesc[i].flag));
396+
}
397+
return 1;
398+
}
399+
366400
void ModApiClient::Initialize(lua_State *L, int top)
367401
{
368402
API_FCT(get_current_modname);
@@ -389,4 +423,5 @@ void ModApiClient::Initialize(lua_State *L, int top)
389423
API_FCT(get_privilege_list);
390424
API_FCT(get_builtin_path);
391425
API_FCT(get_language);
426+
API_FCT(get_csm_restrictions);
392427
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ class ModApiClient : public ModApiBase
9696
// get_builtin_path()
9797
static int l_get_builtin_path(lua_State *L);
9898

99+
// get_csm_restrictions()
100+
static int l_get_csm_restrictions(lua_State *L);
101+
99102
public:
100103
static void Initialize(lua_State *L, int top);
101104
};

0 commit comments

Comments
 (0)
Please sign in to comment.