1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-20 20:23:03 +02:00
vcmi/lib/modding/CModVersion.cpp

92 lines
2.1 KiB
C++
Raw Normal View History

2023-05-24 00:14:06 +02:00
/*
* CModVersion.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
*
*/
#include "StdInc.h"
#include "CModVersion.h"
VCMI_LIB_NAMESPACE_BEGIN
CModVersion CModVersion::GameVersion()
{
return CModVersion(VCMI_VERSION_MAJOR, VCMI_VERSION_MINOR, VCMI_VERSION_PATCH);
}
CModVersion CModVersion::fromString(std::string from)
{
2023-09-01 02:36:53 +02:00
int major = Any;
int minor = Any;
int patch = Any;
2023-05-24 00:14:06 +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));
}
}
catch(const std::invalid_argument &)
{
return CModVersion();
}
return CModVersion(major, minor, patch);
}
std::string CModVersion::toString() const
{
2023-09-01 02:36:53 +02:00
std::string res;
if(major != Any)
{
res += std::to_string(major);
if(minor != Any)
{
res += '.' + std::to_string(minor);
if(patch != Any)
res += '.' + std::to_string(patch);
}
}
return res;
2023-05-24 00:14:06 +02:00
}
bool CModVersion::compatible(const CModVersion & other, bool checkMinor, bool checkPatch) const
{
2023-09-01 13:17:46 +02:00
bool doCheckMinor = checkMinor && minor != Any && other.minor != Any;
bool doCheckPatch = checkPatch && patch != Any && other.patch != Any;
2023-09-01 02:36:53 +02:00
2023-09-01 13:17:46 +02:00
assert(!doCheckPatch || (doCheckPatch && doCheckMinor));
2023-09-01 02:36:53 +02:00
2023-05-24 00:14:06 +02:00
return (major == other.major &&
2023-09-01 13:17:46 +02:00
(!doCheckMinor || minor >= other.minor) &&
(!doCheckPatch || minor > other.minor || (minor == other.minor && patch >= other.patch)));
2023-05-24 00:14:06 +02:00
}
bool CModVersion::isNull() const
{
2023-09-01 02:36:53 +02:00
return major == Any;
2023-05-24 00:14:06 +02:00
}
2023-09-01 02:12:41 +02:00
bool operator < (const CModVersion & lesser, const CModVersion & greater)
{
2023-09-01 02:36:53 +02:00
//specific is "greater" than non-specific, that's why do not check for Any value
2023-09-01 02:12:41 +02:00
if(lesser.major == greater.major)
{
if(lesser.minor == greater.minor)
return lesser.patch < greater.patch;
return lesser.minor < greater.minor;
}
return lesser.major < greater.major;
}
2023-05-24 00:14:06 +02:00
VCMI_LIB_NAMESPACE_END