Merge remote-tracking branch 'upstream/master' into develop

This commit is contained in:
EvilFactory
2024-12-11 10:44:53 -03:00
257 changed files with 4793 additions and 1653 deletions
@@ -1,18 +1,27 @@
using System;
using System.Collections.Generic;
// NOTE: We should use struct in addition to Enum in all the type constraints (at the end of the method signatures), as it
// tells the compiler that we're only ever using value types, which enums always are anyway.
// This avoids a lot of allocations caused by the compiler preparing for anything, which in turn happens because despite
// how it works in practice, Enum is counted as a reference type in C#... for historical reasons.
// We use the (int)(object) cast because generic types can't be cast directly to int, so we box into object and unbox into int instead.
// It avoids some memory allocations that Convert.ToInt32() seems to do.
// NOTE: This should work fine as long as the enum values stay within - to + 2^31, so let's not use uint or long for them.
namespace Barotrauma.Extensions
{
public static class EnumExtensions
{
/// <summary>
/// Enum.HasFlag() checks if all flags matches. This method checks if any of them matches.
/// Enum.HasFlag() checks if all flags matches. This method checks if any of them matches. It also avoids boxing allocations that the built-in version might still sometimes cause.
/// E.g. when myEnum = SomeEnum.First | SomeEnum.Second, myEnum.HasFlag(SomeEnum.First | SomeEnum.Third) returns false, because not all of the flags match, but HasAnyFlag(SomeEnum.First | SomeEnum.Third) returns true, because some of the flags match.
/// </summary>
public static bool HasAnyFlag<T>(this T type, T value) where T : Enum
public static bool HasAnyFlag<T>(this T type, T value) where T : struct, Enum
{
int typeValue = Convert.ToInt32(type);
int flagValue = Convert.ToInt32(value);
int typeValue = (int)(object)type;
int flagValue = (int)(object)value;
return (typeValue & flagValue) != 0;
}
@@ -20,10 +29,10 @@ namespace Barotrauma.Extensions
/// Adds a flag value to an enum.
/// Note that enums are value types, so you need to use the value returned from this method.
/// </summary>
public static T AddFlag<T>(this T @enum, T flag) where T : Enum
public static T AddFlag<T>(this T @enum, T flag) where T : struct, Enum
{
int enumValue = Convert.ToInt32(@enum);
int flagValue = Convert.ToInt32(flag);
int enumValue = (int)(object)@enum;
int flagValue = (int)(object)flag;
return (T)(object)(enumValue | flagValue);
}
@@ -31,18 +40,18 @@ namespace Barotrauma.Extensions
/// Removes a flag value from an enum.
/// Note that enums are value types, so you need to use the value returned from this method.
/// </summary>
public static T RemoveFlag<T>(this T @enum, T flag) where T : Enum
public static T RemoveFlag<T>(this T @enum, T flag) where T : struct, Enum
{
int enumValue = Convert.ToInt32(@enum);
int flagValue = Convert.ToInt32(flag);
int enumValue = (int)(object)@enum;
int flagValue = (int)(object)flag;
return (T)(object)(enumValue & ~flagValue);
}
public static IEnumerable<T> GetIndividualFlags<T>(T flagsEnum) where T : Enum
public static IEnumerable<T> GetIndividualFlags<T>(T flagsEnum) where T : struct, Enum
{
foreach (T value in Enum.GetValues(typeof(T)))
{
if (flagsEnum.HasFlag(value)) { yield return value; }
if (flagsEnum.HasAnyFlag(value)) { yield return value; }
}
}
}
@@ -678,7 +678,7 @@ namespace Barotrauma
}
/// <summary>
/// divide a convex hull into triangles
/// Divide a convex hull into triangles
/// </summary>
/// <returns>List of triangle vertices (sorted counter-clockwise)</returns>
public static List<Vector2[]> TriangulateConvexHull(List<Vector2> vertices, Vector2 center)
@@ -1156,4 +1156,5 @@ namespace Barotrauma
return -CompareCW.Compare(a, b, center);
}
}
}
@@ -180,10 +180,9 @@ namespace Barotrauma
/// Gets a type by its name, with backwards compatibility for types that have been renamed.
/// <see cref="TypePreviouslyKnownAs"/>
/// </summary>
public static Type? GetTypeWithBackwardsCompatibility(string nameSpace, string typeName, bool throwOnError, bool ignoreCase)
public static Type? GetTypeWithBackwardsCompatibility(Assembly assembly, string nameSpace, string typeName, bool throwOnError, bool ignoreCase)
{
if (Assembly.GetEntryAssembly() is not { } entryAssembly) { return null; }
var types = entryAssembly
var types = assembly
.GetTypes()
.Where(t => NameMatches(t.Namespace, nameSpace, ignoreCase));