/// <summary> /// Create the info tree from this struct /// </summary> /// <returns>the info tree</returns> public ItemInfoTree ToInfoTree() { ItemInfoTree tree = new ItemInfoTree(); foreach (var kvpair in IntValues) { tree.WriteInt(kvpair.key, kvpair.value); } foreach (var kvpair in StringValues) { tree.WriteString(kvpair.key, kvpair.value); } foreach (var kvpair in DoubleValues) { tree.WriteDouble(kvpair.key, kvpair.value); } foreach (var kvpair in IntArrayValues) { tree.WriteIntArray(kvpair.key, kvpair.value.ToArray()); } foreach (var kvpair in TreeValues) { tree.WriteTree(kvpair.key, kvpair.value.ToInfoTree()); } return(tree); }
/// <summary> /// Two trees are equal if they have exactly the same elements. (Arrays are compared with sequence euqal) /// </summary> /// <param name="obj">other object(tree)</param> /// <returns></returns> public override bool Equals(object obj) { if (!(obj is ItemInfoTree)) { return(false); } ItemInfoTree otherTree = (ItemInfoTree)obj; if (this.dictionary.Count != otherTree.dictionary.Count) { return(false); } foreach (string key in this.dictionary.Keys) { if (!otherTree.dictionary.ContainsKey(key)) { return(false); } object value1 = otherTree.dictionary[key]; object value2 = this.dictionary[key]; if (value1 is int[] && value2 is int[] && !Enumerable.SequenceEqual((int[])value1, (int[])value2)) { return(false); } else if (!(value1 is int[]) && !value1.Equals(value2)) { return(false); } } return(true); }
/// <summary> /// read tree properties from xml file /// </summary> /// <param name="reader"></param> public void ReadXml(XmlReader reader) { Console.WriteLine("start read"); reader.MoveToContent(); reader.ReadStartElement("infoTree"); while (reader.IsStartElement()) { Console.WriteLine("type: " + reader.GetAttribute("type") + " key: " + reader.GetAttribute("key")); string key = reader.GetAttribute("key"); string type = reader.GetAttribute("type"); reader.ReadStartElement(); switch (type) { case "int": int intVal = reader.ReadContentAsInt(); WriteInt(key, intVal); Console.WriteLine(intVal); break; case "text": string value = reader.ReadContentAsString(); WriteString(key, value); Console.WriteLine(value); break; case "double": double doubleVal = reader.ReadContentAsDouble(); WriteDouble(key, doubleVal); Console.WriteLine(doubleVal); break; case "intArray": List <int> intList = new List <int>(); while (reader.IsStartElement()) { reader.ReadStartElement("value"); int elem = reader.ReadContentAsInt(); Console.WriteLine(elem); intList.Add(elem); reader.ReadEndElement(); } WriteIntArray(key, intList.ToArray()); break; case "tree": ItemInfoTree subTree = new ItemInfoTree(); subTree.ReadXml(reader); WriteTree(key, subTree); break; } reader.ReadEndElement(); } reader.ReadEndElement(); }
/// <summary> /// Get the ids and powers of all attached abilities /// </summary> /// <returns>a list of key value pair representing the id and power of the ability</returns> public List <KeyValuePair <string, int> > GetAbilityIdPowers() { ItemInfoTree abilityTree = infoTree.ReadTree("abilities"); if (abilityTree == null) { return(new List <KeyValuePair <string, int> >()); } List <KeyValuePair <string, int> > abilities = abilityTree.dictionary .Where(kvp => kvp.Value is int) .Select(kvp => new KeyValuePair <string, int>(kvp.Key, (int)kvp.Value)).ToList(); return(abilities); }
public ItemInfoTree(ItemInfoTree other) { foreach (string key in other.dictionary.Keys) { object value = other.dictionary[key]; if (value is ICloneable) { dictionary[key] = ((ICloneable)value).Clone(); } else { dictionary[key] = value; } } }
public ItemStack(int id, int size = 1) { itemId = id; stackSize = size; infoTree = new ItemInfoTree(); Item item = GetItem(); if (item == null) { stackSize = 0; } infoTree.WriteString("description", ""); item.OnCreate(this); }
/// <summary> /// Update the current tree with some other info tree /// </summary> /// <param name="other"></param> public void UpdateWith(ItemInfoTree other) { other.dictionary.ToList().ForEach(x => this.dictionary[x.Key] = x.Value); }
public void WriteTree(string key, ItemInfoTree tree) { dictionary[key] = tree; }