Skip to content

Commit 91615f9

Browse files
rubenwardynerzhul
authored andcommittedApr 6, 2018
Add player:get_meta(), deprecate player attributes (#7202)
* Add player:get_meta(), deprecate player attributes
1 parent 7e3f88f commit 91615f9

22 files changed

+314
-70
lines changed
 

Diff for: ‎build/android/jni/Android.mk

+1
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ LOCAL_SRC_FILES += \
347347
jni/src/script/lua_api/l_nodetimer.cpp \
348348
jni/src/script/lua_api/l_noise.cpp \
349349
jni/src/script/lua_api/l_object.cpp \
350+
jni/src/script/lua_api/l_playermeta.cpp \
350351
jni/src/script/lua_api/l_particles.cpp \
351352
jni/src/script/lua_api/l_particles_local.cpp\
352353
jni/src/script/lua_api/l_rollback.cpp \

Diff for: ‎doc/lua_api.txt

+13-3
Original file line numberDiff line numberDiff line change
@@ -3902,7 +3902,7 @@ An interface to use mod channels on client and server
39023902
* Message size is limited to 65535 characters by protocol.
39033903

39043904
### `MetaDataRef`
3905-
See `StorageRef`, `NodeMetaRef` and `ItemStackMetaRef`.
3905+
See `StorageRef`, `NodeMetaRef`, `ItemStackMetaRef`, and `PlayerMetaRef`.
39063906

39073907
#### Methods
39083908
* `set_string(name, value)`
@@ -3952,6 +3952,15 @@ Can be obtained via `minetest.get_mod_storage()` during load time.
39523952
#### Methods
39533953
* All methods in MetaDataRef
39543954

3955+
### `PlayerMetaRef`
3956+
Player metadata.
3957+
Uses the same method of storage as the deprecated player attribute API, so
3958+
data there will also be in player meta.
3959+
Can be obtained using `player:get_meta()`.
3960+
3961+
#### Methods
3962+
* All methods in MetaDataRef
3963+
39553964
### `NodeTimerRef`
39563965
Node Timers: a high resolution persistent per-node timer.
39573966
Can be gotten via `minetest.get_node_timer(pos)`.
@@ -4101,14 +4110,15 @@ This is basically a reference to a C++ `ServerActiveObject`
41014110
* `0`: player is drowning
41024111
* max: bubbles bar is not shown
41034112
* See Object Properties for more information
4104-
* `set_attribute(attribute, value)`:
4113+
* `set_attribute(attribute, value)`: DEPRECATED, use get_meta() instead
41054114
* Sets an extra attribute with value on player.
41064115
* `value` must be a string, or a number which will be converted to a
41074116
string.
41084117
* If `value` is `nil`, remove attribute from player.
4109-
* `get_attribute(attribute)`:
4118+
* `get_attribute(attribute)`: DEPRECATED, use get_meta() instead
41104119
* Returns value (a string) for extra attribute.
41114120
* Returns `nil` if no attribute found.
4121+
* `get_meta()`: Returns a PlayerMetaRef.
41124122
* `set_inventory_formspec(formspec)`
41134123
* Redefine player's inventory form
41144124
* Should usually be called in `on_joinplayer`

Diff for: ‎games/minimal/mods/test/init.lua

+31-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ assert(pseudo:next() == 13854)
1313
-- HP Change Reasons
1414
--
1515
local expect = nil
16-
minetest.register_on_joinplayer(function(player)
16+
local function run_hpchangereason_tests(player)
1717
expect = { type = "set_hp", from = "mod" }
1818
player:set_hp(3)
1919
assert(expect == nil)
@@ -25,8 +25,7 @@ minetest.register_on_joinplayer(function(player)
2525
expect = { df = 3458973454, type = "fall", from = "mod" }
2626
player:set_hp(10, { type = "fall", df = 3458973454 })
2727
assert(expect == nil)
28-
end)
29-
28+
end
3029
minetest.register_on_player_hpchange(function(player, hp, reason)
3130
for key, value in pairs(reason) do
3231
assert(expect[key] == value)
@@ -38,3 +37,32 @@ minetest.register_on_player_hpchange(function(player, hp, reason)
3837

3938
expect = nil
4039
end)
40+
41+
42+
43+
local function run_player_meta_tests(player)
44+
local meta = player:get_meta()
45+
meta:set_string("foo", "bar")
46+
assert(meta:get_string("foo") == "bar")
47+
48+
local meta2 = player:get_meta()
49+
assert(meta2:get_string("foo") == "bar")
50+
assert(meta:equals(meta2))
51+
assert(player:get_attribute("foo") == "bar")
52+
53+
meta:set_string("bob", "dillan")
54+
assert(meta:get_string("foo") == "bar")
55+
assert(meta:get_string("bob") == "dillan")
56+
assert(meta2:get_string("foo") == "bar")
57+
assert(meta2:get_string("bob") == "dillan")
58+
assert(meta:equals(meta2))
59+
assert(player:get_attribute("foo") == "bar")
60+
assert(player:get_attribute("bob") == "dillan")
61+
end
62+
63+
local function run_player_tests(player)
64+
run_hpchangereason_tests(player)
65+
run_player_meta_tests(player)
66+
minetest.chat_send_all("All tests pass!")
67+
end
68+
minetest.register_on_joinplayer(run_player_tests)

Diff for: ‎src/content_sao.h

+3-46
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ class LagPool
197197
}
198198
};
199199

200-
typedef std::unordered_map<std::string, std::string> PlayerAttributes;
201200
class RemotePlayer;
202201

203202
class PlayerSAO : public UnitSAO
@@ -269,49 +268,6 @@ class PlayerSAO : public UnitSAO
269268
int getWieldIndex() const;
270269
void setWieldIndex(int i);
271270

272-
/*
273-
Modding interface
274-
*/
275-
inline void setExtendedAttribute(const std::string &attr, const std::string &value)
276-
{
277-
m_extra_attributes[attr] = value;
278-
m_extended_attributes_modified = true;
279-
}
280-
281-
inline bool getExtendedAttribute(const std::string &attr, std::string *value)
282-
{
283-
if (m_extra_attributes.find(attr) == m_extra_attributes.end())
284-
return false;
285-
286-
*value = m_extra_attributes[attr];
287-
return true;
288-
}
289-
290-
inline void removeExtendedAttribute(const std::string &attr)
291-
{
292-
PlayerAttributes::iterator it = m_extra_attributes.find(attr);
293-
if (it == m_extra_attributes.end())
294-
return;
295-
296-
m_extra_attributes.erase(it);
297-
m_extended_attributes_modified = true;
298-
}
299-
300-
inline const PlayerAttributes &getExtendedAttributes()
301-
{
302-
return m_extra_attributes;
303-
}
304-
305-
inline bool extendedAttributesModified() const
306-
{
307-
return m_extended_attributes_modified;
308-
}
309-
310-
inline void setExtendedAttributeModified(bool v)
311-
{
312-
m_extended_attributes_modified = v;
313-
}
314-
315271
/*
316272
PlayerSAO-specific
317273
*/
@@ -375,6 +331,8 @@ class PlayerSAO : public UnitSAO
375331
v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
376332
v3f getEyeOffset() const;
377333

334+
inline Metadata &getMeta() { return m_meta; }
335+
378336
private:
379337
std::string getPropertyPacket();
380338
void unlinkPlayerSessionAndSave();
@@ -410,8 +368,7 @@ class PlayerSAO : public UnitSAO
410368
f32 m_fov = 0.0f;
411369
s16 m_wanted_range = 0.0f;
412370

413-
PlayerAttributes m_extra_attributes;
414-
bool m_extended_attributes_modified = false;
371+
Metadata m_meta;
415372
public:
416373
float m_physics_override_speed = 1.0f;
417374
float m_physics_override_jump = 1.0f;

Diff for: ‎src/database/database-postgresql.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player)
518518
}
519519

