Thinking about it, the necessary functions of this would be to return values from the config file to the program, to allow for comments in the config file, and to allow for simple assignments such as "texture_path=texture/".
The result:
class TextConfigLoader { Dictionary<string, string> config; public TextConfigLoader() { config = new Dictionary<string, string>(); } public bool LoadConfig(string location) { try { StreamReader streamReader = new StreamReader(location); string configText = streamReader.ReadToEnd(); streamReader.Close(); string[] configArray = configText.Split('\n'); foreach (string s in configArray) { if (!s.StartsWith("#") || s.Length == 0) { string[] configLine = s.Split('='); config.Add(configLine[0].ToLower(), configLine[1]); //lowercase the setting name so that casing does not matter } } return true; } catch { throw new Exception("Error loading settings.cfg. Incorrect format or nonexistant file."); } } public string GetSetting(string settingName) { settingName = settingName.ToLower(); //lowercase the setting name so that casing does not matter string value = ""; //if TryGetValue fails to get the value, throw an exception. otherwise, return the value. if (!config.TryGetValue(settingName, out value)) { throw new Exception("Setting does not exist in settings.cfg: " + settingName + "\nPlease check your settings.cfg file."); } return value; } }
As you can see, I've added simple error handling. If a setting doesn't exist, it will throw an error. If the settings file is incorrectly formatted or non-existent, it will throw an error.
On a side note, I believe I have tonsillitis. Updates may become slow if it gets worse than it already is.
Now that configuration & loading/drawing of textures is out of the way, we can begin coding the game.
No comments:
Post a Comment