LLSDHelpers.cs 9.9 KB

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