From 071898373e92af12dcad81dbd8bd8c9c89aaa737 Mon Sep 17 00:00:00 2001 From: mark Date: Sat, 24 Jun 2006 19:53:26 +0000 Subject: [PATCH] generics, cleanups --- Scripts/Accounting/Account.cs | 8 ++---- Scripts/Accounting/AccountHandler.cs | 40 ++++++++++---------------- Scripts/Spells/Base/SpecialMove.cs | 25 ++++++++-------- Scripts/Spells/Base/SpellRegistry.cs | 34 +++++++++------------- Scripts/Spells/Ninjitsu/MirrorImage.cs | 27 +++++++++-------- 5 files changed, 58 insertions(+), 76 deletions(-) diff --git a/Scripts/Accounting/Account.cs b/Scripts/Accounting/Account.cs index d401530ce..3e8b6849d 100644 --- a/Scripts/Accounting/Account.cs +++ b/Scripts/Accounting/Account.cs @@ -909,12 +909,10 @@ namespace Server.Accounting return; if ( m_LoginIPs.Length == 0 ) - { - if ( AccountHandler.IPTables[ipAddress] == null ) - AccountHandler.IPTables[ipAddress] = 1; + if ( AccountHandler.IPTable.ContainsKey( ipAddress ) ) + AccountHandler.IPTable[ipAddress]++; else - AccountHandler.IPTables[ipAddress] = (int) AccountHandler.IPTables[ipAddress] + 1; - } + AccountHandler.IPTable[ipAddress] = 1; bool contains = false; diff --git a/Scripts/Accounting/AccountHandler.cs b/Scripts/Accounting/AccountHandler.cs index 049e20efc..85464c6b8 100644 --- a/Scripts/Accounting/AccountHandler.cs +++ b/Scripts/Accounting/AccountHandler.cs @@ -1,12 +1,12 @@ using System; +using System.Collections.Generic; using System.IO; using System.Net; using Server; -using Server.Network; using Server.Accounting; -using Server.Engines.Help; -using System.Collections; using Server.Commands; +using Server.Engines.Help; +using Server.Network; namespace Server.Misc { @@ -205,45 +205,35 @@ namespace Server.Misc public static bool CanCreate( IPAddress ip ) { - if ( IPTables[ip] == null ) //Sanity + if ( !IPTable.ContainsKey( ip ) ) return true; - - - return ((int)IPTables[ip] < MaxAccountsPerIP); - - /* - if ( (int)IPTables[ip] >= MaxAccountsPerIP ) - return false; - else - return true; - */ + + return ( IPTable[ip] < MaxAccountsPerIP ); } - private static Hashtable m_IPTables; + private static Dictionary m_IPTable; - public static Hashtable IPTables + public static Dictionary IPTable { get { - if ( m_IPTables == null ) + if ( m_IPTable == null ) { - m_IPTables = new Hashtable(); + m_IPTable = new Dictionary(); foreach ( Account a in Accounts.GetAccounts() ) - { if ( a.LoginIPs.Length > 0 ) { IPAddress ip = a.LoginIPs[0]; - if ( m_IPTables[ip] == null ) - m_IPTables[ip] = 1; + if ( m_IPTable.ContainsKey( ip ) ) + m_IPTable[ip]++; else - m_IPTables[ip] = (int)m_IPTables[ip] + 1; + m_IPTable[ip] = 1; } - } } - return m_IPTables; + return m_IPTable; } } @@ -299,7 +289,7 @@ namespace Server.Misc if ( acct == null ) { - if ( AutoAccountCreation && un.Trim().Length > 0 ) //To prevent someone from mkaing an account of just '' or a bunch of meaningless spaces + if ( AutoAccountCreation && un.Trim().Length > 0 ) //To prevent someone from making an account of just '' or a bunch of meaningless spaces { e.State.Account = acct = CreateAccount( e.State, un, pw ); e.Accepted = acct == null ? false : acct.CheckAccess( e.State ); diff --git a/Scripts/Spells/Base/SpecialMove.cs b/Scripts/Spells/Base/SpecialMove.cs index 9410dc735..9a9f81837 100644 --- a/Scripts/Spells/Base/SpecialMove.cs +++ b/Scripts/Spells/Base/SpecialMove.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using Server; using Server.Items; using Server.Mobiles; @@ -165,17 +165,15 @@ namespace Server.Spells m.CheckSkill( MoveSkill, RequiredSkill, RequiredSkill + 37.5 ); } - private static Hashtable m_Table = new Hashtable(); + private static Dictionary m_Table = new Dictionary(); - public static Hashtable Table{ get{ return m_Table; } } + public static Dictionary Table{ get{ return m_Table; } } public static void ClearAllMoves( Mobile m ) { - foreach ( DictionaryEntry de in SpellRegistry.SpecialMoves ) + foreach ( KeyValuePair kvp in SpellRegistry.SpecialMoves ) { - SpecialMove move = (SpecialMove)de.Value; - - int moveID = SpellRegistry.GetRegistryNumber( move ); + int moveID = kvp.Key; if ( moveID != -1 ) m.Send( new ToggleSpecialAbility( moveID + 1, false ) ); @@ -195,7 +193,8 @@ namespace Server.Spells return null; } - SpecialMove move = (SpecialMove)m_Table[m]; + SpecialMove move = null; + m_Table.TryGetValue( m, out move ); if ( move != null && move.ValidatesDuringHit && !move.Validate( m ) ) { @@ -248,7 +247,8 @@ namespace Server.Spells public static void ClearCurrentMove( Mobile m ) { - SpecialMove move = (SpecialMove)m_Table[m]; + SpecialMove move = null; + m_Table.TryGetValue( m, out move ); if ( move != null ) { @@ -268,7 +268,7 @@ namespace Server.Spells } - private static Hashtable m_PlayersTable = new Hashtable(); + private static Dictionary m_PlayersTable = new Dictionary(); private static void AddContext( Mobile m, SpecialMoveContext context ) { @@ -289,12 +289,13 @@ namespace Server.Spells private static SpecialMoveContext GetContext( Mobile m ) { - return ( m_PlayersTable[m] as SpecialMoveContext ); + return ( m_PlayersTable.ContainsKey( m ) ? m_PlayersTable[m] : null ); } public static bool GetContext( Mobile m, Type type ) { - SpecialMoveContext context = m_PlayersTable[m] as SpecialMoveContext; + SpecialMoveContext context = null; + m_PlayersTable.TryGetValue( m, out context ); if ( context == null ) return false; diff --git a/Scripts/Spells/Base/SpellRegistry.cs b/Scripts/Spells/Base/SpellRegistry.cs index 7e87bd860..bd66d354c 100644 --- a/Scripts/Spells/Base/SpellRegistry.cs +++ b/Scripts/Spells/Base/SpellRegistry.cs @@ -1,10 +1,10 @@ using System; +using System.Collections.Generic; using System.IO; -using Server.Items; -using System.Collections; -using Server.Spells.Necromancy; -using Server.Spells.Chivalry; using Server.Spells.Bushido; +using Server.Spells.Chivalry; +using Server.Items; +using Server.Spells.Necromancy; using Server.Spells.Ninjitsu; namespace Server.Spells @@ -33,16 +33,19 @@ namespace Server.Spells m_Count = 0; for ( int i = 0; i < m_Types.Length; ++i ) - { if ( m_Types[i] != null ) ++m_Count; - } } return m_Count; } } + private static Dictionary m_IDsFromTypes = new Dictionary( m_Types.Length ); + + private static Dictionary m_SpecialMoves = new Dictionary(); + public static Dictionary SpecialMoves { get { return m_SpecialMoves; } } + public static int GetRegistryNumber( ISpell s ) { return GetRegistryNumber( s.GetType() ); @@ -53,20 +56,14 @@ namespace Server.Spells return GetRegistryNumber( s.GetType() ); } - private static Hashtable m_IDsFromTypes = new Hashtable( m_Types.Length ); - public static int GetRegistryNumber( Type type ) { - if( m_IDsFromTypes.Contains( type ) ) - return (int)m_IDsFromTypes[type]; + if( m_IDsFromTypes.ContainsKey( type ) ) + return m_IDsFromTypes[type]; return -1; } - private static Hashtable m_SpecialMoves = new Hashtable(); - - public static Hashtable SpecialMoves { get { return m_SpecialMoves; } } - public static void Register( int spellID, Type type ) { if ( spellID < 0 || spellID >= m_Types.Length ) @@ -77,7 +74,7 @@ namespace Server.Spells m_Types[spellID] = type; - if( m_IDsFromTypes[type] == null ) + if( !m_IDsFromTypes.ContainsKey( type ) ) m_IDsFromTypes.Add( type, spellID ); if( type.IsSubclassOf( typeof( SpecialMove ) ) ) @@ -104,13 +101,10 @@ namespace Server.Spells Type t = m_Types[spellID]; - if ( t == null || !t.IsSubclassOf( typeof( SpecialMove ) ) ) //Ensure correct registration + if ( t == null || !t.IsSubclassOf( typeof( SpecialMove ) ) || !m_SpecialMoves.ContainsKey( spellID ) ) return null; - if( m_SpecialMoves.ContainsKey( spellID ) ) - return m_SpecialMoves[spellID] as SpecialMove; - - return null; + return m_SpecialMoves[spellID]; } private static object[] m_Params = new object[2]; diff --git a/Scripts/Spells/Ninjitsu/MirrorImage.cs b/Scripts/Spells/Ninjitsu/MirrorImage.cs index fac6c494d..c58a31120 100644 --- a/Scripts/Spells/Ninjitsu/MirrorImage.cs +++ b/Scripts/Spells/Ninjitsu/MirrorImage.cs @@ -1,44 +1,43 @@ using System; -using System.Collections; +using System.Collections.Generic; using Server; +using Server.Items; +using Server.Mobiles; using Server.Spells; using Server.Spells.Necromancy; -using Server.Mobiles; -using Server.Items; -using Server.Spells.Ninjitsu; namespace Server.Spells.Ninjitsu { public class MirrorImage : NinjaSpell { - private static Hashtable m_CloneCount = new Hashtable(); + private static Dictionary m_CloneCount = new Dictionary(); public static bool HasClone( Mobile m ) { - return (m_CloneCount.Contains( m ) && ((int)m_CloneCount[m]) > 0); + return m_CloneCount.ContainsKey( m ); } public static void AddClone( Mobile m ) { - if( m == null ) + if ( m == null ) return; - if( m_CloneCount.Contains( m ) ) - m_CloneCount[m] = ((int)m_CloneCount[m] +1); + if ( m_CloneCount.ContainsKey( m ) ) + m_CloneCount[m]++; else - m_CloneCount.Add( m, 1 ); + m_CloneCount[m] = 1; } public static void RemoveClone( Mobile m ) { - if( m == null ) + if ( m == null ) return; - if( m_CloneCount.Contains( m ) ) + if ( m_CloneCount.ContainsKey( m ) ) { - m_CloneCount[m] = ((int)m_CloneCount[m] -1); + m_CloneCount[m]--; - if( ((int)m_CloneCount[m]) <= 0 ) + if ( m_CloneCount[m] == 0 ) m_CloneCount.Remove( m ); } }