Skip to content

Commit 5e9dd16

Browse files
SmallJokersfan5
authored andcommittedJan 29, 2021
RemotePlayer: Remove Settings writer to Files database
1 parent b5956bd commit 5e9dd16

File tree

4 files changed

+115
-144
lines changed

4 files changed

+115
-144
lines changed
 

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

+107-18
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1919

2020
#include <cassert>
2121
#include <json/json.h>
22+
#include "convert_json.h"
2223
#include "database-files.h"
2324
#include "remoteplayer.h"
2425
#include "settings.h"
@@ -36,29 +37,117 @@ PlayerDatabaseFiles::PlayerDatabaseFiles(const std::string &savedir) : m_savedir
3637
fs::CreateDir(m_savedir);
3738
}
3839

39-
void PlayerDatabaseFiles::serialize(std::ostringstream &os, RemotePlayer *player)
40+
void PlayerDatabaseFiles::deSerialize(RemotePlayer *p, std::istream &is,
41+
const std::string &playername, PlayerSAO *sao)
42+
{
43+
Settings args("PlayerArgsEnd");
44+
45+
if (!args.parseConfigLines(is)) {
46+
throw SerializationError("PlayerArgsEnd of player " + playername + " not found!");
47+
}
48+
49+
p->m_dirty = true;
50+
//args.getS32("version"); // Version field value not used
51+
const std::string &name = args.get("name");
52+
strlcpy(p->m_name, name.c_str(), PLAYERNAME_SIZE);
53+
54+
if (sao) {
55+
try {
56+
sao->setHPRaw(args.getU16("hp"));
57+
} catch(SettingNotFoundException &e) {
58+
sao->setHPRaw(PLAYER_MAX_HP_DEFAULT);
59+
}
60+
61+
try {
62+
sao->setBasePosition(args.getV3F("position"));
63+
} catch (SettingNotFoundException &e) {}
64+
65+
try {
66+
sao->setLookPitch(args.getFloat("pitch"));
67+
} catch (SettingNotFoundException &e) {}
68+
try {
69+
sao->setPlayerYaw(args.getFloat("yaw"));
70+
} catch (SettingNotFoundException &e) {}
71+
72+
try {
73+
sao->setBreath(args.getU16("breath"), false);
74+
} catch (SettingNotFoundException &e) {}
75+
76+
try {
77+
const std::string &extended_attributes = args.get("extended_attributes");
78+
std::istringstream iss(extended_attributes);
79+
Json::CharReaderBuilder builder;
80+
builder.settings_["collectComments"] = false;
81+
std::string errs;
82+
83+
Json::Value attr_root;
84+
Json::parseFromStream(builder, iss, &attr_root, &errs);
85+
86+
const Json::Value::Members attr_list = attr_root.getMemberNames();
87+
for (const auto &it : attr_list) {
88+
Json::Value attr_value = attr_root[it];
89+
sao->getMeta().setString(it, attr_value.asString());
90+
}
91+
sao->getMeta().setModified(false);
92+
} catch (SettingNotFoundException &e) {}
93+
}
94+
95+
try {
96+
p->inventory.deSerialize(is);
97+
} catch (SerializationError &e) {
98+
errorstream << "Failed to deserialize player inventory. player_name="
99+
<< name << " " << e.what() << std::endl;
100+
}
101+
102+
if (!p->inventory.getList("craftpreview") && p->inventory.getList("craftresult")) {
103+
// Convert players without craftpreview
104+
p->inventory.addList("craftpreview", 1);
105+
106+
bool craftresult_is_preview = true;
107+
if(args.exists("craftresult_is_preview"))
108+
craftresult_is_preview = args.getBool("craftresult_is_preview");
109+
if(craftresult_is_preview)
110+
{
111+
// Clear craftresult
112+
p->inventory.getList("craftresult")->changeItem(0, ItemStack());
113+
}
114+
}
115+
}
116+
117+
void PlayerDatabaseFiles::serialize(RemotePlayer *p, std::ostream &os)
40118
{
41119
// Utilize a Settings object for storing values
42-
Settings args;
120+
Settings args("PlayerArgsEnd");
43121
args.setS32("version", 1);
44-
args.set("name", player->getName());
122+
args.set("name", p->m_name);
45123

46-
sanity_check(player->getPlayerSAO());
47-
args.setU16("hp", player->getPlayerSAO()->getHP());
48-
args.setV3F("position", player->getPlayerSAO()->getBasePosition());
49-
args.setFloat("pitch", player->getPlayerSAO()->getLookPitch());
50-
args.setFloat("yaw", player->getPlayerSAO()->getRotation().Y);
51-
args.setU16("breath", player->getPlayerSAO()->getBreath());
124+
// This should not happen
125+
assert(m_sao);
126+
args.setU16("hp", p->m_sao->getHP());
127+
args.setV3F("position", p->m_sao->getBasePosition());
128+
args.setFloat("pitch", p->m_sao->getLookPitch());
129+
args.setFloat("yaw", p->m_sao->getRotation().Y);
130+
args.setU16("breath", p->m_sao->getBreath());
52131

53132
std::string extended_attrs;
54-
player->serializeExtraAttributes(extended_attrs);
133+
{
134+
// serializeExtraAttributes
135+
PlayerSAO *sao = p->getPlayerSAO();
136+
assert(sao);
137+
Json::Value json_root;
138+
139+
const StringMap &attrs = sao->getMeta().getStrings();
140+
for (const auto &attr : attrs) {
141+
json_root[attr.first] = attr.second;
142+
}
143+
144+
extended_attrs = fastWriteJson(json_root);
145+
}
55146
args.set("extended_attributes", extended_attrs);
56147

57148
args.writeLines(os);
58149

59-
os << "PlayerArgsEnd\n";
60-
61-
player->inventory.serialize(os);
150+
p->inventory.serialize(os);
62151
}
63152