520520
execPrepared("remove_player_metadata", 1, rmvalues);
521-
const PlayerAttributes &attrs = sao->getExtendedAttributes();
521+
const StringMap &attrs = sao->getMeta().getStrings();
522522
for (const auto &attr : attrs) {
523523
const char *meta_values[] = {
524524
player->getName(),
@@ -527,6 +527,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player)
527527
};
528528
execPrepared("save_player_metadata", 3, meta_values);
529529
}
530+
sao->getMeta().setModified(false);
530531
endSave();
531532
}
532533

@@ -594,8 +595,9 @@ bool PlayerDatabasePostgreSQL::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
594595

595596
int numrows = PQntuples(results);
596597
for (int row = 0; row < numrows; row++) {
597-
sao->setExtendedAttribute(PQgetvalue(results, row, 0),PQgetvalue(results, row, 1));
598+
sao->getMeta().setString(PQgetvalue(results, row, 0), PQgetvalue(results, row, 1));
598599
}
600+
sao->getMeta().setModified(false);
599601

600602
PQclear(results);
601603

Diff for: ‎src/database/database-sqlite3.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -520,14 +520,15 @@ void PlayerDatabaseSQLite3::savePlayer(RemotePlayer *player)
520520
sqlite3_vrfy(sqlite3_step(m_stmt_player_metadata_remove), SQLITE_DONE);
521521
sqlite3_reset(m_stmt_player_metadata_remove);
522522

