Import YamlDotNet.

This commit is contained in:
2022-12-12 22:23:49 +08:00
parent f559cea826
commit 1477e907e6
462 changed files with 27142 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#if !NET20
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Reflection;
namespace YamlDotNet.Helpers
{
public static class ExpressionExtensions
{
/// <summary>
/// Returns the <see cref="PropertyInfo" /> that describes the property that
/// is being returned in an expression in the form:
/// <code>
/// x => x.SomeProperty
/// </code>
/// </summary>
public static PropertyInfo AsProperty(this LambdaExpression propertyAccessor)
{
var property = TryGetMemberExpression<PropertyInfo>(propertyAccessor);
if (property == null)
{
throw new ArgumentException("Expected a lambda expression in the form: x => x.SomeProperty", nameof(propertyAccessor));
}
return property;
}
[return: MaybeNull]
private static TMemberInfo TryGetMemberExpression<TMemberInfo>(LambdaExpression lambdaExpression)
where TMemberInfo : MemberInfo
{
if (lambdaExpression.Parameters.Count != 1)
{
return null;
}
var body = lambdaExpression.Body;
if (body is UnaryExpression castExpression)
{
if (castExpression.NodeType != ExpressionType.Convert)
{
return null;
}
body = castExpression.Operand;
}
if (body is MemberExpression memberExpression)
{
if (memberExpression.Expression != lambdaExpression.Parameters[0])
{
return null;
}
return memberExpression.Member as TMemberInfo;
}
return null;
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 793a46d362345d548970ee537e488bfe
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,125 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System;
using System.Collections;
using System.Collections.Generic;
namespace YamlDotNet.Helpers
{
/// <summary>
/// Adapts an <see cref="System.Collections.Generic.ICollection{T}" /> to <see cref="IList" />
/// because not all generic collections implement <see cref="IList" />.
/// </summary>
internal sealed class GenericCollectionToNonGenericAdapter<T> : IList
{
private readonly ICollection<T> genericCollection;
public GenericCollectionToNonGenericAdapter(ICollection<T> genericCollection)
{
this.genericCollection = genericCollection ?? throw new ArgumentNullException(nameof(genericCollection));
}
public int Add(object? value)
{
var index = genericCollection.Count;
genericCollection.Add((T)value!);
return index;
}
public void Clear()
{
genericCollection.Clear();
}
public bool Contains(object? value)
{
throw new NotSupportedException();
}
public int IndexOf(object? value)
{
throw new NotSupportedException();
}
public void Insert(int index, object? value)
{
throw new NotSupportedException();
}
public bool IsFixedSize
{
get { throw new NotSupportedException(); }
}
public bool IsReadOnly
{
get { throw new NotSupportedException(); }
}
public void Remove(object? value)
{
throw new NotSupportedException();
}
public void RemoveAt(int index)
{
throw new NotSupportedException();
}
public object? this[int index]
{
get
{
throw new NotSupportedException();
}
set
{
((IList<T>)genericCollection)[index] = (T)value!;
}
}
public void CopyTo(Array array, int index)
{
throw new NotSupportedException();
}
public int Count
{
get { throw new NotSupportedException(); }
}
public bool IsSynchronized
{
get { throw new NotSupportedException(); }
}
public object SyncRoot
{
get { throw new NotSupportedException(); }
}
public IEnumerator GetEnumerator()
{
return genericCollection.GetEnumerator();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9fd433108de12ac4486d0acd3a98635f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,167 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System;
using System.Collections;
using System.Collections.Generic;
namespace YamlDotNet.Helpers
{
/// <summary>
/// Adapts an <see cref="System.Collections.Generic.IDictionary{TKey, TValue}" /> to <see cref="IDictionary" />
/// because not all generic dictionaries implement <see cref="IDictionary" />.
/// </summary>
internal sealed class GenericDictionaryToNonGenericAdapter<TKey, TValue> : IDictionary
where TKey : notnull
{
private readonly IDictionary<TKey, TValue> genericDictionary;
public GenericDictionaryToNonGenericAdapter(IDictionary<TKey, TValue> genericDictionary)
{
this.genericDictionary = genericDictionary ?? throw new ArgumentNullException(nameof(genericDictionary));
}
public void Add(object key, object? value)
{
throw new NotSupportedException();
}
public void Clear()
{
throw new NotSupportedException();
}
public bool Contains(object key)
{
throw new NotSupportedException();
}
public IDictionaryEnumerator GetEnumerator()
{
return new DictionaryEnumerator(genericDictionary.GetEnumerator());
}
public bool IsFixedSize
{
get { throw new NotSupportedException(); }
}
public bool IsReadOnly
{
get { throw new NotSupportedException(); }
}
public ICollection Keys
{
get { throw new NotSupportedException(); }
}
public void Remove(object key)
{
throw new NotSupportedException();
}
public ICollection Values
{
get { throw new NotSupportedException(); }
}
public object? this[object key]
{
get
{
throw new NotSupportedException();
}
set
{
genericDictionary[(TKey)key] = (TValue)value!;
}
}
public void CopyTo(Array array, int index)
{
throw new NotSupportedException();
}
public int Count
{
get { throw new NotSupportedException(); }
}
public bool IsSynchronized
{
get { throw new NotSupportedException(); }
}
public object SyncRoot
{
get { throw new NotSupportedException(); }
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
private class DictionaryEnumerator : IDictionaryEnumerator
{
private readonly IEnumerator<KeyValuePair<TKey, TValue>> enumerator;
public DictionaryEnumerator(IEnumerator<KeyValuePair<TKey, TValue>> enumerator)
{
this.enumerator = enumerator;
}
public DictionaryEntry Entry
{
get
{
return new DictionaryEntry(Key, Value);
}
}
public object Key
{
get { return enumerator.Current.Key!; }
}
public object? Value
{
get { return enumerator.Current.Value; }
}
public object Current
{
get { return Entry; }
}
public bool MoveNext()
{
return enumerator.MoveNext();
}
public void Reset()
{
enumerator.Reset();
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7beef75a3a31dfc478917341adcf708d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,55 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System.Collections.Generic;
namespace YamlDotNet.Helpers
{
public interface IOrderedDictionary<TKey, TValue> : IDictionary<TKey, TValue>
where TKey : notnull
{
/// <summary>
/// Gets or sets the element with the specified index.
/// </summary>
/// <param name="index">The index of the element to get or set.</param>
/// <returns>The element with the specified index.</returns>
KeyValuePair<TKey, TValue> this[int index]
{
get;
set;
}
/// <summary>
/// Adds an element with the provided key and value to the <see cref="IOrderedDictionary{TKey, TValue}"/>
/// at the given index.
/// </summary>
/// <param name="index">The zero-based index at which the item should be inserted.</param>
/// <param name="key">The object to use as the key of the element to add.</param>
/// <param name="value">The object to use as the value of the element to add.</param>
void Insert(int index, TKey key, TValue value);
/// <summary>
/// Removes the element at the specified index.
/// </summary>
/// <param name="index">The zero-based index of the element to remove.</param>
void RemoveAt(int index);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: de2ae2f3fa506fa47b93048ac0bdcb7e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,31 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
namespace YamlDotNet.Helpers
{
internal static class NumberExtensions
{
public static bool IsPowerOfTwo(this int value)
{
return (value & (value - 1)) == 0;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ed9bc129662cfc5439b271df577d9537
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,234 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.Serialization;
namespace YamlDotNet.Helpers
{
[Serializable]
internal class OrderedDictionary<TKey, TValue> : IOrderedDictionary<TKey, TValue>
where TKey : notnull
{
[NonSerialized]
private Dictionary<TKey, TValue> dictionary;
private readonly List<KeyValuePair<TKey, TValue>> list;
private readonly IEqualityComparer<TKey> comparer;
public TValue this[TKey key]
{
get => dictionary[key];
set
{
if (dictionary.ContainsKey(key))
{
var index = list.FindIndex(kvp => comparer.Equals(kvp.Key, key));
dictionary[key] = value;
list[index] = new KeyValuePair<TKey, TValue>(key, value);
}
else
{
Add(key, value);
}
}
}
public ICollection<TKey> Keys => new KeyCollection(this);
public ICollection<TValue> Values => new ValueCollection(this);
public int Count => dictionary.Count;
public bool IsReadOnly => false;
public KeyValuePair<TKey, TValue> this[int index]
{
get => list[index];
set => list[index] = value;
}
public OrderedDictionary() : this(EqualityComparer<TKey>.Default)
{
}
public OrderedDictionary(IEqualityComparer<TKey> comparer)
{
list = new List<KeyValuePair<TKey, TValue>>();
dictionary = new Dictionary<TKey, TValue>(comparer);
this.comparer = comparer;
}
public void Add(TKey key, TValue value) => Add(new KeyValuePair<TKey, TValue>(key, value));
public void Add(KeyValuePair<TKey, TValue> item)
{
dictionary.Add(item.Key, item.Value);
list.Add(item);
}
public void Clear()
{
dictionary.Clear();
list.Clear();
}
public bool Contains(KeyValuePair<TKey, TValue> item) => dictionary.Contains(item);
public bool ContainsKey(TKey key) => dictionary.ContainsKey(key);
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) =>
list.CopyTo(array, arrayIndex);
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => list.GetEnumerator();
public void Insert(int index, TKey key, TValue value)
{
dictionary.Add(key, value);
list.Insert(index, new KeyValuePair<TKey, TValue>(key, value));
}
public bool Remove(TKey key)
{
if (dictionary.ContainsKey(key))
{
var index = list.FindIndex(kvp => comparer.Equals(kvp.Key, key));
list.RemoveAt(index);
if (!dictionary.Remove(key))
{
throw new InvalidOperationException();
}
return true;
}
else
{
return false;
}
}
public bool Remove(KeyValuePair<TKey, TValue> item) => Remove(item.Key);
public void RemoveAt(int index)
{
var key = list[index].Key;
dictionary.Remove(key);
list.RemoveAt(index);
}
#if !(NETCOREAPP3_1)
#pragma warning disable 8767 // Nullability of reference types in type of parameter ... doesn't match implicitly implemented member
#endif
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) =>
dictionary.TryGetValue(key, out value);
#if !(NETCOREAPP3_1)
#pragma warning restore 8767
#endif
IEnumerator IEnumerable.GetEnumerator() => list.GetEnumerator();
[OnDeserialized]
internal void OnDeserializedMethod(StreamingContext context)
{
// Reconstruct the dictionary from the serialized list
dictionary = new Dictionary<TKey, TValue>();
foreach (var kvp in list)
{
dictionary[kvp.Key] = kvp.Value;
}
}
private class KeyCollection : ICollection<TKey>
{
private readonly OrderedDictionary<TKey, TValue> orderedDictionary;
public int Count => orderedDictionary.list.Count;
public bool IsReadOnly => true;
public void Add(TKey item) => throw new NotSupportedException();
public void Clear() => throw new NotSupportedException();
public bool Contains(TKey item) => orderedDictionary.dictionary.Keys.Contains(item);
public KeyCollection(OrderedDictionary<TKey, TValue> orderedDictionary)
{
this.orderedDictionary = orderedDictionary;
}
public void CopyTo(TKey[] array, int arrayIndex)
{
for (var i = 0; i < orderedDictionary.list.Count; i++)
{
array[i] = orderedDictionary.list[i + arrayIndex].Key;
}
}
public IEnumerator<TKey> GetEnumerator() =>
orderedDictionary.list.Select(kvp => kvp.Key).GetEnumerator();
public bool Remove(TKey item) => throw new NotSupportedException();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
private class ValueCollection : ICollection<TValue>
{
private readonly OrderedDictionary<TKey, TValue> orderedDictionary;
public int Count => orderedDictionary.list.Count;
public bool IsReadOnly => true;
public void Add(TValue item) => throw new NotSupportedException();
public void Clear() => throw new NotSupportedException();
public bool Contains(TValue item) => orderedDictionary.dictionary.Values.Contains(item);
public ValueCollection(OrderedDictionary<TKey, TValue> orderedDictionary)
{
this.orderedDictionary = orderedDictionary;
}
public void CopyTo(TValue[] array, int arrayIndex)
{
for (var i = 0; i < orderedDictionary.list.Count; i++)
{
array[i] = orderedDictionary.list[i + arrayIndex].Value;
}
}
public IEnumerator<TValue> GetEnumerator() =>
orderedDictionary.list.Select(kvp => kvp.Value).GetEnumerator();
public bool Remove(TValue item) => throw new NotSupportedException();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fd92f9e9502203346aad33fd2d86c46a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: