1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-29 23:07:48 +02:00

Merge pull request #2889 from Nordsoft91/mod-compatibility-check

Proper mod compatibility check logic
This commit is contained in:
Nordsoft91
2023-09-26 19:29:46 +02:00
committed by GitHub
29 changed files with 282 additions and 152 deletions

View File

@@ -56,7 +56,7 @@ bool CModHandler::hasCircularDependency(const TModID & modID, std::set<TModID> c
if (vstd::contains(currentList, modID))
{
logMod->error("Error: Circular dependency detected! Printing dependency list:");
logMod->error("\t%s -> ", mod.name);
logMod->error("\t%s -> ", mod.getVerificationInfo().name);
return true;
}
@@ -67,7 +67,7 @@ bool CModHandler::hasCircularDependency(const TModID & modID, std::set<TModID> c
{
if (hasCircularDependency(dependency, currentList))
{
logMod->error("\t%s ->\n", mod.name); // conflict detected, print dependency list
logMod->error("\t%s ->\n", mod.getVerificationInfo().name); // conflict detected, print dependency list
return true;
}
}
@@ -129,7 +129,7 @@ std::vector <TModID> CModHandler::validateAndSortDependencies(std::vector <TModI
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);
logMod->error("Mod '%s' will not work: it depends on mod '%s', which is not installed.", brokenMod.getVerificationInfo().name, dependency);
}
}
return sortedValidMods;
@@ -212,7 +212,6 @@ void CModHandler::loadMods(bool onlyEssential)
}
coreMod = std::make_unique<CModInfo>(ModScope::scopeBuiltin(), modConfig[ModScope::scopeBuiltin()], JsonNode(JsonPath::builtin("config/gameConfig.json")));
coreMod->name = "Original game files";
}
std::vector<std::string> CModHandler::getAllMods()
@@ -352,7 +351,7 @@ void CModHandler::initializeConfig()
CModVersion CModHandler::getModVersion(TModID modName) const
{
if (allMods.count(modName))
return allMods.at(modName).version;
return allMods.at(modName).getVerificationInfo().version;
return {};
}
@@ -462,6 +461,7 @@ void CModHandler::afterLoad(bool onlyEssential)
modSettings["activeMods"].resolvePointer(pointer) = modEntry.second.saveLocalData();
}
modSettings[ModScope::scopeBuiltin()] = coreMod->saveLocalData();
modSettings[ModScope::scopeBuiltin()]["name"].String() = "Original game files";
if(!onlyEssential)
{
@@ -471,49 +471,85 @@ void CModHandler::afterLoad(bool onlyEssential)
}
void CModHandler::trySetActiveMods(std::vector<TModID> saveActiveMods, const std::map<TModID, CModVersion> & modList)
void CModHandler::trySetActiveMods(const std::vector<std::pair<TModID, CModInfo::VerificationInfo>> & modList)
{
std::vector<TModID> newActiveMods;
ModIncompatibility::ModList missingMods;
auto searchVerificationInfo = [&modList](const TModID & m) -> const CModInfo::VerificationInfo*
{
for(auto & i : modList)
if(i.first == m)
return &i.second;
return nullptr;
};
std::vector<TModID> missingMods, excessiveMods;
ModIncompatibility::ModListWithVersion missingModsResult;
ModIncompatibility::ModList excessiveModsResult;
for(const auto & m : activeMods)
{
if (vstd::contains(saveActiveMods, m))
if(searchVerificationInfo(m))
continue;
auto & modInfo = allMods.at(m);
if(modInfo.checkModGameplayAffecting())
missingMods.emplace_back(m, modInfo.version.toString());
//TODO: support actual disabling of these mods
if(getModInfo(m).checkModGameplayAffecting())
excessiveMods.push_back(m);
}
for(const auto & m : saveActiveMods)
for(const auto & infoPair : modList)
{
const CModVersion & mver = modList.at(m);
if (allMods.count(m) == 0)
auto & remoteModId = infoPair.first;
auto & remoteModInfo = infoPair.second;
bool modAffectsGameplay = remoteModInfo.impactsGameplay;
//parent mod affects gameplay if child affects too
for(const auto & subInfoPair : modList)
modAffectsGameplay |= (subInfoPair.second.impactsGameplay && subInfoPair.second.parent == remoteModId);
if(!allMods.count(remoteModId))
{
missingMods.emplace_back(m, mver.toString());
if(modAffectsGameplay)
missingMods.push_back(remoteModId); //mod is not installed
continue;
}
auto & modInfo = allMods.at(m);
bool modAffectsGameplay = modInfo.checkModGameplayAffecting();
bool modVersionCompatible = modInfo.version.isNull() || mver.isNull() || modInfo.version.compatible(mver);
bool modEnabledLocally = vstd::contains(activeMods, m);
bool modCanBeEnabled = modEnabledLocally && modVersionCompatible;
allMods[m].setEnabled(modCanBeEnabled);
if (modCanBeEnabled)
newActiveMods.push_back(m);
if (!modCanBeEnabled && modAffectsGameplay)
missingMods.emplace_back(m, mver.toString());
auto & localModInfo = getModInfo(remoteModId).getVerificationInfo();
modAffectsGameplay |= getModInfo(remoteModId).checkModGameplayAffecting();
bool modVersionCompatible = localModInfo.version.isNull()
|| remoteModInfo.version.isNull()
|| localModInfo.version.compatible(remoteModInfo.version);
bool modLocalyEnabled = vstd::contains(activeMods, remoteModId);
if(modVersionCompatible && modAffectsGameplay && modLocalyEnabled)
continue;
if(modAffectsGameplay)
missingMods.push_back(remoteModId); //incompatible mod impacts gameplay
}
std::swap(activeMods, newActiveMods);
//filter mods
for(auto & m : missingMods)
{
if(auto * vInfo = searchVerificationInfo(m))
{
assert(vInfo->parent != m);
if(!vInfo->parent.empty() && vstd::contains(missingMods, vInfo->parent))
continue;
missingModsResult.push_back({vInfo->name, vInfo->version.toString()});
}
}
for(auto & m : excessiveMods)
{
auto & vInfo = getModInfo(m).getVerificationInfo();
assert(vInfo.parent != m);
if(!vInfo.parent.empty() && vstd::contains(excessiveMods, vInfo.parent))
continue;
excessiveModsResult.push_back(vInfo.name);
}
if(!missingModsResult.empty() || !excessiveModsResult.empty())
throw ModIncompatibility(missingModsResult, excessiveModsResult);
//TODO: support actual enabling of required mods
}
VCMI_LIB_NAMESPACE_END

