Fix CA1851

This commit is contained in:
RoosterDragon
2023-07-13 20:08:36 +01:00
committed by abcdefg30
parent 88f830a9e5
commit 3275875ae5
63 changed files with 349 additions and 267 deletions

View File

@@ -185,7 +185,7 @@ namespace OpenRA
public bool HasTraitInfo<T>() where T : ITraitInfoInterface { return traits.Contains<T>(); }
public T TraitInfo<T>() where T : ITraitInfoInterface { return traits.Get<T>(); }
public T TraitInfoOrDefault<T>() where T : ITraitInfoInterface { return traits.GetOrDefault<T>(); }
public IEnumerable<T> TraitInfos<T>() where T : ITraitInfoInterface { return traits.WithInterface<T>(); }
public IReadOnlyCollection<T> TraitInfos<T>() where T : ITraitInfoInterface { return traits.WithInterface<T>(); }
public BitSet<TargetableType> GetAllTargetTypes()
{

View File

@@ -234,7 +234,7 @@ namespace OpenRA.Graphics
{
// PERF: We don't need to search for images if there are no definitions.
// PERF: It's more efficient to send an empty array rather than an array of 9 nulls.
if (!collection.Regions.Any())
if (collection.Regions.Count == 0)
return Array.Empty<Sprite>();
// Support manual definitions for unusual dialog layouts

View File

@@ -139,7 +139,7 @@ namespace OpenRA
return removed;
}
public IEnumerable<T> GetAll<T>() where T : ActorInit
public IReadOnlyCollection<T> GetAll<T>() where T : ActorInit
{
return initDict.Value.WithInterface<T>();
}

View File

@@ -12,7 +12,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace OpenRA
{
@@ -64,9 +63,9 @@ namespace OpenRA
}
/// <summary>Returns the minimal region that covers at least the specified cells.</summary>
public static CellRegion BoundingRegion(MapGridType shape, IEnumerable<CPos> cells)
public static CellRegion BoundingRegion(MapGridType shape, IReadOnlyCollection<CPos> cells)
{
if (cells == null || !cells.Any())
if (cells == null || cells.Count == 0)
throw new ArgumentException("cells must not be null or empty.", nameof(cells));
var minU = int.MaxValue;

View File

@@ -201,8 +201,12 @@ namespace OpenRA.Network
public TypeInfo(Type type)
{
const BindingFlags Flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
var fields = type.GetFields(Flags).Where(fi => !fi.IsLiteral && !fi.IsStatic && fi.HasAttribute<SyncAttribute>());
var properties = type.GetProperties(Flags).Where(pi => pi.HasAttribute<SyncAttribute>());
var fields = type.GetFields(Flags)
.Where(fi => !fi.IsLiteral && !fi.IsStatic && fi.HasAttribute<SyncAttribute>())
.ToList();
var properties = type.GetProperties(Flags)
.Where(pi => pi.HasAttribute<SyncAttribute>())
.ToList();
foreach (var prop in properties)
if (!prop.CanRead || prop.GetIndexParameters().Length > 0)

View File

@@ -134,8 +134,8 @@ namespace OpenRA
public ConstructorInfo GetCtor(Type type)
{
var flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
var ctors = type.GetConstructors(flags).Where(x => x.HasAttribute<UseCtorAttribute>());
if (ctors.Count() > 1)
var ctors = type.GetConstructors(flags).Where(x => x.HasAttribute<UseCtorAttribute>()).ToList();
if (ctors.Count > 1)
throw new InvalidOperationException("ObjectCreator: UseCtor on multiple constructors; invalid.");
return ctors.FirstOrDefault();
}

View File

@@ -133,7 +133,7 @@ namespace OpenRA
static FactionInfo ResolveDisplayFaction(World world, string factionName)
{
var factions = world.Map.Rules.Actors[SystemActors.World].TraitInfos<FactionInfo>().ToArray();
var factions = world.Map.Rules.Actors[SystemActors.World].TraitInfos<FactionInfo>();
return factions.FirstOrDefault(f => f.InternalName == factionName) ?? factions.First();
}

View File

@@ -12,14 +12,20 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace OpenRA.Primitives
{
public class TypeDictionary : IEnumerable<object>
{
static readonly Func<Type, List<object>> CreateList = type => new List<object>();
readonly Dictionary<Type, List<object>> data = new();
static readonly Func<Type, ITypeContainer> CreateTypeContainer = t =>
(ITypeContainer)typeof(TypeContainer<>).MakeGenericType(t).GetConstructor(Type.EmptyTypes).Invoke(null);
readonly Dictionary<Type, ITypeContainer> data = new();
ITypeContainer InnerGet(Type t)
{
return data.GetOrAdd(t, CreateTypeContainer);
}
public void Add(object val)
{
@@ -33,7 +39,7 @@ namespace OpenRA.Primitives
void InnerAdd(Type t, object val)
{
data.GetOrAdd(t, CreateList).Add(val);
InnerGet(t).Add(val);
}
public bool Contains<T>()
@@ -48,35 +54,33 @@ namespace OpenRA.Primitives
public T Get<T>()
{
return (T)Get(typeof(T), true);
return Get<T>(true);
}
public T GetOrDefault<T>()
{
var result = Get(typeof(T), false);
if (result == null)
return default;
return (T)result;
return Get<T>(false);
}
object Get(Type t, bool throwsIfMissing)
T Get<T>(bool throwsIfMissing)
{
if (!data.TryGetValue(t, out var ret))
if (!data.TryGetValue(typeof(T), out var container))
{
if (throwsIfMissing)
throw new InvalidOperationException($"TypeDictionary does not contain instance of type `{t}`");
return null;
throw new InvalidOperationException($"TypeDictionary does not contain instance of type `{typeof(T)}`");
return default;
}
if (ret.Count > 1)
throw new InvalidOperationException($"TypeDictionary contains multiple instances of type `{t}`");
return ret[0];
var list = ((TypeContainer<T>)container).Objects;
if (list.Count > 1)
throw new InvalidOperationException($"TypeDictionary contains multiple instances of type `{typeof(T)}`");
return list[0];
}
public IEnumerable<T> WithInterface<T>()
public IReadOnlyCollection<T> WithInterface<T>()
{
if (data.TryGetValue(typeof(T), out var objs))
return objs.Cast<T>();
if (data.TryGetValue(typeof(T), out var container))
return ((TypeContainer<T>)container).Objects;
return Array.Empty<T>();
}
@@ -92,18 +96,19 @@ namespace OpenRA.Primitives
void InnerRemove(Type t, object val)
{
if (!data.TryGetValue(t, out var objs))
if (!data.TryGetValue(t, out var container))
return;
objs.Remove(val);
if (objs.Count == 0)
container.Remove(val);
if (container.Count == 0)
data.Remove(t);
}
public void TrimExcess()
{
data.TrimExcess();
foreach (var objs in data.Values)
objs.TrimExcess();
foreach (var t in data.Keys)
InnerGet(t).TrimExcess();
}
public IEnumerator<object> GetEnumerator()
@@ -115,6 +120,36 @@ namespace OpenRA.Primitives
{
return GetEnumerator();
}
interface ITypeContainer
{
int Count { get; }
void Add(object value);
void Remove(object value);
void TrimExcess();
}
sealed class TypeContainer<T> : ITypeContainer
{
public List<T> Objects { get; } = new List<T>();
public int Count => Objects.Count;
public void Add(object value)
{
Objects.Add((T)value);
}
public void Remove(object value)
{
Objects.Remove((T)value);
}
public void TrimExcess()
{
Objects.TrimExcess();
}
}
}
public static class TypeExts

View File

@@ -11,7 +11,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Eluant;
using Eluant.ObjectBinding;
using OpenRA.Scripting;
@@ -139,20 +138,21 @@ namespace OpenRA
{
public static WPos Average(this IEnumerable<WPos> source)
{
var length = source.Count();
if (length == 0)
return WPos.Zero;
var length = 0;
var x = 0L;
var y = 0L;
var z = 0L;
foreach (var pos in source)
{
length++;
x += pos.X;
y += pos.Y;
z += pos.Z;
}
if (length == 0)
return WPos.Zero;
x /= length;
y /= length;
z /= length;