git repos / blockattack-game

blame: source/code/common.cpp

normal view · raw

89c4a3a6 sago007 2008-08-29 14:32 1
/*
c53e6443 sago007 2012-04-17 11:04 2
===========================================================================
c53e6443 sago007 2012-04-17 11:04 3
blockattack - Block Attack - Rise of the Blocks
c53e6443 sago007 2012-04-17 11:04 4
Copyright (C) 2005-2012 Poul Sander
c53e6443 sago007 2012-04-17 11:04 5
c53e6443 sago007 2012-04-17 11:04 6
This program is free software: you can redistribute it and/or modify
c53e6443 sago007 2012-04-17 11:04 7
it under the terms of the GNU General Public License as published by
c53e6443 sago007 2012-04-17 11:04 8
the Free Software Foundation, either version 2 of the License, or
c53e6443 sago007 2012-04-17 11:04 9
(at your option) any later version.
c53e6443 sago007 2012-04-17 11:04 10
c53e6443 sago007 2012-04-17 11:04 11
This program is distributed in the hope that it will be useful,
c53e6443 sago007 2012-04-17 11:04 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
c53e6443 sago007 2012-04-17 11:04 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c53e6443 sago007 2012-04-17 11:04 14
GNU General Public License for more details.
c53e6443 sago007 2012-04-17 11:04 15
c53e6443 sago007 2012-04-17 11:04 16
You should have received a copy of the GNU General Public License
c53e6443 sago007 2012-04-17 11:04 17
along with this program.  If not, see http://www.gnu.org/licenses/
c53e6443 sago007 2012-04-17 11:04 18
c53e6443 sago007 2012-04-17 11:04 19
Source information and contacts persons can be found at
30412ca6 sago007 2016-03-25 19:35 20
http://www.blockattack.net
c53e6443 sago007 2012-04-17 11:04 21
===========================================================================
89c4a3a6 sago007 2008-08-29 14:32 22
*/
89c4a3a6 sago007 2008-08-29 14:32 23
89c4a3a6 sago007 2008-08-29 14:32 24
#include "common.h"
769a41a0 sago007 2008-09-24 12:54 25
#include <sstream>
f306e3ca sago007 2015-12-04 21:16 26
#include <cstring>
1899da11 sago007 2015-12-30 19:04 27
#include "os.hpp"
4e257de2 sago007 2016-01-22 17:43 28
#include "sago/SagoMiscSdl2.hpp"
59dcb571 sago007 2016-02-14 15:34 29
#include "sago/SagoMisc.hpp"
15b9c93b sago007 2016-02-03 20:03 30
#include <stdarg.h>
769a41a0 sago007 2008-09-24 12:54 31
96eb7a16 sago007 2016-04-29 17:01 32
using std::string;
96eb7a16 sago007 2016-04-29 17:01 33
using std::stringstream;
96eb7a16 sago007 2016-04-29 17:01 34
using std::cerr;
96eb7a16 sago007 2016-04-29 17:01 35
using std::map;
96eb7a16 sago007 2016-04-29 17:01 36
using std::vector;
444cec07 sago007 2012-05-05 16:42 37
f306e3ca sago007 2015-12-04 21:16 38
bool strequals(const char* a, const char* b) {
f306e3ca sago007 2015-12-04 21:16 39
	return strcmp(a,b) == 0;
f306e3ca sago007 2015-12-04 21:16 40
}
f306e3ca sago007 2015-12-04 21:16 41
078900fe sago007 2015-12-05 15:31 42
void dieOnNullptr(bool ptr, const char* msg) {
078900fe sago007 2015-12-05 15:31 43
	if (!ptr) {
4e257de2 sago007 2016-01-22 17:43 44
		sago::SagoFatalError(msg);
078900fe sago007 2015-12-05 15:31 45
	}
078900fe sago007 2015-12-05 15:31 46
}
078900fe sago007 2015-12-05 15:31 47
ecef5838 sago007 2015-11-24 20:01 48
double str2double(const string& str2parse) {
ecef5838 sago007 2015-11-24 20:01 49
	try {
b5d3c879 sago007 2016-10-09 09:37 50
		return std::stod(str2parse);
c53e6443 sago007 2012-04-17 11:04 51
	}
7025bd6c sago007 2017-04-01 18:27 52
	catch (...) {
c53e6443 sago007 2012-04-17 11:04 53
		return 0.0;
c53e6443 sago007 2012-04-17 11:04 54
	}
769a41a0 sago007 2008-09-24 12:54 55
}
769a41a0 sago007 2008-09-24 12:54 56
15b9c93b sago007 2016-02-03 20:03 57
std::string SPrintStringF(const char* fmt, ...) {
15b9c93b sago007 2016-02-03 20:03 58
	std::string ret;
15b9c93b sago007 2016-02-03 20:03 59
	char buffer[1024];
15b9c93b sago007 2016-02-03 20:03 60
	va_list args;
15b9c93b sago007 2016-02-03 20:03 61
	va_start(args, fmt);
15b9c93b sago007 2016-02-03 20:03 62
	vsnprintf(buffer, sizeof(buffer), fmt, args);
15b9c93b sago007 2016-02-03 20:03 63
	ret = buffer;
15b9c93b sago007 2016-02-03 20:03 64
	va_end(args);
15b9c93b sago007 2016-02-03 20:03 65
	return ret;
15b9c93b sago007 2016-02-03 20:03 66
}
15b9c93b sago007 2016-02-03 20:03 67
15b9c93b sago007 2016-02-03 20:03 68
const char* SPrintCF(const char* fmt, ...) {
15b9c93b sago007 2016-02-03 20:03 69
	static char buffer[1024];
15b9c93b sago007 2016-02-03 20:03 70
	va_list args;
15b9c93b sago007 2016-02-03 20:03 71
	va_start(args, fmt);
15b9c93b sago007 2016-02-03 20:03 72
	vsnprintf(buffer, sizeof(buffer), fmt, args);
15b9c93b sago007 2016-02-03 20:03 73
	va_end(args);
15b9c93b sago007 2016-02-03 20:03 74
	return buffer;
15b9c93b sago007 2016-02-03 20:03 75
}
15b9c93b sago007 2016-02-03 20:03 76
ecef5838 sago007 2015-11-24 20:01 77
int str2int(const string& str2parse) {
ecef5838 sago007 2015-11-24 20:01 78
	try {
b5d3c879 sago007 2016-10-09 09:37 79
		return std::stoi(str2parse);
c53e6443 sago007 2012-04-17 11:04 80
	}
7025bd6c sago007 2017-04-01 18:27 81
	catch (...) {
c53e6443 sago007 2012-04-17 11:04 82
		return 0;
c53e6443 sago007 2012-04-17 11:04 83
	}
769a41a0 sago007 2008-09-24 12:54 84
}
89c4a3a6 sago007 2008-08-29 14:32 85
d07b805e sago007 2009-01-27 13:15 86
/**
d07b805e sago007 2009-01-27 13:15 87
 * Takes a number of milliseconds and returns the value in commonTime format.
d07b805e sago007 2009-01-27 13:15 88
 */
