mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-10 09:18:27 -04:00
Killed history
This commit is contained in:
14
NadekoBot.Core/Common/Yml/CommentAttribute.cs
Normal file
14
NadekoBot.Core/Common/Yml/CommentAttribute.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace NadekoBot.Common.Yml
|
||||
{
|
||||
public class CommentAttribute : Attribute
|
||||
{
|
||||
public string Comment { get; }
|
||||
|
||||
public CommentAttribute(string comment)
|
||||
{
|
||||
Comment = comment;
|
||||
}
|
||||
}
|
||||
}
|
73
NadekoBot.Core/Common/Yml/CommentGatheringTypeInspector.cs
Normal file
73
NadekoBot.Core/Common/Yml/CommentGatheringTypeInspector.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Serialization;
|
||||
using YamlDotNet.Serialization.TypeInspectors;
|
||||
|
||||
namespace NadekoBot.Common.Yml
|
||||
{
|
||||
public class CommentGatheringTypeInspector : TypeInspectorSkeleton
|
||||
{
|
||||
private readonly ITypeInspector innerTypeDescriptor;
|
||||
|
||||
public CommentGatheringTypeInspector(ITypeInspector innerTypeDescriptor)
|
||||
{
|
||||
this.innerTypeDescriptor = innerTypeDescriptor ?? throw new ArgumentNullException("innerTypeDescriptor");
|
||||
}
|
||||
|
||||
public override IEnumerable<IPropertyDescriptor> GetProperties(Type type, object container)
|
||||
{
|
||||
return innerTypeDescriptor
|
||||
.GetProperties(type, container)
|
||||
.Select(d => new CommentsPropertyDescriptor(d));
|
||||
}
|
||||
|
||||
private sealed class CommentsPropertyDescriptor : IPropertyDescriptor
|
||||
{
|
||||
private readonly IPropertyDescriptor baseDescriptor;
|
||||
|
||||
public CommentsPropertyDescriptor(IPropertyDescriptor baseDescriptor)
|
||||
{
|
||||
this.baseDescriptor = baseDescriptor;
|
||||
Name = baseDescriptor.Name;
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public Type Type { get { return baseDescriptor.Type; } }
|
||||
|
||||
public Type TypeOverride {
|
||||
get { return baseDescriptor.TypeOverride; }
|
||||
set { baseDescriptor.TypeOverride = value; }
|
||||
}
|
||||
|
||||
public int Order { get; set; }
|
||||
|
||||
public ScalarStyle ScalarStyle {
|
||||
get { return baseDescriptor.ScalarStyle; }
|
||||
set { baseDescriptor.ScalarStyle = value; }
|
||||
}
|
||||
|
||||
public bool CanWrite { get { return baseDescriptor.CanWrite; } }
|
||||
|
||||
public void Write(object target, object value)
|
||||
{
|
||||
baseDescriptor.Write(target, value);
|
||||
}
|
||||
|
||||
public T GetCustomAttribute<T>() where T : Attribute
|
||||
{
|
||||
return baseDescriptor.GetCustomAttribute<T>();
|
||||
}
|
||||
|
||||
public IObjectDescriptor Read(object target)
|
||||
{
|
||||
var comment = baseDescriptor.GetCustomAttribute<CommentAttribute>();
|
||||
return comment != null
|
||||
? new CommentsObjectDescriptor(baseDescriptor.Read(target), comment.Comment)
|
||||
: baseDescriptor.Read(target);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
24
NadekoBot.Core/Common/Yml/CommentsObjectDescriptor.cs
Normal file
24
NadekoBot.Core/Common/Yml/CommentsObjectDescriptor.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace NadekoBot.Common.Yml
|
||||
{
|
||||
public sealed class CommentsObjectDescriptor : IObjectDescriptor
|
||||
{
|
||||
private readonly IObjectDescriptor innerDescriptor;
|
||||
|
||||
public CommentsObjectDescriptor(IObjectDescriptor innerDescriptor, string comment)
|
||||
{
|
||||
this.innerDescriptor = innerDescriptor;
|
||||
this.Comment = comment;
|
||||
}
|
||||
|
||||
public string Comment { get; private set; }
|
||||
|
||||
public object Value { get { return innerDescriptor.Value; } }
|
||||
public Type Type { get { return innerDescriptor.Type; } }
|
||||
public Type StaticType { get { return innerDescriptor.StaticType; } }
|
||||
public ScalarStyle ScalarStyle { get { return innerDescriptor.ScalarStyle; } }
|
||||
}
|
||||
}
|
26
NadekoBot.Core/Common/Yml/CommentsObjectGraphVisitor.cs
Normal file
26
NadekoBot.Core/Common/Yml/CommentsObjectGraphVisitor.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
using YamlDotNet.Serialization.ObjectGraphVisitors;
|
||||
|
||||
namespace NadekoBot.Common.Yml
|
||||
{
|
||||
public class CommentsObjectGraphVisitor : ChainedObjectGraphVisitor
|
||||
{
|
||||
public CommentsObjectGraphVisitor(IObjectGraphVisitor<IEmitter> nextVisitor)
|
||||
: base(nextVisitor)
|
||||
{
|
||||
}
|
||||
|
||||
public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor value, IEmitter context)
|
||||
{
|
||||
var commentsDescriptor = value as CommentsObjectDescriptor;
|
||||
if (commentsDescriptor != null && !string.IsNullOrWhiteSpace(commentsDescriptor.Comment))
|
||||
{
|
||||
context.Emit(new Comment(commentsDescriptor.Comment.Replace("\n", "\n# "), false));
|
||||
}
|
||||
|
||||
return base.EnterMapping(key, value, context);
|
||||
}
|
||||
}
|
||||
}
|
32
NadekoBot.Core/Common/Yml/MultilineScalarFlowStyleEmitter.cs
Normal file
32
NadekoBot.Core/Common/Yml/MultilineScalarFlowStyleEmitter.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Serialization;
|
||||
using YamlDotNet.Serialization.EventEmitters;
|
||||
|
||||
namespace NadekoBot.Common.Yml
|
||||
{
|
||||
public class MultilineScalarFlowStyleEmitter : ChainedEventEmitter
|
||||
{
|
||||
public MultilineScalarFlowStyleEmitter(IEventEmitter nextEmitter)
|
||||
: base(nextEmitter) { }
|
||||
|
||||
public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
|
||||
{
|
||||
|
||||
if (typeof(string).IsAssignableFrom(eventInfo.Source.Type))
|
||||
{
|
||||
string value = eventInfo.Source.Value as string;
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
{
|
||||
bool isMultiLine = value.IndexOfAny(new char[] { '\r', '\n', '\x85', '\x2028', '\x2029' }) >= 0;
|
||||
if (isMultiLine)
|
||||
eventInfo = new ScalarEventInfo(eventInfo.Source)
|
||||
{
|
||||
Style = ScalarStyle.Literal,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
nextEmitter.Emit(eventInfo, emitter);
|
||||
}
|
||||
}
|
||||
}
|
52
NadekoBot.Core/Common/Yml/Rgba32Converter.cs
Normal file
52
NadekoBot.Core/Common/Yml/Rgba32Converter.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace NadekoBot.Common.Yml
|
||||
{
|
||||
public class Rgba32Converter : IYamlTypeConverter
|
||||
{
|
||||
public bool Accepts(Type type)
|
||||
{
|
||||
return type == typeof(Rgba32);
|
||||
}
|
||||
|
||||
public object ReadYaml(IParser parser, Type type)
|
||||
{
|
||||
var scalar = parser.Consume<Scalar>();
|
||||
var result = Rgba32.ParseHex(scalar.Value);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void WriteYaml(IEmitter emitter, object value, Type type)
|
||||
{
|
||||
var color = (Rgba32)value;
|
||||
var val = (uint) (color.B << 0 | color.G << 8 | color.R << 16);
|
||||
emitter.Emit(new Scalar(val.ToString("X6").ToLower()));
|
||||
}
|
||||
}
|
||||
|
||||
public class CultureInfoConverter : IYamlTypeConverter
|
||||
{
|
||||
public bool Accepts(Type type)
|
||||
{
|
||||
return type == typeof(CultureInfo);
|
||||
}
|
||||
|
||||
public object ReadYaml(IParser parser, Type type)
|
||||
{
|
||||
var scalar = parser.Consume<Scalar>();
|
||||
var result = new CultureInfo(scalar.Value);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void WriteYaml(IEmitter emitter, object value, Type type)
|
||||
{
|
||||
var ci = (CultureInfo)value;
|
||||
emitter.Emit(new Scalar(ci.Name));
|
||||
}
|
||||
}
|
||||
}
|
28
NadekoBot.Core/Common/Yml/UriConverter.cs
Normal file
28
NadekoBot.Core/Common/Yml/UriConverter.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace NadekoBot.Common.Yml
|
||||
{
|
||||
public class UriConverter : IYamlTypeConverter
|
||||
{
|
||||
public bool Accepts(Type type)
|
||||
{
|
||||
return type == typeof(Uri);
|
||||
}
|
||||
|
||||
public object ReadYaml(IParser parser, Type type)
|
||||
{
|
||||
var scalar = parser.Consume<Scalar>();
|
||||
var result = new Uri(scalar.Value);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void WriteYaml(IEmitter emitter, object value, Type type)
|
||||
{
|
||||
var uri = (Uri)value;
|
||||
emitter.Emit(new Scalar(uri.ToString()));
|
||||
}
|
||||
}
|
||||
}
|
25
NadekoBot.Core/Common/Yml/Yaml.cs
Normal file
25
NadekoBot.Core/Common/Yml/Yaml.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace NadekoBot.Common.Yml
|
||||
{
|
||||
public class Yaml
|
||||
{
|
||||
public static ISerializer Serializer => new SerializerBuilder()
|
||||
.WithTypeInspector(inner => new CommentGatheringTypeInspector(inner))
|
||||
.WithEmissionPhaseObjectGraphVisitor(args => new CommentsObjectGraphVisitor(args.InnerVisitor))
|
||||
.WithEventEmitter(args => new MultilineScalarFlowStyleEmitter(args))
|
||||
.WithNamingConvention(YamlDotNet.Serialization.NamingConventions.CamelCaseNamingConvention.Instance)
|
||||
.WithIndentedSequences()
|
||||
.WithTypeConverter(new Rgba32Converter())
|
||||
.WithTypeConverter(new CultureInfoConverter())
|
||||
.WithTypeConverter(new UriConverter())
|
||||
.Build();
|
||||
|
||||
public static IDeserializer Deserializer => new DeserializerBuilder()
|
||||
.WithNamingConvention(YamlDotNet.Serialization.NamingConventions.CamelCaseNamingConvention.Instance)
|
||||
.WithTypeConverter(new Rgba32Converter())
|
||||
.WithTypeConverter(new CultureInfoConverter())
|
||||
.WithTypeConverter(new UriConverter())
|
||||
.Build();
|
||||
}
|
||||
}
|
58
NadekoBot.Core/Common/Yml/YamlHelper.cs
Normal file
58
NadekoBot.Core/Common/Yml/YamlHelper.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
namespace NadekoBot.Common.Yml
|
||||
{
|
||||
public class YamlHelper
|
||||
{
|
||||
// https://github.com/aaubry/YamlDotNet/blob/0f4cc205e8b2dd8ef6589d96de32bf608a687c6f/YamlDotNet/Core/Scanner.cs#L1687
|
||||
/// <summary>
|
||||
/// This is modified code from yamldotnet's repo which handles parsing unicode code points
|
||||
/// it is needed as yamldotnet doesn't support unescaped unicode characters
|
||||
/// </summary>
|
||||
/// <param name="point">Unicode code point</param>
|
||||
/// <returns>Actual character</returns>
|
||||
public static string UnescapeUnicodeCodePoint(string point)
|
||||
{
|
||||
var character = 0;
|
||||
|
||||
// Scan the character value.
|
||||
|
||||
foreach(var c in point)
|
||||
{
|
||||
if (!IsHex(c))
|
||||
{
|
||||
return point;
|
||||
}
|
||||
character = (character << 4) + AsHex(c);
|
||||
}
|
||||
|
||||
// Check the value and write the character.
|
||||
|
||||
if (character >= 0xD800 && character <= 0xDFFF || character > 0x10FFFF)
|
||||
{
|
||||
return point;
|
||||
}
|
||||
|
||||
return char.ConvertFromUtf32(character);
|
||||
}
|
||||
|
||||
public static bool IsHex(char c)
|
||||
{
|
||||
return
|
||||
(c >= '0' && c <= '9') ||
|
||||
(c >= 'A' && c <= 'F') ||
|
||||
(c >= 'a' && c <= 'f');
|
||||
}
|
||||
|
||||
public static int AsHex(char c)
|
||||
{
|
||||
if (c <= '9')
|
||||
{
|
||||
return c - '0';
|
||||
}
|
||||
if (c <= 'F')
|
||||
{
|
||||
return c - 'A' + 10;
|
||||
}
|
||||
return c - 'a' + 10;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user