Skip to content

Commit

Permalink
1.11.6 修复转换String时未进行转义和完善功能
Browse files Browse the repository at this point in the history
完善 Ignore, ConvertNoneLineAttribute 功能
智能不写入不可写入的属性和不读取不可读取的属性
  • Loading branch information
LorisYounger committed Mar 20, 2024
1 parent 193b495 commit f489f98
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 43 deletions.
64 changes: 37 additions & 27 deletions LinePutScript/Converter/LPSConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,13 @@ public static List<TLine> SerializeObjectToList<TLine>(object value, bool? fourc
{
Type type = value.GetType();
List<TLine> list = new List<TLine>();
foreach (PropertyInfo mi in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
foreach (PropertyInfo mi in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(p => p.CanRead))
{
Attribute? att = mi.GetCustomAttribute<LineAttribute>();
if (att != null && att is LineAttribute la)
LineAttribute? att = mi.GetCustomAttributes<LineAttribute>().Combine();
if (att != null)
{
list.Add(la.ConvertToLine<TLine>(mi.Name, mi.GetValue(value), fourceToString));
if (att.Ignore) continue;
list.Add(att.ConvertToLine<TLine>(mi.Name, mi.GetValue(value), fourceToString));
}
else if (convertNoneLineAttribute)
{
Expand All @@ -212,10 +213,11 @@ public static List<TLine> SerializeObjectToList<TLine>(object value, bool? fourc
}
foreach (FieldInfo mi in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(f => !f.Name.StartsWith("<")))
{
Attribute? att = mi.GetCustomAttribute<LineAttribute>();
if (att != null && att is LineAttribute la)
LineAttribute? att = mi.GetCustomAttributes<LineAttribute>().Combine();
if (att != null)
{
list.Add(la.ConvertToLine<TLine>(mi.Name, mi.GetValue(value), fourceToString));
if (att.Ignore) continue;
list.Add(att.ConvertToLine<TLine>(mi.Name, mi.GetValue(value), fourceToString));
}
else if (convertNoneLineAttribute)
{
Expand Down Expand Up @@ -376,13 +378,14 @@ public static bool GetObjectIsString(ConvertType type)
public static string GetObjectString(object? value, ConvertType type = ConvertType.Default, LineAttribute? att = null, bool convertNoneLineAttribute = false)
{
//如果为null储存空
if (value == null)
if (value == null || att?.Ignore == true)
{
return "";
}
ConvertType Type = type == ConvertType.Default ? GetObjectConvertType(value.GetType(), att) : type;
if (Type == ConvertType.Class)
{
convertNoneLineAttribute = att?.ConvertNoneLineAttribute ?? convertNoneLineAttribute;
if (att?.ILineType == null)
return Sub.TextReplace(SerializeObject(value, att?.Name ?? "deflinename", convertNoneLineAttribute: convertNoneLineAttribute).ToString());
Type ex = typeof(LPSConvert);
Expand All @@ -400,7 +403,7 @@ public static string GetObjectString(object? value, ConvertType type = ConvertTy
if (att?.Converter != null)
return ConvertFunction.Convert(att.Converter, value);
else
return value.ToString() ?? "";
return value.ToString() == null ? "" : Sub.TextReplace(value.ToString());
case ConvertType.ToDateTime:
return ((DateTime)value).Ticks.ToString();
case ConvertType.ToFloat:
Expand Down Expand Up @@ -437,7 +440,7 @@ public static string GetObjectString(object? value, ConvertType type = ConvertTy
return sb.ToString().TrimEnd('/', 'n');

default:
return value.ToString() ?? "";
return value.ToString() == null ? "" : Sub.TextReplace(value.ToString());
}
}
/// <summary>
Expand All @@ -456,12 +459,13 @@ public static TLine GetObjectLine<TLine>(object? value, string linename, Convert
TLine t = new TLine();
t.Name = name;
//如果为null储存空
if (value == null)
if (value == null || att?.Ignore == true)
return t;
ConvertType Type = type == ConvertType.Default ? GetObjectConvertType(value.GetType(), att) : type;
switch (Type)
{
case ConvertType.Class:
convertNoneLineAttribute = att?.ConvertNoneLineAttribute ?? convertNoneLineAttribute;
if (att?.ILineType == null)
return SerializeObjectToLine<TLine>(value, linename, convertNoneLineAttribute: convertNoneLineAttribute);
Type ex = typeof(LPSConvert);
Expand Down Expand Up @@ -535,8 +539,11 @@ public static TLine GetObjectLine<TLine>(object? value, string linename, Convert
/// <param name="att">附加参数,若有</param>
/// <returns>指定Type的Object</returns>
/// <param name="convertNoneLineAttribute">是否转换不带LineAttribute的类</param>
public static object? GetStringObject(string value, Type type, ConvertType convtype = ConvertType.Default, LineAttribute? att = null, bool convertNoneLineAttribute = false)
public static object? GetStringObject(string value, Type type, ConvertType convtype = ConvertType.Default, LineAttribute? att = null, bool? convertNoneLineAttribute = null)
{
if(att?.Ignore == true)
return null;
convertNoneLineAttribute = att?.ConvertNoneLineAttribute ?? convertNoneLineAttribute;
if (value == "")
{
if (type.IsValueType)
Expand All @@ -553,7 +560,7 @@ public static TLine GetObjectLine<TLine>(object? value, string linename, Convert
Type? subtype = type.GetGenericArguments().First();
foreach (string str in value.Split(','))
{
list.Add(GetStringObject(Sub.TextDeReplace(str), subtype, convertNoneLineAttribute: true));
list.Add(GetStringObject(Sub.TextDeReplace(str), subtype, convertNoneLineAttribute: convertNoneLineAttribute ?? true));
}
return list;
case ConvertType.ToArray:
Expand All @@ -564,7 +571,7 @@ public static TLine GetObjectLine<TLine>(object? value, string linename, Convert
#pragma warning restore CS8604 // 引用类型参数可能为 null。
for (int i = 0; i < strs.Length; i++)
{
arr.SetValue(GetStringObject(Sub.TextDeReplace(strs[i]), subtype, convertNoneLineAttribute: true), i);
arr.SetValue(GetStringObject(Sub.TextDeReplace(strs[i]), subtype, convertNoneLineAttribute: convertNoneLineAttribute ?? true), i);
}
return arr;
case ConvertType.ToDictionary:
Expand All @@ -573,9 +580,9 @@ public static TLine GetObjectLine<TLine>(object? value, string linename, Convert
foreach (string str in value.Replace("/n", "\n").Split('\n'))
{
strs = str.Split('=');
object? k = GetStringObject(Sub.TextDeReplace(strs[0]), subtypes[0], convertNoneLineAttribute: true);
object? k = GetStringObject(Sub.TextDeReplace(strs[0]), subtypes[0], convertNoneLineAttribute: convertNoneLineAttribute ?? true);
if (k != null)
dict.Add(k, GetStringObject(Sub.TextDeReplace(strs[1]), subtypes[1], convertNoneLineAttribute: true));
dict.Add(k, GetStringObject(Sub.TextDeReplace(strs[1]), subtypes[1], convertNoneLineAttribute: convertNoneLineAttribute ?? true));
}
return dict;
case ConvertType.ToDateTime:
Expand All @@ -601,7 +608,7 @@ public static TLine GetObjectLine<TLine>(object? value, string linename, Convert
object? obj = Activator.CreateInstance(type);
if (obj == null)
return null;
foreach (PropertyInfo mi in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
foreach (PropertyInfo mi in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(p => p.CanWrite))
{
LineAttribute latt = mi.GetCustomAttributes<LineAttribute>().Combine();
if (latt != null)
Expand All @@ -611,9 +618,9 @@ public static TLine GetObjectLine<TLine>(object? value, string linename, Convert
string name = latt.Name ?? mi.Name;
ISub? s = line.Find(name);
if (s != null)
mi.SetValueSafe(obj, GetSubObject(s, mi.PropertyType, att: latt, convertNoneLineAttribute: true));
mi.SetValueSafe(obj, GetSubObject(s, mi.PropertyType, att: latt, convertNoneLineAttribute: convertNoneLineAttribute ?? true));
}
else if (convertNoneLineAttribute)
else if (convertNoneLineAttribute == true)
{
ISub? s = line.Find(mi.Name);
if (s != null)
Expand All @@ -630,9 +637,9 @@ public static TLine GetObjectLine<TLine>(object? value, string linename, Convert
string name = latt.Name ?? mi.Name;
ISub? s = line.Find(name);
if (s != null)
mi.SetValueSafe(obj, GetSubObject(s, mi.FieldType, att: latt, convertNoneLineAttribute: true));
mi.SetValueSafe(obj, GetSubObject(s, mi.FieldType, att: latt, convertNoneLineAttribute: convertNoneLineAttribute ?? true));
}
else if (convertNoneLineAttribute)
else if (convertNoneLineAttribute == true)
{
ISub? s = line.Find(mi.Name);
if (s != null)
Expand All @@ -651,8 +658,11 @@ public static TLine GetObjectLine<TLine>(object? value, string linename, Convert
/// <param name="att">附加参数,若有</param>
/// <param name="convertNoneLineAttribute">是否转换不带LineAttribute的类</param>
/// <returns>指定Type的Object</returns>
public static object? GetSubObject(ISub sub, Type type, ConvertType convtype = ConvertType.Default, LineAttribute? att = null, bool convertNoneLineAttribute = false)
public static object? GetSubObject(ISub sub, Type type, ConvertType convtype = ConvertType.Default, LineAttribute? att = null, bool? convertNoneLineAttribute = null)
{
if (att?.Ignore == true)
return null;
convertNoneLineAttribute = att?.ConvertNoneLineAttribute ?? convertNoneLineAttribute;
ConvertType ct = convtype == ConvertType.Default ? GetObjectConvertType(type, att) : convtype;
if (sub is ILine line)
switch (ct)
Expand All @@ -671,7 +681,7 @@ public static TLine GetObjectLine<TLine>(object? value, string linename, Convert
object obj = Activator.CreateInstance(type);
if (obj == null)
return null;
foreach (PropertyInfo mi in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
foreach (PropertyInfo mi in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(f => f.CanWrite))
{
LineAttribute? latt = mi.GetCustomAttributes<LineAttribute>().Combine();
if (latt != null)
Expand All @@ -688,9 +698,9 @@ public static TLine GetObjectLine<TLine>(object? value, string linename, Convert
else
s = line.Find(name);
if (s != null)
mi.SetValueSafe(obj, GetSubObject(s, mi.PropertyType, att: latt, convertNoneLineAttribute: true));
mi.SetValueSafe(obj, GetSubObject(s, mi.PropertyType, att: latt, convertNoneLineAttribute: convertNoneLineAttribute ?? true));
}
else if (convertNoneLineAttribute)
else if (convertNoneLineAttribute == true)
{
ISub? s = line.Find(mi.Name);
if (s != null)
Expand All @@ -714,9 +724,9 @@ public static TLine GetObjectLine<TLine>(object? value, string linename, Convert
else
s = line.Find(name);
if (s != null)
mi.SetValueSafe(obj, GetSubObject(s, mi.FieldType, att: latt, convertNoneLineAttribute: true));
mi.SetValueSafe(obj, GetSubObject(s, mi.FieldType, att: latt, convertNoneLineAttribute: convertNoneLineAttribute ?? true));
}
else if (convertNoneLineAttribute)
else if (convertNoneLineAttribute == true)
{
ISub? s = line.Find(mi.Name);
if (s != null)
Expand Down
15 changes: 13 additions & 2 deletions LinePutScript/Converter/LineAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public class LineAttribute : Attribute
/// <param name="fourceToString">强制转换内容为String (多用于当前类为Sub)</param>
/// <param name="ignoreCase">忽略名称的大小写</param>
/// <param name="ignore">忽略该属性</param>
public LineAttribute(ConvertType type = ConvertType.Default, Type? converter = null, string? name = null, Type? iLineType = null, bool fourceToString = false, bool ignoreCase = false, bool ignore = false)
/// <param name="convertNoneLineAttribute">是否转换不带LineAttribute的类</param>
public LineAttribute(ConvertType type = ConvertType.Default, Type? converter = null, string? name = null, Type? iLineType = null,
bool fourceToString = false, bool ignoreCase = false, bool ignore = false, bool convertNoneLineAttribute = false)
{
Type = type;
Converter = converter;
Expand All @@ -32,6 +34,7 @@ public LineAttribute(ConvertType type = ConvertType.Default, Type? converter = n
FourceToString = fourceToString;
IgnoreCase = ignoreCase;
Ignore = ignore;
ConvertNoneLineAttribute = convertNoneLineAttribute;
}

/// <summary>
Expand Down Expand Up @@ -72,6 +75,10 @@ public LineAttribute(ConvertType type = ConvertType.Default, Type? converter = n
/// <returns>转换结果</returns>
public T ConvertToLine<T>(string name, object? value, bool? fourceToString = null) where T : ILine, new()
{
if (Ignore)
{
return default(T);
}
if (!fourceToString.HasValue)
{
fourceToString = FourceToString;
Expand Down Expand Up @@ -143,6 +150,10 @@ public static T ConvertToLine<T>(string name, object? value, bool? fourceToStrin
/// </summary>
public bool Ignore { get; set; } = false;
/// <summary>
/// 是否转换不带LineAttribute的类
/// </summary>
public bool ConvertNoneLineAttribute = false;
/// <summary>
/// 将两个LineAttribute合并, 以第二个为准
/// </summary>
/// <param name="a1">第一个</param>
Expand All @@ -151,6 +162,6 @@ public static T ConvertToLine<T>(string name, object? value, bool? fourceToStrin
public static LineAttribute Combine(LineAttribute a1, LineAttribute a2) => new LineAttribute(
(a2.Type == ConvertType.Default ? a1.Type : a2.Type), (a2.Converter == null ? a1.Converter : a2.Converter),
(a2.Name == null ? a1.Name : a2.Name), (a2.ILineType == null ? a1.ILineType : a2.ILineType),
a2.FourceToString, a2.IgnoreCase, a2.Ignore);
a2.FourceToString, a2.IgnoreCase, a2.Ignore, a2.ConvertNoneLineAttribute);
}
}
24 changes: 12 additions & 12 deletions TestConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
using System.Diagnostics;
using System.Drawing;
using System.Numerics;
using System.Reflection.Metadata;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using static LinePutScript.Converter.LPSConvert;
using static TestConsole.Program.MPMessage;

namespace TestConsole
{
Expand Down Expand Up @@ -214,13 +216,14 @@ static void Text()
Console.WriteLine("VA测试4:\t" + (g2.Animat == gi.Animat).ToString());

var mpm = MPMessage.ConverTo(Properties.Resources.test5);
Console.WriteLine("VA测试5:\t" + (mpm.Type == MPMessage.MSGType.DispayGraph).ToString());
Console.WriteLine("VA测试5:\t" + (mpm.Type == (int)MPMessage.MSGType.DispayGraph).ToString());
Console.WriteLine("VA测试6:\t" + (mpm.To == 76561198267979020).ToString());

var g3 = DeserializeObject<GraphInfo>(new Line(mpm.Content), convertNoneLineAttribute: true);
Console.WriteLine("VA测试7:\t" + (g3.Name == "workone").ToString());
Console.WriteLine("VA测试8:\t" + (g3.Type == GraphInfo.GraphType.Work).ToString());
Console.WriteLine("VA测试9:\t" + (g3.Animat == GraphInfo.AnimatType.A_Start).ToString());

}
#pragma warning restore CS8602 // 解引用可能出现空引用。
#pragma warning restore CS8604 // 引用类型参数可能为 null。
Expand Down Expand Up @@ -841,30 +844,26 @@ public enum MSGType
/// </summary>
Empty,
/// <summary>
/// 聊天消息 (string)
/// 聊天消息 (chat)
/// </summary>
Message,
Chat,
/// <summary>
/// 显示动画 (graphinfo)
/// </summary>
DispayGraph,
/// <summary>
/// 摸身体
/// 交互 (Interact)
/// </summary>
TouchHead,
Interact,
/// <summary>
/// 摸头
/// </summary>
TouchBody,
/// <summary>
/// 喂食
/// 喂食 (Feed)
/// </summary>
Feed,
}
/// <summary>
/// 消息类型
/// 消息类型 MOD作者可以随便抽个不是MSGTYPE的数避免冲突,支持负数
/// </summary>
[Line] public MSGType Type { get; set; }
[Line] public int Type { get; set; }

/// <summary>
/// 消息内容
Expand All @@ -882,5 +881,6 @@ public static MPMessage ConverTo(string data)
return LPSConvert.DeserializeObject<MPMessage>(lps);
}
}

}
}
Loading

0 comments on commit f489f98

Please sign in to comment.