ecef5838 sago007 2015-11-24 20:01 89
commonTime TimeHandler::ms2ct(unsigned int milliseconds) {
c53e6443 sago007 2012-04-17 11:04 90
	commonTime ct;
c53e6443 sago007 2012-04-17 11:04 91
	ct.days = 0;
c53e6443 sago007 2012-04-17 11:04 92
	unsigned int time = milliseconds;
c53e6443 sago007 2012-04-17 11:04 93
	ct.hours = time/(1000*60*60);
c53e6443 sago007 2012-04-17 11:04 94
	time = time % (1000*60*60);
c53e6443 sago007 2012-04-17 11:04 95
	ct.minutes = time/(1000*60);
c53e6443 sago007 2012-04-17 11:04 96
	time = time % (1000*60);
c53e6443 sago007 2012-04-17 11:04 97
	ct.seconds = time/1000;
c53e6443 sago007 2012-04-17 11:04 98
	return ct;
c09d0e4f sago007 2009-01-27 01:15 99
}
89c4a3a6 sago007 2008-08-29 14:32 100
ecef5838 sago007 2015-11-24 20:01 101
commonTime TimeHandler::getTime(const string& name) {
c53e6443 sago007 2012-04-17 11:04 102
	commonTime ct;
c53e6443 sago007 2012-04-17 11:04 103
	ct.days = Config::getInstance()->getInt(name+"Days");
c53e6443 sago007 2012-04-17 11:04 104
	ct.hours = Config::getInstance()->getInt(name+"Hours");
c53e6443 sago007 2012-04-17 11:04 105
	ct.minutes = Config::getInstance()->getInt(name+"Minutes");
c53e6443 sago007 2012-04-17 11:04 106
	ct.seconds = Config::getInstance()->getInt(name+"Seconds");
c53e6443 sago007 2012-04-17 11:04 107
	return ct;
89c4a3a6 sago007 2008-08-29 14:32 108
}
89c4a3a6 sago007 2008-08-29 14:32 109
d07b805e sago007 2009-01-27 13:15 110
/**
c53e6443 sago007 2012-04-17 11:04 111
 * Returns the total runtime with toAdd added but without writing it to config file.
d07b805e sago007 2009-01-27 13:15 112
 * Used for stats
c09d0e4f sago007 2009-01-27 01:15 113
 */
