using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace MinecraftClient { /// /// INI File tools for parsing and generating user-friendly INI files /// By ORelio (c) 2014-2020 - CDDL 1.0 /// static class INIFile { /// /// Parse a INI file into a dictionary. /// Values can be accessed like this: dict["section"]["setting"] /// /// INI file to parse /// INI sections and keys will be converted to lowercase unless this parameter is set to false /// If failed to read the file /// Parsed data from INI file public static Dictionary> ParseFile(string iniFile, bool lowerCase = true) { return ParseFile(File.ReadAllLines(iniFile, Encoding.UTF8), lowerCase); } /// /// Parse a INI file into a dictionary. /// Values can be accessed like this: dict["section"]["setting"] /// /// INI file content to parse /// INI sections and keys will be converted to lowercase unless this parameter is set to false /// If failed to read the file /// Parsed data from INI file public static Dictionary> ParseFile(IEnumerable lines, bool lowerCase = true) { var iniContents = new Dictionary>(); string iniSection = "default"; foreach (string lineRaw in lines) { string line = lineRaw.Split('#')[0].Trim(); if (line.Length > 0 && line[0] != ';') { if (line[0] == '[' && line[line.Length - 1] == ']') { iniSection = line.Substring(1, line.Length - 2); if (lowerCase) iniSection = iniSection.ToLower(); } else { string argName = line.Split('=')[0]; if (lowerCase) argName = argName.ToLower(); if (line.Length > (argName.Length + 1)) { string argValue = line.Substring(argName.Length + 1); if (!iniContents.ContainsKey(iniSection)) iniContents[iniSection] = new Dictionary(); iniContents[iniSection][argName] = argValue; } } } } return iniContents; } /// /// Write given data into an INI file /// /// File to write into /// Data to put into the file /// INI file description, inserted as a comment on first line of the INI file /// Automatically change first char of section and keys to uppercase public static void WriteFile(string iniFile, Dictionary> contents, string description = null, bool autoCase = true) { File.WriteAllLines(iniFile, Generate(contents, description, autoCase), Encoding.UTF8); } /// /// Generate given data into the INI format /// /// Data to put into the INI format /// INI file description, inserted as a comment on first line of the INI file /// Automatically change first char of section and keys to uppercase /// Lines of the INI file public static string[] Generate(Dictionary> contents, string description = null, bool autoCase = true) { List lines = new List(); if (!String.IsNullOrWhiteSpace(description)) lines.Add('#' + description); foreach (var section in contents) { if (lines.Count > 0) lines.Add(""); if (!String.IsNullOrEmpty(section.Key)) { lines.Add("[" + (autoCase ? char.ToUpper(section.Key[0]) + section.Key.Substring(1) : section.Key) + ']'); foreach (var item in section.Value) if (!String.IsNullOrEmpty(item.Key)) lines.Add((autoCase ? char.ToUpper(item.Key[0]) + item.Key.Substring(1) : item.Key) + '=' + item.Value); } } return lines.ToArray(); } /// /// Convert an integer from a string or return 0 if failed to parse /// /// String to parse /// Int value public static int Str2Int(string str) { try { return Convert.ToInt32(str); } catch { return 0; } } /// /// Convert a 0/1 or True/False value to boolean /// /// String to parse /// Boolean value public static bool Str2Bool(string str) { return str.ToLower() == "true" || str == "1"; } } }