1/*
2 * LegacyClonk
3 *
4 * Copyright (c) RedWolf Design
5 * Copyright (c) 2017-2022, The LegacyClonk Team and contributors
6 *
7 * Distributed under the terms of the ISC license; see accompanying file
8 * "COPYING" for details.
9 *
10 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11 * See accompanying file "TRADEMARK" for details.
12 *
13 * To redistribute this file separately, substitute the full license texts
14 * for the above references.
15 */
16
17#pragma once
18
19#include "C4Version.h"
20#include "C4InputValidation.h"
21
22#include <cinttypes>
23
24struct C4GameVersion
25{
26 ValidatedStdStrBuf<C4InVal::VAL_NameAllowEmpty> sEngineName; // status only - not used for comparison
27 int32_t iVer[4];
28 int32_t iBuild;
29
30 C4GameVersion(const char *szEngine = C4ENGINENAME, int32_t iVer1 = C4XVER1, int32_t iVer2 = C4XVER2, int32_t iVer3 = C4XVER3, int32_t iVer4 = C4XVER4, int32_t iVerBuild = C4XVERBUILD)
31 {
32 Set(szEngine, iVer1, iVer2, iVer3, iVer4, iVerBuild);
33 }
34
35 void Set(const char *szEngine = C4ENGINENAME, int32_t iVer1 = C4XVER1, int32_t iVer2 = C4XVER2, int32_t iVer3 = C4XVER3, int32_t iVer4 = C4XVER4, int32_t iVerBuild = C4XVERBUILD)
36 {
37 sEngineName.CopyValidated(szFromVal: szEngine); iVer[0] = iVer1; iVer[1] = iVer2; iVer[2] = iVer3; iVer[3] = iVer4; iBuild = iVerBuild;
38 }
39
40 std::string GetString() const
41 {
42 return std::format(fmt: "{} {}.{}.{}.{} [{}]", args: sEngineName.getData(), args: iVer[0], args: iVer[1], args: iVer[2], args: iVer[3], args: iBuild);
43 }
44
45 bool operator==(const C4GameVersion &rCmp) const
46 {
47 return iVer[0] == rCmp.iVer[0] && iVer[1] == rCmp.iVer[1] && iVer[2] == rCmp.iVer[2] && iVer[3] == rCmp.iVer[3] && iBuild == rCmp.iBuild;
48 }
49
50 void CompileFunc(StdCompiler *pComp, bool fEngineName)
51 {
52 if (fEngineName)
53 {
54 pComp->Value(rStruct: mkDefaultAdapt(rValue&: sEngineName, rDefault: ""));
55 pComp->Separator();
56 }
57 else if (pComp->isCompiler())
58 sEngineName = "";
59 pComp->Value(rStruct: mkArrayAdapt(array&: iVer, default_: 0));
60 pComp->Separator();
61 pComp->Value(rStruct: mkDefaultAdapt(rValue&: iBuild, rDefault: 0));
62 }
63};
64
65// helper
66inline int CompareVersion(int iVer1, int iVer2, int iVer3, int iVer4, int iVerBuild,
67 int iRVer1 = C4XVER1, int iRVer2 = C4XVER2, int iRVer3 = C4XVER3, int iRVer4 = C4XVER4, int iRVerBuild = C4XVERBUILD)
68{
69 if (iVer1 > iRVer1) return 1; if (iVer1 < iRVer1) return -1;
70 if (iVer2 > iRVer2) return 1; if (iVer2 < iRVer2) return -1;
71 if (iVer3 > iRVer3) return 1; if (iVer3 < iRVer3) return -1;
72 if (iVer4 > iRVer4) return 1; if (iVer4 < iRVer4) return -1;
73
74 if (iVerBuild > 0)
75 {
76 if (iVerBuild > iRVerBuild) return 1; if (iVerBuild < iRVerBuild) return -1;
77 }
78
79 return 0;
80}
81