1 /*
2  OpenLieroX
3 
4  version parsing
5 
6  file created 29-02-2008 by albert
7  code under LGPL
8  */
9 
10 #include "Version.h"
11 #include "LieroX.h"
12 #include "Debug.h"
13 #include "AuxLib.h"
14 
15 
16 #include "Version_generated.h"
17 
18 #ifndef		LX_VERSION
19 #	define		LX_VERSION	"0.58_rc5"
20 #endif
21 
22 #define		GAMENAME			"OpenLieroX"
23 
24 
25 #ifndef ONLY_MACRODEF  // The above defines are used in resurce.rc
26 
27 const char* const fullGameName = GAMENAME "/" LX_VERSION;
28 
GetGameName()29 const char* GetGameName() {
30 	return GAMENAME;
31 }
32 
setByString__optionalPostCheck(const Version * version,const std::string & versionStr)33 inline void setByString__optionalPostCheck(const Version* version, const std::string& versionStr) {
34 #ifdef DEBUG
35 	if(version->asString() != versionStr) {
36 		notes << "WARNING: Version::setByString: '" << versionStr << "' get parsed as '" << version->asString() << "'" << endl;
37 	}
38 #endif
39 }
40 
setByString(const std::string & versionStr)41 void Version::setByString(const std::string& versionStr) {
42 	if(versionStr == "") { reset(); setByString__optionalPostCheck(this,versionStr); return; }
43 
44 	std::string tmp = versionStr;
45 
46 	size_t p = tmp.find_first_of(" /\\");
47 	if(p == std::string::npos)
48 		gamename = "OpenLieroX"; // old OLX sends sometimes not its name
49 	else {
50 		gamename = tmp.substr(0, p);
51 		tmp.erase(0, p + 1);
52 	}
53 
54 	// reset some values
55 	num = subnum = subsubnum = revnum = 0;
56 	releasetype = RT_NORMAL;
57 	stringlwr(tmp); // to have beta instead of Beta
58 
59 	// num
60 	p = tmp.find(".");
61 	bool fail = false;
62 	num = from_string<int>(tmp.substr(0, p), fail);
63 	if(fail) { num = 0; setByString__optionalPostCheck(this,versionStr); return; }
64 	if(p == std::string::npos) { setByString__optionalPostCheck(this,versionStr); return; }
65 	tmp.erase(0, p + 1);
66 
67 	// subnum
68 	p = tmp.find_first_of("._-");
69 	subnum = from_string<int>(tmp.substr(0, p), fail);
70 	if(fail) { subnum = 0; setByString__optionalPostCheck(this,versionStr); return; }
71 	if(p == std::string::npos) { setByString__optionalPostCheck(this,versionStr); return; }
72 	tmp.erase(0, p + 1);
73 
74 	// releasetype
75 	if(tmp == "") { setByString__optionalPostCheck(this,versionStr); return; }
76 	size_t nextNumP = tmp.find_first_of("0123456789");
77 	if(nextNumP == 0)
78 		releasetype = RT_NORMAL;
79 	else if(tmp.find("alpha") == 0)
80 		releasetype = RT_ALPHA;
81 	else if(tmp.find("beta") == 0)
82 		releasetype = RT_BETA;
83 	else if(tmp.find("rc") == 0)
84 		releasetype = RT_RC;
85 	else
86 		releasetype = RT_UNKNOWN;
87 	tmp.erase(0, nextNumP);
88 
89 	// subsubnum
90 	if(tmp == "") { setByString__optionalPostCheck(this,versionStr); return; }
91 	if(tmp.find_first_of(".") == 0) tmp.erase(0, 1);
92 	p = tmp.find_first_of("._-");
93 	subsubnum = from_string<int>(tmp.substr(0, p), fail);
94 	if(fail) { subsubnum = 0; setByString__optionalPostCheck(this,versionStr); return; }
95 	if(p == std::string::npos) { setByString__optionalPostCheck(this,versionStr); return; }
96 	tmp.erase(0, p + 1);
97 
98 	// revnum
99 	nextNumP = tmp.find_first_of("0123456789");
100 	if(nextNumP == std::string::npos) { setByString__optionalPostCheck(this,versionStr); return; }
101 	tmp.erase(0, nextNumP);
102 	revnum = from_string<int>(tmp, fail);
103 	if(fail) revnum = 0;
104 
105 	setByString__optionalPostCheck(this,versionStr); return;
106 }
107 
108 
asString() const109 std::string Version::asString() const {
110 	std::string ret = gamename;
111 	ret += "/";
112 	ret += itoa(num);
113 	ret += ".";
114 	ret += itoa(subnum);
115 
116 	if(subsubnum > 0) {
117 		switch(releasetype) {
118 		case RT_NORMAL: ret += "."; break;
119 		case RT_ALPHA: ret += "_alpha"; break;
120 		case RT_BETA: ret += "_beta"; break;
121 		case RT_RC: ret += "_rc"; break;
122 		case RT_UNKNOWN: ret += "_"; break;
123 		}
124 		ret += itoa(subsubnum);
125 	}
126 
127 	if(revnum > 0) {
128 		ret += "_r";
129 		ret += itoa(revnum);
130 	}
131 
132 	return ret;
133 }
134 
asHumanString() const135 std::string Version::asHumanString() const
136 {
137 	std::string ret = gamename;
138 	ret += " ";
139 	ret += itoa(num);
140 	ret += ".";
141 	ret += itoa(subnum);
142 
143 	if(subsubnum > 0) {
144 		switch(releasetype) {
145 		case RT_NORMAL: ret += "."; break;
146 		case RT_ALPHA: ret += " alpha "; break;
147 		case RT_BETA: ret += " beta "; break;
148 		case RT_RC: ret += " rc "; break;
149 		case RT_UNKNOWN: ret += " "; break;
150 		}
151 		ret += itoa(subsubnum);
152 	}
153 
154 	return ret;
155 }
156 
releaseType() const157 std::string Version::releaseType() const
158 {
159 	switch(releasetype) {
160 	case RT_NORMAL: return "final";
161 	case RT_ALPHA: return "alpha";
162 	case RT_BETA: return "beta";
163 	case RT_RC: return "rc";
164 	case RT_UNKNOWN: return "unknown";
165 	}
166 
167 	return "error"; // should not happen
168 }
169 
170 
171 static Version gameVersion(GetFullGameName());
172 
GetGameVersion()173 const Version& GetGameVersion() {
174 	return gameVersion;
175 }
176 
177 
isBanned() const178 bool Version::isBanned() const {
179 	return *this == OLXBetaVersion(9) || *this == OLXBetaVersion(0,58,1);
180 }
181 
182 
183 #endif  // ONLY_MACRODEF
184