Unstable v0.19.1.0

This commit is contained in:
Juan Pablo Arce
2022-08-19 13:59:08 -03:00
parent 6b55adcdd9
commit 1219615d64
192 changed files with 3875 additions and 2648 deletions
@@ -1,8 +1,9 @@
#nullable enable
using System;
namespace Barotrauma
{
public abstract class Either<T, U>
public abstract class Either<T, U> where T : notnull where U : notnull
{
public static implicit operator Either<T, U>(T t) => new EitherT<T, U>(t);
public static implicit operator Either<T, U>(U u) => new EitherU<T, U>(u);
@@ -15,22 +16,32 @@ namespace Barotrauma
public abstract bool TryCast<V>(out V v);
public abstract override string ToString();
public abstract override string? ToString();
public abstract override bool Equals(object? obj);
public abstract override int GetHashCode();
public static bool operator ==(Either<T, U>? a, Either<T, U>? b)
=> a is null ? b is null : a.Equals(b);
public static bool operator !=(Either<T, U>? a, Either<T, U>? b)
=> !(a == b);
}
public sealed class EitherT<T, U> : Either<T, U>
public sealed class EitherT<T, U> : Either<T, U> where T : notnull where U : notnull
{
public readonly T Value;
public EitherT(T value) { Value = value; }
public override string ToString()
public override string? ToString()
{
return Value.ToString();
}
public override bool TryGet(out T t) { t = Value; return true; }
public override bool TryGet(out U u) { u = default; return false; }
public override bool TryGet(out U u) { u = default!; return false; }
public override bool TryCast<V>(out V v)
{
@@ -41,24 +52,34 @@ namespace Barotrauma
}
else
{
v = default;
v = default!;
return false;
}
}
public override bool Equals(object? obj)
=> obj switch
{
EitherT<T, U> other => Value.Equals(other.Value),
T value => Value.Equals(value),
_ => false
};
public override int GetHashCode() => Value.GetHashCode();
}
public sealed class EitherU<T, U> : Either<T, U>
public sealed class EitherU<T, U> : Either<T, U> where T : notnull where U : notnull
{
public readonly U Value;
public EitherU(U value) { Value = value; }
public override string ToString()
public override string? ToString()
{
return Value.ToString();
}
public override bool TryGet(out T t) { t = default; return false; }
public override bool TryGet(out T t) { t = default!; return false; }
public override bool TryGet(out U u) { u = Value; return true; }
public override bool TryCast<V>(out V v)
@@ -70,9 +91,19 @@ namespace Barotrauma
}
else
{
v = default;
v = default!;
return false;
}
}
public override bool Equals(object? obj)
=> obj switch
{
EitherU<T, U> other => Value.Equals(other.Value),
U value => Value.Equals(value),
_ => false
};
public override int GetHashCode() => Value.GetHashCode();
}
}
@@ -5,5 +5,13 @@ namespace Barotrauma
private None() { }
public static Option<T> Create() => new None<T>();
public override Option<T> Fallback(Option<T> fallback) => fallback;
public override T Fallback(T fallback) => fallback;
public override bool ValueEquals(T value) => false;
public override string ToString()
=> $"None<{typeof(T).Name}>";
}
}
@@ -1,3 +1,4 @@
#nullable enable
using System;
namespace Barotrauma
@@ -23,7 +24,7 @@ namespace Barotrauma
outValue = value;
return true;
case None<T> _:
outValue = default;
outValue = default!;
return false;
default:
throw new ArgumentOutOfRangeException();
@@ -37,5 +38,30 @@ namespace Barotrauma
None<T> _ => Option<TType>.None(),
_ => throw new ArgumentOutOfRangeException()
};
public abstract Option<T> Fallback(Option<T> fallback);
public abstract T Fallback(T fallback);
public abstract bool ValueEquals(T value);
public override bool Equals(object? obj)
=> obj switch
{
Some<T> { Value: var value } => this is Some<T> { Value: { } selfValue } && selfValue.Equals(value),
None<T> _ => IsNone(),
T value => this is Some<T> { Value: { } selfValue } && selfValue.Equals(value),
_ => false
};
public override int GetHashCode()
=> this is Some<T> { Value: { } value } ? value.GetHashCode() : 0;
public static bool operator ==(Option<T> a, Option<T> b)
=> a.Equals(b);
public static bool operator !=(Option<T> a, Option<T> b)
=> !(a == b);
public abstract override string ToString();
}
}
@@ -13,5 +13,13 @@ namespace Barotrauma
}
public static Option<T> Create(T value) => new Some<T>(value);
public override Option<T> Fallback(Option<T> fallback) => this;
public override T Fallback(T fallback) => Value;
public override bool ValueEquals(T value) => Value.Equals(value);
public override string ToString()
=> $"Some<{typeof(T).Name}>({Value})";
}
}
@@ -1,5 +1,7 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
@@ -7,14 +9,52 @@ namespace Barotrauma
{
public static class ReflectionUtils
{
private static Type[] cachedNonAbstractTypes;
private static readonly Dictionary<Assembly, ImmutableArray<Type>> cachedNonAbstractTypes
= new Dictionary<Assembly, ImmutableArray<Type>>();
public static IEnumerable<Type> GetDerivedNonAbstract<T>()
{
if (cachedNonAbstractTypes == null)
Assembly assembly = typeof(T).Assembly;
if (!cachedNonAbstractTypes.ContainsKey(assembly))
{
cachedNonAbstractTypes = Assembly.GetEntryAssembly().GetTypes().Where(t => !t.IsAbstract).ToArray();
cachedNonAbstractTypes[assembly] = assembly.GetTypes()
.Where(t => !t.IsAbstract).ToImmutableArray();
}
return cachedNonAbstractTypes.Where(t => t.IsSubclassOf(typeof(T)));
return cachedNonAbstractTypes[assembly].Where(t => t.IsSubclassOf(typeof(T)));
}
public static Option<T1> ParseDerived<T1>(string str)
{
static Option<T1> none() => Option<T1>.None();
var derivedTypes = GetDerivedNonAbstract<T1>();
Option<T1> parseOfType(Type t)
{
//every T1 type is expected to have a method with the following signature:
// public static Option<T> Parse(string str)
var parseFunc = t.GetMethod("Parse", BindingFlags.Public | BindingFlags.Static);
if (parseFunc is null) { return none(); }
var parameters = parseFunc.GetParameters();
if (parameters.Length != 1) { return none(); }
var returnType = parseFunc.ReturnType;
if (!returnType.IsConstructedGenericType) { return none(); }
if (returnType.GetGenericTypeDefinition() != typeof(Option<>)) { return none(); }
if (returnType.GenericTypeArguments[0] != t) { return none(); }
//some hacky business to convert from Option<T2> to Option<T1> when we only know T2 at runtime
static Option<T1> convert<T2>(Option<T2> option) where T2 : T1
=> option.Select(v => (T1)v);
Func<Option<T1>, Option<T1>> f = convert;
var constructedConverter = f.Method.GetGenericMethodDefinition().MakeGenericMethod(typeof(T1), t);
return constructedConverter.Invoke(null, new object?[] { parseFunc.Invoke(null, new object[] { str }) })
as Option<T1> ?? none();
}
return derivedTypes.Select(parseOfType).FirstOrDefault(t => t.IsSome()) ?? none();
}
}
}
@@ -4,8 +4,10 @@ using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
#if CLIENT
using Barotrauma.Networking;
using Barotrauma.Steam;
#endif
namespace Barotrauma.IO
{
@@ -26,14 +26,14 @@ namespace Barotrauma
}
}
public static partial class ToolBox
static partial class ToolBox
{
static internal class Epoch
internal static class Epoch
{
private static readonly DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
/// <summary>
/// Returns the current Unix Epoch (Coordinated Universal Time )
/// Returns the current Unix Epoch (Coordinated Universal Time)
/// </summary>
public static int NowUTC
{
@@ -712,5 +712,10 @@ namespace Barotrauma
return e;
}
public static void ThrowIfNull<T>(T o)
{
if (o is null) { throw new ArgumentNullException(); }
}
}
}