64153
void PlayerDatabaseFiles::savePlayer(RemotePlayer *player)
@@ -83,7 +172,7 @@ void PlayerDatabaseFiles::savePlayer(RemotePlayer *player)
83172
return;
84173
}
85174

86-
testplayer.deSerialize(is, path, NULL);
175+
deSerialize(&testplayer, is, path, NULL);
87176
is.close();
88177
if (strcmp(testplayer.getName(), player->getName()) == 0) {
89178
path_found = true;
@@ -101,7 +190,7 @@ void PlayerDatabaseFiles::savePlayer(RemotePlayer *player)
101190

102191
// Open and serialize file
103192
std::ostringstream ss(std::ios_base::binary);
104-
serialize(ss, player);
193+
serialize(&testplayer, ss);
105194
if (!fs::safeWriteToFile(path, ss.str())) {
106195
infostream << "Failed to write " << path << std::endl;
107196
}
@@ -121,7 +210,7 @@ bool PlayerDatabaseFiles::removePlayer(const std::string &name)
121210
if (!is.good())
122211
continue;
123212

124-
temp_player.deSerialize(is, path, NULL);
213+
deSerialize(&temp_player, is, path, NULL);
125214
is.close();
126215

127216
if (temp_player.getName() == name) {
@@ -147,7 +236,7 @@ bool PlayerDatabaseFiles::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
147236
if (!is.good())
148237
continue;
149238

150-
player->deSerialize(is, path, sao);
239+
deSerialize(player, is, path, sao);
151240
is.close();
152241

153242
if (player->getName() == player_to_load)
@@ -180,7 +269,7 @@ void PlayerDatabaseFiles::listPlayers(std::vector<std::string> &res)
180269
// Null env & dummy peer_id
181270
PlayerSAO playerSAO(NULL, &player, 15789, false);
182271

183-
player.deSerialize(is, "", &playerSAO);
272+
deSerialize(&player, is, "", &playerSAO);
184273
is.close();
185274

186275
res.emplace_back(player.getName());

Diff for: ‎src/database/database-files.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,14 @@ class PlayerDatabaseFiles : public PlayerDatabase
3838
void listPlayers(std::vector<std::string> &res);
3939

4040
private:
41-
void serialize(std::ostringstream &os, RemotePlayer *player);
41+
void deSerialize(RemotePlayer *p, std::istream &is,
42+
const std::string &playername, PlayerSAO *sao);
43+
/*
44+
serialize() writes a bunch of text that can contain
45+
any characters except a '\0', and such an ending that
46+
deSerialize stops reading exactly at the right point.
47+
*/
48+
void serialize(RemotePlayer *p, std::ostream &os);
4249

4350
std::string m_savedir;
4451
};

Diff for: ‎src/remoteplayer.cpp

-116
Original file line numberDiff line numberDiff line change
@@ -83,122 +83,6 @@ RemotePlayer::RemotePlayer(const char *name, IItemDefManager *idef):
8383
m_star_params = sky_defaults.getStarDefaults();
8484
}
8585

