Style cleanup
This commit is contained in:
@@ -25,11 +25,9 @@ public static class DeepCloneHandler
|
||||
/// Recursive references will be handled correctly, and will only be cloned once, to prevent infinite loops and invalid
|
||||
/// references.
|
||||
/// </summary>
|
||||
public static T DeepClone<T>(this T? obj, Dictionary<(Type, int), object>? objects = null) where T : IDeepCloneable
|
||||
{
|
||||
return (T)DeepClone((object?)obj, objects)!;
|
||||
}
|
||||
|
||||
public static T DeepClone<T>(this T? obj, Dictionary<(Type, int), object>? objects = null)
|
||||
where T : IDeepCloneable => (T)DeepClone((object?)obj, objects)!;
|
||||
|
||||
private static object? DeepClone(this object? obj, Dictionary<(Type, int), object>? objects = null)
|
||||
{
|
||||
if (obj == null)
|
||||
@@ -41,7 +39,7 @@ public static class DeepCloneHandler
|
||||
// We use GetUninitializedObject to create an object without calling the constructor. This is necessary to prevent
|
||||
// side effects from the constructor, and to not require a parameterless constructor.
|
||||
var newObj = FormatterServices.GetUninitializedObject(type)!;
|
||||
|
||||
|
||||
// If the objects dictionary is null, we create a new one. We use this dictionary to keep track of objects that have
|
||||
// already been cloned, so we can re-use them instead of cloning them again. This is necessary to prevent infinite
|
||||
// loops and invalid references.
|
||||
@@ -74,7 +72,7 @@ public static class DeepCloneHandler
|
||||
// If the object is a value type or a string, we can just return it.
|
||||
if (type.IsValueType || type == typeof(string))
|
||||
return obj;
|
||||
|
||||
|
||||
// If the object is marked as deep cloneable, we will clone it.
|
||||
if (type.GetInterface(nameof(IDeepCloneable)) != null || ExternalDeepCloneTypes.Contains(type))
|
||||
{
|
||||
@@ -91,8 +89,9 @@ public static class DeepCloneHandler
|
||||
var array = (Array)obj;
|
||||
var newArray = Array.CreateInstance(type.GetElementType()!, array.Length);
|
||||
for (var i = 0; i < array.Length; i++)
|
||||
newArray.SetValue(DeepCloneInternal(array.GetValue(i), type.GetElementType()!, objects),
|
||||
i);
|
||||
{
|
||||
newArray.SetValue(DeepCloneInternal(array.GetValue(i), type.GetElementType()!, objects), i);
|
||||
}
|
||||
return newArray;
|
||||
}
|
||||
|
||||
@@ -115,9 +114,10 @@ public static class DeepCloneHandler
|
||||
var dictionary = (IDictionary)obj;
|
||||
var newDictionary = (IDictionary)Activator.CreateInstance(type);
|
||||
foreach (DictionaryEntry entry in dictionary)
|
||||
newDictionary.Add(
|
||||
DeepCloneInternal(entry.Key, type.GetGenericArguments()[0], objects)!,
|
||||
{
|
||||
newDictionary.Add(DeepCloneInternal(entry.Key, type.GetGenericArguments()[0], objects)!,
|
||||
DeepCloneInternal(entry.Value, type.GetGenericArguments()[1], objects));
|
||||
}
|
||||
return newDictionary;
|
||||
}
|
||||
}
|
||||
@@ -133,8 +133,7 @@ public static class DeepCloneHandler
|
||||
/// This method is thread safe, and will only create the expressions once for each type. It returns compiled expressions for
|
||||
/// each field in the type, so that we can get high performance deep cloning.
|
||||
/// </remarks>
|
||||
private static (Func<object, object?> getter, Action<object, object?> setter)[]
|
||||
GetDeepCloneExpressions(Type type)
|
||||
private static (Func<object, object?> getter, Action<object, object?> setter)[] GetDeepCloneExpressions(Type type)
|
||||
{
|
||||
// We use a lock here to prevent multiple threads from trying to create the expressions at the same time.
|
||||
lock (DeepCloneExpressions)
|
||||
|
||||
@@ -2,7 +2,8 @@ namespace PkmnLib.Static.Utils;
|
||||
|
||||
public static class DictionaryHelpers
|
||||
{
|
||||
public static TValue GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
|
||||
public static TValue GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key,
|
||||
TValue defaultValue)
|
||||
{
|
||||
if (dictionary.TryGetValue(key, out var value))
|
||||
return value;
|
||||
|
||||
@@ -10,7 +10,7 @@ public static class EnumerableHelpers
|
||||
/// </summary>
|
||||
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> enumerable) where T : class =>
|
||||
enumerable.Where(x => x is not null)!;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Finds the index of a value in an enumerable. Returns -1 if the value is not found.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
namespace PkmnLib.Static.Utils.Errors;
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -13,7 +13,7 @@ public static class NumericHelpers
|
||||
var result = value * multiplier;
|
||||
return result > byte.MaxValue ? byte.MaxValue : (byte)result;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Multiplies two values. If this overflows, returns <see cref="byte.MaxValue"/>.
|
||||
/// </summary>
|
||||
@@ -22,7 +22,7 @@ public static class NumericHelpers
|
||||
var result = value * multiplier;
|
||||
return result > byte.MaxValue ? byte.MaxValue : (byte)result;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Multiplies two values. If this overflows, returns <see cref="sbyte.MaxValue"/>.
|
||||
/// </summary>
|
||||
@@ -31,7 +31,7 @@ public static class NumericHelpers
|
||||
var result = value * multiplier;
|
||||
return result > sbyte.MaxValue ? sbyte.MaxValue : (sbyte)result;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Multiplies two values. If this overflows, returns <see cref="ushort.MaxValue"/>.
|
||||
/// </summary>
|
||||
@@ -40,7 +40,7 @@ public static class NumericHelpers
|
||||
var result = value * multiplier;
|
||||
return result > ushort.MaxValue ? ushort.MaxValue : (ushort)result;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Multiplies two values. If this overflows, returns <see cref="short.MaxValue"/>.
|
||||
/// </summary>
|
||||
@@ -49,11 +49,10 @@ public static class NumericHelpers
|
||||
var result = value * multiplier;
|
||||
return result > short.MaxValue ? short.MaxValue : (short)result;
|
||||
}
|
||||
|
||||
|
||||
public static uint MultiplyOrMax(this uint value, uint multiplier)
|
||||
{
|
||||
var result = (ulong)value * multiplier;
|
||||
return result > uint.MaxValue ? uint.MaxValue : (uint)result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,19 +21,19 @@ public interface IRandom
|
||||
/// <param name="max">The maximum value (exclusive).</param>
|
||||
/// <returns>A random integer that is greater than or equal to 0 and less than max.</returns>
|
||||
public int GetInt(int max);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get a random integer between 0 and <see cref="int.MaxValue"/>.
|
||||
/// </summary>
|
||||
/// <returns>A random integer that is greater than or equal to 0 and less than <see cref="int.MaxValue"/>.</returns>
|
||||
public int GetInt();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get a random float that is greater than or equal to 0.0 and less than 1.0.
|
||||
/// </summary>
|
||||
/// <returns>A random float that is greater than or equal to 0.0 and less than 1.0.</returns>
|
||||
public float GetFloat();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get a random float that is greater than or equal to min and less than max.
|
||||
/// </summary>
|
||||
@@ -41,7 +41,7 @@ public interface IRandom
|
||||
/// <param name="max">The maximum value (exclusive).</param>
|
||||
/// <returns>A random float that is greater than or equal to min and less than max.</returns>
|
||||
public float GetFloat(float min, float max);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get a random boolean. 50% chance of being true.
|
||||
/// </summary>
|
||||
|
||||
@@ -22,14 +22,12 @@ public readonly record struct StringKey
|
||||
/// Converts a <see cref="StringKey"/> to a <see cref="string"/>.
|
||||
/// </summary>
|
||||
public static implicit operator string(StringKey key) => key._key;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="string"/> to a <see cref="StringKey"/>.
|
||||
/// </summary>
|
||||
public static implicit operator StringKey(string key)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(key) ? default : new StringKey(key);
|
||||
}
|
||||
public static implicit operator StringKey(string key) =>
|
||||
string.IsNullOrWhiteSpace(key) ? default : new StringKey(key);
|
||||
|
||||
/// <inheritdoc cref="string.ToString()"/>
|
||||
public override string ToString() => _key.ToLowerInvariant();
|
||||
|
||||
Reference in New Issue
Block a user