dtsod
This commit is contained in:
67
Dtsod/V30/DtsodDict.cs
Normal file
67
Dtsod/V30/DtsodDict.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace DTLib.Dtsod;
|
||||
|
||||
public class DtsodDict<TKey, TVal> : IDictionary<TKey, TVal>
|
||||
{
|
||||
// да, вместо собственной реализации интерфейса это ссылки на Dictionary
|
||||
readonly Dictionary<TKey, TVal> baseDict;
|
||||
|
||||
public DtsodDict() => baseDict = new();
|
||||
public DtsodDict(IDictionary<TKey, TVal> srcDict) => baseDict = new(srcDict);
|
||||
|
||||
|
||||
public virtual TVal this[TKey key]
|
||||
{
|
||||
get => TryGetValue(key, out TVal value) ? value : throw new Exception($"Dtsod[{key}] key not found");
|
||||
set
|
||||
{
|
||||
if (!TrySetValue(key, value)) throw new KeyNotFoundException($"DtsodDict[{key}] key not found");
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool TryGetValue(TKey key, out TVal value) => baseDict.TryGetValue(key, out value);
|
||||
|
||||
public virtual bool TrySetValue(TKey key, TVal value)
|
||||
{
|
||||
if (ContainsKey(key))
|
||||
{
|
||||
baseDict[key] = value;
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
public virtual void Append(ICollection<KeyValuePair<TKey, TVal>> anotherDtsod)
|
||||
{
|
||||
foreach (var pair in anotherDtsod)
|
||||
Add(pair.Key, pair.Value);
|
||||
}
|
||||
|
||||
public virtual void Add(TKey key, TVal value) => baseDict.Add(key, value);
|
||||
|
||||
public virtual void Add(KeyValuePair<TKey, TVal> pair)
|
||||
=> ((ICollection<KeyValuePair<TKey, TVal>>)baseDict).Add(pair);
|
||||
|
||||
|
||||
public int Count => baseDict.Count;
|
||||
public ICollection<TKey> Keys { get => baseDict.Keys; }
|
||||
public ICollection<TVal> Values { get => baseDict.Values; }
|
||||
public bool IsReadOnly { get; } = false;
|
||||
|
||||
public virtual void Clear() => baseDict.Clear();
|
||||
|
||||
public virtual bool ContainsKey(TKey key) => baseDict.ContainsKey(key);
|
||||
|
||||
bool ICollection<KeyValuePair<TKey, TVal>>.Contains(KeyValuePair<TKey, TVal> pair)
|
||||
=> ((ICollection<KeyValuePair<TKey, TVal>>)baseDict).Contains(pair);
|
||||
|
||||
void ICollection<KeyValuePair<TKey, TVal>>.CopyTo(KeyValuePair<TKey, TVal>[] array, int arrayIndex)
|
||||
=> ((ICollection<KeyValuePair<TKey, TVal>>)baseDict).CopyTo(array, arrayIndex);
|
||||
|
||||
public virtual bool Remove(TKey key) => baseDict.Remove(key);
|
||||
bool ICollection<KeyValuePair<TKey, TVal>>.Remove(KeyValuePair<TKey, TVal> pair)
|
||||
=> ((ICollection<KeyValuePair<TKey, TVal>>)baseDict).Remove(pair);
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => baseDict.GetEnumerator();
|
||||
public IEnumerator<KeyValuePair<TKey, TVal>> GetEnumerator() => baseDict.GetEnumerator();
|
||||
}
|
||||
@@ -1,10 +1,7 @@
|
||||
using System;
|
||||
namespace DTLib.Dtsod;
|
||||
|
||||
namespace DTLib.Dtsod
|
||||
public class DtsodSerializableAttribute : Attribute
|
||||
{
|
||||
public class DtsodSerializableAttribute : Attribute
|
||||
{
|
||||
public DtsodVersion Version;
|
||||
public DtsodSerializableAttribute(DtsodVersion ver) => Version = ver;
|
||||
}
|
||||
public DtsodVersion Version;
|
||||
public DtsodSerializableAttribute(DtsodVersion ver) => Version = ver;
|
||||
}
|
||||
|
||||
@@ -1,17 +1,287 @@
|
||||
namespace DTLib.Dtsod
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
|
||||
namespace DTLib.Dtsod;
|
||||
|
||||
public class DtsodV30 : DtsodDict<string, dynamic>, IDtsod
|
||||
{
|
||||
public static class DtsodV30
|
||||
public DtsodVersion Version { get; } = DtsodVersion.V30;
|
||||
|
||||
public IDictionary<string, dynamic> ToDictionary() => this;
|
||||
|
||||
public DtsodV30() : base() => UpdateLazy();
|
||||
public DtsodV30(IDictionary<string, dynamic> dict) : base(dict) => UpdateLazy();
|
||||
public DtsodV30(string serialized) : this() { Append(Deserialize(serialized)); }
|
||||
|
||||
#if DEBUG
|
||||
static void DebugLog(params string[] msg) => PublicLog.Log(msg);
|
||||
#endif
|
||||
|
||||
static IDictionary<string, dynamic> Deserialize(string text)
|
||||
{
|
||||
/*
|
||||
public static DtsodV30 FromObject(object target)
|
||||
char c;
|
||||
int i = -1; // ++i в ReadType
|
||||
StringBuilder b = new();
|
||||
|
||||
|
||||
Type ReadType()
|
||||
{
|
||||
|
||||
while (i < text.Length)
|
||||
{
|
||||
c = text[++i];
|
||||
switch (c)
|
||||
{
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '\r':
|
||||
case '\n':
|
||||
break;
|
||||
case '#':
|
||||
SkipComment();
|
||||
break;
|
||||
case ':':
|
||||
string _type = b.ToString();
|
||||
b.Clear();
|
||||
return TypeHelper.TypeFromString(_type);
|
||||
case '=':
|
||||
case '"':
|
||||
case ';':
|
||||
case '[':
|
||||
case ']':
|
||||
case '{':
|
||||
case '}':
|
||||
throw new Exception($"DtsodV30.Deserialize() error: unexpected {c}");
|
||||
default:
|
||||
b.Append(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception("DtsodV30.Deserialize.ReadType() error: end of text\ntext:\n" + text);
|
||||
}
|
||||
|
||||
public T ToObject<T>()
|
||||
string ReadName()
|
||||
{
|
||||
|
||||
while (i < text.Length)
|
||||
{
|
||||
c = text[++i];
|
||||
switch (c)
|
||||
{
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '\r':
|
||||
case '\n':
|
||||
break;
|
||||
case '#':
|
||||
SkipComment();
|
||||
break;
|
||||
case '=':
|
||||
string _name = b.ToString();
|
||||
b.Clear();
|
||||
return _name;
|
||||
case ':':
|
||||
case '"':
|
||||
case ';':
|
||||
case '[':
|
||||
case ']':
|
||||
case '{':
|
||||
case '}':
|
||||
throw new Exception($"DtsodV30.Deserialize() error: unexpected {c}");
|
||||
default:
|
||||
b.Append(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception("DtsodV30.Deserialize.ReadName() error: end of text\ntext:\n" + text);
|
||||
}
|
||||
*/
|
||||
|
||||
object[] ReadValue()
|
||||
{
|
||||
void ReadString()
|
||||
{
|
||||
c = text[++i]; //пропускает начальный символ '"'
|
||||
while (c != '"' || (text[i - 1] == '\\' && text[i - 2] != '\\'))
|
||||
{
|
||||
b.Append(c);
|
||||
if (++i >= text.Length) throw new Exception("DtsodV30.Deserialize() error: end of text\ntext:\n" + text);
|
||||
c = text[i];
|
||||
}
|
||||
}
|
||||
|
||||
bool endoflist = false; // выставляется в цикле в ReadValue()
|
||||
List<object> ReadList()
|
||||
{
|
||||
List<dynamic> list = new();
|
||||
while (!endoflist)
|
||||
list.Add(CreateInstance(ReadType(), ReadValue()));
|
||||
endoflist = false;
|
||||
return list;
|
||||
}
|
||||
|
||||
IDictionary<string, dynamic> ReadDictionary()
|
||||
{
|
||||
short bracketBalance = 1;
|
||||
c = text[++i]; //пропускает начальный символ '{'
|
||||
while (bracketBalance != 0)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '\r':
|
||||
case '\n':
|
||||
break;
|
||||
case '#':
|
||||
SkipComment();
|
||||
break;
|
||||
case '{':
|
||||
bracketBalance++;
|
||||
b.Append(c);
|
||||
break;
|
||||
case '}':
|
||||
bracketBalance--;
|
||||
if (bracketBalance != 0)
|
||||
b.Append(c);
|
||||
break;
|
||||
case '"':
|
||||
b.Append('"');
|
||||
ReadString();
|
||||
b.Append('"');
|
||||
break;
|
||||
default:
|
||||
b.Append(c);
|
||||
break;
|
||||
}
|
||||
|
||||
if (++i >= text.Length) throw new Exception("DtsodV30.Deserialize() error: end of text\ntext:\n" + text);
|
||||
c = text[i];
|
||||
}
|
||||
|
||||
return Deserialize(b.ToString());
|
||||
}
|
||||
|
||||
while (i < text.Length)
|
||||
{
|
||||
c = text[++i];
|
||||
switch (c)
|
||||
{
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '\r':
|
||||
case '\n':
|
||||
break;
|
||||
case '#':
|
||||
SkipComment();
|
||||
break;
|
||||
case '"':
|
||||
ReadString();
|
||||
break;
|
||||
case ';': // один параметр
|
||||
case ',': // для листов
|
||||
var str = b.ToString();
|
||||
b.Clear();
|
||||
// hardcoded "null" value
|
||||
return str == "null" ? (new object[] { null }) : (new object[] { str });
|
||||
case '[':
|
||||
{
|
||||
object[] _value = ReadList().ToArray();
|
||||
b.Clear();
|
||||
return _value;
|
||||
}
|
||||
case ']':
|
||||
endoflist = true;
|
||||
goto case ',';
|
||||
case '{':
|
||||
{
|
||||
object[] _value = new object[] { ReadDictionary() };
|
||||
b.Clear();
|
||||
return _value;
|
||||
}
|
||||
case '=':
|
||||
case ':':
|
||||
case '}':
|
||||
throw new Exception($"DtsodV30.Deserialize() error: unexpected {c}");
|
||||
default:
|
||||
b.Append(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception("DtsodV30.Deserialize.ReadValue() error: end of text\ntext:\n" + text);
|
||||
}
|
||||
|
||||
void SkipComment()
|
||||
{
|
||||
while (text[i] != '\n')
|
||||
if (++i >= text.Length) throw new Exception("DtsodV30.Deserialize() error: end of text\ntext:\n" + text);
|
||||
}
|
||||
|
||||
object CreateInstance(Type type, object[] ctor_args)
|
||||
{
|
||||
if (TypeHelper.BaseTypeConstructors.TryGetValue(type, out var ctor))
|
||||
return (object)ctor.Invoke((string)ctor_args[0]);
|
||||
else if (type.CustomAttributes.Any(a => a.AttributeType == typeof(DtsodSerializableAttribute)))
|
||||
return Activator.CreateInstance(type, ctor_args);
|
||||
else throw new Exception($"type {type.AssemblyQualifiedName} doesn't have DtsodSerializableAttribute");
|
||||
}
|
||||
|
||||
Dictionary<string, dynamic> output = new();
|
||||
Type type;
|
||||
string name;
|
||||
object[] value;
|
||||
|
||||
for (; i < text.Length; i++)
|
||||
{
|
||||
type = ReadType();
|
||||
name = ReadName();
|
||||
value = ReadValue();
|
||||
output.Add(name, CreateInstance(type, value));
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
public override void Append(ICollection<KeyValuePair<string, dynamic>> anotherDtsod)
|
||||
{
|
||||
base.Append(anotherDtsod);
|
||||
//UpdateLazy();
|
||||
}
|
||||
|
||||
public override void Add(string key, dynamic value)
|
||||
{
|
||||
base.Add(key, (object)value);
|
||||
//UpdateLazy();
|
||||
}
|
||||
|
||||
protected static string Serialize(IDictionary<string, dynamic> dtsod, ushort tabsCount = 0)
|
||||
{
|
||||
StringBuilder b = new();
|
||||
foreach (var pair in dtsod)
|
||||
{
|
||||
Type type = pair.Value.GetType();
|
||||
b.Append(TypeHelper.TypeToString(type)).Append(':')
|
||||
.Append(pair.Key).Append('=');
|
||||
if (TypeHelper.BaseTypeNames.ContainsKey(type))
|
||||
{
|
||||
if (type == typeof(decimal) || type == typeof(double) || type == typeof(float))
|
||||
b.Append(pair.Value.ToString(CultureInfo.InvariantCulture));
|
||||
else b.Append(pair.Value.ToString());
|
||||
}
|
||||
else if (typeof(IDictionary<string, dynamic>).IsAssignableFrom(type))
|
||||
b.Append("\n{\n").Append(Serialize(pair.Value, tabsCount++)).Append("};\n");
|
||||
else
|
||||
{
|
||||
type.GetProperties().Where(p => p.CustomAttributes.Any(a => a.AttributeType == typeof(DtsodSerializableAttribute)));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return b.ToString();
|
||||
}
|
||||
|
||||
protected Lazy<string> serialized;
|
||||
protected void UpdateLazy() => serialized = new(() => Serialize(this));
|
||||
public override string ToString() => serialized.Value;
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
namespace DTLib.Dtsod
|
||||
{
|
||||
public enum DtsodVersion : byte
|
||||
{
|
||||
V21 = 21,
|
||||
V22 = 22,
|
||||
V30 = 30
|
||||
}
|
||||
}
|
||||
66
Dtsod/V30/TypeHelper.cs
Normal file
66
Dtsod/V30/TypeHelper.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
namespace DTLib.Dtsod;
|
||||
|
||||
public static class TypeHelper
|
||||
{
|
||||
static public readonly Dictionary<Type, Func<string, dynamic>> BaseTypeConstructors = new()
|
||||
{
|
||||
{ typeof(bool), (inp) => inp.ToBool() },
|
||||
{ typeof(char), (inp) => inp.ToChar() },
|
||||
{ typeof(string), (inp) => inp.ToString() },
|
||||
{ typeof(byte), (inp) => inp.ToByte() },
|
||||
{ typeof(sbyte), (inp) => inp.ToSByte() },
|
||||
{ typeof(short), (inp) => inp.ToShort() },
|
||||
{ typeof(ushort), (inp) => inp.ToUShort() },
|
||||
{ typeof(int), (inp) => inp.ToInt() },
|
||||
{ typeof(uint), (inp) => inp.ToUInt() },
|
||||
{ typeof(long), (inp) => inp.ToLong() },
|
||||
{ typeof(ulong), (inp) => inp.ToULong() },
|
||||
{ typeof(float), (inp) => inp.ToFloat() },
|
||||
{ typeof(double), (inp) => inp.ToDouble() },
|
||||
{ typeof(decimal), (inp) => inp.ToDecimal() }
|
||||
};
|
||||
|
||||
static public Dictionary<Type, string> BaseTypeNames = new()
|
||||
{
|
||||
{ typeof(bool), "bool" },
|
||||
{ typeof(char), "char" },
|
||||
{ typeof(string), "string" },
|
||||
{ typeof(byte), "byte" },
|
||||
{ typeof(sbyte), "sbyte" },
|
||||
{ typeof(short), "short" },
|
||||
{ typeof(ushort), "ushort" },
|
||||
{ typeof(int), "int" },
|
||||
{ typeof(uint), "uint" },
|
||||
{ typeof(long), "long" },
|
||||
{ typeof(ulong), "ulong" },
|
||||
{ typeof(float), "float" },
|
||||
{ typeof(double), "double" },
|
||||
{ typeof(decimal), "decimal" }
|
||||
};
|
||||
|
||||
static public string TypeToString(Type t) =>
|
||||
BaseTypeNames.TryGetValue(t, out var name)
|
||||
? name
|
||||
: t.AssemblyQualifiedName;
|
||||
|
||||
static public Type TypeFromString(string str) => str switch
|
||||
{
|
||||
"bool" => typeof(bool),
|
||||
"char" => typeof(char),
|
||||
"string" => typeof(string),
|
||||
"byte" => typeof(byte),
|
||||
"sbyte" => typeof(sbyte),
|
||||
"short" => typeof(short),
|
||||
"ushort" => typeof(ushort),
|
||||
"int" => typeof(int),
|
||||
"uint" => typeof(uint),
|
||||
"long" => typeof(long),
|
||||
"ulong" => typeof(ulong),
|
||||
"float" => typeof(float),
|
||||
"double" => typeof(double),
|
||||
"decimal" => typeof(decimal),
|
||||
_ => Type.GetType(str, false) ??
|
||||
throw new Exception($"DtsodV30.Deserialize.ParseType() error: type {str} doesn't exists")
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user