Fixes casting checks

This commit is contained in:
Kamron Batman 2018-08-19 01:38:55 +08:00
parent 7ea00fbf4f
commit 03dd5f1926
431 changed files with 5616 additions and 6250 deletions

View file

@ -242,9 +242,9 @@ namespace Server.Accounting
/// </summary>
public bool Inactive
{
get
get
{
if( this.AccessLevel != AccessLevel.Player )
if ( this.AccessLevel != AccessLevel.Player )
return false;
TimeSpan inactiveLength = DateTime.UtcNow - m_LastLogin;
@ -263,9 +263,7 @@ namespace Server.Accounting
{
for ( int i = 0; i < m_Mobiles.Length; i++ )
{
PlayerMobile m = m_Mobiles[i] as PlayerMobile;
if ( m != null && m.NetState != null )
if ( m_Mobiles[i] is PlayerMobile m && m.NetState != null )
return m_TotalGameTime + ( DateTime.UtcNow - m.SessionStart );
}
@ -511,9 +509,7 @@ namespace Server.Accounting
private static void EventSink_Connected( ConnectedEventArgs e )
{
Account acc = e.Mobile.Account as Account;
if ( acc == null )
if ( !(e.Mobile.Account is Account acc) )
return;
if ( acc.Young && acc.m_YoungTimer == null )
@ -525,9 +521,7 @@ namespace Server.Accounting
private static void EventSink_Disconnected( DisconnectedEventArgs e )
{
Account acc = e.Mobile.Account as Account;
if ( acc == null )
if ( !(e.Mobile.Account is Account acc) )
return;
if ( acc.m_YoungTimer != null )
@ -536,8 +530,7 @@ namespace Server.Accounting
acc.m_YoungTimer = null;
}
PlayerMobile m = e.Mobile as PlayerMobile;
if ( m == null )
if ( !(e.Mobile is PlayerMobile m) )
return;
acc.m_TotalGameTime += DateTime.UtcNow - m.SessionStart;
@ -545,14 +538,10 @@ namespace Server.Accounting
private static void EventSink_Login( LoginEventArgs e )
{
PlayerMobile m = e.Mobile as PlayerMobile;
if ( m == null )
if ( !(e.Mobile is PlayerMobile m) )
return;
Account acc = m.Account as Account;
if ( acc == null )
if ( !(m.Account is Account acc) )
return;
if ( m.Young && acc.Young )
@ -570,9 +559,7 @@ namespace Server.Accounting
for ( int i = 0; i < m_Mobiles.Length; i++ )
{
PlayerMobile m = m_Mobiles[i] as PlayerMobile;
if ( m != null && m.Young )
if ( m_Mobiles[i] is PlayerMobile m && m.Young )
{
m.Young = false;
@ -614,7 +601,7 @@ namespace Server.Accounting
public Account( string username, string password )
{
m_Username = username;
SetPassword( password );
m_AccessLevel = AccessLevel.Player;
@ -685,7 +672,7 @@ namespace Server.Accounting
m_Flags = Utility.GetXMLInt32( Utility.GetText( node["flags"], "0" ), 0 );
m_Created = Utility.GetXMLDateTime( Utility.GetText( node["created"], null ), DateTime.UtcNow );
m_LastLogin = Utility.GetXMLDateTime( Utility.GetText( node["lastLogin"], null ), DateTime.UtcNow );
TotalGold = Utility.GetXMLInt32( Utility.GetText(node["totalGold"], "0" ), 0 );
TotalPlat = Utility.GetXMLInt32(Utility.GetText(node["totalPlat"], "0"), 0);
@ -706,9 +693,7 @@ namespace Server.Accounting
{
for ( int i = 0; i < m_Mobiles.Length; i++ )
{
PlayerMobile m = m_Mobiles[i] as PlayerMobile;
if ( m != null )
if ( m_Mobiles[i] is PlayerMobile m )
totalGameTime += m.GameTime;
}
}
@ -776,7 +761,7 @@ namespace Server.Accounting
{
IPAddress address;
if( IPAddress.TryParse( Utility.GetText( ip, null ), out address ) )
if ( IPAddress.TryParse( Utility.GetText( ip, null ), out address ) )
{
list[count] = Utility.Intern( address );
count++;
@ -1221,8 +1206,8 @@ namespace Server.Accounting
public int CompareTo( object obj )
{
if ( obj is Account )
return this.CompareTo( (Account) obj );
if ( obj is Account account )
return this.CompareTo( account );
throw new ArgumentException();
}

View file

@ -82,9 +82,8 @@ namespace Server.Misc
public static void Password_OnCommand( CommandEventArgs e )
{
Mobile from = e.Mobile;
Account acct = from.Account as Account;
if ( acct == null )
if ( !(from.Account is Account acct) )
return;
IPAddress[] accessList = acct.LoginIPs;
@ -102,7 +101,7 @@ namespace Server.Misc
from.SendMessage( "You must specify the new password." );
return;
}
else if ( e.Length == 1 )
if ( e.Length == 1 )
{
from.SendMessage( "To prevent potential typing mistakes, you must type the password twice. Use the format:" );
from.SendMessage( "Password \"(newPassword)\" \"(repeated)\"" );
@ -172,9 +171,7 @@ namespace Server.Misc
NetState state = e.State;
int index = e.Index;
Account acct = state.Account as Account;
if ( acct == null )
if ( !(state.Account is Account acct) )
{
state.Dispose();
}
@ -315,9 +312,8 @@ namespace Server.Misc
string pw = e.Password;
e.Accepted = false;
Account acct = Accounts.GetAccount( un ) as Account;
if ( acct == null )
if ( !(Accounts.GetAccount( un ) is Account acct) )
{
if ( AutoAccountCreation && un.Trim().Length > 0 ) // To prevent someone from making an account of just '' or a bunch of meaningless spaces
{
@ -378,9 +374,7 @@ namespace Server.Misc
string un = e.Username;
string pw = e.Password;
Account acct = Accounts.GetAccount( un ) as Account;
if ( acct == null )
if ( !(Accounts.GetAccount( un ) is Account acct) )
{
e.Accepted = false;
}
@ -415,21 +409,16 @@ namespace Server.Misc
public static bool CheckAccount( Mobile mobCheck, Mobile accCheck )
{
if ( accCheck != null )
if ( accCheck?.Account is Account a )
{
Account a = accCheck.Account as Account;
if ( a != null )
for ( int i = 0; i < a.Length; ++i )
{
for ( int i = 0; i < a.Length; ++i )
{
if ( a[i] == mobCheck )
return true;
}
if ( a[i] == mobCheck )
return true;
}
}
return false;
}
}
}
}

View file

@ -34,20 +34,18 @@ namespace Server
public override bool Equals( object obj )
{
if( obj is IPAddress )
if ( obj is IPAddress )
{
return obj.Equals( m_Address );
}
else if( obj is string )
if ( obj is string s )
{
IPAddress otherAddress;
if( IPAddress.TryParse( (string)obj, out otherAddress ) )
if ( IPAddress.TryParse( s, out IPAddress otherAddress ) )
return otherAddress.Equals( m_Address );
}
else if( obj is IPFirewallEntry )
else if ( obj is IPFirewallEntry entry )
{
return m_Address.Equals( ((IPFirewallEntry)obj).m_Address );
return m_Address.Equals( entry.m_Address );
}
return false;
@ -82,31 +80,22 @@ namespace Server
public override bool Equals( object obj )
{
if( obj is string )
if ( obj is string entry )
{
string entry= (string)obj;
string[] str = entry.Split( '/' );
if( str.Length == 2 )
if ( str.Length == 2 )
{
IPAddress cidrPrefix;
if( IPAddress.TryParse( str[0], out cidrPrefix ) )
if ( IPAddress.TryParse( str[0], out IPAddress cidrPrefix ) )
{
int cidrLength;
if( int.TryParse( str[1], out cidrLength ) )
if ( int.TryParse( str[1], out int cidrLength ) )
return m_CIDRPrefix.Equals( cidrPrefix ) && m_CIDRLength.Equals( cidrLength );
}
}
}
else if( obj is CIDRFirewallEntry )
else if ( obj is CIDRFirewallEntry cidrEntry )
{
CIDRFirewallEntry entry = obj as CIDRFirewallEntry;
return m_CIDRPrefix.Equals( entry.m_CIDRPrefix ) && m_CIDRLength.Equals( entry.m_CIDRLength );
return m_CIDRPrefix.Equals( cidrEntry.m_CIDRPrefix ) && m_CIDRLength.Equals( cidrEntry.m_CIDRLength );
}
return false;
@ -131,7 +120,7 @@ namespace Server
public bool IsBlocked( IPAddress address )
{
if( !m_Valid )
if ( !m_Valid )
return false; //Why process if it's invalid? it'll return false anyway after processing it.
return Utility.IPMatch( m_Entry, address, ref m_Valid );
@ -139,17 +128,15 @@ namespace Server
public override string ToString()
{
return m_Entry.ToString();
return m_Entry;
}
public override bool Equals( object obj )
{
if( obj is string )
if ( obj is string )
return obj.Equals( m_Entry );
else if( obj is WildcardIPFirewallEntry )
return m_Entry.Equals( ((WildcardIPFirewallEntry)obj).m_Entry );
return false;
return obj is WildcardIPFirewallEntry entry && m_Entry.Equals( entry.m_Entry );
}
public override int GetHashCode()
@ -186,7 +173,7 @@ namespace Server
object toAdd;
IPAddress addr;
if( IPAddress.TryParse( line, out addr ) )
if ( IPAddress.TryParse( line, out addr ) )
toAdd = addr;
else
toAdd = line;
@ -208,35 +195,29 @@ namespace Server
public static IFirewallEntry ToFirewallEntry( object entry )
{
if( entry is IFirewallEntry )
return (IFirewallEntry)entry;
else if( entry is IPAddress )
return new IPFirewallEntry( (IPAddress)entry );
else if( entry is string )
return ToFirewallEntry( (string)entry );
if ( entry is IFirewallEntry firewallEntry )
return firewallEntry;
if ( entry is IPAddress address )
return new IPFirewallEntry( address );
if ( entry is string s )
return ToFirewallEntry( s );
return null;
}
public static IFirewallEntry ToFirewallEntry( string entry )
{
IPAddress addr;
if( IPAddress.TryParse( entry, out addr ) )
if ( IPAddress.TryParse( entry, out IPAddress addr ) )
return new IPFirewallEntry( addr );
//Try CIDR parse
string[] str = entry.Split( '/' );
if( str.Length == 2 )
if ( str.Length == 2 )
{
IPAddress cidrPrefix;
if( IPAddress.TryParse( str[0], out cidrPrefix ) )
if ( IPAddress.TryParse( str[0], out IPAddress cidrPrefix ) )
{
int cidrLength;
if( int.TryParse( str[1], out cidrLength ) )
if ( int.TryParse( str[1], out int cidrLength ) )
return new CIDRFirewallEntry( cidrPrefix, cidrLength );
}
}
@ -254,7 +235,7 @@ namespace Server
{
IFirewallEntry entry = ToFirewallEntry( obj );
if( entry != null )
if ( entry != null )
{
m_Blocked.Remove( entry );
Save();
@ -263,17 +244,17 @@ namespace Server
public static void Add( object obj )
{
if( obj is IPAddress )
Add( (IPAddress)obj );
else if( obj is string )
Add( (string)obj );
else if( obj is IFirewallEntry )
Add( (IFirewallEntry)obj );
if ( obj is IPAddress address )
Add( address );
else if ( obj is string s )
Add( s );
else if ( obj is IFirewallEntry entry )
Add( entry );
}
public static void Add( IFirewallEntry entry )
{
if( !m_Blocked.Contains( entry ) )
if ( !m_Blocked.Contains( entry ) )
m_Blocked.Add( entry );
Save();
@ -283,7 +264,7 @@ namespace Server
{
IFirewallEntry entry = ToFirewallEntry( pattern );
if( !m_Blocked.Contains( entry ) )
if ( !m_Blocked.Contains( entry ) )
m_Blocked.Add( entry );
Save();
@ -293,7 +274,7 @@ namespace Server
{
IFirewallEntry entry = new IPFirewallEntry( ip );
if( !m_Blocked.Contains( entry ) )
if ( !m_Blocked.Contains( entry ) )
m_Blocked.Add( entry );
Save();
@ -314,7 +295,7 @@ namespace Server
{
for( int i = 0; i < m_Blocked.Count; i++ )
{
if( m_Blocked[i].IsBlocked( ip ) )
if ( m_Blocked[i].IsBlocked( ip ) )
return true;
}
@ -332,7 +313,7 @@ namespace Server
contains = Utility.IPMatchCIDR( s, ip );
if( !contains )
if ( !contains )
contains = Utility.IPMatch( s, ip );
}
}
@ -341,4 +322,4 @@ namespace Server
* */
}
}
}
}