Bug in BodyTable, Bodies with an ID over 1000 we not being properly read.

Using Enum.TryParse where applicable
This commit is contained in:
asayre 2012-01-02 14:21:20 +00:00
parent 26899493c0
commit b9f0138b08
5 changed files with 30 additions and 45 deletions

View file

@ -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_Flags = Utility.GetXMLInt32( Utility.GetText( node["flags"], "0" ), 0 );
m_Created = Utility.GetXMLDateTime( Utility.GetText( node["created"], null ), DateTime.Now ); m_Created = Utility.GetXMLDateTime( Utility.GetText( node["created"], null ), DateTime.Now );
m_LastLogin = Utility.GetXMLDateTime( Utility.GetText( node["lastLogin"], null ), DateTime.Now ); m_LastLogin = Utility.GetXMLDateTime( Utility.GetText( node["lastLogin"], null ), DateTime.Now );

View file

@ -25,16 +25,15 @@ namespace Server.Commands
else else
{ {
SkillName skill; 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. 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 else
{ {
SkillName skill; 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." );
}
} }
} }

View file

@ -122,17 +122,7 @@ namespace Server.Misc
{ {
SkillName index; SkillName index;
try if( !Enum.TryParse( name, true, out index ) || (!Core.SE && (int)index > 51) || (!Core.AOS && (int)index > 48) )
{
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 ) )
{ {
from.SendLocalizedMessage( 1005631 ); // You have specified an invalid skill to set. from.SendLocalizedMessage( 1005631 ); // You have specified an invalid skill to set.
return; return;

View file

@ -45,7 +45,7 @@ namespace Server
{ {
using ( StreamReader ip = new StreamReader( "Data/bodyTable.cfg" ) ) using ( StreamReader ip = new StreamReader( "Data/bodyTable.cfg" ) )
{ {
m_Types = new BodyType[1000]; m_Types = new BodyType[0x1000];
string line; string line;
@ -56,15 +56,14 @@ namespace Server
string[] split = line.Split( '\t' ); string[] split = line.Split( '\t' );
try BodyType type;
{ int bodyID;
int bodyID = int.Parse( split[0] );
BodyType type = (BodyType)Enum.Parse( typeof( BodyType ), split[1], true );
if ( bodyID >= 0 && bodyID < m_Types.Length ) if( int.TryParse( split[0], out bodyID ) && Enum.TryParse( split[1], true, out type ) && bodyID >= 0 && bodyID < m_Types.Length )
m_Types[bodyID] = type; {
m_Types[bodyID] = type;
} }
catch else
{ {
Console.WriteLine( "Warning: Invalid bodyTable entry:" ); Console.WriteLine( "Warning: Invalid bodyTable entry:" );
Console.WriteLine( line ); Console.WriteLine( line );

View file

@ -1126,32 +1126,30 @@ namespace Server
return true; return true;
} }
public static bool ReadEnum<T>( XmlElement xml, string attribute, ref T value ) public static bool ReadEnum<T>( XmlElement xml, string attribute, ref T value ) where T : struct
{ {
return ReadEnum( xml, attribute, ref value, true ); return ReadEnum( xml, attribute, ref value, true );
} }
public static bool ReadEnum<T>( XmlElement xml, string attribute, ref T value, bool mandatory ) public static bool ReadEnum<T>( XmlElement xml, string attribute, ref T value, bool mandatory ) where T : struct
{ {
string s = GetAttribute( xml, attribute, mandatory ); string s = GetAttribute( xml, attribute, mandatory );
if ( s == null ) if ( s == null )
return false; return false;
Type type = typeof(T); T tempVal;
try if( Enum.TryParse( s, true, out tempVal ) )
{ {
value = (T)Enum.Parse(type, s, true); value = tempVal;
//TODO: On .NET 4.0, use Enum.TryParse 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 false;
} }
return true;
} }
public static bool ReadMap( XmlElement xml, string attribute, ref Map value ) public static bool ReadMap( XmlElement xml, string attribute, ref Map value )