forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_manager.h
223 lines (180 loc) · 6.41 KB
/
mod_manager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#pragma once
#ifndef CATA_SRC_MOD_MANAGER_H
#define CATA_SRC_MOD_MANAGER_H
#include <cstddef>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "pimpl.h"
#include "ret_val.h"
#include "type_id.h"
struct WORLD;
using WORLDPTR = WORLD *;
class JsonObject;
class dependency_tree;
class mod_manager;
const std::vector<std::pair<std::string, std::string> > &get_mod_list_categories();
const std::vector<std::pair<std::string, std::string> > &get_mod_list_tabs();
const std::map<std::string, std::string> &get_mod_list_cat_tab();
struct translatable_mod_info {
private:
std::string mod_path;
std::string name_raw;
std::string name_tr;
std::string description_raw;
std::string description_tr;
int language_version = 0;
void update();
public:
translatable_mod_info();
translatable_mod_info( std::string name, std::string description, std::string path );
std::string name();
std::string description();
};
struct MOD_INFORMATION {
private:
mutable translatable_mod_info translatable_info;
public:
std::string name() const;
std::string description() const;
inline void set_translatable_info( translatable_mod_info &&tmi ) {
translatable_info = std::move( tmi );
}
mod_id ident;
/** Directory to load JSON from relative to directory containing modinfo.json */
std::string path;
/** Full path to modinfo.json, for debug purposes */
std::string path_full;
/** All authors who have added content to the mod (excluding maintenance changes) */
std::set<std::string> authors;
/**
* Assigned maintainers responsible for maintaining compatibility with core
* @note these should be verbatim GH account names
*/
std::set<std::string> maintainers;
/**
* Arbitrary string that should help maintainers in figuring out
* what version of the mod the error in a bugreport comes from.
* Recommended use is to set latest mod update date here.
*/
std::string version;
/** What other mods must be loaded prior to this one? */
std::vector<mod_id> dependencies;
/** What mods cannot be loaded together with this one? */
std::vector<mod_id> conflicts;
/** Core mods are loaded before any other mods */
bool core = false;
/** Obsolete mods are loaded for legacy saves but cannot be used when starting new worlds */
bool obsolete = false;
std::pair<int, std::string> category = { -1, "" };
};
namespace mod_management
{
using t_mod_list = std::vector<mod_id>;
/**
* Load all modinfo.json files (recursively) from the given root.
* @param path The root folder from which the modinfo files are searched.
*/
std::vector<MOD_INFORMATION> load_mods_from( const std::string &path );
/**
* Load all mod information from a json file.
* (@see load_modfile)
*/
void load_mod_info( const std::string &info_file_path, std::vector<MOD_INFORMATION> &out );
/**
* Load mod info from a json object.
* @throws JsonError on all kind of errors.
*/
std::optional<MOD_INFORMATION> load_modfile( const JsonObject &jo, const std::string &path );
/**
* Save mod list to file.
* @returns true on success.
*/
bool save_mod_list( const t_mod_list &list, const std::string &path );
/**
* Load mod list from file.
* @returns std::nullopt on error.
*/
std::optional<t_mod_list> load_mod_list( const std::string &path );
/**
* Get id of default core content pack.
*/
mod_id get_default_core_content_pack();
} // namespace mod_management
class mod_manager
{
public:
using t_mod_list = mod_management::t_mod_list;
mod_manager();
~mod_manager();
/**
* Reload the map of available mods (@ref mod_map).
* This also reloads the dependency tree.
*/
void refresh_mod_list();
std::vector<mod_id> all_mods() const;
/**
* Returns the dependency tree for the loaded mods.
* @returns @ref dependency_tree
*/
dependency_tree &get_tree();
/**
* Clear @ref mod_map and delete @ref dependency_tree.
*/
void clear();
/**
* Save list of mods that are active in that world to
* the world folder.
*/
void save_mods_list( WORLDPTR world ) const;
/**
* Load list of mods that should be active in that
* world.
*/
void load_mods_list( WORLDPTR world ) const;
const t_mod_list &get_default_mods() const;
bool set_default_mods( const t_mod_list &mods );
std::vector<mod_id> get_all_sorted() const;
private:
// Make this accessible for now
friend class mod_ui;
friend class worldfactory;
friend mod_id;
/**
* @returns path of a file in the world folder that contains
* the list of mods that should be loaded for this world.
*/
static std::string get_mods_list_file( WORLDPTR world );
/**
* Add mods from given list to the pool.
* Mods with same id are overwritten.
*/
void add_mods( std::vector<MOD_INFORMATION> &&list );
void remove_mod( const mod_id &ident );
void remove_invalid_mods( t_mod_list &mods ) const;
void load_replacement_mods( const std::string &path );
pimpl<dependency_tree> tree;
/**
* The map of known mods, key is the mod ident.
*/
std::map<mod_id, MOD_INFORMATION> mod_map;
t_mod_list default_mods;
/** Second field is optional replacement mod */
std::map<mod_id, mod_id> mod_replacements;
};
class mod_ui
{
public:
mod_ui( mod_manager &mman );
std::string get_information( const MOD_INFORMATION *mod );
mod_manager &active_manager;
dependency_tree &mm_tree;
ret_val<bool> try_add( const mod_id &mod_to_add, std::vector<mod_id> &active_list );
void try_rem( size_t selection, std::vector<mod_id> &active_list );
void try_shift( char direction, size_t &selection, std::vector<mod_id> &active_list );
bool can_shift_up( size_t selection, const std::vector<mod_id> &active_list );
bool can_shift_down( size_t selection, const std::vector<mod_id> &active_list );
};
#endif // CATA_SRC_MOD_MANAGER_H