View File

@@ -9,13 +9,12 @@
*/
#pragma once
#include "CModVersion.h"
#include "CModInfo.h"
VCMI_LIB_NAMESPACE_BEGIN
class CModHandler;
class CModIndentifier;
class CModInfo;
class JsonNode;
class IHandlerBase;
class CIdentifierStorage;
@@ -52,7 +51,7 @@ class DLL_LINKAGE CModHandler : boost::noncopyable
CModVersion getModVersion(TModID modName) const;
/// Attempt to set active mods according to provided list of mods from save, throws on failure
void trySetActiveMods(std::vector<TModID> saveActiveMods, const std::map<TModID, CModVersion> & modList);
void trySetActiveMods(const std::vector<std::pair<TModID, CModInfo::VerificationInfo>> & modList);
public:
std::shared_ptr<CContentHandler> content; //(!)Do not serialize FIXME: make private
@@ -88,22 +87,22 @@ public:
{
h & activeMods;
for(const auto & m : activeMods)
{
CModVersion version = getModVersion(m);
h & version;
}
h & getModInfo(m).getVerificationInfo();
}
else
{
loadMods();
std::vector<TModID> saveActiveMods;
std::map<TModID, CModVersion> modVersions;
h & saveActiveMods;
std::vector<std::pair<TModID, CModInfo::VerificationInfo>> saveModInfos(saveActiveMods.size());
for(int i = 0; i < saveActiveMods.size(); ++i)
{
saveModInfos[i].first = saveActiveMods[i];
h & saveModInfos[i].second;
}
for(const auto & m : saveActiveMods)
h & modVersions[m];
trySetActiveMods(saveActiveMods, modVersions);
trySetActiveMods(saveModInfos);
}
}
};

View File

