]> git.lizzy.rs Git - minetest.git/blob - src/mods.h
9761a910329a87a94329ef1d290c2587e7e83505
[minetest.git] / src / mods.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef MODS_HEADER
21 #define MODS_HEADER
22
23 #include "irrlichttypes.h"
24 #include <irrList.h>
25 #include <list>
26 #include <set>
27 #include <vector>
28 #include <string>
29 #include <map>
30 #include <exception>
31
32 class ModError : public std::exception
33 {
34 public:
35         ModError(const std::string &s)
36         {
37                 m_s = "ModError: ";
38                 m_s += s;
39         }
40         virtual ~ModError() throw()
41         {}
42         virtual const char * what() const throw()
43         {
44                 return m_s.c_str();
45         }
46         std::string m_s;
47 };
48
49 struct ModSpec
50 {
51         std::string name;
52         std::string path;
53         //if normal mod:
54         std::set<std::string> depends;
55         std::set<std::string> unsatisfied_depends;
56
57         bool is_modpack;
58         // if modpack:
59         std::map<std::string,ModSpec> modpack_content;
60         ModSpec(const std::string name_="", const std::string path_="",
61                         const std::set<std::string> depends_=std::set<std::string>()):
62                 name(name_),
63                 path(path_),
64                 depends(depends_),
65                 unsatisfied_depends(depends_),
66                 is_modpack(false),      
67                 modpack_content()       
68         {}
69 };
70
71
72 std::map<std::string,ModSpec> getModsInPath(std::string path);
73
74 // expands modpack contents, but does not replace them.
75 std::map<std::string, ModSpec> flattenModTree(std::map<std::string, ModSpec> mods);
76
77 // replaces modpack Modspecs with their content
78 std::vector<ModSpec> flattenMods(std::map<std::string,ModSpec> mods);
79
80 // removes Mods mentioned in exclude_mod_names
81 std::vector<ModSpec> filterMods(std::vector<ModSpec> mods,
82                                                                 std::set<std::string> exclude_mod_names);
83
84 // a ModConfiguration is a subset of installed mods, expected to have
85 // all dependencies fullfilled, so it can be used as a list of mods to
86 // load when the game starts.
87 class ModConfiguration
88 {
89 public:
90         ModConfiguration():
91                 m_unsatisfied_mods(),
92                 m_sorted_mods()
93         {}
94
95                 
96         ModConfiguration(std::string worldpath);
97
98         // adds all mods in the given path. used for games, modpacks
99         // and world-specific mods (worldmods-folders)
100         void addModsInPath(std::string path)
101         {
102                 addMods(flattenMods(getModsInPath(path)));
103         }
104
105         // adds all mods in the given path whose name does not appear
106         // in the exclude_mods set. 
107         void addModsInPathFiltered(std::string path, std::set<std::string> exclude_mods);
108
109         // adds all mods in the set.
110         void addMods(std::vector<ModSpec> mods);
111
112         // checks if all dependencies are fullfilled.
113         bool isConsistent()
114         {
115                 return m_unsatisfied_mods.empty();
116         }
117
118         std::vector<ModSpec> getMods()
119         {
120                 return m_sorted_mods;
121         }
122
123         std::list<ModSpec> getUnsatisfiedMods()
124         {
125                 return m_unsatisfied_mods;
126         }
127
128 private:
129
130         // mods with unmet dependencies. This is a list and not a
131         // vector because we want easy removal of elements at every
132         // position.
133         std::list<ModSpec> m_unsatisfied_mods;
134
135         // list of mods sorted such that they can be loaded in the
136         // given order with all dependencies being fullfilled. I.e.,
137         // every mod in this list has only dependencies on mods which
138         // appear earlier in the vector.
139         std::vector<ModSpec> m_sorted_mods;
140
141 };
142
143
144 #endif
145