Reapply "OBT1.1.0 Merge branch 'dev_pte' into dev"
This reverts commit 046483b9da.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
@@ -146,10 +147,10 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly Dictionary<Type, ImmutableArray<CachedReflectedVariable>> CachedVariables = new Dictionary<Type, ImmutableArray<CachedReflectedVariable>>();
|
||||
private static readonly ConcurrentDictionary<Type, ImmutableArray<CachedReflectedVariable>> CachedVariables = new ConcurrentDictionary<Type, ImmutableArray<CachedReflectedVariable>>();
|
||||
|
||||
private static readonly Dictionary<Type, IReadWriteBehavior> TypeBehaviors
|
||||
= new Dictionary<Type, IReadWriteBehavior>
|
||||
private static readonly ConcurrentDictionary<Type, IReadWriteBehavior> TypeBehaviors
|
||||
= new ConcurrentDictionary<Type, IReadWriteBehavior>(new Dictionary<Type, IReadWriteBehavior>
|
||||
{
|
||||
{ typeof(Boolean), new ReadWriteBehavior<Boolean>(ReadBoolean, WriteBoolean) },
|
||||
{ typeof(Byte), new ReadWriteBehavior<Byte>(ReadByte, WriteByte) },
|
||||
@@ -168,7 +169,7 @@ namespace Barotrauma
|
||||
{ typeof(Vector2), new ReadWriteBehavior<Vector2>(ReadVector2, WriteVector2) },
|
||||
{ typeof(SerializableDateTime), new ReadWriteBehavior<SerializableDateTime>(ReadSerializableDateTime, WriteSerializableDateTime) },
|
||||
{ typeof(NetLimitedString), new ReadWriteBehavior<NetLimitedString>(ReadNetLString, WriteNetLString) }
|
||||
};
|
||||
});
|
||||
|
||||
private static readonly ImmutableDictionary<Predicate<Type>, Func<Type, IReadWriteBehavior>> BehaviorFactories = new Dictionary<Predicate<Type>, Func<Type, IReadWriteBehavior>>
|
||||
{
|
||||
@@ -584,7 +585,11 @@ namespace Barotrauma
|
||||
if (!predicate(type)) { continue; }
|
||||
|
||||
behavior = factory(type);
|
||||
TypeBehaviors.Add(type, behavior);
|
||||
// Use TryAdd for thread-safety; if another thread already added, use that value
|
||||
if (!TypeBehaviors.TryAdd(type, behavior))
|
||||
{
|
||||
behavior = TypeBehaviors[type];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -594,8 +599,11 @@ namespace Barotrauma
|
||||
|
||||
public static ImmutableArray<CachedReflectedVariable> GetPropertiesAndFields(Type type)
|
||||
{
|
||||
if (CachedVariables.TryGetValue(type, out var cached)) { return cached; }
|
||||
return CachedVariables.GetOrAdd(type, static t => CreateCachedVariables(t));
|
||||
}
|
||||
|
||||
private static ImmutableArray<CachedReflectedVariable> CreateCachedVariables(Type type)
|
||||
{
|
||||
List<CachedReflectedVariable> variables = new List<CachedReflectedVariable>();
|
||||
|
||||
IEnumerable<PropertyInfo> propertyInfos = type.GetProperties().Where(HasAttribute).Where(NotStatic);
|
||||
@@ -633,7 +641,6 @@ namespace Barotrauma
|
||||
}
|
||||
|
||||
ImmutableArray<CachedReflectedVariable> array = variables.All(v => v.HasOwnAttribute) ? variables.OrderBy(v => v.Attribute.OrderKey).ToImmutableArray() : variables.ToImmutableArray();
|
||||
CachedVariables.Add(type, array);
|
||||
return array;
|
||||
|
||||
bool HasAttribute(MemberInfo info) => (info.GetCustomAttribute<NetworkSerialize>() ?? type.GetCustomAttribute<NetworkSerialize>()) != null;
|
||||
@@ -874,4 +881,4 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user