Switches to Kestrel (#102)

This commit is contained in:
Kamron Batman 2020-04-12 20:57:48 -07:00 • committed by GitHub
parent 70fddfebde
commit 2c0d97cd36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
141 changed files with 1040 additions and 1679 deletions

View file

@ -11,18 +11,12 @@ namespace Server.Utilities
{
ConstructorInfo emptyCtor = type.GetConstructor(Type.EmptyTypes);
if (emptyCtor != null && predicate?.Invoke(emptyCtor) != false)
{
return emptyCtor;
}
if (emptyCtor != null && predicate?.Invoke(emptyCtor) != false) return emptyCtor;
ConstructorInfo optionalCtor = type.GetConstructors().SingleOrDefault(info =>
predicate?.Invoke(info) != false && info.GetParameters().All(x => x.IsOptional));
if (optionalCtor != null)
{
return optionalCtor;
}
if (optionalCtor != null) return optionalCtor;
throw new TypeInitializationException(type.ToString(),
new Exception($"There is no empty/default constructor for {type} that matches predicate."));
@ -38,39 +32,26 @@ namespace Server.Utilities
{
ctor = type.GetConstructor(args);
if (ctor != null && predicate?.Invoke(ctor) != false)
{
return ctor;
}
if (ctor != null && predicate?.Invoke(ctor) != false) return ctor;
}
else
{
ctor = type.GetConstructors().SingleOrDefault(info =>
{
if (predicate?.Invoke(info) == false)
{
return false;
}
if (predicate?.Invoke(info) == false) return false;
List<ParameterInfo> paramList = info.GetParameters().ToList();
// If more args are given than parameters, skip.
if (args.Length > paramList.Count)
{
return false;
}
if (args.Length > paramList.Count) return false;
// check all given args map to params.
for (int i = 0; i < args.Length; i++)
{
// if a null reference is passed, but the type is not nullable
// if a null reference is passed, but the type is not nullable
if (args[i] == null && paramList[i].ParameterType.IsValueType
// or if an arg is not null and is not assignable to the parameter type, skip.
|| !(args[i] == null || paramList[i].ParameterType.IsAssignableFrom(args[i])))
{
return false;
}
}
// If there are more parameters, check if they any are not optional, if any are not, skip.
// Otherwise all checks have passed. We have found a match
@ -78,10 +59,7 @@ namespace Server.Utilities
.All(x => x.IsOptional);
});
if (ctor != null)
{
return ctor;
}
if (ctor != null) return ctor;
}
throw new Exception($"There is no empty/default constructor for {type} that matches predicate.");
@ -97,10 +75,7 @@ namespace Server.Utilities
ConstructorInfo cctor = GetConstructor(type, constructorPredicate);
ParameterInfo[] args = cctor.GetParameters();
if (args.Length == 0)
{
return cctor.Invoke(Type.EmptyTypes);
}
if (args.Length == 0) return cctor.Invoke(Type.EmptyTypes);
object[] argList = new object[args.Length];
Array.Fill(argList, Type.Missing);
@ -110,10 +85,7 @@ namespace Server.Utilities
public static object CreateInstance(Type type, Predicate<ConstructorInfo> constructorPredicate = null,
params object[] args)
{
if (args == null || args.Length == 0)
{
return CreateInstance(type, constructorPredicate);
}
if (args == null || args.Length == 0) return CreateInstance(type, constructorPredicate);
ConstructorInfo cctor = GetConstructor(type, constructorPredicate, args.Select(x => x?.GetType()).ToArray());
return cctor.Invoke(args);

View file

@ -20,11 +20,8 @@ namespace Server.Utilities
/// <typeparam name="TDerived"></typeparam>
public abstract class BaseRef<TDerived> : IRef where TDerived : IRef
{
private RefPool<TDerived> m_Pool;
public BaseRef(RefPool<TDerived> pool)
{
m_Pool = pool;
}
private readonly RefPool<TDerived> m_Pool;
public BaseRef(RefPool<TDerived> pool) => m_Pool = pool;
protected abstract void OnDispose();
public void Dispose()
{
@ -42,8 +39,8 @@ namespace Server.Utilities
public delegate TRef Generator(RefPool<TRef> targetPool);
public const int DEFAULT_RESOURCE_RETENTION = 10;
private Stack<TRef> m_Resources = new Stack<TRef>();
private Generator m_Generator;
private readonly Stack<TRef> m_Resources = new Stack<TRef>();
private readonly Generator m_Generator;
private int m_MaxRefrenceRetention;
/// <summary>
@ -90,8 +87,9 @@ namespace Server.Utilities
/// <inheritdoc/>
public class QueueRef<T> : Queue<T>, IRef
{
private RefPool<QueueRef<T>> m_Pool;
private QueueRef(RefPool<QueueRef<T>> pool) { m_Pool = pool; }
private readonly RefPool<QueueRef<T>> m_Pool;
private QueueRef(RefPool<QueueRef<T>> pool) => m_Pool = pool;
/// <summary>Clears the queue and returns this resource to its parent resource pool.</summary>
public void Dispose() { Clear(); m_Pool.Return(this); }
/// <summary>
@ -102,8 +100,9 @@ namespace Server.Utilities
/// <inheritdoc/>
public class StackRef<T> : Stack<T>, IRef
{
private RefPool<StackRef<T>> m_Pool;
private StackRef(RefPool<StackRef<T>> pool) { m_Pool = pool; }
private readonly RefPool<StackRef<T>> m_Pool;
private StackRef(RefPool<StackRef<T>> pool) => m_Pool = pool;
/// <summary>Clears the stack and returns this resource to its parent resource pool.</summary>
public void Dispose() { Clear(); m_Pool.Return(this); }
/// <summary>

View file

@ -36,7 +36,7 @@ namespace Server
private static Dictionary<IPAddress, IPAddress> _ipAddressTable;
private static SkillName[] m_AllSkills =
private static readonly SkillName[] m_AllSkills =
{
SkillName.Alchemy,
SkillName.Anatomy,
@ -95,7 +95,7 @@ namespace Server
SkillName.Spellweaving
};
private static SkillName[] m_CombatSkills =
private static readonly SkillName[] m_CombatSkills =
{
SkillName.Archery,
SkillName.Swords,
@ -104,7 +104,7 @@ namespace Server
SkillName.Wrestling
};
private static SkillName[] m_CraftSkills =
private static readonly SkillName[] m_CraftSkills =
{
SkillName.Alchemy,
SkillName.Blacksmith,
@ -117,7 +117,7 @@ namespace Server
SkillName.Tinkering
};
private static Stack<ConsoleColor> m_ConsoleColors = new Stack<ConsoleColor>();
private static readonly Stack<ConsoleColor> m_ConsoleColors = new Stack<ConsoleColor>();
public static Encoding UTF8 => m_UTF8 ?? (m_UTF8 = new UTF8Encoding(false, false));
public static Encoding UTF8WithEncoding => m_UTF8WithEncoding ?? (m_UTF8WithEncoding = new UTF8Encoding(true, false));