database-dummy.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. /*
  17. Dummy database class
  18. */
  19. #include "database-dummy.h"
  20. #include "remoteplayer.h"
  21. bool Database_Dummy::saveBlock(const v3s16 &pos, std::string_view data)
  22. {
  23. m_database[getBlockAsInteger(pos)] = data;
  24. return true;
  25. }
  26. void Database_Dummy::loadBlock(const v3s16 &pos, std::string *block)
  27. {
  28. s64 i = getBlockAsInteger(pos);
  29. auto it = m_database.find(i);
  30. if (it == m_database.end()) {
  31. block->clear();
  32. return;
  33. }
  34. *block = it->second;
  35. }
  36. bool Database_Dummy::deleteBlock(const v3s16 &pos)
  37. {
  38. m_database.erase(getBlockAsInteger(pos));
  39. return true;
  40. }
  41. void Database_Dummy::listAllLoadableBlocks(std::vector<v3s16> &dst)
  42. {
  43. dst.reserve(m_database.size());
  44. for (std::map<s64, std::string>::const_iterator x = m_database.begin();
  45. x != m_database.end(); ++x) {
  46. dst.push_back(getIntegerAsBlock(x->first));
  47. }
  48. }
  49. void Database_Dummy::savePlayer(RemotePlayer *player)
  50. {
  51. m_player_database.insert(player->getName());
  52. }
  53. bool Database_Dummy::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
  54. {
  55. return m_player_database.find(player->getName()) != m_player_database.end();
  56. }
  57. bool Database_Dummy::removePlayer(const std::string &name)
  58. {
  59. m_player_database.erase(name);
  60. return true;
  61. }
  62. void Database_Dummy::listPlayers(std::vector<std::string> &res)
  63. {
  64. for (const auto &player : m_player_database) {
  65. res.emplace_back(player);
  66. }
  67. }
  68. void Database_Dummy::getModEntries(const std::string &modname, StringMap *storage)
  69. {
  70. const auto mod_pair = m_mod_storage_database.find(modname);
  71. if (mod_pair != m_mod_storage_database.cend()) {
  72. for (const auto &pair : mod_pair->second) {
  73. (*storage)[pair.first] = pair.second;
  74. }
  75. }
  76. }
  77. void Database_Dummy::getModKeys(const std::string &modname, std::vector<std::string> *storage)
  78. {
  79. const auto mod_pair = m_mod_storage_database.find(modname);
  80. if (mod_pair != m_mod_storage_database.cend()) {
  81. storage->reserve(storage->size() + mod_pair->second.size());
  82. for (const auto &pair : mod_pair->second)
  83. storage->push_back(pair.first);
  84. }
  85. }
  86. bool Database_Dummy::getModEntry(const std::string &modname,
  87. const std::string &key, std::string *value)
  88. {
  89. auto mod_pair = m_mod_storage_database.find(modname);
  90. if (mod_pair == m_mod_storage_database.end())
  91. return false;
  92. const StringMap &meta = mod_pair->second;
  93. auto pair = meta.find(key);
  94. if (pair != meta.end()) {
  95. *value = pair->second;
  96. return true;
  97. }
  98. return false;
  99. }
  100. bool Database_Dummy::hasModEntry(const std::string &modname, const std::string &key)
  101. {
  102. auto mod_pair = m_mod_storage_database.find(modname);
  103. if (mod_pair == m_mod_storage_database.end())
  104. return false;
  105. const StringMap &meta = mod_pair->second;
  106. return meta.find(key) != meta.cend();
  107. }
  108. bool Database_Dummy::setModEntry(const std::string &modname,
  109. const std::string &key, std::string_view value)
  110. {
  111. auto mod_pair = m_mod_storage_database.find(modname);
  112. if (mod_pair == m_mod_storage_database.end()) {
  113. auto &map = m_mod_storage_database[modname];
  114. map[key] = value;
  115. } else {
  116. mod_pair->second[key] = value;
  117. }
  118. return true;
  119. }
  120. bool Database_Dummy::removeModEntry(const std::string &modname, const std::string &key)
  121. {
  122. auto mod_pair = m_mod_storage_database.find(modname);
  123. if (mod_pair != m_mod_storage_database.end())
  124. return mod_pair->second.erase(key) > 0;
  125. return false;
  126. }
  127. bool Database_Dummy::removeModEntries(const std::string &modname)
  128. {
  129. auto mod_pair = m_mod_storage_database.find(modname);
  130. if (mod_pair != m_mod_storage_database.end() && !mod_pair->second.empty()) {
  131. mod_pair->second.clear();
  132. return true;
  133. }
  134. return false;
  135. }
  136. void Database_Dummy::listMods(std::vector<std::string> *res)
  137. {
  138. for (const auto &pair : m_mod_storage_database) {
  139. res->push_back(pair.first);
  140. }
  141. }