Readded and optimized mapgen V6
[oweals/minetest.git] / src / mods.h
1 /*
2 Minetest-c55
3 Copyright (C) 2011 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         // if modpack:
57         std::map<std::string,ModSpec> modpack_content;
58         ModSpec(const std::string name_="", const std::string path_="",
59                         const std::set<std::string> depends_=std::set<std::string>()):
60                 name(name_),
61                 path(path_),
62                 depends(depends_),
63                 unsatisfied_depends(depends_),
64                 modpack_content()       
65         {}
66 };
67
68
69 std::map<std::string,ModSpec> getModsInPath(std::string path);
70
71 // expands modpack contents, but does not replace them.
72 std::map<std::string, ModSpec> flattenModTree(std::map<std::string, ModSpec> mods);
73
74 // replaces modpack Modspecs with their content
75 std::vector<ModSpec> flattenMods(std::map<std::string,ModSpec> mods);
76
77 // removes Mods mentioned in exclude_mod_names
78 std::vector<ModSpec> filterMods(std::vector<ModSpec> mods,
79                                                                 std::set<std::string> exclude_mod_names);
80
81 // a ModConfiguration is a subset of installed mods, expected to have
82 // all dependencies fullfilled, so it can be used as a list of mods to
83 // load when the game starts.
84 class ModConfiguration
85 {
86 public:
87         ModConfiguration():
88                 m_unsatisfied_mods(),
89                 m_sorted_mods()
90         {}
91
92                 
93         ModConfiguration(std::string worldpath);
94
95         // adds all mods in the given path. used for games, modpacks
96         // and world-specific mods (worldmods-folders)
97         void addModsInPath(std::string path)
98         {
99                 addMods(flattenMods(getModsInPath(path)));
100         }
101
102         // adds all mods in the given path whose name does not appear
103         // in the exclude_mods set. 
104         void addModsInPathFiltered(std::string path, std::set<std::string> exclude_mods);
105
106         // adds all mods in the set.
107         void addMods(std::vector<ModSpec> mods);
108
109         // checks if all dependencies are fullfilled.
110         bool isConsistent()
111         {
112                 return m_unsatisfied_mods.empty();
113         }
114
115         std::vector<ModSpec> getMods()
116         {
117                 return m_sorted_mods;
118         }
119
120         std::list<ModSpec> getUnsatisfiedMods()
121         {
122                 return m_unsatisfied_mods;
123         }
124
125 private:
126
127         // mods with unmet dependencies. This is a list and not a
128         // vector because we want easy removal of elements at every
129         // position.
130         std::list<ModSpec> m_unsatisfied_mods;
131
132         // list of mods sorted such that they can be loaded in the
133         // given order with all dependencies being fullfilled. I.e.,
134         // every mod in this list has only dependencies on mods which
135         // appear earlier in the vector.
136         std::vector<ModSpec> m_sorted_mods;
137
138 };
139
140
141 #endif
142