From b9f0138b084a2e4d15fa1711efa9045902a8638e Mon Sep 17 00:00:00 2001 From: asayre Date: Mon, 2 Jan 2012 14:21:20 +0000 Subject: [PATCH] Bug in BodyTable, Bodies with an ID over 1000 we not being properly read. Using Enum.TryParse where applicable --- Scripts/Accounting/Account.cs | 3 ++- Scripts/Commands/Skills.cs | 27 +++++++++----------- Scripts/SpecialSystems/Engines/TestCenter.cs | 12 +-------- Server/Body.cs | 15 +++++------ Server/Region.cs | 18 ++++++------- 5 files changed, 30 insertions(+), 45 deletions(-) diff --git a/Scripts/Accounting/Account.cs b/Scripts/Accounting/Account.cs index c7e614ba2..b0199d785 100644 --- a/Scripts/Accounting/Account.cs +++ b/Scripts/Accounting/Account.cs @@ -666,7 +666,8 @@ namespace Server.Accounting } } - m_AccessLevel = (AccessLevel)Enum.Parse( typeof( AccessLevel ), Utility.GetText( node["accessLevel"], "Player" ), true ); + Enum.TryParse( Utility.GetText( node["accessLevel"], "Player" ), true, out m_AccessLevel ); + m_Flags = Utility.GetXMLInt32( Utility.GetText( node["flags"], "0" ), 0 ); m_Created = Utility.GetXMLDateTime( Utility.GetText( node["created"], null ), DateTime.Now ); m_LastLogin = Utility.GetXMLDateTime( Utility.GetText( node["lastLogin"], null ), DateTime.Now ); diff --git a/Scripts/Commands/Skills.cs b/Scripts/Commands/Skills.cs index 67fc93990..bcca1bf34 100644 --- a/Scripts/Commands/Skills.cs +++ b/Scripts/Commands/Skills.cs @@ -25,16 +25,15 @@ namespace Server.Commands else { SkillName skill; - try + + if( Enum.TryParse( arg.GetString( 0 ), true, out skill ) ) { - skill = (SkillName)Enum.Parse( typeof( SkillName ), arg.GetString( 0 ), true ); + arg.Mobile.Target = new SkillTarget( skill, arg.GetDouble( 1 ) ); } - catch + else { arg.Mobile.SendLocalizedMessage( 1005631 ); // You have specified an invalid skill to set. - return; } - arg.Mobile.Target = new SkillTarget( skill, arg.GetDouble( 1 ) ); } } @@ -63,17 +62,15 @@ namespace Server.Commands else { SkillName skill; - try - { - skill = (SkillName)Enum.Parse( typeof( SkillName ), arg.GetString( 0 ), true ); - } - catch - { - arg.Mobile.SendLocalizedMessage( 1005631 ); // You have specified an invalid skill to set. - return; - } - arg.Mobile.Target = new SkillTarget( skill ); + if( Enum.TryParse( arg.GetString( 0 ), true, out skill ) ) + { + arg.Mobile.Target = new SkillTarget( skill ); + } + else + { + arg.Mobile.SendMessage( "You have specified an invalid skill to get." ); + } } } diff --git a/Scripts/SpecialSystems/Engines/TestCenter.cs b/Scripts/SpecialSystems/Engines/TestCenter.cs index 44d1cc07d..588790ef5 100644 --- a/Scripts/SpecialSystems/Engines/TestCenter.cs +++ b/Scripts/SpecialSystems/Engines/TestCenter.cs @@ -122,17 +122,7 @@ namespace Server.Misc { SkillName index; - try - { - index = (SkillName)Enum.Parse( typeof( SkillName ), name, true ); - } - catch - { - from.SendLocalizedMessage( 1005631 ); // You have specified an invalid skill to set. - return; - } - - if ( ( !Core.SE && (int)index > 51 ) || ( !Core.AOS && (int)index > 48 ) ) + if( !Enum.TryParse( name, true, out index ) || (!Core.SE && (int)index > 51) || (!Core.AOS && (int)index > 48) ) { from.SendLocalizedMessage( 1005631 ); // You have specified an invalid skill to set. return; diff --git a/Server/Body.cs b/Server/Body.cs index 3027868d9..e2a7c7c0a 100644 --- a/Server/Body.cs +++ b/Server/Body.cs @@ -45,7 +45,7 @@ namespace Server { using ( StreamReader ip = new StreamReader( "Data/bodyTable.cfg" ) ) { - m_Types = new BodyType[1000]; + m_Types = new BodyType[0x1000]; string line; @@ -56,15 +56,14 @@ namespace Server string[] split = line.Split( '\t' ); - try - { - int bodyID = int.Parse( split[0] ); - BodyType type = (BodyType)Enum.Parse( typeof( BodyType ), split[1], true ); + BodyType type; + int bodyID; - if ( bodyID >= 0 && bodyID < m_Types.Length ) - m_Types[bodyID] = type; + if( int.TryParse( split[0], out bodyID ) && Enum.TryParse( split[1], true, out type ) && bodyID >= 0 && bodyID < m_Types.Length ) + { + m_Types[bodyID] = type; } - catch + else { Console.WriteLine( "Warning: Invalid bodyTable entry:" ); Console.WriteLine( line ); diff --git a/Server/Region.cs b/Server/Region.cs index 195b726c4..a7c32c74c 100644 --- a/Server/Region.cs +++ b/Server/Region.cs @@ -1126,32 +1126,30 @@ namespace Server return true; } - public static bool ReadEnum( XmlElement xml, string attribute, ref T value ) + public static bool ReadEnum( XmlElement xml, string attribute, ref T value ) where T : struct { return ReadEnum( xml, attribute, ref value, true ); } - public static bool ReadEnum( XmlElement xml, string attribute, ref T value, bool mandatory ) + public static bool ReadEnum( XmlElement xml, string attribute, ref T value, bool mandatory ) where T : struct { string s = GetAttribute( xml, attribute, mandatory ); if ( s == null ) return false; - Type type = typeof(T); + T tempVal; - try + if( Enum.TryParse( s, true, out tempVal ) ) { - value = (T)Enum.Parse(type, s, true); - //TODO: On .NET 4.0, use Enum.TryParse + value = tempVal; + return true; } - catch + else { - Console.WriteLine( "Could not parse {0} enum attribute '{1}' in element '{2}'", type, attribute, xml.Name ); + Console.WriteLine( "Could not parse {0} enum attribute '{1}' in element '{2}'", typeof(T), attribute, xml.Name ); return false; } - - return true; } public static bool ReadMap( XmlElement xml, string attribute, ref Map value )