Change to use pattern matching

This commit is contained in:
teinarss
2021-02-28 19:00:32 +01:00
committed by reaperrr
parent 7c0e4b25ae
commit d60c05eff3
35 changed files with 63 additions and 122 deletions

View File

@@ -359,8 +359,7 @@ namespace OpenRA
public override bool Equals(object obj)
{
var o = obj as Actor;
return o != null && Equals(o);
return obj is Actor o && Equals(o);
}
public bool Equals(Actor other)

View File

@@ -511,8 +511,7 @@ namespace OpenRA
public static bool IsTraitEnabled<T>(this T trait)
{
var disabledTrait = trait as IDisabledTrait;
return disabledTrait == null || !disabledTrait.IsTraitDisabled;
return !(trait is IDisabledTrait disabledTrait) || !disabledTrait.IsTraitDisabled;
}
public static T FirstEnabledTraitOrDefault<T>(this IEnumerable<T> ts)

View File

@@ -67,8 +67,7 @@ namespace OpenRA
foreach (var weapon in Weapons)
{
var projectileLoaded = weapon.Value.Projectile as IRulesetLoaded<WeaponInfo>;
if (projectileLoaded != null)
if (weapon.Value.Projectile is IRulesetLoaded<WeaponInfo> projectileLoaded)
{
try
{
@@ -82,8 +81,7 @@ namespace OpenRA
foreach (var warhead in weapon.Value.Warheads)
{
var cacher = warhead as IRulesetLoaded<WeaponInfo>;
if (cacher != null)
if (warhead is IRulesetLoaded<WeaponInfo> cacher)
{
try
{

View File

@@ -41,8 +41,7 @@ namespace OpenRA.Graphics
// See combined.vert for documentation on the channel attribute format
var attribC = r.Channel == TextureChannel.RGBA ? 0x02 : ((byte)r.Channel) << 1 | 0x01;
attribC |= samplers.X << 6;
var ss = r as SpriteWithSecondaryData;
if (ss != null)
if (r is SpriteWithSecondaryData ss)
{
sl = ss.SecondaryLeft;
st = ss.SecondaryTop;

View File

@@ -81,8 +81,7 @@ namespace OpenRA
public override bool Equals(object obj)
{
var o = obj as Hotkey?;
return o != null && o == this;
return obj is Hotkey o && (Hotkey?)o == this;
}
public override string ToString() { return "{0} {1}".F(Key, Modifiers.ToString("F")); }

View File

@@ -148,8 +148,7 @@ namespace OpenRA
{
foreach (var map in package.Contents)
{
var mapPackage = package.OpenPackage(map, modData.ModFiles) as IReadWritePackage;
if (mapPackage != null)
if (package.OpenPackage(map, modData.ModFiles) is IReadWritePackage mapPackage)
yield return mapPackage;
}
}

View File

@@ -131,8 +131,7 @@ namespace OpenRA.Primitives
{
var offset = 0L;
overallBaseStream = stream;
var segmentStream = stream as SegmentStream;
if (segmentStream != null)
if (stream is SegmentStream segmentStream)
offset += segmentStream.BaseOffset + GetOverallNestedOffset(segmentStream.BaseStream, out overallBaseStream);
return offset;
}

View File

@@ -55,8 +55,7 @@ namespace OpenRA
public override bool Equals(object obj)
{
var o = obj as float3?;
return o != null && o == this;
return obj is float3 o && (float3?)o == this;
}
public override string ToString() { return "{0},{1},{2}".F(X, Y, Z); }

View File

@@ -63,13 +63,11 @@ namespace OpenRA
sb.AppendIndentedFormatLine(indent, "Exception of type `{0}`: {1}", ex.GetType().FullName, ex.Message);
var tle = ex as TypeLoadException;
var oom = ex as OutOfMemoryException;
if (tle != null)
if (ex is TypeLoadException tle)
{
sb.AppendIndentedFormatLine(indent, "TypeName=`{0}`", tle.TypeName);
}
else if (oom != null)
else if (ex is OutOfMemoryException)
{
var gcMemoryBeforeCollect = GC.GetTotalMemory(false);
GC.Collect();

View File

@@ -73,11 +73,10 @@ namespace OpenRA.Widgets
public static T LoadWidget<T>(string id, Widget parent, WidgetArgs args) where T : Widget
{
var widget = LoadWidget(id, parent, args) as T;
if (widget == null)
throw new InvalidOperationException(
"Widget {0} is not of type {1}".F(id, typeof(T).Name));
return widget;
if (LoadWidget(id, parent, args) is T widget)
return widget;
throw new InvalidOperationException("Widget {0} is not of type {1}".F(id, typeof(T).Name));
}
public static Widget LoadWidget(string id, Widget parent, WidgetArgs args)

View File

@@ -273,8 +273,7 @@ namespace OpenRA
gameInfo.DisabledSpawnPoints = OrderManager.LobbyInfo.DisabledSpawnPoints;
var echo = OrderManager.Connection as EchoConnection;
var rc = echo != null ? echo.Recorder : null;
var rc = (OrderManager.Connection as EchoConnection)?.Recorder;
if (rc != null)
rc.Metadata = new ReplayMetadata(gameInfo);
@@ -326,12 +325,10 @@ namespace OpenRA
{
effects.Add(e);
var sp = e as ISpatiallyPartitionable;
if (sp == null)
if (!(e is ISpatiallyPartitionable))
unpartitionedEffects.Add(e);
var se = e as ISync;
if (se != null)
if (e is ISync se)
syncedEffects.Add(se);
}
@@ -339,12 +336,10 @@ namespace OpenRA
{
effects.Remove(e);
var sp = e as ISpatiallyPartitionable;
if (sp == null)
if (!(e is ISpatiallyPartitionable))
unpartitionedEffects.Remove(e);
var se = e as ISync;
if (se != null)
if (e is ISync se)
syncedEffects.Remove(se);
}