|
| 1 | +#include "Config.h" |
| 2 | + |
| 3 | +#include <sstream> |
| 4 | +#include <stdexcept> |
| 5 | + |
| 6 | +#include "Util.h" |
| 7 | + |
| 8 | +using std::string; |
| 9 | +using std::stringstream; |
| 10 | +using std::runtime_error; |
| 11 | +using std::istream; |
| 12 | +using std::fstream; |
| 13 | + |
| 14 | +/** |
| 15 | + * Initialize a ConfigValue with the raw string it represents. |
| 16 | + * @param value The raw value. |
| 17 | + */ |
| 18 | +ConfigValue::ConfigValue(const string& value) : value(value) { |
| 19 | + |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Convert the ConfigValue into a float. |
| 24 | + * @return The text of the ConfigValue, interpreted as a float. |
| 25 | + */ |
| 26 | +ConfigValue::operator float() const { |
| 27 | + return fromString<float>(value); |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Convert the ConfigValue into a string. |
| 32 | + * @return The text of the ConfigValue. |
| 33 | + */ |
| 34 | +ConfigValue::operator string() const { |
| 35 | + return value; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Convert the ConfigValue into a game Coordinate. |
| 40 | + * @throws runtime_error If the value cannot be converted. |
| 41 | + * @return The text of the ConfigValue, interpreted as a game Coordinate. |
| 42 | + */ |
| 43 | +ConfigValue::operator Coordinate() const { |
| 44 | + stringstream stream(value); |
| 45 | + Coordinate ret; |
| 46 | + char c = stream.get(); |
| 47 | + if(c != '(') { |
| 48 | + throw runtime_error("Attempt to read a coordinate out of a non-coordinate value"); |
| 49 | + } |
| 50 | + stream >> ret.first; |
| 51 | + c = stream.get(); |
| 52 | + if(c != ',') { |
| 53 | + throw runtime_error("Attempt to read a coordinate out of a non-coordinate value"); |
| 54 | + } |
| 55 | + stream >> ret.second; |
| 56 | + return ret; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Convert the ConfigValue into a ScreenCoordinate. |
| 61 | + * @throws runtime_error If the value cannot be converted. |
| 62 | + * @return The text of the ConfigValue, interpreted as a ScreenCoordinate. |
| 63 | + */ |
| 64 | +ConfigValue::operator ScreenCoordinate() const { |
| 65 | + stringstream stream(value); |
| 66 | + ScreenCoordinate ret; |
| 67 | + char c = stream.get(); |
| 68 | + if(c != '(') { |
| 69 | + throw runtime_error("Attempt to read a coordinate out of a non-coordinate value"); |
| 70 | + } |
| 71 | + stream >> ret.first; |
| 72 | + c = stream.get(); |
| 73 | + if(c != ',') { |
| 74 | + throw runtime_error("Attempt to read a coordinate out of a non-coordinate value"); |
| 75 | + } |
| 76 | + stream >> ret.second; |
| 77 | + return ret; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Initialize a Config file, reading from an input stream. |
| 82 | + * @param source The stream to read config values from. |
| 83 | + */ |
| 84 | +Config::Config(istream& source) { |
| 85 | + init(source); |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Initialize a Config file, reading from a file stream. |
| 90 | + * @param source The file stream to read config values from. |
| 91 | + */ |
| 92 | +Config::Config(fstream&& source) { |
| 93 | + init(source); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Initialize a Config file, reading from a given file. |
| 98 | + * @param filename The path to the file to read. |
| 99 | + */ |
| 100 | +Config::Config(const string& filename) { |
| 101 | + fstream source(filename, fstream::in); |
| 102 | + init(source); |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Read in configuration values from a stream. |
| 107 | + * @param source The place to read configuration values from. |
| 108 | + */ |
| 109 | +void Config::init(istream& source) { |
| 110 | + string line; |
| 111 | + while(source) { |
| 112 | + std::getline(source, line); |
| 113 | + if(line[0] == '#' || line.find('=') == string::npos) { |
| 114 | + continue; |
| 115 | + } |
| 116 | + string name = line.substr(0, line.find('=')); |
| 117 | + string value = line.substr(line.find('=') + 1); |
| 118 | + values.insert(std::make_pair(name, ConfigValue(value))); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Retrieve a configuration value. |
| 124 | + * @throws runtime_error If no configuration value with that name exists. |
| 125 | + * @param name The name to retrieve from the configuration file. |
| 126 | + * @return The configuration value stored at the given name. |
| 127 | + */ |
| 128 | +const ConfigValue& Config::operator[](const string& name) const { |
| 129 | + auto it = values.find(name); |
| 130 | + if(it == values.end()) { |
| 131 | + throw runtime_error("No such key in the config"); |
| 132 | + } else { |
| 133 | + return it->second; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * Load a configuration file at a given path. |
| 139 | + * These configuration files are cached, and only will be loaded once. |
| 140 | + * @throws runtime_error If the file cannot be loaded. |
| 141 | + * @param filename The path to the file to read in. |
| 142 | + * @return The configuration stored at the file. |
| 143 | + */ |
| 144 | +const Config& getConfigFile(const std::string& filename) { |
| 145 | + static std::map<std::string, Config> configs; |
| 146 | + |
| 147 | + auto it = configs.find(filename); |
| 148 | + if(it == configs.end()) { |
| 149 | + fstream file(filename); |
| 150 | + if(!file) { |
| 151 | + throw runtime_error("Cannot open file " + filename); |
| 152 | + } |
| 153 | + return configs.insert(std::make_pair(filename, Config(file))).first->second; |
| 154 | + } else { |
| 155 | + return it->second; |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Load the graphics configuration file. |
| 161 | + * @throws runtime_error If the graphics configuration file is missing. |
| 162 | + * @return The graphics configuration. |
| 163 | + */ |
| 164 | +const Config& getGraphicsConfig() { |
| 165 | + return getConfigFile("resources/graphics.conf"); |
| 166 | +} |
0 commit comments