1 Star 0 Fork 41

maclinuxwindows/Zircon

forked from 黑龙/Zircon 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ConfigReader.cs 21.85 KB
一键复制 编辑 原始数据 按行查看 历史
黑龙 提交于 2019-02-24 11:27 +08:00 . 汉化基础源码
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
namespace Library
{
public static class ConfigReader
{
private static readonly Regex HeaderRegex = new Regex(@"^\[(?<Header>.+)\]$", RegexOptions.Compiled);
private static readonly Regex EntryRegex = new Regex(@"^(?<Key>.*?)=(?<Value>.*)$", RegexOptions.Compiled);
private static readonly Regex ColorRegex = new Regex(@"\[A:\s?(?<A>[0-9]{1,3}),\s?R:\s?(?<R>[0-9]{1,3}),\s?G:\s?(?<G>[0-9]{1,3}),\s?B:\s?(?<B>[0-9]{1,3})\]", RegexOptions.Compiled);
public static readonly Dictionary<Type, object> ConfigObjects = new Dictionary<Type, object>();
private static readonly Dictionary<Type, Dictionary<string, Dictionary<string, string>>> ConfigContents = new Dictionary<Type, Dictionary<string, Dictionary<string, string>>>();
public static void Load()
{
Type[] types = Assembly.GetEntryAssembly().GetTypes();
foreach (Type type in types)
{
ConfigPath config = type.GetCustomAttribute<ConfigPath>();
if (config == null) continue;
object ob = null;
if (!type.IsAbstract || !type.IsSealed)
ConfigObjects[type] = ob = Activator.CreateInstance(type);
ReadConfig(type, config.Path, ob);
}
}
public static void Save()
{
Type[] types = Assembly.GetEntryAssembly().GetTypes();
foreach (Type type in types)
{
ConfigPath config = type.GetCustomAttribute<ConfigPath>();
if (config == null) continue;
object ob = null;
if (!type.IsAbstract || !type.IsSealed)
ob = ConfigObjects[type];
SaveConfig(type, config.Path, ob);
}
}
private static void ReadConfig(Type type, string path, object ob)
{
if (!File.Exists(path)) return;
PropertyInfo[] properties = type.GetProperties();
Dictionary<string, Dictionary<string, string>> contents = ConfigContents[type] = new Dictionary<string, Dictionary<string, string>>();
string[] lines = File.ReadAllLines(path);
Dictionary<string, string> section = null;
foreach (string line in lines)
{
Match match = HeaderRegex.Match(line);
if (match.Success)
{
section = new Dictionary<string, string>();
contents[match.Groups["Header"].Value] = section;
continue;
}
if (section == null) continue;
match = EntryRegex.Match(line);
if (!match.Success) continue;
section[match.Groups["Key"].Value] = match.Groups["Value"].Value;
}
string lastSection = null;
foreach (PropertyInfo property in properties)
{
ConfigSection config = property.GetCustomAttribute<ConfigSection>();
if (config != null) lastSection = config.Section;
if (lastSection == null) continue;
MethodInfo method = typeof(ConfigReader).GetMethod("Read", new[] { typeof(Type), typeof(string), typeof(string), property.PropertyType });
property.SetValue(ob, method.Invoke(null, new[] { type, lastSection, property.Name, property.GetValue(ob) }));
}
}
private static void SaveConfig(Type type, string path, object ob)
{
PropertyInfo[] properties = type.GetProperties();
Dictionary<string, Dictionary<string, string>> contents = ConfigContents[type] = new Dictionary<string, Dictionary<string, string>>();
string lastSection = null;
foreach (PropertyInfo property in properties)
{
ConfigSection config = property.GetCustomAttribute<ConfigSection>();
if (config != null) lastSection = config.Section;
if (lastSection == null) continue;
MethodInfo method = typeof(ConfigReader).GetMethod("Write", new[] { typeof(Type), typeof(string), typeof(string), property.PropertyType });
method.Invoke(ob, new[] { type, lastSection, property.Name, property.GetValue(ob) });
}
List<string> lines = new List<string>();
foreach (KeyValuePair<string, Dictionary<string, string>> header in contents)
{
lines.Add($"[{header.Key}]");
foreach (KeyValuePair<string, string> entries in header.Value)
lines.Add($"{entries.Key}={entries.Value}");
lines.Add(string.Empty);
}
if (!Directory.Exists(Path.GetDirectoryName(path)))
Directory.CreateDirectory(Path.GetDirectoryName(path));
File.WriteAllLines(path, lines, Encoding.Unicode);
}
private static bool TryGetEntry(Type type, string section, string key, out string value)
{
value = null;
Dictionary<string, Dictionary<string, string>> contents;
Dictionary<string, string> entries;
if (!ConfigContents.TryGetValue(type, out contents))
ConfigContents[type] = contents = new Dictionary<string, Dictionary<string, string>>();
if (contents.TryGetValue(section, out entries))
return entries.TryGetValue(key, out value);
entries = new Dictionary<string, string>();
contents[section] = entries;
return false;
}
#region Reads
public static Boolean Read(Type type,string section, string key, Boolean value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
Boolean result;
if (Boolean.TryParse(entry, out result))
return result;
}
ConfigContents[type][section][key] = value.ToString();
return value;
}
public static Byte Read(Type type, string section, string key, Byte value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
Byte result;
if (Byte.TryParse(entry, out result))
return result;
}
ConfigContents[type][section][key] = value.ToString();
return value;
}
public static Int16 Read(Type type, string section, string key, Int16 value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
Int16 result;
if (Int16.TryParse(entry, out result))
return result;
}
ConfigContents[type][section][key] = value.ToString();
return value;
}
public static Int32 Read(Type type, string section, string key, Int32 value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
Int32 result;
if (Int32.TryParse(entry, out result))
return result;
}
ConfigContents[type][section][key] = value.ToString();
return value;
}
public static Int64 Read(Type type, string section, string key, Int64 value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
Int64 result;
if (Int64.TryParse(entry, out result))
return result;
}
ConfigContents[type][section][key] = value.ToString();
return value;
}
public static SByte Read(Type type, string section, string key, SByte value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
SByte result;
if (SByte.TryParse(entry, out result))
return result;
}
ConfigContents[type][section][key] = value.ToString();
return value;
}
public static UInt16 Read(Type type, string section, string key, UInt16 value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
UInt16 result;
if (UInt16.TryParse(entry, out result))
return result;
}
ConfigContents[type][section][key] = value.ToString();
return value;
}
public static UInt32 Read(Type type, string section, string key, UInt32 value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
UInt32 result;
if (UInt32.TryParse(entry, out result))
return result;
}
ConfigContents[type][section][key] = value.ToString();
return value;
}
public static UInt64 Read(Type type, string section, string key, UInt64 value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
UInt64 result;
if (UInt64.TryParse(entry, out result))
return result;
}
ConfigContents[type][section][key] = value.ToString();
return value;
}
public static Single Read(Type type, string section, string key, Single value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
Single result;
if (Single.TryParse(entry, out result))
return result;
}
ConfigContents[type][section][key] = value.ToString(CultureInfo.InvariantCulture);
return value;
}
public static Double Read(Type type, string section, string key, Double value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
Double result;
if (Double.TryParse(entry, out result))
return result;
}
ConfigContents[type][section][key] = value.ToString(CultureInfo.InvariantCulture);
return value;
}
public static Decimal Read(Type type, string section, string key, Decimal value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
Decimal result;
if (Decimal.TryParse(entry, out result))
return result;
}
ConfigContents[type][section][key] = value.ToString(CultureInfo.InvariantCulture);
return value;
}
public static Char Read(Type type, string section, string key, Char value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
Char result;
if (Char.TryParse(entry, out result))
return result;
}
ConfigContents[type][section][key] = value.ToString();
return value;
}
public static String Read(Type type, string section, string key, String value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
return entry;
ConfigContents[type][section][key] = value;
return value;
}
public static Point Read(Type type, string section, string key, Point value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
string[] data = entry.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
int x, y;
if (data.Length == 2 && int.TryParse(data[0], out x) && int.TryParse(data[1], out y))
return new Point(x, y);
}
ConfigContents[type][section][key] = $"{value.X}, {value.Y}";
return value;
}
public static Size Read(Type type, string section, string key, Size value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
string[] data = entry.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
int width, height;
if (data.Length == 2 && int.TryParse(data[0], out width) && int.TryParse(data[1], out height))
return new Size(width, height);
}
ConfigContents[type][section][key] = $"{value.Width}, {value.Height}";
return value;
}
public static SizeF Read(Type type, string section, string key, SizeF value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
string[] data = entry.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
float width, height;
if (data.Length == 2 && float.TryParse(data[0], out width) && float.TryParse(data[1], out height))
return new SizeF(width, height);
}
ConfigContents[type][section][key] = $"{value.Width}, {value.Height}";
return value;
}
public static TimeSpan Read(Type type, string section, string key, TimeSpan value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
TimeSpan result;
if (TimeSpan.TryParse(entry, out result))
return result;
}
ConfigContents[type][section][key] = value.ToString();
return value;
}
public static DateTime Read(Type type, string section, string key, DateTime value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
DateTime result;
if (DateTime.TryParse(entry, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
return result;
}
ConfigContents[type][section][key] = value.ToString(CultureInfo.InvariantCulture);
return value;
}
public static Color Read(Type type, string section, string key, Color value)
{
string entry;
if (TryGetEntry(type, section, key, out entry))
{
Match match = ColorRegex.Match(entry);
if (match.Success)
{
int a = int.Parse(match.Groups["A"].Value);
int r = int.Parse(match.Groups["R"].Value);
int g = int.Parse(match.Groups["G"].Value);
int b = int.Parse(match.Groups["B"].Value);
return Color.FromArgb(
Math.Min(Byte.MaxValue, Math.Max(Byte.MinValue, a)),
Math.Min(Byte.MaxValue, Math.Max(Byte.MinValue, r)),
Math.Min(Byte.MaxValue, Math.Max(Byte.MinValue, g)),
Math.Min(Byte.MaxValue, Math.Max(Byte.MinValue, b)));
}
}
ConfigContents[type][section][key] = $"[A:{value.A}, R:{value.R}, G:{value.G}, B:{value.B}]";
return value;
}
#endregion
#region Writes
public static void Write(Type type, string section, string key, Boolean value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = value.ToString();
}
public static void Write(Type type, string section, string key, Byte value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = value.ToString();
}
public static void Write(Type type, string section, string key, Int16 value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = value.ToString();
}
public static void Write(Type type, string section, string key, Int32 value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = value.ToString();
}
public static void Write(Type type, string section, string key, Int64 value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = value.ToString();
}
public static void Write(Type type, string section, string key, SByte value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = value.ToString();
}
public static void Write(Type type, string section, string key, UInt16 value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = value.ToString();
}
public static void Write(Type type, string section, string key, UInt32 value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = value.ToString();
}
public static void Write(Type type, string section, string key, UInt64 value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = value.ToString();
}
public static void Write(Type type, string section, string key, Single value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = value.ToString(CultureInfo.InvariantCulture);
}
public static void Write(Type type, string section, string key, Double value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = value.ToString(CultureInfo.InvariantCulture);
}
public static void Write(Type type, string section, string key, Decimal value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = value.ToString(CultureInfo.InvariantCulture);
}
public static void Write(Type type, string section, string key, Char value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = value.ToString();
}
public static void Write(Type type, string section, string key, String value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = value;
}
public static void Write(Type type, string section, string key, Point value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = $"{value.X}, {value.Y}";
}
public static void Write(Type type, string section, string key, Size value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = $"{value.Width}, {value.Height}";
}
public static void Write(Type type, string section, string key, TimeSpan value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = value.ToString();
}
public static void Write(Type type, string section, string key, DateTime value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = value.ToString(CultureInfo.InvariantCulture);
}
public static void Write(Type type, string section, string key, Color value)
{
if (!ConfigContents[type].ContainsKey(section)) ConfigContents[type][section] = new Dictionary<string, string>();
ConfigContents[type][section][key] = $"[A:{value.A}, R:{value.R}, G:{value.G}, B:{value.B}]";
}
#endregion
}
[AttributeUsage(AttributeTargets.Class)]
public class ConfigPath : Attribute
{
public string Path { get; set; }
public ConfigPath(string path)
{
Path = path;
}
}
[AttributeUsage(AttributeTargets.Property)]
public class ConfigSection : Attribute
{
public string Section { get; set; }
public ConfigSection(string section)
{
Section = section;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/maclinuxwindows/Zircon.git
git@gitee.com:maclinuxwindows/Zircon.git
maclinuxwindows
Zircon
Zircon
master

搜索帮助