database-files.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Minetest
  3. Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
  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. #pragma once
  17. // !!! WARNING !!!
  18. // This backend is intended to be used on Minetest 0.4.16 only for the transition backend
  19. // for player files
  20. #include "database.h"
  21. #include <unordered_map>
  22. #include <unordered_set>
  23. #include <json/json.h> // for Json::Value
  24. class PlayerDatabaseFiles : public PlayerDatabase
  25. {
  26. public:
  27. PlayerDatabaseFiles(const std::string &savedir);
  28. virtual ~PlayerDatabaseFiles() = default;
  29. void savePlayer(RemotePlayer *player);
  30. bool loadPlayer(RemotePlayer *player, PlayerSAO *sao);
  31. bool removePlayer(const std::string &name);
  32. void listPlayers(std::vector<std::string> &res);
  33. private:
  34. void deSerialize(RemotePlayer *p, std::istream &is, const std::string &playername,
  35. PlayerSAO *sao);
  36. /*
  37. serialize() writes a bunch of text that can contain
  38. any characters except a '\0', and such an ending that
  39. deSerialize stops reading exactly at the right point.
  40. */
  41. void serialize(RemotePlayer *p, std::ostream &os);
  42. std::string m_savedir;
  43. };
  44. class AuthDatabaseFiles : public AuthDatabase
  45. {
  46. public:
  47. AuthDatabaseFiles(const std::string &savedir);
  48. virtual ~AuthDatabaseFiles() = default;
  49. virtual bool getAuth(const std::string &name, AuthEntry &res);
  50. virtual bool saveAuth(const AuthEntry &authEntry);
  51. virtual bool createAuth(AuthEntry &authEntry);
  52. virtual bool deleteAuth(const std::string &name);
  53. virtual void listNames(std::vector<std::string> &res);
  54. virtual void reload();
  55. private:
  56. std::unordered_map<std::string, AuthEntry> m_auth_list;
  57. std::string m_savedir;
  58. bool readAuthFile();
  59. bool writeAuthFile();
  60. };
  61. class ModStorageDatabaseFiles : public ModStorageDatabase
  62. {
  63. public:
  64. ModStorageDatabaseFiles(const std::string &savedir);
  65. virtual ~ModStorageDatabaseFiles() = default;
  66. virtual void getModEntries(const std::string &modname, StringMap *storage);
  67. virtual void getModKeys(const std::string &modname, std::vector<std::string> *storage);
  68. virtual bool getModEntry(const std::string &modname,
  69. const std::string &key, std::string *value);
  70. virtual bool hasModEntry(const std::string &modname, const std::string &key);
  71. virtual bool setModEntry(const std::string &modname,
  72. const std::string &key, std::string_view value);
  73. virtual bool removeModEntry(const std::string &modname, const std::string &key);
  74. virtual bool removeModEntries(const std::string &modname);
  75. virtual void listMods(std::vector<std::string> *res);
  76. virtual void beginSave();
  77. virtual void endSave();
  78. private:
  79. Json::Value *getOrCreateJson(const std::string &modname);
  80. bool writeJson(const std::string &modname, const Json::Value &json);
  81. std::string m_storage_dir;
  82. std::unordered_map<std::string, Json::Value> m_mod_storage;
  83. std::unordered_set<std::string> m_modified;
  84. };