3 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
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.
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.
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.
21 #include <json/json.h>
22 #include "database-files.h"
23 #include "content_sao.h"
24 #include "remoteplayer.h"
30 // This backend is intended to be used on Minetest 0.4.16 only for the transition backend
33 void PlayerDatabaseFiles::serialize(std::ostringstream &os, RemotePlayer *player)
35 // Utilize a Settings object for storing values
37 args.setS32("version", 1);
38 args.set("name", player->getName());
40 sanity_check(player->getPlayerSAO());
41 args.setS32("hp", player->getPlayerSAO()->getHP());
42 args.setV3F("position", player->getPlayerSAO()->getBasePosition());
43 args.setFloat("pitch", player->getPlayerSAO()->getPitch());
44 args.setFloat("yaw", player->getPlayerSAO()->getYaw());
45 args.setS32("breath", player->getPlayerSAO()->getBreath());
47 std::string extended_attrs = "";
48 player->serializeExtraAttributes(extended_attrs);
49 args.set("extended_attributes", extended_attrs);
53 os << "PlayerArgsEnd\n";
55 player->inventory.serialize(os);
58 void PlayerDatabaseFiles::savePlayer(RemotePlayer *player)
60 std::string savedir = m_savedir + DIR_DELIM;
61 std::string path = savedir + player->getName();
62 bool path_found = false;
63 RemotePlayer testplayer("", NULL);
65 for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES && !path_found; i++) {
66 if (!fs::PathExists(path)) {
71 // Open and deserialize file to check player name
72 std::ifstream is(path.c_str(), std::ios_base::binary);
74 errorstream << "Failed to open " << path << std::endl;
78 testplayer.deSerialize(is, path, NULL);
80 if (strcmp(testplayer.getName(), player->getName()) == 0) {
85 path = savedir + player->getName() + itos(i);
89 errorstream << "Didn't find free file for player " << player->getName()
94 // Open and serialize file
95 std::ostringstream ss(std::ios_base::binary);
96 serialize(ss, player);
97 if (!fs::safeWriteToFile(path, ss.str())) {
98 infostream << "Failed to write " << path << std::endl;
100 player->setModified(false);
103 bool PlayerDatabaseFiles::removePlayer(const std::string &name)
105 std::string players_path = m_savedir + DIR_DELIM;
106 std::string path = players_path + name;
108 RemotePlayer temp_player("", NULL);
109 for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES; i++) {
110 // Open file and deserialize
111 std::ifstream is(path.c_str(), std::ios_base::binary);
115 temp_player.deSerialize(is, path, NULL);
118 if (temp_player.getName() == name) {
119 fs::DeleteSingleFileOrEmptyDirectory(path);
123 path = players_path + name + itos(i);
129 bool PlayerDatabaseFiles::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
131 std::string players_path = m_savedir + DIR_DELIM;
132 std::string path = players_path + player->getName();
134 const std::string player_to_load = player->getName();
135 for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES; i++) {
136 // Open file and deserialize
137 std::ifstream is(path.c_str(), std::ios_base::binary);
141 player->deSerialize(is, path, sao);
144 if (player->getName() == player_to_load)
147 path = players_path + player_to_load + itos(i);
150 infostream << "Player file for player " << player_to_load << " not found" << std::endl;
154 void PlayerDatabaseFiles::listPlayers(std::vector<std::string> &res)
156 std::vector<fs::DirListNode> files = fs::GetDirListing(m_savedir);
157 // list files into players directory
158 for (std::vector<fs::DirListNode>::const_iterator it = files.begin(); it !=
160 // Ignore directories
164 const std::string &filename = it->name;
165 std::string full_path = m_savedir + DIR_DELIM + filename;
166 std::ifstream is(full_path.c_str(), std::ios_base::binary);
170 RemotePlayer player(filename.c_str(), NULL);
171 // Null env & dummy peer_id
172 PlayerSAO playerSAO(NULL, &player, 15789, false);
174 player.deSerialize(is, "", &playerSAO);
177 res.push_back(player.getName());