@@ -23,7 +23,6 @@ static JsonNode addMeta(JsonNode config, const std::string & meta)
}
CModInfo::CModInfo():
checksum(0),
explicitlyEnabled(false),
implicitlyEnabled(true),
validation(PENDING)
@@ -33,17 +32,20 @@ CModInfo::CModInfo():
CModInfo::CModInfo(const std::string & identifier, const JsonNode & local, const JsonNode & config):
identifier(identifier),
name(config["name"].String()),
description(config["description"].String()),
dependencies(config["depends"].convertTo<std::set<std::string>>()),
conflicts(config["conflicts"].convertTo<std::set<std::string>>()),
checksum(0),
explicitlyEnabled(false),
implicitlyEnabled(true),
validation(PENDING),
config(addMeta(config, identifier))
{
version = CModVersion::fromString(config["version"].String());
verificationInfo.name = config["name"].String();
verificationInfo.version = CModVersion::fromString(config["version"].String());
verificationInfo.parent = identifier.substr(0, identifier.find_last_of('.'));
if(verificationInfo.parent == identifier)
verificationInfo.parent.clear();
if(!config["compatibility"].isNull())
{
vcmiCompatibleMin = CModVersion::fromString(config["compatibility"]["min"].String());
@@ -61,7 +63,7 @@ CModInfo::CModInfo(const std::string & identifier, const JsonNode & local, const
JsonNode CModInfo::saveLocalData() const
{
std::ostringstream stream;
stream << std::noshowbase << std::hex << std::setw(8) << std::setfill('0') << checksum;
stream << std::noshowbase << std::hex << std::setw(8) << std::setfill('0') << verificationInfo.checksum;
JsonNode conf;
conf["active"].Bool() = explicitlyEnabled;
@@ -83,9 +85,9 @@ JsonPath CModInfo::getModFile(const std::string & name)
void CModInfo::updateChecksum(ui32 newChecksum)
{
// comment-out next line to force validation of all mods ignoring checksum
if (newChecksum != checksum)
if (newChecksum != verificationInfo.checksum)
{
checksum = newChecksum;
verificationInfo.checksum = newChecksum;
validation = PENDING;
}
}
@@ -95,7 +97,7 @@ void CModInfo::loadLocalData(const JsonNode & data)
bool validated = false;
implicitlyEnabled = true;
explicitlyEnabled = !config["keepDisabled"].Bool();
checksum = 0;
verificationInfo.checksum = 0;
if (data.getType() == JsonNode::JsonType::DATA_BOOL)
{
explicitlyEnabled = data.Bool();
@@ -104,7 +106,7 @@ void CModInfo::loadLocalData(const JsonNode & data)
{
explicitlyEnabled = data["active"].Bool();
validated = data["validated"].Bool();
checksum = strtol(data["checksum"].String().c_str(), nullptr, 16);
updateChecksum(strtol(data["checksum"].String().c_str(), nullptr, 16));
}
//check compatibility
@@ -112,13 +114,13 @@ void CModInfo::loadLocalData(const JsonNode & data)
implicitlyEnabled &= (vcmiCompatibleMax.isNull() || vcmiCompatibleMax.compatible(CModVersion::GameVersion(), true, true));
if(!implicitlyEnabled)
logGlobal->warn("Mod %s is incompatible with current version of VCMI and cannot be enabled", name);
logGlobal->warn("Mod %s is incompatible with current version of VCMI and cannot be enabled", verificationInfo.name);
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);
logGlobal->warn("Translation mod %s was not loaded: language mismatch!", verificationInfo.name);
implicitlyEnabled = false;
}
}
@@ -127,6 +129,8 @@ void CModInfo::loadLocalData(const JsonNode & data)
validation = validated ? PASSED : PENDING;
else
validation = validated ? PASSED : FAILED;
verificationInfo.impactsGameplay = checkModGameplayAffecting();
}
bool CModInfo::checkModGameplayAffecting() const
@@ -171,6 +175,11 @@ bool CModInfo::checkModGameplayAffecting() const
return *modGameplayAffecting;
}
const CModInfo::VerificationInfo & CModInfo::getVerificationInfo() const
{
return verificationInfo;
}
bool CModInfo::isEnabled() const
{
return implicitlyEnabled && explicitlyEnabled;

View File

@@ -29,17 +29,41 @@ public:
FAILED,
PASSED
};
struct VerificationInfo
{
/// human-readable mod name
std::string name;
/// version of the mod
CModVersion version;
/// CRC-32 checksum of the mod
ui32 checksum = 0;
/// parent mod ID, empty if root-level mod
TModID parent;
/// for serialization purposes
bool impactsGameplay = true;
template <typename Handler>
void serialize(Handler & h, const int v)
{
h & name;
h & version;
h & checksum;
h & parent;
h & impactsGameplay;
}
};
/// identifier, identical to name of folder with mod
std::string identifier;
/// human-readable strings
std::string name;
/// detailed mod description
std::string description;
/// version of the mod
CModVersion version;
/// Base language of mod, all mod strings are assumed to be in this language
std::string baseLanguage;
@@ -52,9 +76,6 @@ public:
/// list of mods that can't be used in the same time as this one
std::set <TModID> conflicts;
/// CRC-32 checksum of the mod
ui32 checksum;
EValidationStatus validation;
JsonNode config;
@@ -73,6 +94,8 @@ public:
/// return true if this mod can affect gameplay, e.g. adds or modifies any game objects
bool checkModGameplayAffecting() const;
const VerificationInfo & getVerificationInfo() const;
private:
/// true if mod is enabled by user, e.g. in Launcher UI
@@ -80,6 +103,8 @@ private:
/// true if mod can be loaded - compatible and has no missing deps
bool implicitlyEnabled;
VerificationInfo verificationInfo;
void loadLocalData(const JsonNode & data);
};

