/// <summary> /// Set the setting /// </summary> /// <param name="key">key to save value to</param> /// <param name="value">value to set setting to</param> /// <param name="description">Write a description (optional)</param> /// <remarks>If the setting does not exist, it will create a new one</remarks> public static void SetSetting(string key, bool value, string description = null) { var pair = GetPair(key); if (pair == null) { pair = new SettingDescriptionPair(key, value.ToString(CultureInfo.InvariantCulture), description); _values.Add(pair); if (OnSettingChanged != null) { OnSettingChanged(null, new SettingsChangedEventArgs(key, null, pair.Value)); } return; } if (OnSettingChanged != null) { OnSettingChanged(null, new SettingsChangedEventArgs(key, pair.Value, value.ToString(CultureInfo.InvariantCulture))); } pair.Description = description; pair.Value = string.Join(",", value.ToString(CultureInfo.InvariantCulture)); }
/// <summary> /// Loads all the settings into the memory, if no properties file is found nothing will happen /// </summary> public static void LoadSettings() { if (!File.Exists(FileUtils.PropertiesPath + "server.properties")) { return; } var text = File.ReadAllLines(FileUtils.PropertiesPath + "server.properties"); _values.Clear(); for (int i = 0; i < text.Count(); i++) { string read = text[i]; SettingDescriptionPair pair; if (String.IsNullOrWhiteSpace(read)) { continue; } if (read[0] == '#' && (i + 1 < text.Count()) ? text[i + 1][0] != '#' && !String.IsNullOrWhiteSpace(text[i + 1]) : false) { i++; pair = new SettingDescriptionPair(text[i].Split('=')[0].Trim(), text[i].Split('=')[1].Trim(), read.Substring(1)); } else { if (read[0] != '#') { pair = new SettingDescriptionPair(read.Split('=')[0].Trim(), read.Split('=')[1].Trim(), null); } else { pair = new SettingDescriptionPair(null, text[i], null); } } _values.Add(pair); } }
/// <summary> /// Set the setting /// </summary> /// <param name="key">key to save value to</param> /// <param name="description">Write a description (optional)</param> /// <param name="values">for each string in values, it will be seperated by a comma ','</param> /// <remarks>If the setting does not exist, it will create a new one</remarks> public static void SetSetting(string key, string description = null, params string[] values) { var pair = GetPair(key); if (pair == null) { pair = new SettingDescriptionPair(key, string.Join(",", values), description); _values.Add(pair); if (OnSettingChanged != null) { OnSettingChanged(null, new SettingsChangedEventArgs(key, null, pair.Value)); } return; } if (OnSettingChanged != null) { OnSettingChanged(null, new SettingsChangedEventArgs(key, pair.Value, string.Join(",", values))); } pair.Description = description; pair.Value = string.Join(",", values); }