// load game settings from a specified file name public bool loadFrom(string fileName) { // use a variable system to parse the settings file VariableCollection newVariables = VariableCollection.readFrom(fileName); if(newVariables == null) { return false; } m_variables = newVariables; // create local variables instantiated with data parsed from the variable system return true; }
// parse a collection of variables from a file public static VariableCollection readFrom(string fileName) { if(fileName == null || fileName.Length == 0) { return null; } string data; // open the file StreamReader input = null; try { input = File.OpenText(fileName); } catch(Exception) { return null; } VariableCollection variables = new VariableCollection(); string category = null; int categoryIndex = -1; // read to the end of the file while((data = input.ReadLine()) != null) { data = data.Trim(); if(data.Length == 0) { category = null; categoryIndex = -1; continue; } // parse a category if(data.Length >= 2 && data[0] == '[' && data[data.Length-1] == ']') { category = data.Substring(1, data.Length - 2); categoryIndex = variables.addCategory(category); } // parse a variable else { Variable v = Variable.parseFrom(data); if(v != null) { v.category = categoryIndex; variables.addVariable(v); } } } if(input != null) { input.Close(); } return variables; }
public SettingsManager() { m_variables = new VariableCollection(); reset(); }