CModVersion.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * CModVersion.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CModVersion.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. CModVersion CModVersion::GameVersion()
  14. {
  15. return CModVersion(VCMI_VERSION_MAJOR, VCMI_VERSION_MINOR, VCMI_VERSION_PATCH);
  16. }
  17. CModVersion CModVersion::fromString(std::string from)
  18. {
  19. int major = 0;
  20. int minor = 0;
  21. int patch = 0;
  22. try
  23. {
  24. auto pointPos = from.find('.');
  25. major = std::stoi(from.substr(0, pointPos));
  26. if(pointPos != std::string::npos)
  27. {
  28. from = from.substr(pointPos + 1);
  29. pointPos = from.find('.');
  30. minor = std::stoi(from.substr(0, pointPos));
  31. if(pointPos != std::string::npos)
  32. patch = std::stoi(from.substr(pointPos + 1));
  33. }
  34. }
  35. catch(const std::invalid_argument &)
  36. {
  37. return CModVersion();
  38. }
  39. return CModVersion(major, minor, patch);
  40. }
  41. std::string CModVersion::toString() const
  42. {
  43. return std::to_string(major) + '.' + std::to_string(minor) + '.' + std::to_string(patch);
  44. }
  45. bool CModVersion::compatible(const CModVersion & other, bool checkMinor, bool checkPatch) const
  46. {
  47. return (major == other.major &&
  48. (!checkMinor || minor >= other.minor) &&
  49. (!checkPatch || minor > other.minor || (minor == other.minor && patch >= other.patch)));
  50. }
  51. bool CModVersion::isNull() const
  52. {
  53. return major == 0 && minor == 0 && patch == 0;
  54. }
  55. VCMI_LIB_NAMESPACE_END