diff --git a/Shared/OptionsFile.cs b/Shared/OptionsFile.cs index cc5766a7d..7bf064c41 100644 --- a/Shared/OptionsFile.cs +++ b/Shared/OptionsFile.cs @@ -1,14 +1,12 @@ -using System.Collections.Generic; -using System.IO; -using System.Linq; +using System.IO; namespace Shared { public class OptionsFile { - private string m_FileName; - private string[] m_CompatFileNames; - private Dictionary m_Options = new Dictionary(); + private readonly string m_FileName; + private readonly string[] m_CompatFileNames; + private Dictionary m_Options = new(); public OptionsFile(string fileName, params string[] compatFileNames) { @@ -37,12 +35,12 @@ public IEnumerable GetKeys() } } - public string ReadValue(string key, string defaultValue = null, params string[] compatKeys) + public string? ReadValue(string key, string? defaultValue = null, params string[] compatKeys) { var keys = compatKeys.Prepend(key); foreach (var _key in keys) { - if (!m_Options.ContainsKey(_key) || m_Options[_key] == null) + if (!m_Options.ContainsKey(_key) || m_Options[_key] is null) { continue; } @@ -55,8 +53,7 @@ public string ReadValue(string key, string defaultValue = null, params string[] public int ReadValueInt(string key, int defaultValue, params string[] compatKeys) { - int result; - if (!int.TryParse(ReadValue(key, null, compatKeys), out result)) + if (!int.TryParse(ReadValue(key, null, compatKeys), out int result)) { result = defaultValue; } @@ -66,8 +63,7 @@ public int ReadValueInt(string key, int defaultValue, params string[] compatKeys public long ReadValueLong(string key, long defaultValue, params string[] compatKeys) { - long result; - if (!long.TryParse(ReadValue(key, null, compatKeys), out result)) + if (!long.TryParse(ReadValue(key, null, compatKeys), out long result)) { result = defaultValue; } @@ -94,24 +90,24 @@ public bool ReadValueBool(string key, bool defaultValue, params string[] compatK return defaultValue; } - public void WriteValue(string key, string value) + public void WriteValue(string key, string? value) { - m_Options[key] = string.IsNullOrEmpty(value) ? null : value; + m_Options[key] = value is null || string.IsNullOrEmpty(value) ? null : value; } - public void WriteValue(string key, int value) + public void WriteValue(string key, int? value) { WriteValue(key, $"{value}"); } - public void WriteValue(string key, bool value) + public void WriteValue(string key, bool? value) { - WriteValue(key, value ? 1 : 0); + WriteValue(key, value is true ? 1 : 0); } public void ReadFile() { - string readData(string fileName) + static string? readData(string fileName) { if (!File.Exists(fileName)) { @@ -127,7 +123,7 @@ string readData(string fileName) return _data; } - string data; + string? data; if ((data = readData(m_FileName)) == null) { bool dataRead = false; @@ -148,22 +144,30 @@ string readData(string fileName) m_Options.Clear(); - foreach (string line in data.Split('\n')) + if (data is not null) { - if (!line.Contains("=")) + foreach (string line in data.Split('\n')) { - continue; - } + if (!line.Contains('=')) + { + continue; + } - var keyValuePair = line.Split('=', 2, System.StringSplitOptions.RemoveEmptyEntries - | System.StringSplitOptions.TrimEntries); + var keyValuePair = line.Split('=', 2, System.StringSplitOptions.RemoveEmptyEntries + | System.StringSplitOptions.TrimEntries); - m_Options[keyValuePair[0]] = keyValuePair.Length == 2 ? keyValuePair[1] : null; + m_Options[keyValuePair[0]] = keyValuePair.Length == 2 ? keyValuePair[1] : null; + } } } public void WriteFile() { + if (m_FileName is null) + { + return; + } + string data = string.Empty; foreach (var option in m_Options) @@ -171,17 +175,30 @@ public void WriteFile() data += $"{option.Key}={option.Value}\n"; } - Directory.CreateDirectory(Path.GetDirectoryName(m_FileName)); + var directory = Path.GetDirectoryName(m_FileName); + if (directory is not null) + { + Directory.CreateDirectory(directory); + } File.WriteAllText(m_FileName, data); } public void ResetFile() { - Directory.CreateDirectory(Path.GetDirectoryName(m_FileName)); + if (m_FileName is null) + { + return; + } + + var directory = Path.GetDirectoryName(m_FileName); + if (directory is not null) + { + Directory.CreateDirectory(directory); + } File.WriteAllText(m_FileName, null); // Reset all options too - m_Options = new Dictionary(); + m_Options = new(); } public bool HasCompatFile()