File: ModManager.h

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (175 lines) | stat: -rw-r--r-- 5,458 bytes parent folder | download
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
/*
 * ModManager.h, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */
#pragma once

#include "../json/JsonNode.h"

VCMI_LIB_NAMESPACE_BEGIN

class JsonNode;
class ModDescription;
struct CModVersion;

using TModID = std::string;
using TModList = std::vector<TModID>;
using TModSet = std::set<TModID>;

/// Provides interface to access list of locally installed mods
class ModsState : boost::noncopyable
{
	TModList modList;

	TModList scanModsDirectory(const std::string & modDir) const;

public:
	ModsState();

	TModList getInstalledMods() const;
	double getInstalledModSizeMegabytes(const TModID & modName) const;

	uint32_t computeChecksum(const TModID & modName) const;
};

/// Provides interface to access or change current mod preset
class ModsPresetState : boost::noncopyable
{
	JsonNode modConfig;

	void createInitialPreset();
	void importInitialPreset();

	const JsonNode & getActivePresetConfig() const;

public:
	ModsPresetState();

	void createNewPreset(const std::string & presetName);
	void deletePreset(const std::string & presetName);
	void activatePreset(const std::string & presetName);
	void renamePreset(const std::string & oldPresetName, const std::string & newPresetName);

	std::vector<std::string> getAllPresets() const;
	std::string getActivePreset() const;

	JsonNode exportCurrentPreset() const;

	/// Imports preset from provided json
	/// Returns name of imported preset on success
	std::string importPreset(const JsonNode & data);

	void setModActive(const TModID & modName, bool isActive);

	void addRootMod(const TModID & modName);
	void eraseRootMod(const TModID & modName);
	void removeOldMods(const TModList & modsToKeep);

	void setSettingActive(const TModID & modName, const TModID & settingName, bool isActive);
	void eraseModSetting(const TModID & modName, const TModID & settingName);

	/// Returns list of all mods active in current preset. Mod order is unspecified
	TModList getActiveMods() const;

	/// Returns list of currently active root mods (non-submod)
	TModList getActiveRootMods() const;
	/// Returns list of root mods present in specified preset
	TModList getRootMods(const std::string & presetName) const;

	/// Returns list of all known settings (submods) for a specified mod
	std::map<TModID, bool> getModSettings(const TModID & modID) const;
	std::optional<uint32_t> getValidatedChecksum(const TModID & modName) const;
	void setValidatedChecksum(const TModID & modName, std::optional<uint32_t> value);

	void saveConfigurationState() const;
};

/// Provides access to mod properties
class ModsStorage : boost::noncopyable
{
	std::map<TModID, ModDescription> mods;

public:
	ModsStorage(const TModList & modsToLoad, const JsonNode & repositoryList);

	const ModDescription & getMod(const TModID & fullID) const;

	TModList getAllMods() const;
};

class ModDependenciesResolver : boost::noncopyable
{
	/// all currently active mods, in their load order
	TModList activeMods;

	/// Mods from current preset that failed to load due to invalid dependencies
	TModList brokenMods;

public:
	ModDependenciesResolver(const TModList & modsToResolve, const ModsStorage & storage);

	void tryAddMods(TModList modsToResolve, const ModsStorage & storage);

	const TModList & getActiveMods() const;
	const TModList & getBrokenMods() const;
};

/// Provides public interface to access mod state
class DLL_LINKAGE ModManager : boost::noncopyable
{
	std::unique_ptr<ModsState> modsState;
	std::unique_ptr<ModsPresetState> modsPreset;
	std::unique_ptr<ModsStorage> modsStorage;
	std::unique_ptr<ModDependenciesResolver> depedencyResolver;

	void generateLoadOrder(TModList desiredModList);
	void eraseMissingModsFromPreset();
	void addNewModsToPreset();
	void updatePreset(const ModDependenciesResolver & newData);

	TModList getInstalledValidMods() const;
	TModList collectDependenciesRecursive(const TModID & modID) const;

	void tryEnableMod(const TModID & modList);

public:
	ModManager(const JsonNode & repositoryList);
	ModManager();
	~ModManager();

	const ModDescription & getModDescription(const TModID & modID) const;
	const TModList & getActiveMods() const;
	TModList getAllMods() const;

	bool isModSettingActive(const TModID & rootModID, const TModID & modSettingID) const;
	bool isModActive(const TModID & modID) const;
	uint32_t computeChecksum(const TModID & modName) const;
	std::optional<uint32_t> getValidatedChecksum(const TModID & modName) const;
	void setValidatedChecksum(const TModID & modName, std::optional<uint32_t> value);
	void saveConfigurationState() const;
	double getInstalledModSizeMegabytes(const TModID & modName) const;

	void tryEnableMods(const TModList & modList);
	void tryDisableMod(const TModID & modName);

	void createNewPreset(const std::string & presetName);
	void deletePreset(const std::string & presetName);
	void activatePreset(const std::string & presetName);
	void renamePreset(const std::string & oldPresetName, const std::string & newPresetName);

	std::vector<std::string> getAllPresets() const;
	std::string getActivePreset() const;

	JsonNode exportCurrentPreset() const;

	/// Imports preset from provided json
	/// Returns name of imported preset and list of mods that must be installed to activate preset
	std::tuple<std::string, TModList> importPreset(const JsonNode & data);
};

VCMI_LIB_NAMESPACE_END