diff options
Diffstat (limited to 'OpenSim/Region/Capabilities/LLSDHelpers.cs')
-rw-r--r-- | OpenSim/Region/Capabilities/LLSDHelpers.cs | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/OpenSim/Region/Capabilities/LLSDHelpers.cs b/OpenSim/Region/Capabilities/LLSDHelpers.cs new file mode 100644 index 0000000..76d9345 --- /dev/null +++ b/OpenSim/Region/Capabilities/LLSDHelpers.cs | |||
@@ -0,0 +1,165 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://www.openmetaverse.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 | */ | ||
28 | using System; | ||
29 | using System.Collections; | ||
30 | using System.Collections.Generic; | ||
31 | using System.Text; | ||
32 | using System.IO; | ||
33 | using System.Xml; | ||
34 | using libsecondlife; | ||
35 | |||
36 | namespace OpenSim.Region.Capabilities | ||
37 | { | ||
38 | public class LLSDHelpers | ||
39 | { | ||
40 | public static string SerialiseLLSDReply(object obj) | ||
41 | { | ||
42 | StringWriter sw = new StringWriter(); | ||
43 | XmlTextWriter writer = new XmlTextWriter(sw); | ||
44 | writer.Formatting = Formatting.None; | ||
45 | writer.WriteStartElement(String.Empty, "llsd", String.Empty); | ||
46 | LLSDHelpers.SerializeLLSDType(writer, obj); | ||
47 | writer.WriteEndElement(); | ||
48 | writer.Close(); | ||
49 | return sw.ToString(); | ||
50 | } | ||
51 | |||
52 | public static void SerializeLLSDType(XmlTextWriter writer, object obj) | ||
53 | { | ||
54 | Type myType = obj.GetType(); | ||
55 | LLSDType[] llsdattributes = (LLSDType[])myType.GetCustomAttributes(typeof(LLSDType), false); | ||
56 | if (llsdattributes.Length > 0) | ||
57 | { | ||
58 | switch (llsdattributes[0].ObjectType) | ||
59 | { | ||
60 | case "MAP": | ||
61 | writer.WriteStartElement(String.Empty, "map", String.Empty); | ||
62 | System.Reflection.FieldInfo[] fields = myType.GetFields(); | ||
63 | for (int i = 0; i < fields.Length; i++) | ||
64 | { | ||
65 | object fieldValue = fields[i].GetValue(obj); | ||
66 | LLSDType[] fieldAttributes = (LLSDType[])fieldValue.GetType().GetCustomAttributes(typeof(LLSDType), false); | ||
67 | if (fieldAttributes.Length > 0) | ||
68 | { | ||
69 | writer.WriteStartElement(String.Empty, "key", String.Empty); | ||
70 | writer.WriteString(fields[i].Name); | ||
71 | writer.WriteEndElement(); | ||
72 | SerializeLLSDType(writer, fieldValue); | ||
73 | } | ||
74 | else | ||
75 | { | ||
76 | writer.WriteStartElement(String.Empty, "key", String.Empty); | ||
77 | writer.WriteString(fields[i].Name); | ||
78 | writer.WriteEndElement(); | ||
79 | LLSD.LLSDWriteOne(writer, fieldValue); | ||
80 | } | ||
81 | } | ||
82 | writer.WriteEndElement(); | ||
83 | break; | ||
84 | case "ARRAY": | ||
85 | // LLSDArray arrayObject = obj as LLSDArray; | ||
86 | // ArrayList a = arrayObject.Array; | ||
87 | ArrayList a = (ArrayList)obj.GetType().GetField("Array").GetValue(obj); | ||
88 | if (a != null) | ||
89 | { | ||
90 | writer.WriteStartElement(String.Empty, "array", String.Empty); | ||
91 | foreach (object item in a) | ||
92 | { | ||
93 | SerializeLLSDType(writer, item); | ||
94 | } | ||
95 | writer.WriteEndElement(); | ||
96 | } | ||
97 | break; | ||
98 | } | ||
99 | } | ||
100 | else | ||
101 | { | ||
102 | LLSD.LLSDWriteOne(writer, obj); | ||
103 | } | ||
104 | } | ||
105 | |||
106 | public static object DeserialiseLLSDMap(Hashtable llsd, object obj) | ||
107 | { | ||
108 | Type myType = obj.GetType(); | ||
109 | LLSDType[] llsdattributes = (LLSDType[])myType.GetCustomAttributes(typeof(LLSDType), false); | ||
110 | if (llsdattributes.Length > 0) | ||
111 | { | ||
112 | switch (llsdattributes[0].ObjectType) | ||
113 | { | ||
114 | case "MAP": | ||
115 | IDictionaryEnumerator enumerator = llsd.GetEnumerator(); | ||
116 | while (enumerator.MoveNext()) | ||
117 | { | ||
118 | System.Reflection.FieldInfo field = myType.GetField((string)enumerator.Key); | ||
119 | if (field != null) | ||
120 | { | ||
121 | if (enumerator.Value is Hashtable) | ||
122 | { | ||
123 | object fieldValue = field.GetValue(obj); | ||
124 | DeserialiseLLSDMap((Hashtable) enumerator.Value, fieldValue); | ||
125 | } | ||
126 | else if (enumerator.Value is ArrayList) | ||
127 | { | ||
128 | object fieldValue = field.GetValue(obj); | ||
129 | fieldValue.GetType().GetField("Array").SetValue(fieldValue, enumerator.Value); | ||
130 | //TODO | ||
131 | // the LLSD map/array types in the array need to be deserialised | ||
132 | // but first we need to know the right class to deserialise them into. | ||
133 | } | ||
134 | else | ||
135 | { | ||
136 | field.SetValue(obj, enumerator.Value); | ||
137 | } | ||
138 | } | ||
139 | } | ||
140 | break; | ||
141 | } | ||
142 | } | ||
143 | return obj; | ||
144 | } | ||
145 | } | ||
146 | |||
147 | |||
148 | |||
149 | |||
150 | |||
151 | |||
152 | |||
153 | |||
154 | |||
155 | |||
156 | |||
157 | |||
158 | |||
159 | |||
160 | |||
161 | |||
162 | |||
163 | |||
164 | |||
165 | } | ||