2012-08-10 18:16:42 +03:00
|
|
|
#pragma once
|
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
#include "filesystem/Filesystem.h"
|
2012-08-10 16:07:53 +03:00
|
|
|
|
|
|
|
#include "VCMI_Lib.h"
|
2013-04-21 15:49:26 +03:00
|
|
|
#include "JsonNode.h"
|
2012-08-10 16:07:53 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CModHandler.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
class CModHandler;
|
|
|
|
class CModIndentifier;
|
|
|
|
class CModInfo;
|
2012-11-13 14:52:23 +03:00
|
|
|
class JsonNode;
|
2013-04-21 15:49:26 +03:00
|
|
|
class IHandlerBase;
|
2012-08-10 16:07:53 +03:00
|
|
|
|
2012-12-03 19:00:17 +03:00
|
|
|
/// class that stores all object identifiers strings and maps them to numeric ID's
|
|
|
|
/// if possible, objects ID's should be in format <type>.<name>, camelCase e.g. "creature.grandElf"
|
|
|
|
class CIdentifierStorage
|
|
|
|
{
|
2013-04-25 17:03:35 +03:00
|
|
|
struct ObjectCallback // entry created on ID request
|
|
|
|
{
|
|
|
|
std::string localScope; /// scope from which this ID was requested
|
|
|
|
std::string remoteScope; /// scope in which this object must be found
|
|
|
|
std::string type; /// type, e.g. creature, faction, hero, etc
|
|
|
|
std::string name; /// string ID
|
2013-06-26 14:18:27 +03:00
|
|
|
std::function<void(si32)> callback;
|
2013-12-02 14:58:02 +03:00
|
|
|
bool optional;
|
2013-04-25 17:03:35 +03:00
|
|
|
|
2013-12-02 14:58:02 +03:00
|
|
|
ObjectCallback(std::string localScope, std::string remoteScope, std::string type, std::string name, const std::function<void(si32)> & callback, bool optional);
|
2013-04-25 17:03:35 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ObjectData // entry created on ID registration
|
|
|
|
{
|
|
|
|
si32 id;
|
|
|
|
std::string scope; /// scope in which this ID located
|
2014-02-02 17:31:56 +03:00
|
|
|
|
|
|
|
|
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
2014-02-19 04:04:27 +03:00
|
|
|
h & id & scope;
|
2014-02-02 17:31:56 +03:00
|
|
|
}
|
2013-04-25 17:03:35 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
std::multimap<std::string, ObjectData > registeredObjects;
|
|
|
|
std::vector<ObjectCallback> scheduledRequests;
|
2012-12-03 19:00:17 +03:00
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
/// Check if identifier can be valid (camelCase, point as separator)
|
2012-12-22 19:47:12 +03:00
|
|
|
void checkIdentifier(std::string & ID);
|
2013-04-25 17:03:35 +03:00
|
|
|
|
|
|
|
void requestIdentifier(ObjectCallback callback);
|
|
|
|
bool resolveIdentifier(const ObjectCallback & callback);
|
2013-12-31 02:09:58 +03:00
|
|
|
std::vector<ObjectData> getPossibleIdentifiers(const ObjectCallback & callback);
|
2012-12-03 19:00:17 +03:00
|
|
|
public:
|
2013-12-02 14:58:02 +03:00
|
|
|
/// request identifier for specific object name.
|
|
|
|
/// Function callback will be called during ID resolution phase of loading
|
2013-06-26 14:18:27 +03:00
|
|
|
void requestIdentifier(std::string scope, std::string type, std::string name, const std::function<void(si32)> & callback);
|
|
|
|
void requestIdentifier(std::string type, const JsonNode & name, const std::function<void(si32)> & callback);
|
|
|
|
void requestIdentifier(const JsonNode & name, const std::function<void(si32)> & callback);
|
2013-04-25 17:03:35 +03:00
|
|
|
|
2013-12-02 14:58:02 +03:00
|
|
|
/// try to request ID. If ID with such name won't be loaded, callback function will not be called
|
|
|
|
void tryRequestIdentifier(std::string scope, std::string type, std::string name, const std::function<void(si32)> & callback);
|
|
|
|
void tryRequestIdentifier(std::string type, const JsonNode & name, const std::function<void(si32)> & callback);
|
|
|
|
|
|
|
|
/// get identifier immediately. If identifier is not know and not silent call will result in error message
|
2014-05-17 17:50:11 +03:00
|
|
|
boost::optional<si32> getIdentifier(std::string scope, std::string type, std::string name, bool silent = false);
|
2013-12-02 14:58:02 +03:00
|
|
|
boost::optional<si32> getIdentifier(std::string type, const JsonNode & name, bool silent = false);
|
2013-12-31 02:09:58 +03:00
|
|
|
boost::optional<si32> getIdentifier(const JsonNode & name, bool silent = false);
|
2013-12-02 14:58:02 +03:00
|
|
|
|
|
|
|
/// registers new object
|
2013-04-21 15:49:26 +03:00
|
|
|
void registerObject(std::string scope, std::string type, std::string name, si32 identifier);
|
2012-12-03 19:00:17 +03:00
|
|
|
|
|
|
|
/// called at the very end of loading to check for any missing ID's
|
2013-04-21 15:49:26 +03:00
|
|
|
void finalize();
|
2014-02-02 17:31:56 +03:00
|
|
|
|
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
2014-02-19 04:04:27 +03:00
|
|
|
h & registeredObjects;
|
2014-02-02 17:31:56 +03:00
|
|
|
}
|
2013-04-21 15:49:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/// class used to load all game data into handlers. Used only during loading
|
|
|
|
class CContentHandler
|
|
|
|
{
|
|
|
|
/// internal type to handle loading of one data type (e.g. artifacts, creatures)
|
|
|
|
class ContentTypeHandler
|
|
|
|
{
|
|
|
|
struct ModInfo
|
|
|
|
{
|
|
|
|
/// mod data from this mod and for this mod
|
|
|
|
JsonNode modData;
|
|
|
|
/// mod data for this mod from other mods (patches)
|
|
|
|
JsonNode patches;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// handler to which all data will be loaded
|
|
|
|
IHandlerBase * handler;
|
|
|
|
|
|
|
|
std::string objectName;
|
|
|
|
|
|
|
|
/// contains all loaded H3 data
|
|
|
|
std::vector<JsonNode> originalData;
|
|
|
|
std::map<std::string, ModInfo> modData;
|
|
|
|
|
|
|
|
public:
|
2013-04-26 00:50:55 +03:00
|
|
|
ContentTypeHandler(IHandlerBase * handler, std::string objectName);
|
2013-04-21 15:49:26 +03:00
|
|
|
|
|
|
|
/// local version of methods in ContentHandler
|
2014-03-23 15:59:03 +03:00
|
|
|
/// returns true if loading was successful
|
2013-11-09 22:10:16 +03:00
|
|
|
bool preloadModData(std::string modName, std::vector<std::string> fileList, bool validate);
|
|
|
|
bool loadMod(std::string modName, bool validate);
|
2013-07-21 17:19:29 +03:00
|
|
|
void afterLoadFinalization();
|
2013-04-21 15:49:26 +03:00
|
|
|
};
|
|
|
|
|
2013-11-11 23:11:51 +03:00
|
|
|
/// preloads all data from fileList as data from modName.
|
|
|
|
bool preloadModData(std::string modName, JsonNode modConfig, bool validate);
|
|
|
|
|
|
|
|
/// actually loads data in mod
|
|
|
|
bool loadMod(std::string modName, bool validate);
|
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
std::map<std::string, ContentTypeHandler> handlers;
|
|
|
|
public:
|
|
|
|
/// fully initialize object. Will cause reading of H3 config files
|
|
|
|
CContentHandler();
|
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
/// preloads all data from fileList as data from modName.
|
2013-11-11 23:11:51 +03:00
|
|
|
void preloadData(CModInfo & mod);
|
2013-04-21 15:49:26 +03:00
|
|
|
|
|
|
|
/// actually loads data in mod
|
2013-11-11 23:11:51 +03:00
|
|
|
void load(CModInfo & mod);
|
2013-07-21 17:19:29 +03:00
|
|
|
|
|
|
|
/// all data was loaded, time for final validation / integration
|
|
|
|
void afterLoadFinalization();
|
2012-12-03 19:00:17 +03:00
|
|
|
};
|
|
|
|
|
2012-12-12 14:13:57 +03:00
|
|
|
typedef std::string TModID;
|
2012-08-10 16:07:53 +03:00
|
|
|
|
|
|
|
class DLL_LINKAGE CModInfo
|
|
|
|
{
|
|
|
|
public:
|
2013-11-11 23:11:51 +03:00
|
|
|
enum EValidationStatus
|
|
|
|
{
|
|
|
|
PENDING,
|
|
|
|
FAILED,
|
|
|
|
PASSED
|
|
|
|
};
|
|
|
|
|
2012-12-12 14:13:57 +03:00
|
|
|
/// identifier, identical to name of folder with mod
|
|
|
|
std::string identifier;
|
|
|
|
|
|
|
|
/// human-readable strings
|
|
|
|
std::string name;
|
|
|
|
std::string description;
|
|
|
|
|
2013-01-10 21:53:49 +03:00
|
|
|
/// list of mods that should be loaded before this one
|
|
|
|
std::set <TModID> dependencies;
|
2012-12-12 14:13:57 +03:00
|
|
|
|
2013-01-10 21:53:49 +03:00
|
|
|
/// list of mods that can't be used in the same time as this one
|
|
|
|
std::set <TModID> conflicts;
|
2012-08-10 16:07:53 +03:00
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
/// CRC-32 checksum of the mod
|
|
|
|
ui32 checksum;
|
|
|
|
|
2013-11-09 22:10:16 +03:00
|
|
|
/// true if mod is enabled
|
|
|
|
bool enabled;
|
|
|
|
|
2013-11-11 23:11:51 +03:00
|
|
|
EValidationStatus validation;
|
|
|
|
|
|
|
|
JsonNode config;
|
|
|
|
|
|
|
|
CModInfo(){}
|
|
|
|
CModInfo(std::string identifier, const JsonNode & local, const JsonNode & config);
|
|
|
|
|
2014-03-15 13:57:34 +03:00
|
|
|
JsonNode saveLocalData() const;
|
2013-11-11 23:11:51 +03:00
|
|
|
void updateChecksum(ui32 newChecksum);
|
2012-08-10 16:07:53 +03:00
|
|
|
|
2014-03-15 13:57:34 +03:00
|
|
|
static std::string getModDir(std::string name);
|
|
|
|
static std::string getModFile(std::string name);
|
|
|
|
|
2012-08-10 16:07:53 +03:00
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
2013-11-11 23:11:51 +03:00
|
|
|
h & identifier & description & name;
|
|
|
|
h & dependencies & conflicts & config;
|
|
|
|
h & checksum & validation & enabled;
|
2012-08-10 16:07:53 +03:00
|
|
|
}
|
2013-11-11 23:11:51 +03:00
|
|
|
private:
|
|
|
|
void loadLocalData(const JsonNode & data);
|
2012-08-10 16:07:53 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class DLL_LINKAGE CModHandler
|
|
|
|
{
|
2012-11-13 14:52:23 +03:00
|
|
|
std::map <TModID, CModInfo> allMods;
|
2012-12-12 14:13:57 +03:00
|
|
|
std::vector <TModID> activeMods;//active mods, in order in which they were loaded
|
2013-11-11 23:11:51 +03:00
|
|
|
CModInfo coreMod;
|
2012-08-10 16:07:53 +03:00
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
void loadConfigFromFile(std::string name);
|
2013-01-10 21:53:49 +03:00
|
|
|
|
|
|
|
bool hasCircularDependency(TModID mod, std::set <TModID> currentList = std::set <TModID>()) const;
|
|
|
|
|
2013-04-02 20:06:43 +03:00
|
|
|
//returns false if mod list is incorrect and prints error to console. Possible errors are:
|
2013-01-10 21:53:49 +03:00
|
|
|
// - missing dependency mod
|
|
|
|
// - conflicting mod in load order
|
|
|
|
// - circular dependencies
|
|
|
|
bool checkDependencies(const std::vector <TModID> & input) const;
|
|
|
|
|
|
|
|
// returns load order in which all dependencies are resolved, e.g. loaded after required mods
|
|
|
|
// function assumes that input list is valid (checkDependencies returned true)
|
|
|
|
std::vector <TModID> resolveDependencies(std::vector<TModID> input) const;
|
2014-03-15 13:57:34 +03:00
|
|
|
|
|
|
|
std::vector<std::string> getModList(std::string path);
|
2014-03-20 20:06:25 +03:00
|
|
|
void loadMods(std::string path, std::string namePrefix, const JsonNode & modSettings, bool enableMods);
|
2012-12-03 19:00:17 +03:00
|
|
|
public:
|
2013-04-02 20:06:43 +03:00
|
|
|
|
2012-12-03 19:00:17 +03:00
|
|
|
CIdentifierStorage identifiers;
|
|
|
|
|
2012-12-12 14:13:57 +03:00
|
|
|
/// receives list of available mods and trying to load mod.json from all of them
|
2013-11-09 22:10:16 +03:00
|
|
|
void initializeConfig();
|
2014-03-15 13:57:34 +03:00
|
|
|
void loadMods();
|
|
|
|
void loadModFilesystems();
|
2012-11-13 14:52:23 +03:00
|
|
|
|
2013-04-25 17:03:35 +03:00
|
|
|
CModInfo & getModData(TModID modId);
|
|
|
|
|
2014-03-15 13:57:34 +03:00
|
|
|
/// returns list of all (active) mods
|
|
|
|
std::vector<std::string> getAllMods();
|
|
|
|
std::vector<std::string> getActiveMods();
|
|
|
|
|
2012-11-13 14:52:23 +03:00
|
|
|
/// load content from all available mods
|
2013-11-09 22:10:16 +03:00
|
|
|
void load();
|
|
|
|
void afterLoad();
|
2012-11-13 14:52:23 +03:00
|
|
|
|
2012-08-11 12:06:23 +03:00
|
|
|
struct DLL_LINKAGE hardcodedFeatures
|
|
|
|
{
|
2013-04-26 00:50:55 +03:00
|
|
|
JsonNode data;
|
|
|
|
|
2012-08-11 12:06:23 +03:00
|
|
|
int CREEP_SIZE; // neutral stacks won't grow beyond this number
|
|
|
|
int WEEKLY_GROWTH; //percent
|
|
|
|
int NEUTRAL_STACK_EXP;
|
2012-09-05 15:49:23 +03:00
|
|
|
int MAX_BUILDING_PER_TURN;
|
2012-08-11 12:06:23 +03:00
|
|
|
bool DWELLINGS_ACCUMULATE_CREATURES;
|
|
|
|
bool ALL_CREATURES_GET_DOUBLE_MONTHS;
|
2014-04-27 09:43:42 +03:00
|
|
|
int MAX_HEROES_AVAILABLE_PER_PLAYER;
|
|
|
|
int MAX_HEROES_ON_MAP_PER_PLAYER;
|
2012-08-11 12:06:23 +03:00
|
|
|
|
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
2013-05-27 17:20:46 +03:00
|
|
|
h & data & CREEP_SIZE & WEEKLY_GROWTH & NEUTRAL_STACK_EXP & MAX_BUILDING_PER_TURN;
|
2014-04-27 09:43:42 +03:00
|
|
|
h & DWELLINGS_ACCUMULATE_CREATURES & ALL_CREATURES_GET_DOUBLE_MONTHS & MAX_HEROES_AVAILABLE_PER_PLAYER & MAX_HEROES_ON_MAP_PER_PLAYER;
|
2012-08-11 12:06:23 +03:00
|
|
|
}
|
|
|
|
} settings;
|
|
|
|
|
2012-08-24 12:37:52 +03:00
|
|
|
struct DLL_LINKAGE gameModules
|
|
|
|
{
|
|
|
|
bool STACK_EXP;
|
|
|
|
bool STACK_ARTIFACT;
|
|
|
|
bool COMMANDERS;
|
|
|
|
bool MITHRIL;
|
|
|
|
|
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
|
|
|
h & STACK_EXP & STACK_ARTIFACT & COMMANDERS & MITHRIL;
|
|
|
|
}
|
|
|
|
} modules;
|
|
|
|
|
2012-08-10 16:07:53 +03:00
|
|
|
CModHandler();
|
|
|
|
|
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
2014-02-19 04:04:27 +03:00
|
|
|
h & allMods & activeMods & settings & modules & identifiers;
|
2012-08-10 16:07:53 +03:00
|
|
|
}
|
2012-08-10 18:16:42 +03:00
|
|
|
};
|