ecef5838 sago007 2015-11-24 20:01 114
commonTime TimeHandler::peekTime(const string& name, const commonTime& toAdd) {
c53e6443 sago007 2012-04-17 11:04 115
	commonTime ct = getTime(name);
c09d0e4f sago007 2009-01-27 01:15 116
c53e6443 sago007 2012-04-17 11:04 117
	ct.seconds +=toAdd.seconds;
c53e6443 sago007 2012-04-17 11:04 118
	ct.minutes +=ct.seconds/60;
c53e6443 sago007 2012-04-17 11:04 119
	ct.seconds = ct.seconds%60;
c09d0e4f sago007 2009-01-27 01:15 120
c53e6443 sago007 2012-04-17 11:04 121
	ct.minutes += toAdd.minutes;
c53e6443 sago007 2012-04-17 11:04 122
	ct.hours += ct.minutes/60;
c53e6443 sago007 2012-04-17 11:04 123
	ct.minutes = ct.minutes%60;
c09d0e4f sago007 2009-01-27 01:15 124
c53e6443 sago007 2012-04-17 11:04 125
	ct.hours += toAdd.hours;
c53e6443 sago007 2012-04-17 11:04 126
	ct.days += ct.hours/24;
c53e6443 sago007 2012-04-17 11:04 127
	ct.hours = ct.hours%24;
c09d0e4f sago007 2009-01-27 01:15 128
c53e6443 sago007 2012-04-17 11:04 129
	ct.days += toAdd.days;
c53e6443 sago007 2012-04-17 11:04 130
	return ct;
c09d0e4f sago007 2009-01-27 01:15 131
}
c09d0e4f sago007 2009-01-27 01:15 132
d07b805e sago007 2009-01-27 13:15 133
/**
c09d0e4f sago007 2009-01-27 01:15 134
 * Same as peekTotalTime but writes the time to the config file.
d07b805e sago007 2009-01-27 13:15 135
 * Should only be called only once! when the program shuts down
c09d0e4f sago007 2009-01-27 01:15 136
 */
