#region Arthea License /*********************************************************************** * Arthea MUD by R. Jennings (2007) http://arthea.googlecode.com/ * * By using this code you comply with the Artistic and GPLv2 Licenses. * ***********************************************************************/ #endregion using System.Collections.Generic; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; using Arthea.Connections.Players; using Arthea.Creation; using Arthea.Environment; using Arthea.Interfaces; namespace Arthea.Classes { /// <summary> /// Implementation of byte values for each class. /// </summary> public class ClassValues : Dictionary<string, byte>, IXmlSerializable, CustomEditType { #region [rgn] Properties (2) /// <summary> /// Gets or sets the value for specified name. /// </summary> /// <value>the byte value</value> public new byte this[string name] { get { try { return base[name]; } catch { return 0; } } set { if (value == 0) { Remove(name); } else if (ContainsKey(name)) { base[name] = value; } else { Add(name, value); } } } /// <summary> /// Gets or sets the <see cref="System.Byte"/> specified by class. /// </summary> /// <value></value> public byte this[Class @class] { get { return this[@class.Name]; } set { this[@class] = value; } } #endregion [rgn] #region CustomEditType Members /// <summary> /// Sets the value for a class. /// </summary> /// <param name="player">The player.</param> /// <param name="editer">Editer information.</param> /// <param name="argument">The argument.</param> public void CustomEdit(Player player, OlcField editer, String argument) { String arg = argument.FirstArg(); Class @class = Lists.Classes.FindName(arg); byte level; if (!byte.TryParse(argument, out level)) { player.WriteLine("That is not a valid value."); return; } this[@class.Name] = level; player.WriteLine("{0} set to {1}.", @class.Name, level); } #endregion #region IXmlSerializable Members ///<summary> ///This property is reserved, apply the <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute"></see> to the class instead. ///</summary> /// ///<returns> ///An <see cref="T:System.Xml.Schema.XmlSchema"></see> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)"></see> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)"></see> method. ///</returns> /// public XmlSchema GetSchema() { return null; } ///<summary> ///Generates an object from its XML representation. ///</summary> /// ///<param name="reader">The <see cref="T:System.Xml.XmlReader"></see> stream from which the object is deserialized. </param> public void ReadXml(XmlReader reader) { if (reader.IsEmptyElement) return; reader.ReadStartElement(); while (reader.IsStartElement()) { reader.ReadStartElement(); Add(reader.GetAttribute("name"), byte.Parse(reader.GetAttribute("value"))); reader.ReadEndElement(); } reader.ReadEndElement(); } ///<summary> ///Converts an object into its XML representation. ///</summary> /// ///<param name="writer">The <see cref="T:System.Xml.XmlWriter"></see> stream to which the object is serialized. </param> public void WriteXml(XmlWriter writer) { if (Count == 0) return; foreach (KeyValuePair<string, byte> entry in this) { writer.WriteStartElement("ClassVal"); writer.WriteAttributeString("name", entry.Key); writer.WriteAttributeString("value", entry.Value.ToString()); writer.WriteEndElement(); } } #endregion } }