mod_configuration.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Minetest
  3. Copyright (C) 2013-22 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 "mods.h"
  18. /**
  19. * ModConfiguration is a subset of installed mods. This class
  20. * is used to resolve dependencies and return a sorted list of mods.
  21. *
  22. * This class should not be extended from, but instead used as a
  23. * component in other classes.
  24. */
  25. class ModConfiguration
  26. {
  27. public:
  28. /**
  29. * @returns true if all dependencies are fulfilled.
  30. */
  31. inline bool isConsistent() const { return m_unsatisfied_mods.empty(); }
  32. inline const std::vector<ModSpec> &getUnsatisfiedMods() const
  33. {
  34. return m_unsatisfied_mods;
  35. }
  36. /**
  37. * List of mods sorted such that they can be loaded in the
  38. * given order with all dependencies being fulfilled.
  39. *
  40. * I.e: every mod in this list has only dependencies on mods which
  41. * appear earlier in the vector.
  42. */
  43. const std::vector<ModSpec> &getMods() const { return m_sorted_mods; }
  44. std::string getUnsatisfiedModsError() const;
  45. /**
  46. * Adds all mods in the given path. used for games, modpacks
  47. * and world-specific mods (worldmods-folders)
  48. *
  49. * @param path To search, should be absolute
  50. * @param virtual_path Virtual path for this directory, see comment in ModSpec
  51. */
  52. void addModsInPath(const std::string &path, const std::string &virtual_path);
  53. /**
  54. * Adds all mods in `new_mods`
  55. */
  56. void addMods(const std::vector<ModSpec> &new_mods);
  57. /**
  58. * Adds game mods
  59. */
  60. void addGameMods(const SubgameSpec &gamespec);
  61. /**
  62. * Adds mods specified by a world.mt config
  63. *
  64. * @param settings_path Path to world.mt
  65. * @param modPaths Map from virtual name to mod path
  66. */
  67. void addModsFromConfig(const std::string &settings_path,
  68. const std::unordered_map<std::string, std::string> &modPaths);
  69. /**
  70. * Call this function once all mods have been added
  71. */
  72. void checkConflictsAndDeps();
  73. private:
  74. std::vector<ModSpec> m_sorted_mods;
  75. /**
  76. * move mods from m_unsatisfied_mods to m_sorted_mods
  77. * in an order that satisfies dependencies
  78. */
  79. void resolveDependencies();
  80. // mods with unmet dependencies. Before dependencies are resolved,
  81. // this is where all mods are stored. Afterwards this contains
  82. // only the ones with really unsatisfied dependencies.
  83. std::vector<ModSpec> m_unsatisfied_mods;
  84. // set of mod names for which an unresolved name conflict
  85. // exists. A name conflict happens when two or more mods
  86. // at the same level have the same name but different paths.
  87. // Levels (mods in higher levels override mods in lower levels):
  88. // 1. game mod in modpack; 2. game mod;
  89. // 3. world mod in modpack; 4. world mod;
  90. // 5. addon mod in modpack; 6. addon mod.
  91. std::unordered_set<std::string> m_name_conflicts;
  92. };