LLSDHelpers.cs 10 KB

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