From c373bc22e8a0ac0a1b4708f22659a4466c9cdb8d Mon Sep 17 00:00:00 2001 From: ScottNZ Date: Tue, 12 Nov 2013 17:12:00 +1300 Subject: [PATCH] Rewrite our Equals implementations so they don't crash when comparing incompatible objects --- OpenRA.FileFormats/HSLColor.cs | 7 ++----- OpenRA.FileFormats/Hotkey.cs | 6 ++---- OpenRA.FileFormats/Primitives/Pair.cs | 6 ++---- OpenRA.FileFormats/Primitives/float2.cs | 7 ++----- OpenRA.FileFormats/Primitives/int2.cs | 7 ++----- OpenRA.FileFormats/WAngle.cs | 7 ++----- OpenRA.FileFormats/WPos.cs | 7 ++----- OpenRA.FileFormats/WRange.cs | 7 ++----- OpenRA.FileFormats/WRot.cs | 7 ++----- OpenRA.FileFormats/WVec.cs | 7 ++----- OpenRA.Game/CPos.cs | 7 ++----- OpenRA.Game/CVec.cs | 7 ++----- 12 files changed, 24 insertions(+), 58 deletions(-) diff --git a/OpenRA.FileFormats/HSLColor.cs b/OpenRA.FileFormats/HSLColor.cs index b19fa432c7..f9701593ee 100644 --- a/OpenRA.FileFormats/HSLColor.cs +++ b/OpenRA.FileFormats/HSLColor.cs @@ -95,11 +95,8 @@ namespace OpenRA.FileFormats public override bool Equals(object obj) { - if (obj == null) - return false; - - HSLColor o = (HSLColor)obj; - return o == this; + var o = obj as HSLColor?; + return o != null && o == this; } } } diff --git a/OpenRA.FileFormats/Hotkey.cs b/OpenRA.FileFormats/Hotkey.cs index d185032baf..d95dff8243 100755 --- a/OpenRA.FileFormats/Hotkey.cs +++ b/OpenRA.FileFormats/Hotkey.cs @@ -72,10 +72,8 @@ namespace OpenRA public override bool Equals(object obj) { - if (obj == null) - return false; - - return (Hotkey)obj == this; + var o = obj as Hotkey?; + return o != null && o == this; } public override string ToString() { return "{0} {1}".F(Key, Modifiers.ToString("F")); } diff --git a/OpenRA.FileFormats/Primitives/Pair.cs b/OpenRA.FileFormats/Primitives/Pair.cs index 33dd09e806..26a86639dc 100644 --- a/OpenRA.FileFormats/Primitives/Pair.cs +++ b/OpenRA.FileFormats/Primitives/Pair.cs @@ -40,10 +40,8 @@ namespace OpenRA.FileFormats public override bool Equals(object obj) { - if (!(obj is Pair)) - return false; - - return (Pair)obj == this; + var o = obj as Pair?; + return o != null && o == this; } public override int GetHashCode() diff --git a/OpenRA.FileFormats/Primitives/float2.cs b/OpenRA.FileFormats/Primitives/float2.cs index 83e0872be0..83aa4fc000 100644 --- a/OpenRA.FileFormats/Primitives/float2.cs +++ b/OpenRA.FileFormats/Primitives/float2.cs @@ -73,11 +73,8 @@ namespace OpenRA public override bool Equals(object obj) { - if (obj == null) - return false; - - float2 o = (float2)obj; - return o == this; + var o = obj as float2?; + return o != null && o == this; } public static readonly float2 Zero = new float2(0, 0); diff --git a/OpenRA.FileFormats/Primitives/int2.cs b/OpenRA.FileFormats/Primitives/int2.cs index c40d4c67fe..89399682cd 100644 --- a/OpenRA.FileFormats/Primitives/int2.cs +++ b/OpenRA.FileFormats/Primitives/int2.cs @@ -43,11 +43,8 @@ namespace OpenRA public override bool Equals(object obj) { - if (obj == null) - return false; - - int2 o = (int2)obj; - return o == this; + var o = obj as int2?; + return o != null && o == this; } public static readonly int2 Zero = new int2(0, 0); diff --git a/OpenRA.FileFormats/WAngle.cs b/OpenRA.FileFormats/WAngle.cs index e3e6f860a2..3704783c9d 100644 --- a/OpenRA.FileFormats/WAngle.cs +++ b/OpenRA.FileFormats/WAngle.cs @@ -40,11 +40,8 @@ namespace OpenRA public override bool Equals(object obj) { - if (obj == null) - return false; - - WAngle o = (WAngle)obj; - return o == this; + var o = obj as WAngle?; + return o != null && o == this; } public int Sin() { return new WAngle(Angle - 256).Cos(); } diff --git a/OpenRA.FileFormats/WPos.cs b/OpenRA.FileFormats/WPos.cs index 589b3c003a..7c48ccbb57 100644 --- a/OpenRA.FileFormats/WPos.cs +++ b/OpenRA.FileFormats/WPos.cs @@ -56,11 +56,8 @@ namespace OpenRA public override bool Equals(object obj) { - if (obj == null) - return false; - - WPos o = (WPos)obj; - return o == this; + var o = obj as WPos?; + return o != null && o == this; } public override string ToString() { return "{0},{1},{2}".F(X, Y, Z); } diff --git a/OpenRA.FileFormats/WRange.cs b/OpenRA.FileFormats/WRange.cs index c49be11a67..07ce9ff85b 100644 --- a/OpenRA.FileFormats/WRange.cs +++ b/OpenRA.FileFormats/WRange.cs @@ -80,11 +80,8 @@ namespace OpenRA public override bool Equals(object obj) { - if (obj == null) - return false; - - WRange o = (WRange)obj; - return o == this; + var o = obj as WRange?; + return o != null && o == this; } public override string ToString() { return "{0}".F(Range); } diff --git a/OpenRA.FileFormats/WRot.cs b/OpenRA.FileFormats/WRot.cs index 8bdb244df8..4a4b7bd18b 100644 --- a/OpenRA.FileFormats/WRot.cs +++ b/OpenRA.FileFormats/WRot.cs @@ -97,11 +97,8 @@ namespace OpenRA public override bool Equals(object obj) { - if (obj == null) - return false; - - WRot o = (WRot)obj; - return o == this; + var o = obj as WRot?; + return o != null && o == this; } public override string ToString() { return "{0},{1},{2}".F(Roll, Pitch, Yaw); } diff --git a/OpenRA.FileFormats/WVec.cs b/OpenRA.FileFormats/WVec.cs index 4039374384..f68cd36450 100644 --- a/OpenRA.FileFormats/WVec.cs +++ b/OpenRA.FileFormats/WVec.cs @@ -83,11 +83,8 @@ namespace OpenRA public override bool Equals(object obj) { - if (obj == null) - return false; - - WVec o = (WVec)obj; - return o == this; + var o = obj as WVec?; + return o != null && o == this; } public override string ToString() { return "{0},{1},{2}".F(X, Y, Z); } diff --git a/OpenRA.Game/CPos.cs b/OpenRA.Game/CPos.cs index cb43935556..2fef5e5dda 100644 --- a/OpenRA.Game/CPos.cs +++ b/OpenRA.Game/CPos.cs @@ -54,11 +54,8 @@ namespace OpenRA public override bool Equals(object obj) { - if (obj == null) - return false; - - CPos o = (CPos)obj; - return o == this; + var o = obj as CPos?; + return o != null && o == this; } public override string ToString() { return "{0},{1}".F(X, Y); } diff --git a/OpenRA.Game/CVec.cs b/OpenRA.Game/CVec.cs index aad8d732d3..a77ae4aa1d 100644 --- a/OpenRA.Game/CVec.cs +++ b/OpenRA.Game/CVec.cs @@ -65,11 +65,8 @@ namespace OpenRA public override bool Equals(object obj) { - if (obj == null) - return false; - - CVec o = (CVec)obj; - return o == this; + var o = obj as CVec?; + return o != null && o == this; } public override string ToString() { return "{0},{1}".F(X, Y); }