View File

@@ -212,7 +212,8 @@ void CContentHandler::preloadData(CModInfo & mod)
bool validate = (mod.validation != CModInfo::PASSED);
// print message in format [<8-symbols checksum>] <modname>
logMod->info("\t\t[%08x]%s", mod.checksum, mod.name);
auto & info = mod.getVerificationInfo();
logMod->info("\t\t[%08x]%s", info.checksum, info.name);
if (validate && mod.identifier != ModScope::scopeBuiltin())
{
@@ -233,12 +234,12 @@ void CContentHandler::load(CModInfo & mod)
if (validate)
{
if (mod.validation != CModInfo::FAILED)
logMod->info("\t\t[DONE] %s", mod.name);
logMod->info("\t\t[DONE] %s", mod.getVerificationInfo().name);
else
logMod->error("\t\t[FAIL] %s", mod.name);
logMod->error("\t\t[FAIL] %s", mod.getVerificationInfo().name);
}
else
logMod->info("\t\t[SKIP] %s", mod.name);
logMod->info("\t\t[SKIP] %s", mod.getVerificationInfo().name);
}
const ContentTypeHandler & CContentHandler::operator[](const std::string & name) const

View File

@@ -14,29 +14,44 @@ VCMI_LIB_NAMESPACE_BEGIN
class DLL_LINKAGE ModIncompatibility: public std::exception
{
public:
using StringPair = std::pair<const std::string, const std::string>;
using ModList = std::list<StringPair>;
using ModListWithVersion = std::vector<std::pair<const std::string, const std::string>>;
using ModList = std::vector<std::string>;
ModIncompatibility(ModList && _missingMods):
missingMods(std::move(_missingMods))
ModIncompatibility(const ModListWithVersion & _missingMods)
{
std::ostringstream _ss;
for(const auto & m : missingMods)
for(const auto & m : _missingMods)
_ss << m.first << ' ' << m.second << std::endl;
message = _ss.str();
messageMissingMods = _ss.str();
}
ModIncompatibility(const ModListWithVersion & _missingMods, ModList & _excessiveMods)
: ModIncompatibility(_missingMods)
{
std::ostringstream _ss;
for(const auto & m : _excessiveMods)
_ss << m << std::endl;
messageExcessiveMods = _ss.str();
}
const char * what() const noexcept override
{
return message.c_str();
static const std::string w("Mod incompatibility exception");
return w.c_str();
}
const std::string & whatMissing() const noexcept
{
return messageMissingMods;
}
const std::string & whatExcessive() const noexcept
{
return messageExcessiveMods;
}
private:
//list of mods required to load the game
// first: mod name
// second: mod version
const ModList missingMods;
std::string message;
std::string messageMissingMods, messageExcessiveMods;
};
VCMI_LIB_NAMESPACE_END