mods.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #pragma once
  17. #include "irrlichttypes.h"
  18. #include <list>
  19. #include <set>
  20. #include <vector>
  21. #include <string>
  22. #include <map>
  23. #include "json-forwards.h"
  24. #include <unordered_set>
  25. #include "util/basic_macros.h"
  26. #include "config.h"
  27. #include "metadata.h"
  28. #include "subgames.h"
  29. class ModStorageDatabase;
  30. #define MODNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyz0123456789_"
  31. struct ModSpec
  32. {
  33. std::string name;
  34. std::string author;
  35. std::string path;
  36. std::string desc;
  37. int release = 0;
  38. // if normal mod:
  39. std::unordered_set<std::string> depends;
  40. std::unordered_set<std::string> optdepends;
  41. std::unordered_set<std::string> unsatisfied_depends;
  42. bool part_of_modpack = false;
  43. bool is_modpack = false;
  44. /**
  45. * A constructed canonical path to represent this mod's location.
  46. * This intended to be used as an identifier for a modpath that tolerates file movement,
  47. * and cannot be used to read the mod files.
  48. *
  49. * Note that `mymod` is the directory name, not the mod name specified in mod.conf.
  50. *
  51. * Ex:
  52. *
  53. * - mods/mymod
  54. * - mods/mymod (1)
  55. * (^ this would have name=mymod in mod.conf)
  56. * - mods/modpack1/mymod
  57. * - games/mygame/mods/mymod
  58. * - worldmods/mymod
  59. */
  60. std::string virtual_path;
  61. // For logging purposes
  62. std::vector<const char *> deprecation_msgs;
  63. // if modpack:
  64. std::map<std::string, ModSpec> modpack_content;
  65. ModSpec()
  66. {
  67. }
  68. ModSpec(const std::string &name, const std::string &path, bool part_of_modpack, const std::string &virtual_path) :
  69. name(name), path(path), part_of_modpack(part_of_modpack), virtual_path(virtual_path)
  70. {
  71. }
  72. void checkAndLog() const;
  73. };
  74. /**
  75. * Retrieves depends, optdepends, is_modpack and modpack_content
  76. *
  77. * @returns false if not a mod
  78. */
  79. bool parseModContents(ModSpec &mod);
  80. /**
  81. * Gets a list of all mods and modpacks in path
  82. *
  83. * @param Path to search, should be absolute
  84. * @param part_of_modpack Is this searching within a modpack?
  85. * @param virtual_path Virtual path for this directory, see comment in ModSpec
  86. * @returns map of mods
  87. */
  88. std::map<std::string, ModSpec> getModsInPath(const std::string &path,
  89. const std::string &virtual_path, bool part_of_modpack = false);
  90. // replaces modpack Modspecs with their content
  91. std::vector<ModSpec> flattenMods(const std::map<std::string, ModSpec> &mods);
  92. class ModStorage : public IMetadata
  93. {
  94. public:
  95. ModStorage() = delete;
  96. ModStorage(const std::string &mod_name, ModStorageDatabase *database);
  97. ~ModStorage() = default;
  98. const std::string &getModName() const { return m_mod_name; }
  99. void clear() override;
  100. bool contains(const std::string &name) const override;
  101. bool setString(const std::string &name, std::string_view var) override;
  102. const StringMap &getStrings(StringMap *place) const override;
  103. const std::vector<std::string> &getKeys(std::vector<std::string> *place) const override;
  104. protected:
  105. const std::string *getStringRaw(const std::string &name,
  106. std::string *place) const override;
  107. private:
  108. std::string m_mod_name;
  109. ModStorageDatabase *m_database;
  110. };