12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
using System; using System.IO; using System.Xml; using System.Collections; using Server; namespace Server { public class NameList { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private string m_Type; private string[] m_List; public string Type{ get{ return m_Type; } } public string[] List{ get{ return m_List; } } public NameList( string type, XmlElement xml ) { m_Type = type; m_List = xml.InnerText.Split( ',' ); for (int i = m_List.Length - 1; i >= 0; --i) m_List[i] = m_List[i].Trim(); } public string GetRandomName() { if ( m_List.Length > 0 ) return m_List[Utility.Random( m_List.Length )]; return ""; } public static NameList GetNameList( string type ) { return (NameList)m_Table[type]; } public static string RandomName( string type ) { NameList list = GetNameList( type ); if ( list != null ) return list.GetRandomName(); return ""; } private static Hashtable m_Table; static NameList() { m_Table = new Hashtable( CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default ); string filePath = Path.Combine(Core.Config.ConfigDirectory, "names.xml"); if ( !File.Exists( filePath ) ) return; try { Load( filePath ); } catch ( Exception e ) { log.Error( "Warning: Exception caught loading name lists", e ); } } private static void Load( string filePath ) { XmlDocument doc = new XmlDocument(); doc.Load( filePath ); XmlElement root = doc["names"]; foreach ( XmlElement element in root.GetElementsByTagName( "namelist" ) ) { string type = element.GetAttribute( "type" ); if ( type == null || type == String.Empty ) continue; try { NameList list = new NameList( type, element ); m_Table[type] = list; } catch { } } } } }