From fb3b23f1dcbc9a9c1735a31450c103df01ccb577 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Sat, 14 Oct 2017 15:58:22 +0300 Subject: [PATCH 1/3] Use new C# features in SteamID class --- SteamKit2/SteamKit2/Types/SteamID.cs | 241 ++++++++------------------- 1 file changed, 71 insertions(+), 170 deletions(-) diff --git a/SteamKit2/SteamKit2/Types/SteamID.cs b/SteamKit2/SteamKit2/Types/SteamID.cs index d804e39cf..650cbb15e 100644 --- a/SteamKit2/SteamKit2/Types/SteamID.cs +++ b/SteamKit2/SteamKit2/Types/SteamID.cs @@ -15,32 +15,17 @@ namespace SteamKit2 { class BitVector64 { - private UInt64 data; + public ulong Data { get; set; } - public BitVector64() + public BitVector64( ulong value ) { - } - public BitVector64( UInt64 value ) - { - data = value; + Data = value; } - public UInt64 Data + public ulong this[ uint bitoffset, ulong valuemask ] { - get { return data; } - set { data = value; } - } - - public UInt64 this[ uint bitoffset, UInt64 valuemask ] - { - get - { - return ( data >> ( ushort )bitoffset ) & valuemask; - } - set - { - data = ( data & ~( valuemask << ( ushort )bitoffset ) ) | ( ( value & valuemask ) << ( ushort )bitoffset ); - } + get => ( Data >> ( ushort )bitoffset ) & valuemask; + set => Data = ( Data & ~( valuemask << ( ushort )bitoffset ) ) | ( ( value & valuemask ) << ( ushort )bitoffset ); } } @@ -50,21 +35,21 @@ public UInt64 Data [DebuggerDisplay( "{Render()}, {ConvertToUInt64()}" )] public class SteamID { - BitVector64 steamid; + readonly BitVector64 steamid; - static Regex Steam2Regex = new Regex( + static readonly Regex Steam2Regex = new Regex( @"STEAM_(?[0-4]):(?[0-1]):(?\d+)", RegexOptions.Compiled | RegexOptions.IgnoreCase ); - static Regex Steam3Regex = new Regex( + static readonly Regex Steam3Regex = new Regex( @"\[(?[AGMPCgcLTIUai]):(?[0-4]):(?\d+)(:(?\d+))?\]", RegexOptions.Compiled ); - static Regex Steam3FallbackRegex = new Regex( + static readonly Regex Steam3FallbackRegex = new Regex( @"\[(?[AGMPCgcLTIUai]):(?[0-4]):(?\d+)(\((?\d+)\))?\]", RegexOptions.Compiled ); - static Dictionary AccountTypeChars = new Dictionary + static readonly Dictionary AccountTypeChars = new Dictionary { { EAccountType.AnonGameServer, 'A' }, { EAccountType.GameServer, 'G' }, @@ -127,6 +112,7 @@ public enum ChatInstanceFlags : uint MMSLobby = ( AccountInstanceMask + 1 ) >> 3, } + /// /// /// Initializes a new instance of the class. /// @@ -135,18 +121,17 @@ public SteamID() { } + /// /// /// Initializes a new instance of the class. /// /// The account ID. /// The universe. /// The account type. - public SteamID( UInt32 unAccountID, EUniverse eUniverse, EAccountType eAccountType ) - : this() - { - Set( unAccountID, eUniverse, eAccountType ); - } + public SteamID( uint unAccountID, EUniverse eUniverse, EAccountType eAccountType ) + : this() => Set( unAccountID, eUniverse, eAccountType ); + /// /// /// Initializes a new instance of the class. /// @@ -154,20 +139,14 @@ public SteamID( UInt32 unAccountID, EUniverse eUniverse, EAccountType eAccountTy /// The instance. /// The universe. /// The account type. - public SteamID( UInt32 unAccountID, UInt32 unInstance, EUniverse eUniverse, EAccountType eAccountType ) - : this() - { - InstancedSet( unAccountID, unInstance, eUniverse, eAccountType ); - } + public SteamID( uint unAccountID, uint unInstance, EUniverse eUniverse, EAccountType eAccountType ) + : this() => InstancedSet( unAccountID, unInstance, eUniverse, eAccountType ); /// /// Initializes a new instance of the class. /// /// The 64bit integer to assign this SteamID from. - public SteamID( UInt64 id ) - { - this.steamid = new BitVector64( id ); - } + public SteamID( ulong id ) => this.steamid = new BitVector64( id ); /// /// Initializes a new instance of the class from a Steam2 "STEAM_" rendered form. @@ -197,7 +176,7 @@ public SteamID( string steamId, EUniverse eUniverse ) /// The account ID. /// The universe. /// The account type. - public void Set( UInt32 unAccountID, EUniverse eUniverse, EAccountType eAccountType ) + public void Set( uint unAccountID, EUniverse eUniverse, EAccountType eAccountType ) { this.AccountID = unAccountID; this.AccountUniverse = eUniverse; @@ -220,7 +199,7 @@ public void Set( UInt32 unAccountID, EUniverse eUniverse, EAccountType eAccountT /// The instance. /// The universe. /// The account type. - public void InstancedSet( UInt32 unAccountID, UInt32 unInstance, EUniverse eUniverse, EAccountType eAccountType ) + public void InstancedSet( uint unAccountID, uint unInstance, EUniverse eUniverse, EAccountType eAccountType ) { this.AccountID = unAccountID; this.AccountUniverse = eUniverse; @@ -358,30 +337,19 @@ public bool SetFromSteam3String( string steamId ) /// Sets the various components of this SteamID from a 64bit integer form. /// /// The 64bit integer to assign this SteamID from. - public void SetFromUInt64( UInt64 ulSteamID ) - { - this.steamid.Data = ulSteamID; - } + public void SetFromUInt64( ulong ulSteamID ) => this.steamid.Data = ulSteamID; /// /// Converts this SteamID into it's 64bit integer form. /// /// A 64bit integer representing this SteamID. - public UInt64 ConvertToUInt64() - { - return this.steamid.Data; - } + public ulong ConvertToUInt64() => this.steamid.Data; /// /// Returns a static account key used for grouping accounts with differing instances. /// /// A 64bit static account key. - public ulong GetStaticAccountKey() - { - return ( ( ulong )AccountUniverse << 56 ) + ( ( ulong )AccountType << 52 ) + AccountID; - } - - + public ulong GetStaticAccountKey() => ( ( ulong )AccountUniverse << 56 ) + ( ( ulong )AccountType << 52 ) + AccountID; /// @@ -390,20 +358,15 @@ public ulong GetStaticAccountKey() /// /// true if this instance is a blank anon account; otherwise, false. /// - public bool IsBlankAnonAccount - { - get { return this.AccountID == 0 && IsAnonAccount && this.AccountInstance == 0; } - } + public bool IsBlankAnonAccount => this.AccountID == 0 && IsAnonAccount && this.AccountInstance == 0; + /// /// Gets a value indicating whether this instance is a game server account. /// /// /// true if this instance is a game server account; otherwise, false. /// - public bool IsGameServerAccount - { - get { return this.AccountType == EAccountType.GameServer || this.AccountType == EAccountType.AnonGameServer; } - } + public bool IsGameServerAccount => this.AccountType == EAccountType.GameServer || this.AccountType == EAccountType.AnonGameServer; /// /// Gets a value indicating whether this instance is a persistent game server account. @@ -411,10 +374,7 @@ public bool IsGameServerAccount /// /// true if this instance is a persistent game server account; otherwise, false. /// - public bool IsPersistentGameServerAccount - { - get { return this.AccountType == EAccountType.GameServer; } - } + public bool IsPersistentGameServerAccount => this.AccountType == EAccountType.GameServer; /// /// Gets a value indicating whether this instance is an anonymous game server account. @@ -422,10 +382,7 @@ public bool IsPersistentGameServerAccount /// /// true if this instance is an anon game server account; otherwise, false. /// - public bool IsAnonGameServerAccount - { - get { return this.AccountType == EAccountType.AnonGameServer; } - } + public bool IsAnonGameServerAccount => this.AccountType == EAccountType.AnonGameServer; /// /// Gets a value indicating whether this instance is a content server account. @@ -433,73 +390,55 @@ public bool IsAnonGameServerAccount /// /// true if this instance is a content server account; otherwise, false. /// - public bool IsContentServerAccount - { - get { return this.AccountType == EAccountType.ContentServer; } - } + public bool IsContentServerAccount => this.AccountType == EAccountType.ContentServer; + /// /// Gets a value indicating whether this instance is a clan account. /// /// /// true if this instance is a clan account; otherwise, false. /// - public bool IsClanAccount - { - get { return this.AccountType == EAccountType.Clan; } - } + public bool IsClanAccount => this.AccountType == EAccountType.Clan; + /// /// Gets a value indicating whether this instance is a chat account. /// /// /// true if this instance is a chat account; otherwise, false. /// - public bool IsChatAccount - { - get { return this.AccountType == EAccountType.Chat; } - } + public bool IsChatAccount => this.AccountType == EAccountType.Chat; + /// /// Gets a value indicating whether this instance is a lobby. /// /// /// true if this instance is a lobby; otherwise, false. /// - public bool IsLobby - { - get { - return ( this.AccountType == EAccountType.Chat ) && - ( ( this.AccountInstance & ( uint )ChatInstanceFlags.Lobby ) > 0 ); - } - } + public bool IsLobby => this.AccountType == EAccountType.Chat && ( this.AccountInstance & ( uint )ChatInstanceFlags.Lobby ) > 0; + /// /// Gets a value indicating whether this instance is an individual account. /// /// /// true if this instance is an individual account; otherwise, false. /// - public bool IsIndividualAccount - { - get { return this.AccountType == EAccountType.Individual || this.AccountType == EAccountType.ConsoleUser; } - } + public bool IsIndividualAccount => this.AccountType == EAccountType.Individual || this.AccountType == EAccountType.ConsoleUser; + /// /// Gets a value indicating whether this instance is an anonymous account. /// /// /// true if this instance is an anon account; otherwise, false. /// - public bool IsAnonAccount - { - get { return this.AccountType == EAccountType.AnonUser || this.AccountType == EAccountType.AnonGameServer; } - } + public bool IsAnonAccount => this.AccountType == EAccountType.AnonUser || this.AccountType == EAccountType.AnonGameServer; + /// /// Gets a value indicating whether this instance is an anonymous user account. /// /// /// true if this instance is an anon user account; otherwise, false. /// - public bool IsAnonUserAccount - { - get { return this.AccountType == EAccountType.AnonUser; } - } + public bool IsAnonUserAccount => this.AccountType == EAccountType.AnonUser; /// /// Gets a value indicating whether this instance is a console user account. @@ -507,10 +446,7 @@ public bool IsAnonUserAccount /// /// true if this instance is a console user account; otherwise, false. /// - public bool IsConsoleUserAccount - { - get { return this.AccountType == EAccountType.ConsoleUser; } - } + public bool IsConsoleUserAccount => this.AccountType == EAccountType.ConsoleUser; /// /// Gets a value indicating whether this instance is valid. @@ -556,16 +492,10 @@ public bool IsValid /// /// The account id. /// - public UInt32 AccountID + public uint AccountID { - get - { - return ( UInt32 )steamid[ 0, 0xFFFFFFFF ]; - } - set - { - steamid[ 0, 0xFFFFFFFF ] = value; - } + get => ( uint )steamid[ 0, 0xFFFFFFFF ]; + set => steamid[ 0, 0xFFFFFFFF ] = value; } /// /// Gets or sets the account instance. @@ -573,16 +503,10 @@ public UInt32 AccountID /// /// The account instance. /// - public UInt32 AccountInstance + public uint AccountInstance { - get - { - return ( UInt32 )steamid[ 32, 0xFFFFF ]; - } - set - { - steamid[ 32, 0xFFFFF ] = ( UInt64 )value; - } + get => ( uint )steamid[ 32, 0xFFFFF ]; + set => steamid[ 32, 0xFFFFF ] = ( ulong )value; } /// /// Gets or sets the account type. @@ -592,14 +516,8 @@ public UInt32 AccountInstance /// public EAccountType AccountType { - get - { - return ( EAccountType )steamid[ 52, 0xF ]; - } - set - { - steamid[ 52, 0xF ] = ( UInt64 )value; - } + get => ( EAccountType )steamid[ 52, 0xF ]; + set => steamid[ 52, 0xF ] = ( ulong )value; } /// /// Gets or sets the account universe. @@ -609,14 +527,8 @@ public EAccountType AccountType /// public EUniverse AccountUniverse { - get - { - return ( EUniverse )steamid[ 56, 0xFF ]; - } - set - { - steamid[ 56, 0xFF ] = ( UInt64 )value; - } + get => ( EUniverse )steamid[ 56, 0xFF ]; + set => steamid[ 56, 0xFF ] = ( ulong )value; } /// @@ -643,7 +555,7 @@ string RenderSteam2() case EAccountType.Invalid: case EAccountType.Individual: var universeDigit = ( AccountUniverse <= EUniverse.Public ) ? "0" : Enum.Format( typeof( EUniverse ), AccountUniverse, "D" ); - return String.Format( "STEAM_{0}:{1}:{2}", universeDigit, AccountID & 1, AccountID >> 1 ); + return $"STEAM_{universeDigit}:{AccountID & 1}:{AccountID >> 1}"; default: return Convert.ToString( this ); } @@ -651,8 +563,7 @@ string RenderSteam2() string RenderSteam3() { - char accountTypeChar; - if ( !AccountTypeChars.TryGetValue( AccountType, out accountTypeChar ) ) + if ( !AccountTypeChars.TryGetValue( AccountType, out var accountTypeChar ) ) { accountTypeChar = UnknownAccountTypeChar; } @@ -685,35 +596,28 @@ string RenderSteam3() if ( renderInstance ) { - return string.Format( "[{0}:{1}:{2}:{3}]", accountTypeChar, ( uint )AccountUniverse, AccountID, AccountInstance ); - } - else - { - return string.Format( "[{0}:{1}:{2}]", accountTypeChar, ( uint )AccountUniverse, AccountID ); + return $"[{accountTypeChar}:{(uint)AccountUniverse}:{AccountID}:{AccountInstance}]"; } + + return $"[{accountTypeChar}:{(uint)AccountUniverse}:{AccountID}]"; } /// - /// Returns a that represents this instance. + /// Returns a that represents this instance. /// /// - /// A that represents this instance. + /// A that represents this instance. /// - public override string ToString() - { - // for compatibility, we will always return a Steam2 rendering when ToString()'d - return Render( false ); - } - + public override string ToString() => Render( false ); // for compatibility, we will always return a Steam2 rendering when ToString()'d /// - /// Performs an implicit conversion from to . + /// Performs an implicit conversion from to . /// /// The SteamID. /// /// The result of the conversion. /// - public static implicit operator UInt64( SteamID sid ) + public static implicit operator ulong( SteamID sid ) { if ( sid == null ) { @@ -724,25 +628,22 @@ public static implicit operator UInt64( SteamID sid ) } /// - /// Performs an implicit conversion from to . + /// Performs an implicit conversion from to . /// /// A 64bit integer representing the SteamID. /// /// The result of the conversion. /// - public static implicit operator SteamID( UInt64 id ) - { - return new SteamID( id ); - } + public static implicit operator SteamID( ulong id ) => new SteamID( id ); /// - /// Determines whether the specified is equal to this instance. + /// Determines whether the specified is equal to this instance. /// - /// The to compare with this instance. + /// The to compare with this instance. /// - /// true if the specified is equal to this instance; otherwise, false. + /// true if the specified is equal to this instance; otherwise, false. /// - public override bool Equals( System.Object obj ) + public override bool Equals( object obj ) { if ( obj == null ) { @@ -750,7 +651,7 @@ public override bool Equals( System.Object obj ) } SteamID sid = obj as SteamID; - if ( ( System.Object )sid == null ) + if ( ( object )sid == null ) { return false; } @@ -785,7 +686,7 @@ public bool Equals( SteamID sid ) /// public static bool operator ==( SteamID a, SteamID b ) { - if ( System.Object.ReferenceEquals( a, b ) ) + if ( ReferenceEquals( a, b ) ) { return true; } From 6f9836075c6efc966e0ad23f94e99812ba838558 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Sat, 14 Oct 2017 15:59:29 +0300 Subject: [PATCH 2/3] Render Steam3 by default --- SteamKit2/SteamKit2/Types/SteamID.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SteamKit2/SteamKit2/Types/SteamID.cs b/SteamKit2/SteamKit2/Types/SteamID.cs index 650cbb15e..b200aec84 100644 --- a/SteamKit2/SteamKit2/Types/SteamID.cs +++ b/SteamKit2/SteamKit2/Types/SteamID.cs @@ -538,7 +538,7 @@ public EUniverse AccountUniverse /// /// A string Steam2 "STEAM_" representation of this SteamID, or a Steam3 representation. /// - public string Render( bool steam3 = false ) + public string Render( bool steam3 = true ) { if ( steam3 ) { @@ -608,7 +608,7 @@ string RenderSteam3() /// /// A that represents this instance. /// - public override string ToString() => Render( false ); // for compatibility, we will always return a Steam2 rendering when ToString()'d + public override string ToString() => Render(); /// /// Performs an implicit conversion from to . From 29cfd09f0ba2a089996a62a5ce85a19df92b0c3b Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Sat, 14 Oct 2017 16:14:30 +0300 Subject: [PATCH 3/3] Fix tests for Steam3 rendering --- SteamKit2/Tests/SteamIDFacts.cs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/SteamKit2/Tests/SteamIDFacts.cs b/SteamKit2/Tests/SteamIDFacts.cs index 60c1c02e6..6e8ecbfb1 100644 --- a/SteamKit2/Tests/SteamIDFacts.cs +++ b/SteamKit2/Tests/SteamIDFacts.cs @@ -149,7 +149,7 @@ public void Steam3StringSymmetric() SteamID sid = new SteamID(); bool parsed = sid.SetFromSteam3String( steamId ); Assert.True( parsed ); - Assert.Equal( steamId, sid.Render( steam3: true ) ); + Assert.Equal( steamId, sid.Render() ); } } @@ -230,14 +230,22 @@ public void Steam2RenderIsValid() { SteamID sid = 76561197969249708; - Assert.Equal( "STEAM_0:0:4491990", sid.Render() ); - Assert.Equal( sid.Render(), sid.ToString() ); + Assert.Equal( "STEAM_0:0:4491990", sid.Render( steam3: false ) ); sid.AccountUniverse = EUniverse.Beta; - Assert.Equal( "STEAM_2:0:4491990", sid.Render() ); + Assert.Equal( "STEAM_2:0:4491990", sid.Render( steam3: false ) ); sid.AccountType = EAccountType.GameServer; - Assert.Equal( "157625991261918636", sid.Render() ); + Assert.Equal( "157625991261918636", sid.Render( steam3: false ) ); + } + + [Fact] + public void RendersSteam3ByDefault() + { + SteamID sid = 76561197969249708; + + Assert.Equal( "[U:1:8983980]", sid.Render() ); + Assert.Equal( "[U:1:8983980]", sid.ToString() ); } [Fact] @@ -305,7 +313,7 @@ public void InitializesInstancesCorrectly() public void RendersOutOfRangeAccountTypeAsLowercaseI() { SteamID sid = new SteamID( 123, EUniverse.Beta, (EAccountType)(-1) ); - Assert.Equal( "[i:2:123]", sid.Render(steam3: true) ); + Assert.Equal( "[i:2:123]", sid.Render() ); } } }