LLSDHelpers.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.Collections;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Xml;
  32. namespace OpenSim.Framework.Capabilities
  33. {
  34. public class LLSDHelpers
  35. {
  36. // private static readonly log4net.ILog m_log
  37. // = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  38. public static string SerialiseLLSDReply(object obj)
  39. {
  40. StringWriter sw = new StringWriter();
  41. XmlTextWriter writer = new XmlTextWriter(sw);
  42. writer.Formatting = Formatting.None;
  43. writer.WriteStartElement(String.Empty, "llsd", String.Empty);
  44. SerializeOSDType(writer, obj);
  45. writer.WriteEndElement();
  46. writer.Close();
  47. //m_log.DebugFormat("[LLSD Helpers]: Generated serialized LLSD reply {0}", sw.ToString());
  48. return sw.ToString();
  49. }
  50. private static void SerializeOSDType(XmlTextWriter writer, object obj)
  51. {
  52. Type myType = obj.GetType();
  53. LLSDType[] llsdattributes = (LLSDType[]) myType.GetCustomAttributes(typeof (LLSDType), false);
  54. if (llsdattributes.Length > 0)
  55. {
  56. switch (llsdattributes[0].ObjectType)
  57. {
  58. case "MAP":
  59. writer.WriteStartElement(String.Empty, "map", String.Empty);
  60. FieldInfo[] fields = myType.GetFields();
  61. for (int i = 0; i < fields.Length; i++)
  62. {
  63. if (fields[i] != null && fields[i].GetValue(obj) != null)
  64. {
  65. object fieldValue = fields[i].GetValue(obj);
  66. LLSDType[] fieldAttributes =
  67. (LLSDType[]) fieldValue.GetType().GetCustomAttributes(typeof (LLSDType), false);
  68. if (fieldAttributes.Length > 0)
  69. {
  70. writer.WriteStartElement(String.Empty, "key", String.Empty);
  71. string fieldName = fields[i].Name;
  72. fieldName = fieldName.Replace("___", "-");
  73. writer.WriteString(fieldName);
  74. writer.WriteEndElement();
  75. SerializeOSDType(writer, fieldValue);
  76. }
  77. else
  78. {
  79. writer.WriteStartElement(String.Empty, "key", String.Empty);
  80. string fieldName = fields[i].Name;
  81. fieldName = fieldName.Replace("___", "-");
  82. writer.WriteString(fieldName);
  83. writer.WriteEndElement();
  84. LLSD.LLSDWriteOne(writer, fieldValue);
  85. // OpenMetaverse.StructuredData.LLSDParser.SerializeXmlElement(
  86. // writer, OpenMetaverse.StructuredData.OSD.FromObject(fieldValue));
  87. }
  88. }
  89. else
  90. {
  91. // TODO from ADAM: There is a nullref being caused by fields[i] being null
  92. // on some computers. Unsure what is causing this, but would appreciate
  93. // if sdague could take a look at this.
  94. }
  95. }
  96. writer.WriteEndElement();
  97. break;
  98. case "ARRAY":
  99. // OSDArray arrayObject = obj as OSDArray;
  100. // ArrayList a = arrayObject.Array;
  101. ArrayList a = (ArrayList) obj.GetType().GetField("Array").GetValue(obj);
  102. if (a != null)
  103. {
  104. writer.WriteStartElement(String.Empty, "array", String.Empty);
  105. foreach (object item in a)
  106. {
  107. SerializeOSDType(writer, item);
  108. }
  109. writer.WriteEndElement();
  110. }
  111. break;
  112. }
  113. }
  114. else
  115. {
  116. LLSD.LLSDWriteOne(writer, obj);
  117. //OpenMetaverse.StructuredData.LLSDParser.SerializeXmlElement(
  118. // writer, OpenMetaverse.StructuredData.OSD.FromObject(obj));
  119. }
  120. }
  121. public static object DeserialiseOSDMap(Hashtable llsd, object obj)
  122. {
  123. Type myType = obj.GetType();
  124. LLSDType[] llsdattributes = (LLSDType[]) myType.GetCustomAttributes(typeof (LLSDType), false);
  125. if (llsdattributes.Length > 0)
  126. {
  127. switch (llsdattributes[0].ObjectType)
  128. {
  129. case "MAP":
  130. IDictionaryEnumerator enumerator = llsd.GetEnumerator();
  131. while (enumerator.MoveNext())
  132. {
  133. string keyName = (string)enumerator.Key;
  134. keyName = keyName.Replace("-","_");
  135. FieldInfo field = myType.GetField(keyName);
  136. if (field != null)
  137. {
  138. // if (enumerator.Value is OpenMetaverse.StructuredData.OSDMap)
  139. if (enumerator.Value is Hashtable)
  140. {
  141. object fieldValue = field.GetValue(obj);
  142. DeserialiseOSDMap((Hashtable) enumerator.Value, fieldValue);
  143. // DeserialiseOSDMap((OpenMetaverse.StructuredData.OSDMap) enumerator.Value, fieldValue);
  144. }
  145. else if (enumerator.Value is ArrayList)
  146. {
  147. object fieldValue = field.GetValue(obj);
  148. fieldValue.GetType().GetField("Array").SetValue(fieldValue, enumerator.Value);
  149. //TODO
  150. // the LLSD map/array types in the array need to be deserialised
  151. // but first we need to know the right class to deserialise them into.
  152. }
  153. else if(enumerator.Value is Boolean && field.FieldType == typeof(int) )
  154. {
  155. int i = (bool)enumerator.Value ? 1 : 0;
  156. field.SetValue(obj, (object)i);
  157. }
  158. else
  159. {
  160. field.SetValue(obj, enumerator.Value);
  161. }
  162. }
  163. }
  164. break;
  165. }
  166. }
  167. return obj;
  168. }
  169. }
  170. }