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