From 4216501c458698b2235a185f3912cf6bfd5dcfcc Mon Sep 17 00:00:00 2001 From: Ruby Rose Date: Thu, 13 Jul 2017 16:00:40 +0300 Subject: [PATCH 1/4] Make User implement IEquatable for more consistent comparisons --- TShockAPI/DB/UserManager.cs | 64 ++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/TShockAPI/DB/UserManager.cs b/TShockAPI/DB/UserManager.cs index e79fb933..88103899 100644 --- a/TShockAPI/DB/UserManager.cs +++ b/TShockAPI/DB/UserManager.cs @@ -363,7 +363,7 @@ namespace TShockAPI.DB } /// A database user. - public class User + public class User : IEquatable { /// The database ID of the user. public int ID { get; set; } @@ -581,10 +581,66 @@ namespace TShockAPI.DB return HashPassword(Encoding.UTF8.GetBytes(password)); } - } + #region IEquatable - /// UserManagerException - An exception generated by the user manager. - [Serializable] + /// Indicates whether the current is equal to another . + /// true if the is equal to the parameter; otherwise, false. + /// An to compare with this . + public bool Equals(User other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return ID == other.ID && string.Equals(Name, other.Name); + } + + /// Indicates whether the current is equal to another object. + /// true if the is equal to the parameter; otherwise, false. + /// An to compare with this . + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((User)obj); + } + + /// Serves as the hash function. + /// A hash code for the current . + public override int GetHashCode() + { + unchecked + { + return (ID * 397) ^ (Name != null ? Name.GetHashCode() : 0); + } + } + + /// + /// Compares equality of two objects. + /// + /// Left hand of the comparison. + /// Right hand of the comparison. + /// true if the objects are equal; otherwise, false. + public static bool operator ==(User left, User right) + { + return Equals(left, right); + } + + /// + /// Compares equality of two objects. + /// + /// Left hand of the comparison. + /// Right hand of the comparison. + /// true if the objects aren't equal; otherwise, false. + public static bool operator !=(User left, User right) + { + return !Equals(left, right); + } + + #endregion + } + + /// UserManagerException - An exception generated by the user manager. + [Serializable] public class UserManagerException : Exception { /// Creates a new UserManagerException object. From c8e31231ba135465160eb43e20858d3c38c41a68 Mon Sep 17 00:00:00 2001 From: Ruby Rose Date: Thu, 13 Jul 2017 16:01:47 +0300 Subject: [PATCH 2/4] override ToString of user --- TShockAPI/DB/UserManager.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/TShockAPI/DB/UserManager.cs b/TShockAPI/DB/UserManager.cs index 88103899..f93f9935 100644 --- a/TShockAPI/DB/UserManager.cs +++ b/TShockAPI/DB/UserManager.cs @@ -637,7 +637,12 @@ namespace TShockAPI.DB } #endregion - } + + public override string ToString() + { + return Name; + } + } /// UserManagerException - An exception generated by the user manager. [Serializable] From 5e1be5b19b08350d833a96c46917248641b32782 Mon Sep 17 00:00:00 2001 From: Ruby Rose Date: Thu, 13 Jul 2017 16:04:17 +0300 Subject: [PATCH 3/4] Fix formatting below IEquatable block --- TShockAPI/DB/UserManager.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/TShockAPI/DB/UserManager.cs b/TShockAPI/DB/UserManager.cs index f93f9935..368c2dae 100644 --- a/TShockAPI/DB/UserManager.cs +++ b/TShockAPI/DB/UserManager.cs @@ -644,8 +644,8 @@ namespace TShockAPI.DB } } - /// UserManagerException - An exception generated by the user manager. - [Serializable] + /// UserManagerException - An exception generated by the user manager. + [Serializable] public class UserManagerException : Exception { /// Creates a new UserManagerException object. @@ -704,4 +704,4 @@ namespace TShockAPI.DB { } } -} \ No newline at end of file +} From 02be378a2ec5b71ab1e878d22ac14a37b19f3da1 Mon Sep 17 00:00:00 2001 From: Ruby Rose Date: Fri, 14 Jul 2017 08:03:16 +0300 Subject: [PATCH 4/4] change doc of Equals --- TShockAPI/DB/UserManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TShockAPI/DB/UserManager.cs b/TShockAPI/DB/UserManager.cs index 368c2dae..07662b2b 100644 --- a/TShockAPI/DB/UserManager.cs +++ b/TShockAPI/DB/UserManager.cs @@ -595,7 +595,7 @@ namespace TShockAPI.DB /// Indicates whether the current is equal to another object. /// true if the is equal to the parameter; otherwise, false. - /// An to compare with this . + /// An to compare with this . public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false;