]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mods.h
Limit speed in collisionMoveResult for avoiding hangs
[dragonfireclient.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 #include <list>
32
33 class ModError : public std::exception
34 {
35 public:
36         ModError(const std::string &s)
37         {
38                 m_s = "ModError: ";
39                 m_s += s;
40         }
41         virtual ~ModError() throw()
42         {}
43         virtual const char * what() const throw()
44         {
45                 return m_s.c_str();
46         }
47         std::string m_s;
48 };
49
50 struct ModSpec
51 {
52         std::string name;
53         std::string path;
54         //if normal mod:
55         std::set<std::string> depends;
56         std::set<std::string> unsatisfied_depends;
57
58         bool is_modpack;
59         // if modpack:
60         std::map<std::string,ModSpec> modpack_content;
61         ModSpec(const std::string name_="", const std::string path_="",
62                         const std::set<std::string> depends_=std::set<std::string>()):
63                 name(name_),
64                 path(path_),
65                 depends(depends_),
66                 unsatisfied_depends(depends_),
67                 is_modpack(false),      
68                 modpack_content()       
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 #endif