523-
const PlayerAttributes &attrs = sao->getExtendedAttributes();
523+
const StringMap &attrs = sao->getMeta().getStrings();
524524
for (const auto &attr : attrs) {
525525
str_to_sqlite(m_stmt_player_metadata_add, 1, player->getName());
526526
str_to_sqlite(m_stmt_player_metadata_add, 2, attr.first);
527527
str_to_sqlite(m_stmt_player_metadata_add, 3, attr.second);
528528
sqlite3_vrfy(sqlite3_step(m_stmt_player_metadata_add), SQLITE_DONE);
529529
sqlite3_reset(m_stmt_player_metadata_add);
530530
}
531+
sao->getMeta().setModified(false);
531532

532533
endSave();
533534
}
@@ -578,8 +579,9 @@ bool PlayerDatabaseSQLite3::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
578579
std::string attr = sqlite_to_string(m_stmt_player_metadata_load, 0);
579580
std::string value = sqlite_to_string(m_stmt_player_metadata_load, 1);
580581

581-
sao->setExtendedAttribute(attr, value);
582+
sao->getMeta().setString(attr, value);
582583
}
584+
sao->getMeta().setModified(false);
583585
sqlite3_reset(m_stmt_player_metadata_load);
584586
return true;
585587
}

Diff for: ‎src/itemstackmetadata.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/*
2+
Minetest
3+
Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.com>
4+
5+
This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU Lesser General Public License as published by
7+
the Free Software Foundation; either version 2.1 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public License along
16+
with this program; if not, write to the Free Software Foundation, Inc.,
17+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
20+
121
#include "itemstackmetadata.h"
222
#include "util/serialize.h"
323
#include "util/strfnd.h"

Diff for: ‎src/itemstackmetadata.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Minetest
3-
Copyright (C) 2010-2013 rubenwardy <rubenwardy@gmail.com>
3+
Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.com>
44
55
This program is free software; you can redistribute it and/or modify
66
it under the terms of the GNU Lesser General Public License as published by

Diff for: ‎src/metadata.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2727
void Metadata::clear()
2828
{
2929
m_stringvars.clear();
30+
m_modified = true;
3031
}
3132

3233
bool Metadata::empty() const
@@ -68,6 +69,18 @@ const std::string &Metadata::getString(const std::string &name, u16 recursion) c
6869
return resolveString(it->second, recursion);
6970
}
7071

72+
bool Metadata::getStringToRef(
73+
const std::string &name, std::string &str, u16 recursion) const
74+
{
75+
StringMap::const_iterator it = m_stringvars.find(name);
76+
if (it == m_stringvars.end()) {
77+
return false;
78+
}
79+
80+
str = resolveString(it->second, recursion);
81+
return true;
82+
}
83+
7184
/**
7285
* Sets var to name key in the metadata storage
7386
*
@@ -88,6 +101,7 @@ bool Metadata::setString(const std::string &name, const std::string &var)
88101
}
89102

90103
m_stringvars[name] = var;
104+
m_modified = true;
91105
return true;
92106
}
93107

Diff for: ‎src/metadata.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2626

2727
class Metadata
2828
{
29+
bool m_modified = false;
2930
public:
3031
virtual ~Metadata() = default;
3132

@@ -45,14 +46,18 @@ class Metadata
4546
size_t size() const;
4647
bool contains(const std::string &name) const;
4748
const std::string &getString(const std::string &name, u16 recursion = 0) const;
49+
bool getStringToRef(const std::string &name, std::string &str, u16 recursion = 0) const;
4850
virtual bool setString(const std::string &name, const std::string &var);
51+
inline bool removeString(const std::string &name) { return setString(name, ""); }
4952
const StringMap &getStrings() const
5053
{
5154
return m_stringvars;
5255
}
5356
// Add support for variable names in values
5457
const std::string &resolveString(const std::string &str, u16 recursion = 0) const;
58+
59+
inline bool isModified() const { return m_modified; }
60+
inline void setModified(bool v) { m_modified = v; }
5561
protected:
5662
StringMap m_stringvars;
57-
5863
};

Diff for: ‎src/remoteplayer.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,15 @@ void RemotePlayer::serializeExtraAttributes(std::string &output)
7272
{
7373
assert(m_sao);
7474
Json::Value json_root;
75-
const PlayerAttributes &attrs = m_sao->getExtendedAttributes();
75+
76+
const StringMap &attrs = m_sao->getMeta().getStrings();
7677
for (const auto &attr : attrs) {
7778
json_root[attr.first] = attr.second;
7879
}
7980

8081
output = fastWriteJson(json_root);
8182

82-
m_sao->setExtendedAttributeModified(false);
83+
m_sao->getMeta().setModified(false);
8384
}
8485

8586

@@ -132,8 +133,9 @@ void RemotePlayer::deSerialize(std::istream &is, const std::string &playername,
132133
const Json::Value::Members attr_list = attr_root.getMemberNames();
133134
for (const auto &it : attr_list) {
134135
Json::Value attr_value = attr_root[it];
135-
sao->setExtendedAttribute(it, attr_value.asString());
136+
sao->getMeta().setString(it, attr_value.asString());
136137
}
138+
sao->getMeta().setModified(false);
137139
} catch (SettingNotFoundException &e) {}
138140
}
139141

Diff for: ‎src/script/lua_api/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ set(common_SCRIPT_LUA_API_SRCS
1515
${CMAKE_CURRENT_SOURCE_DIR}/l_noise.cpp
1616
${CMAKE_CURRENT_SOURCE_DIR}/l_object.cpp
1717
${CMAKE_CURRENT_SOURCE_DIR}/l_particles.cpp
18+
${CMAKE_CURRENT_SOURCE_DIR}/l_playermeta.cpp
1819
${CMAKE_CURRENT_SOURCE_DIR}/l_rollback.cpp
1920
${CMAKE_CURRENT_SOURCE_DIR}/l_server.cpp
2021
${CMAKE_CURRENT_SOURCE_DIR}/l_settings.cpp

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

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
22
Minetest
33
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4+
Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.com>
5+
Copyright (C) 2017 raymoo
46
57
This program is free software; you can redistribute it and/or modify
68
it under the terms of the GNU Lesser General Public License as published by

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

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
22
Minetest
33
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4+
Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.com>
5+
Copyright (C) 2017 raymoo
46
57
This program is free software; you can redistribute it and/or modify
68
it under the terms of the GNU Lesser General Public License as published by

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
Minetest
33
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4+
Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.com>
45
56
This program is free software; you can redistribute it and/or modify
67
it under the terms of the GNU Lesser General Public License as published by

0 commit comments

Comments
 (0)
Please sign in to comment.