Rewrite our Equals implementations so they don't crash when comparing incompatible objects

This commit is contained in:
ScottNZ
2013-11-12 17:12:00 +13:00
parent 4f91c22fed
commit c373bc22e8
12 changed files with 24 additions and 58 deletions

View File

@@ -95,11 +95,8 @@ namespace OpenRA.FileFormats
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (obj == null) var o = obj as HSLColor?;
return false; return o != null && o == this;
HSLColor o = (HSLColor)obj;
return o == this;
} }
} }
} }

View File

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

View File

@@ -40,10 +40,8 @@ namespace OpenRA.FileFormats
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (!(obj is Pair<T, U>)) var o = obj as Pair<T, U>?;
return false; return o != null && o == this;
return (Pair<T, U>)obj == this;
} }
public override int GetHashCode() public override int GetHashCode()

View File

@@ -73,11 +73,8 @@ namespace OpenRA
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (obj == null) var o = obj as float2?;
return false; return o != null && o == this;
float2 o = (float2)obj;
return o == this;
} }
public static readonly float2 Zero = new float2(0, 0); public static readonly float2 Zero = new float2(0, 0);

View File

@@ -43,11 +43,8 @@ namespace OpenRA
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (obj == null) var o = obj as int2?;
return false; return o != null && o == this;
int2 o = (int2)obj;
return o == this;
} }
public static readonly int2 Zero = new int2(0, 0); public static readonly int2 Zero = new int2(0, 0);

View File

@@ -40,11 +40,8 @@ namespace OpenRA
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (obj == null) var o = obj as WAngle?;
return false; return o != null && o == this;
WAngle o = (WAngle)obj;
return o == this;
} }
public int Sin() { return new WAngle(Angle - 256).Cos(); } public int Sin() { return new WAngle(Angle - 256).Cos(); }

View File

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

View File

@@ -80,11 +80,8 @@ namespace OpenRA
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (obj == null) var o = obj as WRange?;
return false; return o != null && o == this;
WRange o = (WRange)obj;
return o == this;
} }
public override string ToString() { return "{0}".F(Range); } public override string ToString() { return "{0}".F(Range); }

View File

@@ -97,11 +97,8 @@ namespace OpenRA
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (obj == null) var o = obj as WRot?;
return false; return o != null && o == this;
WRot o = (WRot)obj;
return o == this;
} }
public override string ToString() { return "{0},{1},{2}".F(Roll, Pitch, Yaw); } public override string ToString() { return "{0},{1},{2}".F(Roll, Pitch, Yaw); }

View File

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

View File

@@ -54,11 +54,8 @@ namespace OpenRA
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (obj == null) var o = obj as CPos?;
return false; return o != null && o == this;
CPos o = (CPos)obj;
return o == this;
} }
public override string ToString() { return "{0},{1}".F(X, Y); } public override string ToString() { return "{0},{1}".F(X, Y); }

View File

@@ -65,11 +65,8 @@ namespace OpenRA
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (obj == null) var o = obj as CVec?;
return false; return o != null && o == this;
CVec o = (CVec)obj;
return o == this;
} }
public override string ToString() { return "{0},{1}".F(X, Y); } public override string ToString() { return "{0},{1}".F(X, Y); }