preconditions

This commit is contained in:
Thomas 2022-01-09 20:40:55 +01:00
parent aad76d162b
commit f2984fffb8
No known key found for this signature in database
GPG Key ID: E538821A6CDFDAD7
3 changed files with 31 additions and 27 deletions

View File

@ -4,7 +4,7 @@ general:
# Players config # Players config
players: players:
width: 'aza' width: 1000
startXPosition: 50 startXPosition: 50
fireCooldown: 10 fireCooldown: 10
speed: 20 speed: 20

View File

@ -30,8 +30,8 @@ private:
const string& getString(const configKey& key) const; const string& getString(const configKey& key) const;
char getChar(const configKey& key, char def) const; char getChar(const configKey& key, char def) const;
char getChar(const configKey& key) 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) 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 nsGraphics::RGBAcolor& def) const;
void getColor(const configKey& key, nsGraphics::RGBAcolor& color) const; void getColor(const configKey& key, nsGraphics::RGBAcolor& color) const;
void getList(const configKey& key, vector<string>& vec) const; void getList(const configKey& key, vector<string>& vec) const;

View File

@ -154,7 +154,7 @@ void ConfigBuilder::readConfig() {
readGrid("grid"); readGrid("grid");
// players // players
collectedData.playersWidth = getInt("players.width", 5); collectedData.playersWidth = getInt("players.width", 200, 100, 500);
collectedData.startXPosition = getInt("players.startXPosition"); collectedData.startXPosition = getInt("players.startXPosition");
collectedData.playersSpeed = getInt("players.speed"); collectedData.playersSpeed = getInt("players.speed");
collectedData.playersFireCooldown = getInt("players.fireCooldown"); 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{ try{
return stoi(getString(key)); int val = getInt(key);
}catch(invalid_argument& e){ if(val > min || val < max){
throw config_error("Invalid int data for key "+key+" : |"+getString(key)+"|"); throw config_error("Value for key " + key + " do not follow preconditions : " +
} to_string(min) + "<=" + to_string(val) + "<=" + to_string(max));
} }
int ConfigBuilder::getInt(const configKey& key, int def) const {
try{
return getInt(key);
}catch(config_error& e){ }catch(config_error& e){
cerr << e.what() << " . Using default value" << endl; cerr << e.what() << " . Using default value" << endl;
return def; return def;
} }
} }
char ConfigBuilder::getChar(const configKey& key) const { int ConfigBuilder::getInt(const configKey& key) const {
string s = getString(key); try{
if(s.size()!=1)throw config_error("Invalid char data for key "+key+" : |"+s+"|"); return stoi(getString(key));
return s[0]; }catch(invalid_argument& e){
throw config_error("Invalid int data for key "+key+" : |"+getString(key)+"|");
}
} }
char ConfigBuilder::getChar(const configKey& key, char def) const { 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 { char ConfigBuilder::getChar(const configKey& key) const {
size_t i=0; string s = getString(key);
string fullKey = key+".0"; if(s.size()!=1)throw config_error("Invalid char data for key "+key+" : |"+s+"|");
if(!internalValues.contains(fullKey))throw config_error("Non-existent list key requested : "+key); return s[0];
do{
toPopulate.push_back(internalValues.at(fullKey));
++i;
fullKey = key+"."+to_string(i);
}while(internalValues.contains(key+"."+to_string(i)));
} }
void ConfigBuilder::getColor(const configKey& key, nsGraphics::RGBAcolor& color, const nsGraphics::RGBAcolor& def) const { 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); 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() { bool Game::reloadConfig() {
map<string, string> strValues; map<string, string> strValues;