UserInventoryItemSerializer.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 OpenSimulator 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.IO;
  29. using System.Text;
  30. using System.Xml;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. namespace OpenSim.Framework.Serialization.External
  34. {
  35. /// <summary>
  36. /// Serialize and deserialize user inventory items as an external format.
  37. /// </summary>
  38. /// XXX: Please do not use yet.
  39. public class UserInventoryItemSerializer
  40. {
  41. /// <summary>
  42. /// Deserialize item
  43. /// </summary>
  44. /// <param name="serializedSettings"></param>
  45. /// <returns></returns>
  46. /// <exception cref="System.Xml.XmlException"></exception>
  47. public static InventoryItemBase Deserialize(byte[] serialization)
  48. {
  49. return Deserialize(Encoding.ASCII.GetString(serialization, 0, serialization.Length));
  50. }
  51. /// <summary>
  52. /// Deserialize settings
  53. /// </summary>
  54. /// <param name="serializedSettings"></param>
  55. /// <returns></returns>
  56. /// <exception cref="System.Xml.XmlException"></exception>
  57. public static InventoryItemBase Deserialize(string serialization)
  58. {
  59. InventoryItemBase item = new InventoryItemBase();
  60. StringReader sr = new StringReader(serialization);
  61. XmlTextReader xtr = new XmlTextReader(sr);
  62. xtr.ReadStartElement("InventoryItem");
  63. item.Name = xtr.ReadElementString("Name");
  64. item.ID = UUID.Parse( xtr.ReadElementString("ID"));
  65. item.InvType = Convert.ToInt32( xtr.ReadElementString("InvType"));
  66. item.CreatorId = xtr.ReadElementString("CreatorUUID");
  67. item.CreationDate = Convert.ToInt32( xtr.ReadElementString("CreationDate"));
  68. item.Owner = UUID.Parse( xtr.ReadElementString("Owner"));
  69. item.Description = xtr.ReadElementString("Description");
  70. item.AssetType = Convert.ToInt32( xtr.ReadElementString("AssetType"));
  71. item.AssetID = UUID.Parse( xtr.ReadElementString("AssetID"));
  72. item.SaleType = Convert.ToByte( xtr.ReadElementString("SaleType"));
  73. item.SalePrice = Convert.ToInt32( xtr.ReadElementString("SalePrice"));
  74. item.BasePermissions = Convert.ToUInt32( xtr.ReadElementString("BasePermissions"));
  75. item.CurrentPermissions = Convert.ToUInt32( xtr.ReadElementString("CurrentPermissions"));
  76. item.EveryOnePermissions = Convert.ToUInt32( xtr.ReadElementString("EveryOnePermissions"));
  77. item.NextPermissions = Convert.ToUInt32( xtr.ReadElementString("NextPermissions"));
  78. item.Flags = Convert.ToUInt32( xtr.ReadElementString("Flags"));
  79. item.GroupID = UUID.Parse( xtr.ReadElementString("GroupID"));
  80. item.GroupOwned = Convert.ToBoolean(xtr.ReadElementString("GroupOwned"));
  81. xtr.ReadEndElement();
  82. xtr.Close();
  83. sr.Close();
  84. return item;
  85. }
  86. public static string Serialize(InventoryItemBase inventoryItem)
  87. {
  88. StringWriter sw = new StringWriter();
  89. XmlTextWriter writer = new XmlTextWriter(sw);
  90. writer.Formatting = Formatting.Indented;
  91. writer.WriteStartDocument();
  92. writer.WriteStartElement("InventoryItem");
  93. writer.WriteStartElement("Name");
  94. writer.WriteString(inventoryItem.Name);
  95. writer.WriteEndElement();
  96. writer.WriteStartElement("ID");
  97. writer.WriteString(inventoryItem.ID.ToString());
  98. writer.WriteEndElement();
  99. writer.WriteStartElement("InvType");
  100. writer.WriteString(inventoryItem.InvType.ToString());
  101. writer.WriteEndElement();
  102. writer.WriteStartElement("CreatorUUID");
  103. writer.WriteString(inventoryItem.CreatorId);
  104. writer.WriteEndElement();
  105. writer.WriteStartElement("CreationDate");
  106. writer.WriteString(inventoryItem.CreationDate.ToString());
  107. writer.WriteEndElement();
  108. writer.WriteStartElement("Owner");
  109. writer.WriteString(inventoryItem.Owner.ToString());
  110. writer.WriteEndElement();
  111. writer.WriteStartElement("Description");
  112. writer.WriteString(inventoryItem.Description);
  113. writer.WriteEndElement();
  114. writer.WriteStartElement("AssetType");
  115. writer.WriteString(inventoryItem.AssetType.ToString());
  116. writer.WriteEndElement();
  117. writer.WriteStartElement("AssetID");
  118. writer.WriteString(inventoryItem.AssetID.ToString());
  119. writer.WriteEndElement();
  120. writer.WriteStartElement("SaleType");
  121. writer.WriteString(inventoryItem.SaleType.ToString());
  122. writer.WriteEndElement();
  123. writer.WriteStartElement("SalePrice");
  124. writer.WriteString(inventoryItem.SalePrice.ToString());
  125. writer.WriteEndElement();
  126. writer.WriteStartElement("BasePermissions");
  127. writer.WriteString(inventoryItem.BasePermissions.ToString());
  128. writer.WriteEndElement();
  129. writer.WriteStartElement("CurrentPermissions");
  130. writer.WriteString(inventoryItem.CurrentPermissions.ToString());
  131. writer.WriteEndElement();
  132. writer.WriteStartElement("EveryOnePermissions");
  133. writer.WriteString(inventoryItem.EveryOnePermissions.ToString());
  134. writer.WriteEndElement();
  135. writer.WriteStartElement("NextPermissions");
  136. writer.WriteString(inventoryItem.NextPermissions.ToString());
  137. writer.WriteEndElement();
  138. writer.WriteStartElement("Flags");
  139. writer.WriteString(inventoryItem.Flags.ToString());
  140. writer.WriteEndElement();
  141. writer.WriteStartElement("GroupID");
  142. writer.WriteString(inventoryItem.GroupID.ToString());
  143. writer.WriteEndElement();
  144. writer.WriteStartElement("GroupOwned");
  145. writer.WriteString(inventoryItem.GroupOwned.ToString());
  146. writer.WriteEndElement();
  147. writer.WriteEndElement();
  148. writer.Close();
  149. sw.Close();
  150. return sw.ToString();
  151. }
  152. }
  153. }