/// <summary> /// Constructor /// Reads configuration data from a file. If the file /// read fails, the object contains default values for /// the configuration data /// </summary> public ConfigurationData() { StreamReader file = null; try { file = File.OpenText(Path.Combine(Application.streamingAssetsPath, ConfigurationDataFileName)); string currentLine = file.ReadLine(); while (currentLine != null) { string[] tokens = currentLine.Split(','); ConfigurationDataValueName valueName = (ConfigurationDataValueName)Enum.Parse(typeof(ConfigurationDataValueName), tokens[0]); values.Add(valueName, float.Parse(tokens[1])); currentLine = file.ReadLine(); } } catch (Exception e) { Debug.LogError(e); SetDeafultValues(); } finally { if (file != null) { file.Close(); } } }
/// <summary> /// Reads from configuration file. /// </summary> /// <param name="streamReader">Stream reader.</param> /// <param name="values">The dictionary of values</param> static void ReadFromConfigurationFile(StreamReader streamReader, Dictionary <ConfigurationDataValueName, float> values) { streamReader = File.OpenText(Path.Combine(Application.streamingAssetsPath, ConfigurationDataFileName)); string currentLine = streamReader.ReadLine(); while (currentLine != null) { string[] tokens = currentLine.Split(','); ConfigurationDataValueName valueName = (ConfigurationDataValueName)Enum.Parse( typeof(ConfigurationDataValueName), tokens[0]); float val = float.Parse(tokens[1]); if (values.ContainsKey(valueName)) { values[valueName] = val; } else { values.Add(valueName, val); } currentLine = streamReader.ReadLine(); } }
/// <summary> /// Constructor /// Reads configuration data from a file. If the file /// read fails, the object contains default values for /// the configuration data /// </summary> public ConfigurationData() { // read and save configuration data from file StreamReader input = null; try { // create stream reader object input = File.OpenText(Path.Combine( Application.streamingAssetsPath, ConfigurationDataFileName)); // populate values string currentLine = input.ReadLine(); while (currentLine != null) { string[] tokens = currentLine.Split(','); ConfigurationDataValueName valueName = (ConfigurationDataValueName)Enum.Parse( typeof(ConfigurationDataValueName), tokens[0]); values.Add(valueName, float.Parse(tokens[1])); currentLine = input.ReadLine(); } } catch (Exception e) { // set default values if something went wrong SetDefaultValues(); } finally { // always close input file if (input != null) { input.Close(); } } }
private void parseLine(string currentLine) { // ignore empty lines and ones beginning with // if (!string.IsNullOrEmpty(currentLine) && !currentLine.StartsWith("//", StringComparison.CurrentCulture)) { string[] tokens = currentLine.Split(','); // property delimiter ConfigurationDataValueName valueName = (ConfigurationDataValueName)Enum.Parse( typeof(ConfigurationDataValueName), tokens[0].ToUpper()); switch (valueName) { case ConfigurationDataValueName.TITLE: this.name = tokens[1]; break; case ConfigurationDataValueName.ID: this.id = tokens[1]; break; case ConfigurationDataValueName.MUSIC: this.music = tokens[1]; break; case ConfigurationDataValueName.ITEMCOUNT: this.itemCount = int.Parse(tokens[1]); break; case ConfigurationDataValueName.CREATURECOUNT: this.creatureCount = int.Parse(tokens[1]); break; default: roomParameters.Add(currentLine); break; } } }
/// <summary> /// Constructor /// Reads configuration data from a file. If the file read fails, the /// object contains default values for the configuration data /// </summary> public ConfigurationData() { // Read and save configuration data from file StreamReader input = null; try { // create stream reader object input = File.OpenText(Path.Combine(Application.streamingAssetsPath, ConfigurationDataFileName)); // populate values string currentLine = input.ReadLine(); while (currentLine != null) { string[] split = currentLine.Split(','); ConfigurationDataValueName valueName = (ConfigurationDataValueName)Enum.Parse( typeof(ConfigurationDataValueName), split[0]); values.Add(valueName, float.Parse(split[1])); currentLine = input.ReadLine(); } } catch (Exception e) { // Set default values if issue with reading file Console.WriteLine(e.ToString()); SetDefaultValues(); } finally { // close input file if (input != null) { input.Close(); } } }