2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CModHandler.cpp, 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
|
|
|
|
*
|
|
|
|
*/
|
2012-08-10 16:07:53 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CModHandler.h"
|
2014-06-05 14:19:47 +03:00
|
|
|
#include "mapObjects/CObjectClassesHandler.h"
|
2023-03-13 23:26:44 +02:00
|
|
|
#include "rmg/CRmgTemplateStorage.h"
|
2016-01-16 17:36:16 +02:00
|
|
|
#include "filesystem/FileStream.h"
|
2013-12-29 14:27:38 +03:00
|
|
|
#include "filesystem/AdapterLoaders.h"
|
|
|
|
#include "filesystem/CFilesystemLoader.h"
|
2023-03-15 21:34:29 +02:00
|
|
|
#include "filesystem/Filesystem.h"
|
2012-12-03 19:00:17 +03:00
|
|
|
|
2012-12-15 11:47:02 +03:00
|
|
|
#include "CCreatureHandler.h"
|
|
|
|
#include "CArtHandler.h"
|
|
|
|
#include "CTownHandler.h"
|
|
|
|
#include "CHeroHandler.h"
|
2014-06-05 14:19:47 +03:00
|
|
|
#include "mapObjects/CObjectHandler.h"
|
2012-12-26 12:46:09 +03:00
|
|
|
#include "StringConstants.h"
|
2013-04-21 15:49:26 +03:00
|
|
|
#include "CStopWatch.h"
|
|
|
|
#include "IHandlerBase.h"
|
2015-02-02 10:25:26 +02:00
|
|
|
#include "spells/CSpellHandler.h"
|
2017-08-22 11:40:10 +02:00
|
|
|
#include "CSkillHandler.h"
|
2023-01-16 23:20:24 +02:00
|
|
|
#include "CGeneralTextHandler.h"
|
2023-02-09 22:26:25 +02:00
|
|
|
#include "Languages.h"
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
#include "ScriptHandler.h"
|
2023-01-11 15:17:24 +02:00
|
|
|
#include "RoadHandler.h"
|
2023-03-15 21:34:29 +02:00
|
|
|
#include "GameSettings.h"
|
2023-01-11 15:17:24 +02:00
|
|
|
#include "RiverHandler.h"
|
2023-01-09 01:17:37 +02:00
|
|
|
#include "TerrainHandler.h"
|
2022-06-28 10:05:30 +02:00
|
|
|
#include "BattleFieldHandler.h"
|
2022-09-15 10:06:54 +02:00
|
|
|
#include "ObstacleHandler.h"
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
|
|
|
|
#include <vstd/StringUtils.h>
|
2012-12-15 11:47:02 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2014-06-15 23:25:10 +03:00
|
|
|
CIdentifierStorage::CIdentifierStorage():
|
|
|
|
state(LOADING)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-12-22 19:47:12 +03:00
|
|
|
void CIdentifierStorage::checkIdentifier(std::string & ID)
|
|
|
|
{
|
|
|
|
if (boost::algorithm::ends_with(ID, "."))
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->warn("BIG WARNING: identifier %s seems to be broken!", ID);
|
2012-12-22 19:47:12 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (std::tolower(ID[pos]) != ID[pos] ) //Not in camelCase
|
|
|
|
{
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->warn("Warning: identifier %s is not in camelCase!", ID);
|
2012-12-22 19:47:12 +03:00
|
|
|
ID[pos] = std::tolower(ID[pos]);// Try to fix the ID
|
|
|
|
}
|
|
|
|
pos = ID.find('.', pos);
|
|
|
|
}
|
|
|
|
while(pos++ != std::string::npos);
|
|
|
|
}
|
|
|
|
}
|
2012-08-10 16:07:53 +03:00
|
|
|
|
2013-04-25 17:03:35 +03:00
|
|
|
void CIdentifierStorage::requestIdentifier(ObjectCallback callback)
|
|
|
|
{
|
|
|
|
checkIdentifier(callback.type);
|
|
|
|
checkIdentifier(callback.name);
|
2013-02-25 12:48:26 +03:00
|
|
|
|
2013-04-25 17:03:35 +03:00
|
|
|
assert(!callback.localScope.empty());
|
2013-04-21 15:49:26 +03:00
|
|
|
|
2014-06-15 23:25:10 +03:00
|
|
|
if (state != FINISHED) // enqueue request if loading is still in progress
|
|
|
|
scheduledRequests.push_back(callback);
|
|
|
|
else // execute immediately for "late" requests
|
|
|
|
resolveIdentifier(callback);
|
2013-04-25 17:03:35 +03:00
|
|
|
}
|
|
|
|
|
2023-04-03 14:14:34 +02:00
|
|
|
CIdentifierStorage::ObjectCallback CIdentifierStorage::ObjectCallback::fromNameWithType(const std::string & scope, const std::string & fullName, const std::function<void(si32)> & callback, bool optional)
|
2013-04-25 17:03:35 +03:00
|
|
|
{
|
2023-04-03 14:14:34 +02:00
|
|
|
assert(!scope.empty());
|
|
|
|
|
|
|
|
auto scopeAndFullName = vstd::splitStringToPair(fullName, ':');
|
|
|
|
auto typeAndName = vstd::splitStringToPair(scopeAndFullName.second, '.');
|
2013-04-25 17:03:35 +03:00
|
|
|
|
2023-04-03 14:14:34 +02:00
|
|
|
if (scope == scopeAndFullName.first)
|
|
|
|
logMod->debug("Target scope for identifier '%s' is redundant! Identifier already defined in mod '%s'", fullName, scope);
|
|
|
|
|
|
|
|
ObjectCallback result;
|
|
|
|
result.localScope = scope;
|
|
|
|
result.remoteScope = scopeAndFullName.first;
|
|
|
|
result.type = typeAndName.first;
|
|
|
|
result.name = typeAndName.second;
|
|
|
|
result.callback = callback;
|
|
|
|
result.optional = optional;
|
|
|
|
return result;
|
2013-04-25 17:03:35 +03:00
|
|
|
}
|
|
|
|
|
2023-04-03 14:14:34 +02:00
|
|
|
CIdentifierStorage::ObjectCallback CIdentifierStorage::ObjectCallback::fromNameAndType(const std::string & scope, const std::string & type, const std::string & fullName, const std::function<void(si32)> & callback, bool optional)
|
2014-11-12 04:50:51 +02:00
|
|
|
{
|
2023-04-03 14:14:34 +02:00
|
|
|
assert(!scope.empty());
|
|
|
|
|
2020-12-13 02:33:28 +02:00
|
|
|
auto scopeAndFullName = vstd::splitStringToPair(fullName, ':');
|
|
|
|
auto typeAndName = vstd::splitStringToPair(scopeAndFullName.second, '.');
|
2015-11-14 15:50:29 +02:00
|
|
|
|
2023-04-03 14:14:34 +02:00
|
|
|
if(!typeAndName.first.empty())
|
|
|
|
{
|
|
|
|
if (typeAndName.first != type)
|
|
|
|
logMod->error("Identifier '%s' from mod '%s' requested with different type! Type '%s' expected!", fullName, scope, type);
|
|
|
|
else
|
|
|
|
logMod->debug("Target type for identifier '%s' defined in mod '%s' is redundant!", fullName, scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scope == scopeAndFullName.first)
|
|
|
|
logMod->debug("Target scope for identifier '%s' is redundant! Identifier already defined in mod '%s'", fullName, scope);
|
|
|
|
|
|
|
|
ObjectCallback result;
|
|
|
|
result.localScope = scope;
|
|
|
|
result.remoteScope = scopeAndFullName.first;
|
|
|
|
result.type = type;
|
|
|
|
result.name = typeAndName.second;
|
|
|
|
result.callback = callback;
|
|
|
|
result.optional = optional;
|
|
|
|
return result;
|
2014-11-12 04:50:51 +02:00
|
|
|
}
|
|
|
|
|
2023-04-03 14:14:34 +02:00
|
|
|
void CIdentifierStorage::requestIdentifier(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback)
|
2013-04-25 17:03:35 +03:00
|
|
|
{
|
2023-04-03 14:14:34 +02:00
|
|
|
requestIdentifier(ObjectCallback::fromNameAndType(scope, type, name, callback, false));
|
|
|
|
}
|
2013-04-25 17:03:35 +03:00
|
|
|
|
2023-04-03 14:14:34 +02:00
|
|
|
void CIdentifierStorage::requestIdentifier(const std::string & scope, const std::string & fullName, const std::function<void(si32)> & callback)
|
|
|
|
{
|
|
|
|
requestIdentifier(ObjectCallback::fromNameWithType(scope, fullName, callback, false));
|
2013-04-25 17:03:35 +03:00
|
|
|
}
|
|
|
|
|
2023-04-03 14:14:34 +02:00
|
|
|
void CIdentifierStorage::requestIdentifier(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback)
|
2013-04-25 17:03:35 +03:00
|
|
|
{
|
2023-04-03 14:14:34 +02:00
|
|
|
requestIdentifier(ObjectCallback::fromNameAndType(name.meta, type, name.String(), callback, false));
|
|
|
|
}
|
2013-04-25 17:03:35 +03:00
|
|
|
|
2023-04-03 14:14:34 +02:00
|
|
|
void CIdentifierStorage::requestIdentifier(const JsonNode & name, const std::function<void(si32)> & callback)
|
|
|
|
{
|
|
|
|
requestIdentifier(ObjectCallback::fromNameWithType(name.meta, name.String(), callback, false));
|
2013-12-02 14:58:02 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
void CIdentifierStorage::tryRequestIdentifier(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback)
|
2013-12-02 14:58:02 +03:00
|
|
|
{
|
2023-04-03 14:14:34 +02:00
|
|
|
requestIdentifier(ObjectCallback::fromNameAndType(scope, type, name, callback, true));
|
2013-12-02 14:58:02 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
void CIdentifierStorage::tryRequestIdentifier(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback)
|
2013-12-02 14:58:02 +03:00
|
|
|
{
|
2023-04-03 14:14:34 +02:00
|
|
|
requestIdentifier(ObjectCallback::fromNameAndType(name.meta, type, name.String(), callback, true));
|
2013-12-02 14:58:02 +03:00
|
|
|
}
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
std::optional<si32> CIdentifierStorage::getIdentifier(const std::string & scope, const std::string & type, const std::string & name, bool silent)
|
2014-05-17 17:50:11 +03:00
|
|
|
{
|
2023-04-03 14:14:34 +02:00
|
|
|
auto idList = getPossibleIdentifiers(ObjectCallback::fromNameAndType(scope, type, name, std::function<void(si32)>(), silent));
|
2014-05-17 17:50:11 +03:00
|
|
|
|
|
|
|
if (idList.size() == 1)
|
|
|
|
return idList.front().id;
|
|
|
|
if (!silent)
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->error("Failed to resolve identifier %s of type %s from mod %s", name , type ,scope);
|
2014-05-17 17:50:11 +03:00
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
return std::optional<si32>();
|
2014-05-17 17:50:11 +03:00
|
|
|
}
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
std::optional<si32> CIdentifierStorage::getIdentifier(const std::string & type, const JsonNode & name, bool silent)
|
2013-12-02 14:58:02 +03:00
|
|
|
{
|
2023-04-03 14:14:34 +02:00
|
|
|
auto idList = getPossibleIdentifiers(ObjectCallback::fromNameAndType(name.meta, type, name.String(), std::function<void(si32)>(), silent));
|
2013-12-02 14:58:02 +03:00
|
|
|
|
|
|
|
if (idList.size() == 1)
|
|
|
|
return idList.front().id;
|
|
|
|
if (!silent)
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->error("Failed to resolve identifier %s of type %s from mod %s", name.String(), type, name.meta);
|
2013-12-02 14:58:02 +03:00
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
return std::optional<si32>();
|
2012-12-03 19:00:17 +03:00
|
|
|
}
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
std::optional<si32> CIdentifierStorage::getIdentifier(const JsonNode & name, bool silent)
|
2013-12-31 02:09:58 +03:00
|
|
|
{
|
2023-04-03 14:14:34 +02:00
|
|
|
auto idList = getPossibleIdentifiers(ObjectCallback::fromNameWithType(name.meta, name.String(), std::function<void(si32)>(), silent));
|
2013-12-31 02:09:58 +03:00
|
|
|
|
|
|
|
if (idList.size() == 1)
|
|
|
|
return idList.front().id;
|
|
|
|
if (!silent)
|
2023-04-03 14:14:34 +02:00
|
|
|
logMod->error("Failed to resolve identifier %s from mod %s", name.String(), name.meta);
|
2013-12-31 02:09:58 +03:00
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
return std::optional<si32>();
|
2013-12-31 02:09:58 +03:00
|
|
|
}
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
std::optional<si32> CIdentifierStorage::getIdentifier(const std::string & scope, const std::string & fullName, bool silent)
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
2023-04-03 14:14:34 +02:00
|
|
|
auto idList = getPossibleIdentifiers(ObjectCallback::fromNameWithType(scope, fullName, std::function<void(si32)>(), silent));
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
if (idList.size() == 1)
|
|
|
|
return idList.front().id;
|
|
|
|
if (!silent)
|
2023-04-03 14:14:34 +02:00
|
|
|
logMod->error("Failed to resolve identifier %s from mod %s", fullName, scope);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
return std::optional<si32>();
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
void CIdentifierStorage::registerObject(const std::string & scope, const std::string & type, const std::string & name, si32 identifier)
|
2012-12-03 19:00:17 +03:00
|
|
|
{
|
2013-04-25 17:03:35 +03:00
|
|
|
ObjectData data;
|
|
|
|
data.scope = scope;
|
|
|
|
data.id = identifier;
|
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
std::string fullID = type + '.' + name;
|
|
|
|
checkIdentifier(fullID);
|
2012-12-22 19:47:12 +03:00
|
|
|
|
2017-08-25 08:29:34 +02:00
|
|
|
std::pair<const std::string, ObjectData> mapping = std::make_pair(fullID, data);
|
|
|
|
if(!vstd::containsMapping(registeredObjects, mapping))
|
2017-08-23 14:15:33 +02:00
|
|
|
{
|
2017-08-26 04:06:47 +02:00
|
|
|
logMod->trace("registered %s as %s:%s", fullID, scope, identifier);
|
2017-08-23 14:15:33 +02:00
|
|
|
registeredObjects.insert(mapping);
|
|
|
|
}
|
2013-04-25 17:03:35 +03:00
|
|
|
}
|
|
|
|
|
2013-12-31 02:09:58 +03:00
|
|
|
std::vector<CIdentifierStorage::ObjectData> CIdentifierStorage::getPossibleIdentifiers(const ObjectCallback & request)
|
2013-04-25 17:03:35 +03:00
|
|
|
{
|
|
|
|
std::set<std::string> allowedScopes;
|
2022-03-21 00:33:04 +02:00
|
|
|
bool isValidScope = true;
|
2013-04-25 17:03:35 +03:00
|
|
|
|
2022-11-30 17:36:55 +02:00
|
|
|
// called have not specified destination mod explicitly
|
2013-04-25 17:03:35 +03:00
|
|
|
if (request.remoteScope.empty())
|
|
|
|
{
|
2022-12-07 15:18:19 +02:00
|
|
|
// special scope that should have access to all in-game objects
|
|
|
|
if (request.localScope == CModHandler::scopeGame())
|
2022-11-29 22:31:33 +02:00
|
|
|
{
|
2023-02-24 22:38:12 +02:00
|
|
|
for(const auto & modName : VLC->modh->getActiveMods())
|
2022-11-29 22:31:33 +02:00
|
|
|
allowedScopes.insert(modName);
|
|
|
|
}
|
|
|
|
|
2022-12-07 15:18:19 +02:00
|
|
|
// normally ID's from all required mods, own mod and virtual built-in mod are allowed
|
|
|
|
else if(request.localScope != CModHandler::scopeBuiltin() && !request.localScope.empty())
|
2022-03-21 00:33:04 +02:00
|
|
|
{
|
|
|
|
allowedScopes = VLC->modh->getModDependencies(request.localScope, isValidScope);
|
2013-04-25 17:03:35 +03:00
|
|
|
|
2022-03-21 00:33:04 +02:00
|
|
|
if(!isValidScope)
|
|
|
|
return std::vector<ObjectData>();
|
2022-11-30 17:36:55 +02:00
|
|
|
|
|
|
|
allowedScopes.insert(request.localScope);
|
2022-03-21 00:33:04 +02:00
|
|
|
}
|
2022-11-30 17:36:55 +02:00
|
|
|
|
|
|
|
// all mods can access built-in mod
|
2022-12-07 15:18:19 +02:00
|
|
|
allowedScopes.insert(CModHandler::scopeBuiltin());
|
2013-04-25 17:03:35 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-30 17:36:55 +02:00
|
|
|
//if destination mod was specified explicitly, restrict lookup to this mod
|
2022-12-07 15:18:19 +02:00
|
|
|
if(request.remoteScope == CModHandler::scopeBuiltin() )
|
2022-11-29 22:31:33 +02:00
|
|
|
{
|
2022-12-07 15:18:19 +02:00
|
|
|
//built-in mod is an implicit dependency for all mods, allow access into it
|
2022-11-29 22:31:33 +02:00
|
|
|
allowedScopes.insert(request.remoteScope);
|
|
|
|
}
|
2022-12-07 15:18:19 +02:00
|
|
|
else if ( request.localScope == CModHandler::scopeGame() )
|
2022-11-29 22:31:33 +02:00
|
|
|
{
|
2022-12-07 15:18:19 +02:00
|
|
|
// allow access, this is special scope that should have access to all in-game objects
|
2022-11-29 22:31:33 +02:00
|
|
|
allowedScopes.insert(request.remoteScope);
|
|
|
|
}
|
|
|
|
else if(request.remoteScope == request.localScope )
|
2016-02-21 19:58:09 +02:00
|
|
|
{
|
2022-11-29 22:31:33 +02:00
|
|
|
// allow self-access
|
2013-05-22 01:11:44 +03:00
|
|
|
allowedScopes.insert(request.remoteScope);
|
2016-02-21 19:58:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-29 22:31:33 +02:00
|
|
|
// allow access only if mod is in our dependencies
|
2022-03-21 00:33:04 +02:00
|
|
|
auto myDeps = VLC->modh->getModDependencies(request.localScope, isValidScope);
|
|
|
|
|
|
|
|
if(!isValidScope)
|
|
|
|
return std::vector<ObjectData>();
|
|
|
|
|
2022-11-29 22:31:33 +02:00
|
|
|
if(myDeps.count(request.remoteScope))
|
2016-02-21 19:58:09 +02:00
|
|
|
allowedScopes.insert(request.remoteScope);
|
|
|
|
}
|
2013-04-25 17:03:35 +03:00
|
|
|
}
|
2012-12-03 19:00:17 +03:00
|
|
|
|
2013-04-25 17:03:35 +03:00
|
|
|
std::string fullID = request.type + '.' + request.name;
|
2012-12-03 19:00:17 +03:00
|
|
|
|
2013-04-25 17:03:35 +03:00
|
|
|
auto entries = registeredObjects.equal_range(fullID);
|
|
|
|
if (entries.first != entries.second)
|
2012-12-03 19:00:17 +03:00
|
|
|
{
|
2013-12-02 14:58:02 +03:00
|
|
|
std::vector<ObjectData> locatedIDs;
|
2013-09-21 21:29:26 +03:00
|
|
|
|
2013-04-25 17:03:35 +03:00
|
|
|
for (auto it = entries.first; it != entries.second; it++)
|
|
|
|
{
|
|
|
|
if (vstd::contains(allowedScopes, it->second.scope))
|
|
|
|
{
|
2013-12-02 14:58:02 +03:00
|
|
|
locatedIDs.push_back(it->second);
|
2013-04-25 17:03:35 +03:00
|
|
|
}
|
|
|
|
}
|
2013-12-02 14:58:02 +03:00
|
|
|
return locatedIDs;
|
|
|
|
}
|
|
|
|
return std::vector<ObjectData>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CIdentifierStorage::resolveIdentifier(const ObjectCallback & request)
|
|
|
|
{
|
2013-12-31 02:09:58 +03:00
|
|
|
auto identifiers = getPossibleIdentifiers(request);
|
2013-12-02 14:58:02 +03:00
|
|
|
if (identifiers.size() == 1) // normally resolved ID
|
|
|
|
{
|
|
|
|
request.callback(identifiers.front().id);
|
|
|
|
return true;
|
|
|
|
}
|
2013-04-25 17:03:35 +03:00
|
|
|
|
2013-12-02 14:58:02 +03:00
|
|
|
if (request.optional && identifiers.empty()) // failed to resolve optinal ID
|
2014-06-15 19:43:01 +03:00
|
|
|
{
|
2013-12-02 14:58:02 +03:00
|
|
|
return true;
|
2014-06-15 19:43:01 +03:00
|
|
|
}
|
2013-09-21 21:29:26 +03:00
|
|
|
|
2013-12-02 14:58:02 +03:00
|
|
|
// error found. Try to generate some debug info
|
2023-03-13 23:26:44 +02:00
|
|
|
if(identifiers.empty())
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->error("Unknown identifier!");
|
2013-12-02 14:58:02 +03:00
|
|
|
else
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->error("Ambiguous identifier request!");
|
2013-09-21 21:29:26 +03:00
|
|
|
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->error("Request for %s.%s from mod %s", request.type, request.name, request.localScope);
|
2013-09-21 21:29:26 +03:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & id : identifiers)
|
2013-12-02 14:58:02 +03:00
|
|
|
{
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->error("\tID is available in mod %s", id.scope);
|
2013-04-25 17:03:35 +03:00
|
|
|
}
|
|
|
|
return false;
|
2012-12-03 19:00:17 +03:00
|
|
|
}
|
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
void CIdentifierStorage::finalize()
|
2012-12-03 19:00:17 +03:00
|
|
|
{
|
2014-06-15 23:25:10 +03:00
|
|
|
state = FINALIZING;
|
2013-04-25 17:03:35 +03:00
|
|
|
bool errorsFound = false;
|
|
|
|
|
2022-11-30 18:07:21 +02:00
|
|
|
while ( !scheduledRequests.empty() )
|
2013-04-21 15:49:26 +03:00
|
|
|
{
|
2022-11-30 18:07:21 +02:00
|
|
|
// Use local copy since new requests may appear during resolving, invalidating any iterators
|
|
|
|
auto request = scheduledRequests.back();
|
|
|
|
scheduledRequests.pop_back();
|
|
|
|
|
|
|
|
if (!resolveIdentifier(request))
|
|
|
|
errorsFound = true;
|
2013-04-21 15:49:26 +03:00
|
|
|
}
|
|
|
|
|
2013-04-25 17:03:35 +03:00
|
|
|
if (errorsFound)
|
2012-12-03 19:00:17 +03:00
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & object : registeredObjects)
|
2013-03-09 16:16:35 +03:00
|
|
|
{
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->trace("%s : %s -> %d", object.second.scope, object.first, object.second.id);
|
2013-03-09 16:16:35 +03:00
|
|
|
}
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->error("All known identifiers were dumped into log file");
|
2012-12-03 19:00:17 +03:00
|
|
|
}
|
2013-04-25 17:03:35 +03:00
|
|
|
assert(errorsFound == false);
|
2014-06-15 23:25:10 +03:00
|
|
|
state = FINISHED;
|
2012-12-03 19:00:17 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
ContentTypeHandler::ContentTypeHandler(IHandlerBase * handler, const std::string & objectName):
|
2016-03-12 03:41:27 +02:00
|
|
|
handler(handler),
|
|
|
|
objectName(objectName),
|
2023-03-15 21:34:29 +02:00
|
|
|
originalData(handler->loadLegacyData())
|
2013-04-21 15:49:26 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & node : originalData)
|
2013-04-25 17:03:35 +03:00
|
|
|
{
|
2022-12-07 15:18:19 +02:00
|
|
|
node.setMeta(CModHandler::scopeBuiltin());
|
2013-04-25 17:03:35 +03:00
|
|
|
}
|
2013-04-21 15:49:26 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
bool ContentTypeHandler::preloadModData(const std::string & modName, const std::vector<std::string> & fileList, bool validate)
|
2013-04-21 15:49:26 +03:00
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
bool result = false;
|
2013-11-08 23:36:26 +03:00
|
|
|
JsonNode data = JsonUtils::assembleFromFiles(fileList, result);
|
2013-04-25 17:03:35 +03:00
|
|
|
data.setMeta(modName);
|
2013-04-21 15:49:26 +03:00
|
|
|
|
|
|
|
ModInfo & modInfo = modData[modName];
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto entry : data.Struct())
|
2013-04-21 15:49:26 +03:00
|
|
|
{
|
|
|
|
size_t colon = entry.first.find(':');
|
|
|
|
|
|
|
|
if (colon == std::string::npos)
|
|
|
|
{
|
|
|
|
// normal object, local to this mod
|
|
|
|
modInfo.modData[entry.first].swap(entry.second);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string remoteName = entry.first.substr(0, colon);
|
|
|
|
std::string objectName = entry.first.substr(colon + 1);
|
|
|
|
|
|
|
|
// patching this mod? Send warning and continue - this situation can be handled normally
|
|
|
|
if (remoteName == modName)
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->warn("Redundant namespace definition for %s", objectName);
|
2013-04-21 15:49:26 +03:00
|
|
|
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->trace("Patching object %s (%s) from %s", objectName, remoteName, modName);
|
2013-04-21 15:49:26 +03:00
|
|
|
JsonNode & remoteConf = modData[remoteName].patches[objectName];
|
|
|
|
|
|
|
|
JsonUtils::merge(remoteConf, entry.second);
|
|
|
|
}
|
|
|
|
}
|
2013-11-08 23:36:26 +03:00
|
|
|
return result;
|
2013-04-21 15:49:26 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
bool ContentTypeHandler::loadMod(const std::string & modName, bool validate)
|
2013-04-21 15:49:26 +03:00
|
|
|
{
|
|
|
|
ModInfo & modInfo = modData[modName];
|
2013-11-08 23:36:26 +03:00
|
|
|
bool result = true;
|
2015-11-14 15:50:29 +02:00
|
|
|
|
2014-05-18 17:47:18 +03:00
|
|
|
auto performValidate = [&,this](JsonNode & data, const std::string & name){
|
|
|
|
handler->beforeValidate(data);
|
|
|
|
if (validate)
|
2015-11-14 15:50:29 +02:00
|
|
|
result &= JsonUtils::validate(data, "vcmi:" + objectName, name);
|
2014-05-18 17:47:18 +03:00
|
|
|
};
|
2013-04-21 15:49:26 +03:00
|
|
|
|
|
|
|
// apply patches
|
|
|
|
if (!modInfo.patches.isNull())
|
|
|
|
JsonUtils::merge(modInfo.modData, modInfo.patches);
|
|
|
|
|
2013-11-14 16:21:09 +03:00
|
|
|
for(auto & entry : modInfo.modData.Struct())
|
2013-04-21 15:49:26 +03:00
|
|
|
{
|
|
|
|
const std::string & name = entry.first;
|
|
|
|
JsonNode & data = entry.second;
|
|
|
|
|
|
|
|
if (vstd::contains(data.Struct(), "index") && !data["index"].isNull())
|
|
|
|
{
|
2022-12-19 12:22:02 +02:00
|
|
|
if (modName != "core")
|
|
|
|
logMod->warn("Mod %s is attempting to load original data! This should be reserved for built-in mod.", modName);
|
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
// try to add H3 object data
|
2020-10-01 10:38:06 +02:00
|
|
|
size_t index = static_cast<size_t>(data["index"].Float());
|
2013-04-21 15:49:26 +03:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
if(originalData.size() > index)
|
2013-04-21 15:49:26 +03:00
|
|
|
{
|
2017-08-26 04:06:47 +02:00
|
|
|
logMod->trace("found original data in loadMod(%s) at index %d", name, index);
|
2013-04-21 15:49:26 +03:00
|
|
|
JsonUtils::merge(originalData[index], data);
|
2017-07-20 06:08:49 +02:00
|
|
|
std::swap(originalData[index], data);
|
2013-04-21 15:49:26 +03:00
|
|
|
originalData[index].clear(); // do not use same data twice (same ID)
|
|
|
|
}
|
2017-08-24 08:01:02 +02:00
|
|
|
else
|
|
|
|
{
|
2022-12-19 12:22:02 +02:00
|
|
|
logMod->trace("no original data in loadMod(%s) at index %d", name, index);
|
2017-08-24 08:01:02 +02:00
|
|
|
}
|
2017-07-20 06:08:49 +02:00
|
|
|
performValidate(data, name);
|
|
|
|
handler->loadObject(modName, name, data, index);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// normal new object
|
|
|
|
logMod->trace("no index in loadMod(%s)", name);
|
|
|
|
performValidate(data,name);
|
|
|
|
handler->loadObject(modName, name, data);
|
2013-04-21 15:49:26 +03:00
|
|
|
}
|
|
|
|
}
|
2013-11-08 23:36:26 +03:00
|
|
|
return result;
|
2013-04-21 15:49:26 +03:00
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
void ContentTypeHandler::loadCustom()
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
|
|
|
handler->loadCustom();
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
void ContentTypeHandler::afterLoadFinalization()
|
2013-07-21 17:19:29 +03:00
|
|
|
{
|
|
|
|
handler->afterLoadFinalization();
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
void CContentHandler::init()
|
2013-04-21 15:49:26 +03:00
|
|
|
{
|
2023-01-16 23:20:24 +02:00
|
|
|
handlers.insert(std::make_pair("heroClasses", ContentTypeHandler(&VLC->heroh->classes, "heroClass")));
|
2013-04-26 00:50:55 +03:00
|
|
|
handlers.insert(std::make_pair("artifacts", ContentTypeHandler(VLC->arth, "artifact")));
|
|
|
|
handlers.insert(std::make_pair("creatures", ContentTypeHandler(VLC->creh, "creature")));
|
|
|
|
handlers.insert(std::make_pair("factions", ContentTypeHandler(VLC->townh, "faction")));
|
2014-05-24 15:45:29 +03:00
|
|
|
handlers.insert(std::make_pair("objects", ContentTypeHandler(VLC->objtypeh, "object")));
|
2013-04-26 00:50:55 +03:00
|
|
|
handlers.insert(std::make_pair("heroes", ContentTypeHandler(VLC->heroh, "hero")));
|
2014-05-24 15:45:29 +03:00
|
|
|
handlers.insert(std::make_pair("spells", ContentTypeHandler(VLC->spellh, "spell")));
|
2017-08-22 11:40:10 +02:00
|
|
|
handlers.insert(std::make_pair("skills", ContentTypeHandler(VLC->skillh, "skill")));
|
2023-03-13 23:26:44 +02:00
|
|
|
handlers.insert(std::make_pair("templates", ContentTypeHandler(VLC->tplh, "template")));
|
2022-09-21 18:31:14 +02:00
|
|
|
#if SCRIPTING_ENABLED
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
handlers.insert(std::make_pair("scripts", ContentTypeHandler(VLC->scriptHandler, "script")));
|
2022-09-21 18:31:14 +02:00
|
|
|
#endif
|
2022-06-28 10:05:30 +02:00
|
|
|
handlers.insert(std::make_pair("battlefields", ContentTypeHandler(VLC->battlefieldsHandler, "battlefield")));
|
2022-12-20 16:14:06 +02:00
|
|
|
handlers.insert(std::make_pair("terrains", ContentTypeHandler(VLC->terrainTypeHandler, "terrain")));
|
|
|
|
handlers.insert(std::make_pair("rivers", ContentTypeHandler(VLC->riverTypeHandler, "river")));
|
|
|
|
handlers.insert(std::make_pair("roads", ContentTypeHandler(VLC->roadTypeHandler, "road")));
|
2022-09-15 10:06:54 +02:00
|
|
|
handlers.insert(std::make_pair("obstacles", ContentTypeHandler(VLC->obstacleHandler, "obstacle")));
|
2014-05-24 15:45:29 +03:00
|
|
|
//TODO: any other types of moddables?
|
2013-04-21 15:49:26 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
bool CContentHandler::preloadModData(const std::string & modName, JsonNode modConfig, bool validate)
|
2013-04-21 15:49:26 +03:00
|
|
|
{
|
2013-11-08 23:36:26 +03:00
|
|
|
bool result = true;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & handler : handlers)
|
2013-04-21 15:49:26 +03:00
|
|
|
{
|
2013-11-09 22:10:16 +03:00
|
|
|
result &= handler.second.preloadModData(modName, modConfig[handler.first].convertTo<std::vector<std::string> >(), validate);
|
2013-04-21 15:49:26 +03:00
|
|
|
}
|
2013-11-08 23:36:26 +03:00
|
|
|
return result;
|
2013-04-21 15:49:26 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
bool CContentHandler::loadMod(const std::string & modName, bool validate)
|
2013-04-21 15:49:26 +03:00
|
|
|
{
|
2013-11-08 23:36:26 +03:00
|
|
|
bool result = true;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & handler : handlers)
|
2013-04-21 15:49:26 +03:00
|
|
|
{
|
2013-11-09 22:10:16 +03:00
|
|
|
result &= handler.second.loadMod(modName, validate);
|
2013-04-21 15:49:26 +03:00
|
|
|
}
|
2013-11-08 23:36:26 +03:00
|
|
|
return result;
|
2013-04-21 15:49:26 +03:00
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void CContentHandler::loadCustom()
|
|
|
|
{
|
|
|
|
for(auto & handler : handlers)
|
|
|
|
{
|
|
|
|
handler.second.loadCustom();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-21 17:19:29 +03:00
|
|
|
void CContentHandler::afterLoadFinalization()
|
|
|
|
{
|
|
|
|
for(auto & handler : handlers)
|
|
|
|
{
|
|
|
|
handler.second.afterLoadFinalization();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-11 23:11:51 +03:00
|
|
|
void CContentHandler::preloadData(CModInfo & mod)
|
|
|
|
{
|
|
|
|
bool validate = (mod.validation != CModInfo::PASSED);
|
|
|
|
|
|
|
|
// print message in format [<8-symbols checksum>] <modname>
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->info("\t\t[%08x]%s", mod.checksum, mod.name);
|
2013-11-11 23:11:51 +03:00
|
|
|
|
2022-12-07 15:18:19 +02:00
|
|
|
if (validate && mod.identifier != CModHandler::scopeBuiltin())
|
2013-11-11 23:11:51 +03:00
|
|
|
{
|
|
|
|
if (!JsonUtils::validate(mod.config, "vcmi:mod", mod.identifier))
|
|
|
|
mod.validation = CModInfo::FAILED;
|
|
|
|
}
|
|
|
|
if (!preloadModData(mod.identifier, mod.config, validate))
|
|
|
|
mod.validation = CModInfo::FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CContentHandler::load(CModInfo & mod)
|
|
|
|
{
|
2023-01-09 01:17:37 +02:00
|
|
|
bool validate = (mod.validation != CModInfo::PASSED);
|
2013-11-11 23:11:51 +03:00
|
|
|
|
|
|
|
if (!loadMod(mod.identifier, validate))
|
|
|
|
mod.validation = CModInfo::FAILED;
|
|
|
|
|
|
|
|
if (validate)
|
|
|
|
{
|
|
|
|
if (mod.validation != CModInfo::FAILED)
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->info("\t\t[DONE] %s", mod.name);
|
2013-11-11 23:11:51 +03:00
|
|
|
else
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->error("\t\t[FAIL] %s", mod.name);
|
2013-11-11 23:11:51 +03:00
|
|
|
}
|
|
|
|
else
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->info("\t\t[SKIP] %s", mod.name);
|
2013-11-11 23:11:51 +03:00
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
const ContentTypeHandler & CContentHandler::operator[](const std::string & name) const
|
|
|
|
{
|
|
|
|
return handlers.at(name);
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
static JsonNode loadModSettings(const std::string & path)
|
2014-03-15 13:57:34 +03:00
|
|
|
{
|
|
|
|
if (CResourceHandler::get("local")->existsResource(ResourceID(path)))
|
|
|
|
{
|
|
|
|
return JsonNode(ResourceID(path, EResType::TEXT));
|
|
|
|
}
|
|
|
|
// Probably new install. Create initial configuration
|
|
|
|
CResourceHandler::get("local")->createResource(path);
|
|
|
|
return JsonNode();
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
JsonNode addMeta(JsonNode config, const std::string & meta)
|
2014-03-15 13:57:34 +03:00
|
|
|
{
|
|
|
|
config.setMeta(meta);
|
2015-12-04 01:06:02 +02:00
|
|
|
return config;
|
2014-03-15 13:57:34 +03:00
|
|
|
}
|
|
|
|
|
2022-09-17 15:43:59 +02:00
|
|
|
CModInfo::Version CModInfo::Version::GameVersion()
|
|
|
|
{
|
2022-10-04 11:29:38 +02:00
|
|
|
return Version(VCMI_VERSION_MAJOR, VCMI_VERSION_MINOR, VCMI_VERSION_PATCH);
|
2022-09-17 15:43:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CModInfo::Version CModInfo::Version::fromString(std::string from)
|
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
int major = 0;
|
|
|
|
int minor = 0;
|
|
|
|
int patch = 0;
|
2022-09-17 15:43:59 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
auto pointPos = from.find('.');
|
|
|
|
major = std::stoi(from.substr(0, pointPos));
|
|
|
|
if(pointPos != std::string::npos)
|
|
|
|
{
|
|
|
|
from = from.substr(pointPos + 1);
|
|
|
|
pointPos = from.find('.');
|
|
|
|
minor = std::stoi(from.substr(0, pointPos));
|
|
|
|
if(pointPos != std::string::npos)
|
|
|
|
patch = std::stoi(from.substr(pointPos + 1));
|
|
|
|
}
|
|
|
|
}
|
2022-12-08 23:20:42 +02:00
|
|
|
catch(const std::invalid_argument &)
|
2022-09-17 15:43:59 +02:00
|
|
|
{
|
|
|
|
return Version();
|
|
|
|
}
|
|
|
|
return Version(major, minor, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CModInfo::Version::toString() const
|
|
|
|
{
|
|
|
|
return std::to_string(major) + '.' + std::to_string(minor) + '.' + std::to_string(patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModInfo::Version::compatible(const Version & other, bool checkMinor, bool checkPatch) const
|
|
|
|
{
|
|
|
|
return (major == other.major &&
|
|
|
|
(!checkMinor || minor >= other.minor) &&
|
|
|
|
(!checkPatch || minor > other.minor || (minor == other.minor && patch >= other.patch)));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModInfo::Version::isNull() const
|
|
|
|
{
|
|
|
|
return major == 0 && minor == 0 && patch == 0;
|
|
|
|
}
|
|
|
|
|
2016-11-27 21:07:01 +02:00
|
|
|
CModInfo::CModInfo():
|
|
|
|
checksum(0),
|
2023-03-14 15:59:33 +02:00
|
|
|
explicitlyEnabled(false),
|
|
|
|
implicitlyEnabled(true),
|
2016-11-27 21:07:01 +02:00
|
|
|
validation(PENDING)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
CModInfo::CModInfo(const std::string & identifier, const JsonNode & local, const JsonNode & config):
|
2014-03-15 13:57:34 +03:00
|
|
|
identifier(identifier),
|
|
|
|
name(config["name"].String()),
|
|
|
|
description(config["description"].String()),
|
2023-03-13 23:26:44 +02:00
|
|
|
dependencies(config["depends"].convertTo<std::set<std::string>>()),
|
|
|
|
conflicts(config["conflicts"].convertTo<std::set<std::string>>()),
|
2016-11-27 21:07:01 +02:00
|
|
|
checksum(0),
|
2023-03-14 15:59:33 +02:00
|
|
|
explicitlyEnabled(false),
|
|
|
|
implicitlyEnabled(true),
|
2014-03-15 13:57:34 +03:00
|
|
|
validation(PENDING),
|
|
|
|
config(addMeta(config, identifier))
|
|
|
|
{
|
2022-09-17 15:43:59 +02:00
|
|
|
version = Version::fromString(config["version"].String());
|
|
|
|
if(!config["compatibility"].isNull())
|
|
|
|
{
|
|
|
|
vcmiCompatibleMin = Version::fromString(config["compatibility"]["min"].String());
|
|
|
|
vcmiCompatibleMax = Version::fromString(config["compatibility"]["max"].String());
|
|
|
|
}
|
2023-02-09 15:03:49 +02:00
|
|
|
|
|
|
|
if (!config["language"].isNull())
|
|
|
|
baseLanguage = config["language"].String();
|
|
|
|
else
|
|
|
|
baseLanguage = "english";
|
|
|
|
|
2014-03-15 13:57:34 +03:00
|
|
|
loadLocalData(local);
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode CModInfo::saveLocalData() const
|
|
|
|
{
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream << std::noshowbase << std::hex << std::setw(8) << std::setfill('0') << checksum;
|
|
|
|
|
|
|
|
JsonNode conf;
|
2023-03-14 15:59:33 +02:00
|
|
|
conf["active"].Bool() = explicitlyEnabled;
|
2014-03-15 13:57:34 +03:00
|
|
|
conf["validated"].Bool() = validation != FAILED;
|
|
|
|
conf["checksum"].String() = stream.str();
|
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
std::string CModInfo::getModDir(const std::string & name)
|
2014-03-15 13:57:34 +03:00
|
|
|
{
|
|
|
|
return "MODS/" + boost::algorithm::replace_all_copy(name, ".", "/MODS/");
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
std::string CModInfo::getModFile(const std::string & name)
|
2014-03-15 13:57:34 +03:00
|
|
|
{
|
|
|
|
return getModDir(name) + "/mod.json";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CModInfo::updateChecksum(ui32 newChecksum)
|
|
|
|
{
|
|
|
|
// comment-out next line to force validation of all mods ignoring checksum
|
|
|
|
if (newChecksum != checksum)
|
|
|
|
{
|
|
|
|
checksum = newChecksum;
|
|
|
|
validation = PENDING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CModInfo::loadLocalData(const JsonNode & data)
|
|
|
|
{
|
|
|
|
bool validated = false;
|
2023-03-14 15:59:33 +02:00
|
|
|
implicitlyEnabled = true;
|
2023-03-25 22:37:10 +02:00
|
|
|
explicitlyEnabled = !config["keepDisabled"].Bool();
|
2014-03-15 13:57:34 +03:00
|
|
|
checksum = 0;
|
2017-11-26 23:18:18 +02:00
|
|
|
if (data.getType() == JsonNode::JsonType::DATA_BOOL)
|
2014-03-15 13:57:34 +03:00
|
|
|
{
|
2023-03-14 15:59:33 +02:00
|
|
|
explicitlyEnabled = data.Bool();
|
2014-03-15 13:57:34 +03:00
|
|
|
}
|
2017-11-26 23:18:18 +02:00
|
|
|
if (data.getType() == JsonNode::JsonType::DATA_STRUCT)
|
2014-03-15 13:57:34 +03:00
|
|
|
{
|
2023-03-14 15:59:33 +02:00
|
|
|
explicitlyEnabled = data["active"].Bool();
|
2014-03-15 13:57:34 +03:00
|
|
|
validated = data["validated"].Bool();
|
|
|
|
checksum = strtol(data["checksum"].String().c_str(), nullptr, 16);
|
|
|
|
}
|
2023-01-16 23:20:24 +02:00
|
|
|
|
2022-09-17 15:43:59 +02:00
|
|
|
//check compatibility
|
2023-03-14 15:59:33 +02:00
|
|
|
implicitlyEnabled &= (vcmiCompatibleMin.isNull() || Version::GameVersion().compatible(vcmiCompatibleMin));
|
|
|
|
implicitlyEnabled &= (vcmiCompatibleMax.isNull() || vcmiCompatibleMax.compatible(Version::GameVersion()));
|
2022-09-18 23:18:17 +02:00
|
|
|
|
2023-03-14 15:59:33 +02:00
|
|
|
if(!implicitlyEnabled)
|
2022-09-17 15:43:59 +02:00
|
|
|
logGlobal->warn("Mod %s is incompatible with current version of VCMI and cannot be enabled", name);
|
2014-03-15 13:57:34 +03:00
|
|
|
|
2023-03-14 15:59:33 +02:00
|
|
|
if (boost::iequals(config["modType"].String(), "translation")) // compatibility code - mods use "Translation" type at the moment
|
|
|
|
{
|
|
|
|
if (baseLanguage != VLC->generaltexth->getPreferredLanguage())
|
|
|
|
{
|
|
|
|
logGlobal->warn("Translation mod %s was not loaded: language mismatch!", name);
|
|
|
|
implicitlyEnabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isEnabled())
|
2014-03-15 13:57:34 +03:00
|
|
|
validation = validated ? PASSED : PENDING;
|
|
|
|
else
|
|
|
|
validation = validated ? PASSED : FAILED;
|
|
|
|
}
|
|
|
|
|
2023-03-14 15:59:33 +02:00
|
|
|
bool CModInfo::isEnabled() const
|
|
|
|
{
|
|
|
|
return implicitlyEnabled && explicitlyEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CModInfo::setEnabled(bool on)
|
|
|
|
{
|
|
|
|
explicitlyEnabled = on;
|
|
|
|
}
|
|
|
|
|
2020-10-09 23:31:23 +02:00
|
|
|
CModHandler::CModHandler() : content(std::make_shared<CContentHandler>())
|
2012-08-10 16:07:53 +03:00
|
|
|
{
|
2012-12-26 12:46:09 +03:00
|
|
|
for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; ++i)
|
|
|
|
{
|
2022-12-07 15:18:19 +02:00
|
|
|
identifiers.registerObject(CModHandler::scopeBuiltin(), "resource", GameConstants::RESOURCE_NAMES[i], i);
|
2012-12-26 12:46:09 +03:00
|
|
|
}
|
|
|
|
|
2013-02-25 12:48:26 +03:00
|
|
|
for(int i=0; i<GameConstants::PRIMARY_SKILLS; ++i)
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
2022-12-07 15:18:19 +02:00
|
|
|
identifiers.registerObject(CModHandler::scopeBuiltin(), "primSkill", PrimarySkill::names[i], i);
|
|
|
|
identifiers.registerObject(CModHandler::scopeBuiltin(), "primarySkill", PrimarySkill::names[i], i);
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
2012-08-10 16:07:53 +03:00
|
|
|
}
|
|
|
|
|
2013-01-10 21:53:49 +03:00
|
|
|
// currentList is passed by value to get current list of depending mods
|
2023-03-13 23:26:44 +02:00
|
|
|
bool CModHandler::hasCircularDependency(const TModID & modID, std::set<TModID> currentList) const
|
2013-01-10 21:53:49 +03:00
|
|
|
{
|
|
|
|
const CModInfo & mod = allMods.at(modID);
|
|
|
|
|
|
|
|
// Mod already present? We found a loop
|
|
|
|
if (vstd::contains(currentList, modID))
|
|
|
|
{
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->error("Error: Circular dependency detected! Printing dependency list:");
|
|
|
|
logMod->error("\t%s -> ", mod.name);
|
2013-01-10 21:53:49 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentList.insert(modID);
|
|
|
|
|
|
|
|
// recursively check every dependency of this mod
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const TModID & dependency : mod.dependencies)
|
2013-01-10 21:53:49 +03:00
|
|
|
{
|
|
|
|
if (hasCircularDependency(dependency, currentList))
|
|
|
|
{
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->error("\t%s ->\n", mod.name); // conflict detected, print dependency list
|
2013-01-10 21:53:49 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-10 17:23:58 +02:00
|
|
|
// Returned vector affects the resource loaders call order (see CFilesystemList::load).
|
|
|
|
// The loaders call order matters when dependent mod overrides resources in its dependencies.
|
|
|
|
std::vector <TModID> CModHandler::validateAndSortDependencies(std::vector <TModID> modsToResolve) const
|
2013-01-10 21:53:49 +03:00
|
|
|
{
|
2021-04-10 17:23:58 +02:00
|
|
|
// Topological sort algorithm.
|
|
|
|
// TODO: Investigate possible ways to improve performance.
|
|
|
|
boost::range::sort(modsToResolve); // Sort mods per name
|
2023-01-16 23:20:24 +02:00
|
|
|
std::vector <TModID> sortedValidMods; // Vector keeps order of elements (LIFO)
|
2021-04-10 17:23:58 +02:00
|
|
|
sortedValidMods.reserve(modsToResolve.size()); // push_back calls won't cause memory reallocation
|
|
|
|
std::set <TModID> resolvedModIDs; // Use a set for validation for performance reason, but set does not keep order of elements
|
2013-04-21 15:49:26 +03:00
|
|
|
|
2021-04-10 17:23:58 +02:00
|
|
|
// Mod is resolved if it has not dependencies or all its dependencies are already resolved
|
|
|
|
auto isResolved = [&](const CModInfo & mod) -> CModInfo::EValidationStatus
|
2013-01-10 21:53:49 +03:00
|
|
|
{
|
2021-04-10 17:23:58 +02:00
|
|
|
if(mod.dependencies.size() > resolvedModIDs.size())
|
|
|
|
return CModInfo::PENDING;
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const TModID & dependency : mod.dependencies)
|
2013-01-10 21:53:49 +03:00
|
|
|
{
|
2021-04-10 17:23:58 +02:00
|
|
|
if(!vstd::contains(resolvedModIDs, dependency))
|
|
|
|
return CModInfo::PENDING;
|
2013-01-10 21:53:49 +03:00
|
|
|
}
|
2021-04-10 17:23:58 +02:00
|
|
|
return CModInfo::PASSED;
|
2013-01-10 21:53:49 +03:00
|
|
|
};
|
|
|
|
|
2021-01-20 10:23:58 +02:00
|
|
|
while(true)
|
2013-01-10 21:53:49 +03:00
|
|
|
{
|
2021-04-10 17:23:58 +02:00
|
|
|
std::set <TModID> resolvedOnCurrentTreeLevel;
|
|
|
|
for(auto it = modsToResolve.begin(); it != modsToResolve.end();) // One iteration - one level of mods tree
|
2021-01-20 10:23:58 +02:00
|
|
|
{
|
2021-04-10 17:23:58 +02:00
|
|
|
if(isResolved(allMods.at(*it)) == CModInfo::PASSED)
|
2013-01-10 21:53:49 +03:00
|
|
|
{
|
2021-04-10 17:23:58 +02:00
|
|
|
resolvedOnCurrentTreeLevel.insert(*it); // Not to the resolvedModIDs, so current node childs will be resolved on the next iteration
|
|
|
|
sortedValidMods.push_back(*it);
|
|
|
|
it = modsToResolve.erase(it);
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
continue;
|
2021-04-04 15:54:00 +02:00
|
|
|
}
|
|
|
|
it++;
|
2013-01-10 21:53:49 +03:00
|
|
|
}
|
2023-03-13 23:26:44 +02:00
|
|
|
if(!resolvedOnCurrentTreeLevel.empty())
|
2021-04-10 17:23:58 +02:00
|
|
|
{
|
|
|
|
resolvedModIDs.insert(resolvedOnCurrentTreeLevel.begin(), resolvedOnCurrentTreeLevel.end());
|
2023-01-16 23:20:24 +02:00
|
|
|
continue;
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
}
|
2021-04-10 17:23:58 +02:00
|
|
|
// If there're no valid mods on the current mods tree level, no more mod can be resolved, should be end.
|
2021-02-20 03:57:50 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-04-04 15:54:00 +02:00
|
|
|
|
2021-04-10 17:23:58 +02:00
|
|
|
// Left mods have unresolved dependencies, output all to log.
|
|
|
|
for(const auto & brokenModID : modsToResolve)
|
2021-04-04 15:54:00 +02:00
|
|
|
{
|
2021-04-10 17:23:58 +02:00
|
|
|
const CModInfo & brokenMod = allMods.at(brokenModID);
|
|
|
|
for(const TModID & dependency : brokenMod.dependencies)
|
|
|
|
{
|
|
|
|
if(!vstd::contains(resolvedModIDs, dependency))
|
|
|
|
logMod->error("Mod '%s' will not work: it depends on mod '%s', which is not installed.", brokenMod.name, dependency);
|
|
|
|
}
|
2021-04-04 15:54:00 +02:00
|
|
|
}
|
2021-04-10 17:23:58 +02:00
|
|
|
return sortedValidMods;
|
2021-02-20 03:57:50 +02:00
|
|
|
}
|
2013-01-10 21:53:49 +03:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
std::vector<std::string> CModHandler::getModList(const std::string & path) const
|
2012-08-11 12:06:23 +03:00
|
|
|
{
|
2014-03-15 13:57:34 +03:00
|
|
|
std::string modDir = boost::to_upper_copy(path + "MODS/");
|
|
|
|
size_t depth = boost::range::count(modDir, '/');
|
2013-04-03 00:39:32 +03:00
|
|
|
|
2014-03-15 13:57:34 +03:00
|
|
|
auto list = CResourceHandler::get("initial")->getFilteredFiles([&](const ResourceID & id) -> bool
|
|
|
|
{
|
|
|
|
if (id.getType() != EResType::DIRECTORY)
|
|
|
|
return false;
|
|
|
|
if (!boost::algorithm::starts_with(id.getName(), modDir))
|
|
|
|
return false;
|
|
|
|
if (boost::range::count(id.getName(), '/') != depth )
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
});
|
2013-11-11 23:11:51 +03:00
|
|
|
|
2014-03-15 13:57:34 +03:00
|
|
|
//storage for found mods
|
|
|
|
std::vector<std::string> foundMods;
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & entry : list)
|
2014-03-15 13:57:34 +03:00
|
|
|
{
|
|
|
|
std::string name = entry.getName();
|
|
|
|
name.erase(0, modDir.size()); //Remove path prefix
|
2013-11-11 23:11:51 +03:00
|
|
|
|
2014-03-15 13:57:34 +03:00
|
|
|
if (!name.empty())
|
|
|
|
foundMods.push_back(name);
|
2013-11-11 23:11:51 +03:00
|
|
|
}
|
2014-03-15 13:57:34 +03:00
|
|
|
return foundMods;
|
2013-11-11 23:11:51 +03:00
|
|
|
}
|
|
|
|
|
2022-12-07 15:18:19 +02:00
|
|
|
bool CModHandler::isScopeReserved(const TModID & scope)
|
|
|
|
{
|
2023-02-24 22:38:12 +02:00
|
|
|
//following scopes are reserved - either in use by mod system or by filesystem
|
|
|
|
static const std::array<TModID, 9> reservedScopes = {
|
|
|
|
"core", "map", "game", "root", "saves", "config", "local", "initial", "mapEditor"
|
2022-12-07 15:18:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return std::find(reservedScopes.begin(), reservedScopes.end(), scope) != reservedScopes.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
const TModID & CModHandler::scopeBuiltin()
|
|
|
|
{
|
|
|
|
static const TModID scope = "core";
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TModID & CModHandler::scopeGame()
|
|
|
|
{
|
|
|
|
static const TModID scope = "game";
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TModID & CModHandler::scopeMap()
|
|
|
|
{
|
|
|
|
//TODO: implement accessing map dependencies for both H3 and VCMI maps
|
|
|
|
// for now, allow access to any identifiers
|
|
|
|
static const TModID scope = "game";
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
void CModHandler::loadMods(const std::string & path, const std::string & parent, const JsonNode & modSettings, bool enableMods)
|
2013-11-11 23:11:51 +03:00
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const std::string & modName : getModList(path))
|
2017-07-20 06:08:49 +02:00
|
|
|
loadOneMod(modName, parent, modSettings, enableMods);
|
|
|
|
}
|
2013-11-11 23:11:51 +03:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
void CModHandler::loadOneMod(std::string modName, const std::string & parent, const JsonNode & modSettings, bool enableMods)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
boost::to_lower(modName);
|
|
|
|
std::string modFullName = parent.empty() ? modName : parent + '.' + modName;
|
2014-03-15 13:57:34 +03:00
|
|
|
|
2022-12-07 15:18:19 +02:00
|
|
|
if ( isScopeReserved(modFullName))
|
|
|
|
{
|
|
|
|
logMod->error("Can not load mod %s - this name is reserved for internal use!", modFullName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
if(CResourceHandler::get("initial")->existsResource(ResourceID(CModInfo::getModFile(modFullName))))
|
|
|
|
{
|
|
|
|
CModInfo mod(modFullName, modSettings[modName], JsonNode(ResourceID(CModInfo::getModFile(modFullName))));
|
|
|
|
if (!parent.empty()) // this is submod, add parent to dependencies
|
|
|
|
mod.dependencies.insert(parent);
|
2014-03-20 20:06:25 +03:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
allMods[modFullName] = mod;
|
2023-03-14 15:59:33 +02:00
|
|
|
if (mod.isEnabled() && enableMods)
|
2017-07-20 06:08:49 +02:00
|
|
|
activeMods.push_back(modFullName);
|
|
|
|
|
2023-03-14 15:59:33 +02:00
|
|
|
loadMods(CModInfo::getModDir(modFullName) + '/', modFullName, modSettings[modName]["mods"], enableMods && mod.isEnabled());
|
2014-03-15 13:57:34 +03:00
|
|
|
}
|
2013-11-09 22:10:16 +03:00
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
void CModHandler::loadMods(bool onlyEssential)
|
2013-11-09 22:10:16 +03:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
JsonNode modConfig;
|
2013-01-10 21:53:49 +03:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
if(onlyEssential)
|
|
|
|
{
|
|
|
|
loadOneMod("vcmi", "", modConfig, true);//only vcmi and submods
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
modConfig = loadModSettings("config/modSettings.json");
|
|
|
|
loadMods("", "", modConfig["activeMods"], true);
|
|
|
|
}
|
2013-11-09 22:10:16 +03:00
|
|
|
|
2022-12-07 15:18:19 +02:00
|
|
|
coreMod = CModInfo(CModHandler::scopeBuiltin(), modConfig[CModHandler::scopeBuiltin()], JsonNode(ResourceID("config/gameConfig.json")));
|
2013-11-11 23:11:51 +03:00
|
|
|
coreMod.name = "Original game files";
|
2014-03-15 13:57:34 +03:00
|
|
|
}
|
2012-08-11 12:06:23 +03:00
|
|
|
|
2014-03-15 13:57:34 +03:00
|
|
|
std::vector<std::string> CModHandler::getAllMods()
|
|
|
|
{
|
|
|
|
std::vector<std::string> modlist;
|
2023-03-13 23:26:44 +02:00
|
|
|
modlist.reserve(allMods.size());
|
2014-03-15 13:57:34 +03:00
|
|
|
for (auto & entry : allMods)
|
|
|
|
modlist.push_back(entry.first);
|
|
|
|
return modlist;
|
|
|
|
}
|
2013-01-10 21:53:49 +03:00
|
|
|
|
2014-03-15 13:57:34 +03:00
|
|
|
std::vector<std::string> CModHandler::getActiveMods()
|
|
|
|
{
|
|
|
|
return activeMods;
|
2013-11-08 23:36:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static JsonNode genDefaultFS()
|
|
|
|
{
|
|
|
|
// default FS config for mods: directory "Content" that acts as H3 root directory
|
|
|
|
JsonNode defaultFS;
|
|
|
|
defaultFS[""].Vector().resize(2);
|
|
|
|
defaultFS[""].Vector()[0]["type"].String() = "zip";
|
|
|
|
defaultFS[""].Vector()[0]["path"].String() = "/Content.zip";
|
|
|
|
defaultFS[""].Vector()[1]["type"].String() = "dir";
|
|
|
|
defaultFS[""].Vector()[1]["path"].String() = "/Content";
|
|
|
|
return defaultFS;
|
2012-08-11 12:06:23 +03:00
|
|
|
}
|
2012-08-30 17:57:24 +03:00
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
static ISimpleResourceLoader * genModFilesystem(const std::string & modName, const JsonNode & conf)
|
2012-11-13 14:52:23 +03:00
|
|
|
{
|
2013-11-08 23:36:26 +03:00
|
|
|
static const JsonNode defaultFS = genDefaultFS();
|
|
|
|
|
|
|
|
if (!conf["filesystem"].isNull())
|
2014-03-15 13:57:34 +03:00
|
|
|
return CResourceHandler::createFileSystem(CModInfo::getModDir(modName), conf["filesystem"]);
|
2013-11-08 23:36:26 +03:00
|
|
|
else
|
2014-03-15 13:57:34 +03:00
|
|
|
return CResourceHandler::createFileSystem(CModInfo::getModDir(modName), defaultFS);
|
2013-11-08 23:36:26 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
static ui32 calculateModChecksum(const std::string & modName, ISimpleResourceLoader * filesystem)
|
2013-11-08 23:36:26 +03:00
|
|
|
{
|
|
|
|
boost::crc_32_type modChecksum;
|
|
|
|
// first - add current VCMI version into checksum to force re-validation on VCMI updates
|
|
|
|
modChecksum.process_bytes(reinterpret_cast<const void*>(GameConstants::VCMI_VERSION.data()), GameConstants::VCMI_VERSION.size());
|
|
|
|
|
|
|
|
// second - add mod.json into checksum because filesystem does not contains this file
|
2013-11-11 23:11:51 +03:00
|
|
|
// FIXME: remove workaround for core mod
|
2022-12-07 15:18:19 +02:00
|
|
|
if (modName != CModHandler::scopeBuiltin())
|
2013-11-11 23:11:51 +03:00
|
|
|
{
|
2014-03-15 13:57:34 +03:00
|
|
|
ResourceID modConfFile(CModInfo::getModFile(modName), EResType::TEXT);
|
2014-03-08 19:05:23 +03:00
|
|
|
ui32 configChecksum = CResourceHandler::get("initial")->load(modConfFile)->calculateCRC32();
|
2013-11-11 23:11:51 +03:00
|
|
|
modChecksum.process_bytes(reinterpret_cast<const void *>(&configChecksum), sizeof(configChecksum));
|
|
|
|
}
|
|
|
|
// third - add all detected text files from this mod into checksum
|
2013-11-08 23:36:26 +03:00
|
|
|
auto files = filesystem->getFilteredFiles([](const ResourceID & resID)
|
|
|
|
{
|
2013-11-11 23:11:51 +03:00
|
|
|
return resID.getType() == EResType::TEXT &&
|
|
|
|
( boost::starts_with(resID.getName(), "DATA") ||
|
|
|
|
boost::starts_with(resID.getName(), "CONFIG"));
|
2013-11-08 23:36:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
for (const ResourceID & file : files)
|
|
|
|
{
|
|
|
|
ui32 fileChecksum = filesystem->load(file)->calculateCRC32();
|
|
|
|
modChecksum.process_bytes(reinterpret_cast<const void *>(&fileChecksum), sizeof(fileChecksum));
|
|
|
|
}
|
|
|
|
return modChecksum.checksum();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CModHandler::loadModFilesystems()
|
|
|
|
{
|
2023-03-14 15:59:33 +02:00
|
|
|
CGeneralTextHandler::detectInstallParameters();
|
|
|
|
|
2021-04-10 17:23:58 +02:00
|
|
|
activeMods = validateAndSortDependencies(activeMods);
|
2014-03-15 13:57:34 +03:00
|
|
|
|
2022-12-07 15:18:19 +02:00
|
|
|
coreMod.updateChecksum(calculateModChecksum(CModHandler::scopeBuiltin(), CResourceHandler::get(CModHandler::scopeBuiltin())));
|
2013-11-11 23:11:51 +03:00
|
|
|
|
2013-11-08 23:36:26 +03:00
|
|
|
for(std::string & modName : activeMods)
|
|
|
|
{
|
2013-11-11 23:11:51 +03:00
|
|
|
CModInfo & mod = allMods[modName];
|
2014-03-08 19:05:23 +03:00
|
|
|
CResourceHandler::addFilesystem("data", modName, genModFilesystem(modName, mod.config));
|
2013-11-08 23:36:26 +03:00
|
|
|
}
|
2012-11-13 14:52:23 +03:00
|
|
|
}
|
2012-09-20 21:41:16 +03:00
|
|
|
|
2023-02-24 22:38:12 +02:00
|
|
|
TModID CModHandler::findResourceOrigin(const ResourceID & name)
|
2023-02-09 15:03:49 +02:00
|
|
|
{
|
2023-02-24 22:38:12 +02:00
|
|
|
for(const auto & modID : boost::adaptors::reverse(activeMods))
|
|
|
|
{
|
|
|
|
if(CResourceHandler::get(modID)->existsResource(name))
|
|
|
|
return modID;
|
|
|
|
}
|
|
|
|
|
2023-02-25 01:11:42 +02:00
|
|
|
if(CResourceHandler::get("core")->existsResource(name))
|
|
|
|
return "core";
|
|
|
|
|
2023-03-14 15:59:33 +02:00
|
|
|
if(CResourceHandler::get("mapEditor")->existsResource(name))
|
|
|
|
return "core"; // Workaround for loading maps via map editor
|
|
|
|
|
2023-02-24 22:38:12 +02:00
|
|
|
assert(0);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CModHandler::getModLanguage(const TModID& modId) const
|
|
|
|
{
|
|
|
|
if ( modId == "core")
|
|
|
|
return VLC->generaltexth->getInstalledLanguage();
|
2023-02-09 15:03:49 +02:00
|
|
|
return allMods.at(modId).baseLanguage;
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
std::set<TModID> CModHandler::getModDependencies(const TModID & modId, bool & isModFound) const
|
2013-04-25 17:03:35 +03:00
|
|
|
{
|
2016-02-21 19:58:09 +02:00
|
|
|
auto it = allMods.find(modId);
|
2022-03-21 00:33:04 +02:00
|
|
|
isModFound = (it != allMods.end());
|
2016-02-21 19:58:09 +02:00
|
|
|
|
2022-03-21 00:33:04 +02:00
|
|
|
if(isModFound)
|
|
|
|
return it->second.dependencies;
|
|
|
|
|
|
|
|
logMod->error("Mod not found: '%s'", modId);
|
2023-02-09 15:03:49 +02:00
|
|
|
return {};
|
2013-04-25 17:03:35 +03:00
|
|
|
}
|
2013-11-09 22:10:16 +03:00
|
|
|
|
|
|
|
void CModHandler::initializeConfig()
|
2012-11-13 14:52:23 +03:00
|
|
|
{
|
2023-03-15 21:34:29 +02:00
|
|
|
VLC->settingsHandler->load(coreMod.config["settings"]);
|
|
|
|
|
|
|
|
for(const TModID & modName : activeMods)
|
|
|
|
{
|
|
|
|
const auto & mod = allMods[modName];
|
2023-03-16 17:39:35 +02:00
|
|
|
if (!mod.config["settings"].isNull())
|
|
|
|
VLC->settingsHandler->load(mod.config["settings"]);
|
2023-03-15 21:34:29 +02:00
|
|
|
}
|
2013-04-28 18:06:14 +03:00
|
|
|
}
|
2013-04-26 00:50:55 +03:00
|
|
|
|
2023-02-09 19:15:39 +02:00
|
|
|
bool CModHandler::validateTranslations(TModID modName) const
|
2023-01-16 23:20:24 +02:00
|
|
|
{
|
2023-02-09 19:15:39 +02:00
|
|
|
bool result = true;
|
|
|
|
const auto & mod = allMods.at(modName);
|
2023-01-16 23:20:24 +02:00
|
|
|
|
2023-02-09 15:03:49 +02:00
|
|
|
{
|
2023-02-09 19:15:39 +02:00
|
|
|
auto fileList = mod.config["translations"].convertTo<std::vector<std::string> >();
|
|
|
|
JsonNode json = JsonUtils::assembleFromFiles(fileList);
|
|
|
|
result |= VLC->generaltexth->validateTranslation(mod.baseLanguage, modName, json);
|
2023-02-09 15:03:49 +02:00
|
|
|
}
|
2023-01-16 23:20:24 +02:00
|
|
|
|
2023-02-24 22:38:12 +02:00
|
|
|
for(const auto & language : Languages::getLanguageList())
|
2023-02-09 15:03:49 +02:00
|
|
|
{
|
2023-02-09 22:26:25 +02:00
|
|
|
if (!language.hasTranslation)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (mod.config[language.identifier].isNull())
|
2023-02-09 19:15:39 +02:00
|
|
|
continue;
|
2023-02-09 15:03:49 +02:00
|
|
|
|
2023-03-07 00:06:22 +02:00
|
|
|
if (mod.config[language.identifier]["skipValidation"].Bool())
|
|
|
|
continue;
|
|
|
|
|
2023-02-09 22:26:25 +02:00
|
|
|
auto fileList = mod.config[language.identifier]["translations"].convertTo<std::vector<std::string> >();
|
2023-02-09 19:15:39 +02:00
|
|
|
JsonNode json = JsonUtils::assembleFromFiles(fileList);
|
2023-02-09 22:26:25 +02:00
|
|
|
result |= VLC->generaltexth->validateTranslation(language.identifier, modName, json);
|
2023-02-09 15:03:49 +02:00
|
|
|
}
|
2023-02-09 19:15:39 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
void CModHandler::loadTranslation(const TModID & modName)
|
2023-02-09 19:15:39 +02:00
|
|
|
{
|
2023-02-24 22:38:12 +02:00
|
|
|
const auto & mod = allMods[modName];
|
2023-02-09 19:15:39 +02:00
|
|
|
|
|
|
|
std::string preferredLanguage = VLC->generaltexth->getPreferredLanguage();
|
|
|
|
std::string modBaseLanguage = allMods[modName].baseLanguage;
|
|
|
|
|
|
|
|
auto baseTranslationList = mod.config["translations"].convertTo<std::vector<std::string> >();
|
|
|
|
auto extraTranslationList = mod.config[preferredLanguage]["translations"].convertTo<std::vector<std::string> >();
|
|
|
|
|
|
|
|
JsonNode baseTranslation = JsonUtils::assembleFromFiles(baseTranslationList);
|
|
|
|
JsonNode extraTranslation = JsonUtils::assembleFromFiles(extraTranslationList);
|
|
|
|
|
|
|
|
VLC->generaltexth->loadTranslationOverrides(modBaseLanguage, modName, baseTranslation);
|
|
|
|
VLC->generaltexth->loadTranslationOverrides(preferredLanguage, modName, extraTranslation);
|
2023-01-16 23:20:24 +02:00
|
|
|
}
|
|
|
|
|
2013-11-09 22:10:16 +03:00
|
|
|
void CModHandler::load()
|
2013-04-28 18:06:14 +03:00
|
|
|
{
|
2023-02-09 19:15:39 +02:00
|
|
|
CStopWatch totalTime;
|
|
|
|
CStopWatch timer;
|
2013-11-09 22:10:16 +03:00
|
|
|
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->info("\tInitializing content handler: %d ms", timer.getDiff());
|
2013-04-21 15:49:26 +03:00
|
|
|
|
2020-10-09 23:31:23 +02:00
|
|
|
content->init();
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2013-12-11 20:12:39 +03:00
|
|
|
for(const TModID & modName : activeMods)
|
|
|
|
{
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->trace("Generating checksum for %s", modName);
|
2013-12-11 20:12:39 +03:00
|
|
|
allMods[modName].updateChecksum(calculateModChecksum(modName, CResourceHandler::get(modName)));
|
|
|
|
}
|
|
|
|
|
2022-12-07 15:18:19 +02:00
|
|
|
// first - load virtual builtin mod that contains all data
|
2013-04-21 15:49:26 +03:00
|
|
|
// TODO? move all data into real mods? RoE, AB, SoD, WoG
|
2020-10-09 23:31:23 +02:00
|
|
|
content->preloadData(coreMod);
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const TModID & modName : activeMods)
|
2020-10-09 23:31:23 +02:00
|
|
|
content->preloadData(allMods[modName]);
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->info("\tParsing mod data: %d ms", timer.getDiff());
|
2013-04-21 15:49:26 +03:00
|
|
|
|
2020-10-09 23:31:23 +02:00
|
|
|
content->load(coreMod);
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const TModID & modName : activeMods)
|
2020-10-09 23:31:23 +02:00
|
|
|
content->load(allMods[modName]);
|
2013-11-09 22:10:16 +03:00
|
|
|
|
2022-09-21 18:31:14 +02:00
|
|
|
#if SCRIPTING_ENABLED
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
VLC->scriptHandler->performRegistration(VLC);//todo: this should be done before any other handlers load
|
2022-09-21 18:31:14 +02:00
|
|
|
#endif
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
|
2020-10-09 23:31:23 +02:00
|
|
|
content->loadCustom();
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2023-02-09 19:15:39 +02:00
|
|
|
for(const TModID & modName : activeMods)
|
|
|
|
loadTranslation(modName);
|
|
|
|
|
|
|
|
for(const TModID & modName : activeMods)
|
|
|
|
if (!validateTranslations(modName))
|
|
|
|
allMods[modName].validation = CModInfo::FAILED;
|
|
|
|
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->info("\tLoading mod data: %d ms", timer.getDiff());
|
2023-04-11 17:16:18 +02:00
|
|
|
VLC->creh->loadCrExpMod();
|
2012-12-03 19:00:17 +03:00
|
|
|
identifiers.finalize();
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->info("\tResolving identifiers: %d ms", timer.getDiff());
|
2013-07-21 17:19:29 +03:00
|
|
|
|
2020-10-09 23:31:23 +02:00
|
|
|
content->afterLoadFinalization();
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->info("\tHandlers post-load finalization: %d ms ", timer.getDiff());
|
|
|
|
logMod->info("\tAll game content loaded in %d ms", totalTime.getDiff());
|
2012-11-13 14:52:23 +03:00
|
|
|
}
|
2012-08-30 17:57:24 +03:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
void CModHandler::afterLoad(bool onlyEssential)
|
2013-11-09 22:10:16 +03:00
|
|
|
{
|
|
|
|
JsonNode modSettings;
|
|
|
|
for (auto & modEntry : allMods)
|
2014-03-15 13:57:34 +03:00
|
|
|
{
|
|
|
|
std::string pointer = "/" + boost::algorithm::replace_all_copy(modEntry.first, ".", "/mods/");
|
|
|
|
|
|
|
|
modSettings["activeMods"].resolvePointer(pointer) = modEntry.second.saveLocalData();
|
|
|
|
}
|
2022-12-07 15:18:19 +02:00
|
|
|
modSettings[CModHandler::scopeBuiltin()] = coreMod.saveLocalData();
|
2013-11-09 22:10:16 +03:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
if(!onlyEssential)
|
|
|
|
{
|
|
|
|
FileStream file(*CResourceHandler::get()->getResourceName(ResourceID("config/modSettings.json")), std::ofstream::out | std::ofstream::trunc);
|
|
|
|
file << modSettings.toJson();
|
|
|
|
}
|
|
|
|
|
2012-08-10 16:07:53 +03:00
|
|
|
}
|
2015-11-16 16:38:42 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
std::string CModHandler::normalizeIdentifier(const std::string & scope, const std::string & remoteScope, const std::string & identifier)
|
2015-11-16 16:38:42 +02:00
|
|
|
{
|
2020-12-13 02:33:28 +02:00
|
|
|
auto p = vstd::splitStringToPair(identifier, ':');
|
2015-11-16 16:38:42 +02:00
|
|
|
|
|
|
|
if(p.first.empty())
|
|
|
|
p.first = scope;
|
|
|
|
|
|
|
|
if(p.first == remoteScope)
|
|
|
|
p.first.clear();
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
return p.first.empty() ? p.second : p.first + ":" + p.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CModHandler::parseIdentifier(const std::string & fullIdentifier, std::string & scope, std::string & type, std::string & identifier)
|
|
|
|
{
|
2020-12-13 02:33:28 +02:00
|
|
|
auto p = vstd::splitStringToPair(fullIdentifier, ':');
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
scope = p.first;
|
|
|
|
|
2020-12-13 02:33:28 +02:00
|
|
|
auto p2 = vstd::splitStringToPair(p.second, '.');
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
if(!p2.first.empty())
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
|
|
|
type = p2.first;
|
|
|
|
identifier = p2.second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
type = p.second;
|
2022-11-15 02:20:55 +02:00
|
|
|
identifier.clear();
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CModHandler::makeFullIdentifier(const std::string & scope, const std::string & type, const std::string & identifier)
|
|
|
|
{
|
2022-11-15 02:20:55 +02:00
|
|
|
if(type.empty())
|
2017-07-20 06:08:49 +02:00
|
|
|
logGlobal->error("Full identifier (%s %s) requires type name", scope, identifier);
|
|
|
|
|
|
|
|
std::string actualScope = scope;
|
|
|
|
std::string actualName = identifier;
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
//ignore scope if identifier is scoped
|
2020-12-13 02:33:28 +02:00
|
|
|
auto scopeAndName = vstd::splitStringToPair(identifier, ':');
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
if(!scopeAndName.first.empty())
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
actualScope = scopeAndName.first;
|
|
|
|
actualName = scopeAndName.second;
|
|
|
|
}
|
|
|
|
|
2022-11-15 02:20:55 +02:00
|
|
|
if(actualScope.empty())
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
2022-11-15 02:20:55 +02:00
|
|
|
return actualName.empty() ? type : type + "." + actualName;
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
2016-11-13 12:38:42 +02:00
|
|
|
else
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
2022-11-15 02:20:55 +02:00
|
|
|
return actualName.empty() ? actualScope+ ":" + type : actualScope + ":" + type + "." + actualName;
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
2015-11-16 16:38:42 +02:00
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|