ecef5838 sago007 2015-11-24 20:01 137
commonTime TimeHandler::addTime(const string& name, const commonTime& toAdd) {
c53e6443 sago007 2012-04-17 11:04 138
	commonTime ct = peekTime(name,toAdd);
c53e6443 sago007 2012-04-17 11:04 139
c53e6443 sago007 2012-04-17 11:04 140
	Config::getInstance()->setInt(name+"Days",ct.days);
c53e6443 sago007 2012-04-17 11:04 141
	Config::getInstance()->setInt(name+"Hours",ct.hours);
c53e6443 sago007 2012-04-17 11:04 142
	Config::getInstance()->setInt(name+"Minutes",ct.minutes);
c53e6443 sago007 2012-04-17 11:04 143
	Config::getInstance()->setInt(name+"Seconds",ct.seconds);
c53e6443 sago007 2012-04-17 11:04 144
	return ct;
89c4a3a6 sago007 2008-08-29 14:32 145
}
769a41a0 sago007 2008-09-24 12:54 146
769a41a0 sago007 2008-09-24 12:54 147
Config* Config::instance = 0;
769a41a0 sago007 2008-09-24 12:54 148
ecef5838 sago007 2015-11-24 20:01 149
Config::Config() {
c53e6443 sago007 2012-04-17 11:04 150
	configMap.clear();
c53e6443 sago007 2012-04-17 11:04 151
	load();
c53e6443 sago007 2012-04-17 11:04 152
	shuttingDown = 0; // Not shutting down
769a41a0 sago007 2008-09-24 12:54 153
}
769a41a0 sago007 2008-09-24 12:54 154
ecef5838 sago007 2015-11-24 20:01 155
void Config::load() {
30412ca6 sago007 2016-03-25 19:35 156
	string filecontent = sago::GetFileContent("configFile");
30412ca6 sago007 2016-03-25 19:35 157
	stringstream inFile(filecontent);
c53e6443 sago007 2012-04-17 11:04 158
	string key;
c53e6443 sago007 2012-04-17 11:04 159
	string previuskey;
c53e6443 sago007 2012-04-17 11:04 160
	char value[MAX_VAR_LENGTH];
ecef5838 sago007 2015-11-24 20:01 161
	if (inFile) {
ecef5838 sago007 2015-11-24 20:01 162
		while (!inFile.eof()) {
c53e6443 sago007 2012-04-17 11:04 163
			inFile >> key;
ecef5838 sago007 2015-11-24 20:01 164
			if (key==previuskey) { //the last entry will be read 2 times if a linebreak is missing in the end
c53e6443 sago007 2012-04-17 11:04 165
				continue;
acd79baf sago007 2015-11-22 19:37 166
			}
c53e6443 sago007 2012-04-17 11:04 167
			previuskey = key;
c53e6443 sago007 2012-04-17 11:04 168
			inFile.get(); //Read the space between the key and the content
c53e6443 sago007 2012-04-17 11:04 169
			inFile.getline(value,MAX_VAR_LENGTH);
c53e6443 sago007 2012-04-17 11:04 170
#if DEBUG
161a010c sago007 2016-05-06 14:00 171
			cerr << "Config read: " << key << " with:\"" << value << "\"" << "\n";
c53e6443 sago007 2012-04-17 11:04 172
#endif
c53e6443 sago007 2012-04-17 11:04 173
			configMap[key] = (string)value;
c53e6443 sago007 2012-04-17 11:04 174
		}
c53e6443 sago007 2012-04-17 11:04 175
	}
769a41a0 sago007 2008-09-24 12:54 176
}
769a41a0 sago007 2008-09-24 12:54 177
ecef5838 sago007 2015-11-24 20:01 178
Config* Config::getInstance() {
ecef5838 sago007 2015-11-24 20:01 179
	if (Config::instance==0) {
c53e6443 sago007 2012-04-17 11:04 180
		Config::instance = new Config();
c53e6443 sago007 2012-04-17 11:04 181
c53e6443 sago007 2012-04-17 11:04 182
	}
c53e6443 sago007 2012-04-17 11:04 183
	return Config::instance;
769a41a0 sago007 2008-09-24 12:54 184
}
769a41a0 sago007 2008-09-24 12:54 185
ecef5838 sago007 2015-11-24 20:01 186
void Config::save() {
59dcb571 sago007 2016-02-14 15:34 187
	std::stringstream outFile;
59dcb571 sago007 2016-02-14 15:34 188
	map<string,string>::iterator iter;
59dcb571 sago007 2016-02-14 15:34 189
	for (iter = configMap.begin(); iter != configMap.end(); iter++) {
161a010c sago007 2016-05-06 14:00 190
		outFile << iter->first << " " << iter->second << "\n";
c53e6443 sago007 2012-04-17 11:04 191
	}
59dcb571 sago007 2016-02-14 15:34 192
	outFile << "\n"; //The last entry in the file will be read double if a linebreak is missing
59dcb571 sago007 2016-02-14 15:34 193
	//This is checked on load too in case a user changes it himself.
59dcb571 sago007 2016-02-14 15:34 194
	sago::WriteFileContent("configFile", outFile.str());
769a41a0 sago007 2008-09-24 12:54 195
}
769a41a0 sago007 2008-09-24 12:54 196
ecef5838 sago007 2015-11-24 20:01 197
bool Config::exists(const string& varName) const {
c53e6443 sago007 2012-04-17 11:04 198
	//Using that find returns an iterator to the end of the map if not found
c53e6443 sago007 2012-04-17 11:04 199
	return configMap.find(varName) != configMap.end();
769a41a0 sago007 2008-09-24 12:54 200
}
769a41a0 sago007 2008-09-24 12:54 201
ecef5838 sago007 2015-11-24 20:01 202
void Config::setDefault(const string& varName,const string& content) {
ecef5838 sago007 2015-11-24 20:01 203
	if (exists(varName)) {
acd79baf sago007 2015-11-22 19:37 204
		return;    //Already exists do not change
acd79baf sago007 2015-11-22 19:37 205
	}
c53e6443 sago007 2012-04-17 11:04 206
	setString(varName,content);
42f8ed5f sago007 2009-03-05 11:47 207
}
42f8ed5f sago007 2009-03-05 11:47 208
ecef5838 sago007 2015-11-24 20:01 209
void Config::setShuttingDown(long shuttingDown) {
c53e6443 sago007 2012-04-17 11:04 210
	this->shuttingDown = shuttingDown;
cc3ef158 sago007 2011-07-03 16:13 211
}
cc3ef158 sago007 2011-07-03 16:13 212
ecef5838 sago007 2015-11-24 20:01 213
long Config::isShuttingDown() const {
c53e6443 sago007 2012-04-17 11:04 214
	return shuttingDown;
cc3ef158 sago007 2011-07-03 16:13 215
}
cc3ef158 sago007 2011-07-03 16:13 216
ecef5838 sago007 2015-11-24 20:01 217
void Config::setString(const string& varName, const string& content) {
c53e6443 sago007 2012-04-17 11:04 218
	configMap[varName] = content;
769a41a0 sago007 2008-09-24 12:54 219
}
769a41a0 sago007 2008-09-24 12:54 220
ecef5838 sago007 2015-11-24 20:01 221
void Config::setInt(const string& varName, int content) {
549ecfae sago007 2016-10-09 08:37 222
	configMap[varName] = std::to_string(content);
769a41a0 sago007 2008-09-24 12:54 223
}
769a41a0 sago007 2008-09-24 12:54 224
ecef5838 sago007 2015-11-24 20:01 225
void Config::setValue(const string& varName,double content) {
549ecfae sago007 2016-10-09 08:37 226
	configMap[varName] = std::to_string(content);
769a41a0 sago007 2008-09-24 12:54 227
}
769a41a0 sago007 2008-09-24 12:54 228
ecef5838 sago007 2015-11-24 20:01 229
string Config::getString(const string& varName) {
ecef5838 sago007 2015-11-24 20:01 230
	if (exists(varName)) {
c53e6443 sago007 2012-04-17 11:04 231
		return configMap[varName];
c53e6443 sago007 2012-04-17 11:04 232
	}
acd79baf sago007 2015-11-22 19:37 233
	else {
c53e6443 sago007 2012-04-17 11:04 234
		return "";
acd79baf sago007 2015-11-22 19:37 235
	}
769a41a0 sago007 2008-09-24 12:54 236
}
769a41a0 sago007 2008-09-24 12:54 237
ecef5838 sago007 2015-11-24 20:01 238
int Config::getInt(const string& varName) {
ecef5838 sago007 2015-11-24 20:01 239
	if (exists(varName)) {
c53e6443 sago007 2012-04-17 11:04 240
		return str2int(configMap[varName]);
c53e6443 sago007 2012-04-17 11:04 241
	}
acd79baf sago007 2015-11-22 19:37 242
	else {
c53e6443 sago007 2012-04-17 11:04 243
		return 0;
acd79baf sago007 2015-11-22 19:37 244
	}
769a41a0 sago007 2008-09-24 12:54 245
}
769a41a0 sago007 2008-09-24 12:54 246
ecef5838 sago007 2015-11-24 20:01 247
double Config::getValue(const string& varName) {
ecef5838 sago007 2015-11-24 20:01 248
	if (exists(varName)) {
c53e6443 sago007 2012-04-17 11:04 249
		return str2double(configMap[varName]);
c53e6443 sago007 2012-04-17 11:04 250
	}
acd79baf sago007 2015-11-22 19:37 251
	else {
c53e6443 sago007 2012-04-17 11:04 252
		return 0.0;
acd79baf sago007 2015-11-22 19:37 253
	}
769a41a0 sago007 2008-09-24 12:54 254
}
1970-01-01 00:00 255