preconditions
This commit is contained in:
parent
aad76d162b
commit
f2984fffb8
@ -4,7 +4,7 @@ general:
|
||||
|
||||
# Players config
|
||||
players:
|
||||
width: 'aza'
|
||||
width: 1000
|
||||
startXPosition: 50
|
||||
fireCooldown: 10
|
||||
speed: 20
|
||||
|
@ -30,8 +30,8 @@ private:
|
||||
const string& getString(const configKey& key) const;
|
||||
char getChar(const configKey& key, char def) const;
|
||||
char getChar(const configKey& key) const;
|
||||
int getInt(const configKey& key, int def, int min=INT_MIN, int max=INT_MAX) const;
|
||||
int getInt(const configKey& key) const;
|
||||
int getInt(const configKey& key, int def) const;
|
||||
void getColor(const configKey& key, nsGraphics::RGBAcolor& color, const nsGraphics::RGBAcolor& def) const;
|
||||
void getColor(const configKey& key, nsGraphics::RGBAcolor& color) const;
|
||||
void getList(const configKey& key, vector<string>& vec) const;
|
||||
|
@ -154,7 +154,7 @@ void ConfigBuilder::readConfig() {
|
||||
readGrid("grid");
|
||||
|
||||
// players
|
||||
collectedData.playersWidth = getInt("players.width", 5);
|
||||
collectedData.playersWidth = getInt("players.width", 200, 100, 500);
|
||||
collectedData.startXPosition = getInt("players.startXPosition");
|
||||
collectedData.playersSpeed = getInt("players.speed");
|
||||
collectedData.playersFireCooldown = getInt("players.fireCooldown");
|
||||
@ -208,27 +208,25 @@ const string& ConfigBuilder::getString(const configKey& key) const {
|
||||
}
|
||||
}
|
||||
|
||||
int ConfigBuilder::getInt(const configKey& key) const {
|
||||
int ConfigBuilder::getInt(const configKey& key, int def, int min, int max) const {
|
||||
try{
|
||||
return stoi(getString(key));
|
||||
}catch(invalid_argument& e){
|
||||
throw config_error("Invalid int data for key "+key+" : |"+getString(key)+"|");
|
||||
}
|
||||
}
|
||||
|
||||
int ConfigBuilder::getInt(const configKey& key, int def) const {
|
||||
try{
|
||||
return getInt(key);
|
||||
int val = getInt(key);
|
||||
if(val > min || val < max){
|
||||
throw config_error("Value for key " + key + " do not follow preconditions : " +
|
||||
to_string(min) + "<=" + to_string(val) + "<=" + to_string(max));
|
||||
}
|
||||
}catch(config_error& e){
|
||||
cerr << e.what() << " . Using default value" << endl;
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
char ConfigBuilder::getChar(const configKey& key) const {
|
||||
string s = getString(key);
|
||||
if(s.size()!=1)throw config_error("Invalid char data for key "+key+" : |"+s+"|");
|
||||
return s[0];
|
||||
int ConfigBuilder::getInt(const configKey& key) const {
|
||||
try{
|
||||
return stoi(getString(key));
|
||||
}catch(invalid_argument& e){
|
||||
throw config_error("Invalid int data for key "+key+" : |"+getString(key)+"|");
|
||||
}
|
||||
}
|
||||
|
||||
char ConfigBuilder::getChar(const configKey& key, char def) const {
|
||||
@ -240,16 +238,10 @@ char ConfigBuilder::getChar(const configKey& key, char def) const {
|
||||
}
|
||||
}
|
||||
|
||||
void ConfigBuilder::getList(const configKey& key, vector<string>& toPopulate) const {
|
||||
size_t i=0;
|
||||
string fullKey = key+".0";
|
||||
if(!internalValues.contains(fullKey))throw config_error("Non-existent list key requested : "+key);
|
||||
|
||||
do{
|
||||
toPopulate.push_back(internalValues.at(fullKey));
|
||||
++i;
|
||||
fullKey = key+"."+to_string(i);
|
||||
}while(internalValues.contains(key+"."+to_string(i)));
|
||||
char ConfigBuilder::getChar(const configKey& key) const {
|
||||
string s = getString(key);
|
||||
if(s.size()!=1)throw config_error("Invalid char data for key "+key+" : |"+s+"|");
|
||||
return s[0];
|
||||
}
|
||||
|
||||
void ConfigBuilder::getColor(const configKey& key, nsGraphics::RGBAcolor& color, const nsGraphics::RGBAcolor& def) const {
|
||||
@ -283,6 +275,18 @@ void ConfigBuilder::getColor(const configKey& key, nsGraphics::RGBAcolor& color)
|
||||
else throw config_error("Invalid color string : "+colorStr);
|
||||
}
|
||||
|
||||
void ConfigBuilder::getList(const configKey& key, vector<string>& toPopulate) const {
|
||||
size_t i=0;
|
||||
string fullKey = key+".0";
|
||||
if(!internalValues.contains(fullKey))throw config_error("Non-existent list key requested : "+key);
|
||||
|
||||
do{
|
||||
toPopulate.push_back(internalValues.at(fullKey));
|
||||
++i;
|
||||
fullKey = key+"."+to_string(i);
|
||||
}while(internalValues.contains(key+"."+to_string(i)));
|
||||
}
|
||||
|
||||
|
||||
bool Game::reloadConfig() {
|
||||
map<string, string> strValues;
|
||||
|
Loading…
Reference in New Issue
Block a user