TaskInventoryDictionary.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Xml;
  31. using System.Xml.Schema;
  32. using System.Xml.Serialization;
  33. using OpenMetaverse;
  34. using log4net;
  35. namespace OpenSim.Framework
  36. {
  37. /// <summary>
  38. /// A dictionary for task inventory.
  39. ///
  40. /// This class is not thread safe. Callers must synchronize on Dictionary methods.
  41. /// </summary>
  42. public class TaskInventoryDictionary : Dictionary<UUID, TaskInventoryItem>,
  43. ICloneable, IXmlSerializable
  44. {
  45. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private static XmlSerializer tiiSerializer = new XmlSerializer(typeof (TaskInventoryItem));
  47. #region ICloneable Members
  48. public Object Clone()
  49. {
  50. TaskInventoryDictionary clone = new TaskInventoryDictionary();
  51. lock (this)
  52. {
  53. foreach (UUID uuid in Keys)
  54. {
  55. clone.Add(uuid, (TaskInventoryItem) this[uuid].Clone());
  56. }
  57. }
  58. return clone;
  59. }
  60. #endregion
  61. // The alternative of simply serializing the list doesn't appear to work on mono, since
  62. // we get a
  63. //
  64. // System.TypeInitializationException: An exception was thrown by the type initializer for OpenSim.Framework.TaskInventoryDictionary ---> System.ArgumentOutOfRangeException: < 0
  65. // Parameter name: length
  66. // at System.String.Substring (Int32 startIndex, Int32 length) [0x00088] in /build/buildd/mono-1.2.4/mcs/class/corlib/System/String.cs:381
  67. // at System.Xml.Serialization.TypeTranslator.GetTypeData (System.Type runtimeType, System.String xmlDataType) [0x001f6] in /build/buildd/mono-1.2.4/mcs/class/System.XML/System.Xml.Serialization/TypeTranslator.cs:217
  68. // ...
  69. // private static XmlSerializer tiiSerializer
  70. // = new XmlSerializer(typeof(Dictionary<UUID, TaskInventoryItem>.ValueCollection));
  71. // see IXmlSerializable
  72. #region IXmlSerializable Members
  73. public XmlSchema GetSchema()
  74. {
  75. return null;
  76. }
  77. // see IXmlSerializable
  78. public void ReadXml(XmlReader reader)
  79. {
  80. // m_log.DebugFormat("[TASK INVENTORY]: ReadXml current node before actions, {0}", reader.Name);
  81. if (!reader.IsEmptyElement)
  82. {
  83. reader.Read();
  84. while (tiiSerializer.CanDeserialize(reader))
  85. {
  86. TaskInventoryItem item = (TaskInventoryItem) tiiSerializer.Deserialize(reader);
  87. Add(item.ItemID, item);
  88. //m_log.DebugFormat("[TASK INVENTORY]: Instanted prim item {0}, {1} from xml", item.Name, item.ItemID);
  89. }
  90. // m_log.DebugFormat("[TASK INVENTORY]: Instantiated {0} prim items in total from xml", Count);
  91. }
  92. // else
  93. // {
  94. // m_log.DebugFormat("[TASK INVENTORY]: Skipping empty element {0}", reader.Name);
  95. // }
  96. // For some .net implementations, this last read is necessary so that we advance beyond the end tag
  97. // of the element wrapping this object so that the rest of the serialization can complete normally.
  98. reader.Read();
  99. // m_log.DebugFormat("[TASK INVENTORY]: ReadXml current node after actions, {0}", reader.Name);
  100. }
  101. // see IXmlSerializable
  102. public void WriteXml(XmlWriter writer)
  103. {
  104. lock (this)
  105. {
  106. foreach (TaskInventoryItem item in Values)
  107. {
  108. tiiSerializer.Serialize(writer, item);
  109. }
  110. }
  111. //tiiSerializer.Serialize(writer, Values);
  112. }
  113. #endregion
  114. // see ICloneable
  115. }
  116. }