86-
void RemotePlayer::serializeExtraAttributes(std::string &output)
87-
{
88-
assert(m_sao);
89-
Json::Value json_root;
90-
91-
const StringMap &attrs = m_sao->getMeta().getStrings();
92-
for (const auto &attr : attrs) {
93-
json_root[attr.first] = attr.second;
94-
}
95-
96-
output = fastWriteJson(json_root);
97-
}
98-
99-
100-
void RemotePlayer::deSerialize(std::istream &is, const std::string &playername,
101-
PlayerSAO *sao)
102-
{
103-
Settings args;
104-
105-
if (!args.parseConfigLines(is, "PlayerArgsEnd")) {
106-
throw SerializationError("PlayerArgsEnd of player " + playername + " not found!");
107-
}
108-
109-
m_dirty = true;
110-
//args.getS32("version"); // Version field value not used
111-
const std::string &name = args.get("name");
112-
strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE);
113-
114-
if (sao) {
115-
try {
116-
sao->setHPRaw(args.getU16("hp"));
117-
} catch(SettingNotFoundException &e) {
118-
sao->setHPRaw(PLAYER_MAX_HP_DEFAULT);
119-
}
120-
121-
try {
122-
sao->setBasePosition(args.getV3F("position"));
123-
} catch (SettingNotFoundException &e) {}
124-
125-
try {
126-
sao->setLookPitch(args.getFloat("pitch"));
127-
} catch (SettingNotFoundException &e) {}
128-
try {
129-
sao->setPlayerYaw(args.getFloat("yaw"));
130-
} catch (SettingNotFoundException &e) {}
131-
132-
try {
133-
sao->setBreath(args.getU16("breath"), false);
134-
} catch (SettingNotFoundException &e) {}
135-
136-
try {
137-
const std::string &extended_attributes = args.get("extended_attributes");
138-
std::istringstream iss(extended_attributes);
139-
Json::CharReaderBuilder builder;
140-
builder.settings_["collectComments"] = false;
141-
std::string errs;
142-
143-
Json::Value attr_root;
144-
Json::parseFromStream(builder, iss, &attr_root, &errs);
145-
146-
const Json::Value::Members attr_list = attr_root.getMemberNames();
147-
for (const auto &it : attr_list) {
148-
Json::Value attr_value = attr_root[it];
149-
sao->getMeta().setString(it, attr_value.asString());
150-
}
151-
sao->getMeta().setModified(false);
152-
} catch (SettingNotFoundException &e) {}
153-
}
154-
155-
try {
156-
inventory.deSerialize(is);
157-
} catch (SerializationError &e) {
158-
errorstream << "Failed to deserialize player inventory. player_name="
159-
<< name << " " << e.what() << std::endl;
160-
}
161-
162-
if (!inventory.getList("craftpreview") && inventory.getList("craftresult")) {
163-
// Convert players without craftpreview
164-
inventory.addList("craftpreview", 1);
165-
166-
bool craftresult_is_preview = true;
167-
if(args.exists("craftresult_is_preview"))
168-
craftresult_is_preview = args.getBool("craftresult_is_preview");
169-
if(craftresult_is_preview)
170-
{
171-
// Clear craftresult
172-
inventory.getList("craftresult")->changeItem(0, ItemStack());
173-
}
174-
}
175-
}
176-
177-
void RemotePlayer::serialize(std::ostream &os)
178-
{
179-
// Utilize a Settings object for storing values
180-
Settings args;
181-
args.setS32("version", 1);
182-
args.set("name", m_name);
183-
184-
// This should not happen
185-
assert(m_sao);
186-
args.setU16("hp", m_sao->getHP());
187-
args.setV3F("position", m_sao->getBasePosition());
188-
args.setFloat("pitch", m_sao->getLookPitch());
189-
args.setFloat("yaw", m_sao->getRotation().Y);
190-
args.setU16("breath", m_sao->getBreath());
191-
192-
std::string extended_attrs;
193-
serializeExtraAttributes(extended_attrs);
194-
args.set("extended_attributes", extended_attrs);
195-
196-
args.writeLines(os);
197-
198-
os<<"PlayerArgsEnd\n";
199-
200-
inventory.serialize(os);
201-
}
20286

20387
const RemotePlayerChatResult RemotePlayer::canSendChatMessage()
20488
{

Diff for: ‎src/remoteplayer.h

-9
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ class RemotePlayer : public Player
4444
RemotePlayer(const char *name, IItemDefManager *idef);
4545
virtual ~RemotePlayer() = default;
4646

47-
void deSerialize(std::istream &is, const std::string &playername, PlayerSAO *sao);
48-
4947
PlayerSAO *getPlayerSAO() { return m_sao; }
5048
void setPlayerSAO(PlayerSAO *sao) { m_sao = sao; }
5149

@@ -142,13 +140,6 @@ class RemotePlayer : public Player
142140
void onSuccessfulSave();
143141

144142
private:
145-
/*
146-
serialize() writes a bunch of text that can contain
147-
any characters except a '\0', and such an ending that
148-
deSerialize stops reading exactly at the right point.
149-
*/
150-
void serialize(std::ostream &os);
151-
void serializeExtraAttributes(std::string &output);
152143

153144
PlayerSAO *m_sao = nullptr;
154145
bool m_dirty = false;

0 commit comments

Comments
 (0)
Please sign in to comment.