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