diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Region/Caps/LLSDHelpers.cs | 246 |
1 files changed, 246 insertions, 0 deletions
diff --git a/OpenSim/Region/Caps/LLSDHelpers.cs b/OpenSim/Region/Caps/LLSDHelpers.cs new file mode 100644 index 0000000..051520c --- /dev/null +++ b/OpenSim/Region/Caps/LLSDHelpers.cs | |||
@@ -0,0 +1,246 @@ | |||
1 | using System; | ||
2 | using System.Collections; | ||
3 | using System.Collections.Generic; | ||
4 | using System.Text; | ||
5 | using System.IO; | ||
6 | using System.Xml; | ||
7 | using libsecondlife; | ||
8 | |||
9 | namespace OpenSim.Framework | ||
10 | { | ||
11 | public class LLSDHelpers | ||
12 | { | ||
13 | public static string SerialiseLLSDReply(object obj) | ||
14 | { | ||
15 | StringWriter sw = new StringWriter(); | ||
16 | XmlTextWriter writer = new XmlTextWriter(sw); | ||
17 | writer.Formatting = Formatting.None; | ||
18 | writer.WriteStartElement(String.Empty, "llsd", String.Empty); | ||
19 | LLSDHelpers.SerializeLLSDType(writer, obj); | ||
20 | writer.WriteEndElement(); | ||
21 | writer.Close(); | ||
22 | return sw.ToString(); | ||
23 | } | ||
24 | |||
25 | public static void SerializeLLSDType(XmlTextWriter writer, object obj) | ||
26 | { | ||
27 | Type myType = obj.GetType(); | ||
28 | LLSDType[] llsdattributes = (LLSDType[])myType.GetCustomAttributes(typeof(LLSDType), false); | ||
29 | if (llsdattributes.Length > 0) | ||
30 | { | ||
31 | switch (llsdattributes[0].ObjectType) | ||
32 | { | ||
33 | case "MAP": | ||
34 | writer.WriteStartElement(String.Empty, "map", String.Empty); | ||
35 | System.Reflection.FieldInfo[] fields = myType.GetFields(); | ||
36 | for (int i = 0; i < fields.Length; i++) | ||
37 | { | ||
38 | object fieldValue = fields[i].GetValue(obj); | ||
39 | LLSDType[] fieldAttributes = (LLSDType[])fieldValue.GetType().GetCustomAttributes(typeof(LLSDType), false); | ||
40 | if (fieldAttributes.Length > 0) | ||
41 | { | ||
42 | writer.WriteStartElement(String.Empty, "key", String.Empty); | ||
43 | writer.WriteString(fields[i].Name); | ||
44 | writer.WriteEndElement(); | ||
45 | SerializeLLSDType(writer, fieldValue); | ||
46 | } | ||
47 | else | ||
48 | { | ||
49 | //Console.WriteLine("LLSD field name" + fields[i].Name + " , " + fields[i].GetValue(obj).GetType()); | ||
50 | writer.WriteStartElement(String.Empty, "key", String.Empty); | ||
51 | writer.WriteString(fields[i].Name); | ||
52 | writer.WriteEndElement(); | ||
53 | LLSD.LLSDWriteOne(writer, fieldValue); | ||
54 | } | ||
55 | } | ||
56 | writer.WriteEndElement(); | ||
57 | break; | ||
58 | case "ARRAY": | ||
59 | // LLSDArray arrayObject = obj as LLSDArray; | ||
60 | // ArrayList a = arrayObject.Array; | ||
61 | ArrayList a = (ArrayList)obj.GetType().GetField("Array").GetValue(obj); | ||
62 | writer.WriteStartElement(String.Empty, "array", String.Empty); | ||
63 | foreach (object item in a) | ||
64 | { | ||
65 | SerializeLLSDType(writer, item); | ||
66 | } | ||
67 | writer.WriteEndElement(); | ||
68 | break; | ||
69 | } | ||
70 | } | ||
71 | else | ||
72 | { | ||
73 | LLSD.LLSDWriteOne(writer, obj); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | public static object DeserialiseLLSDMap(Hashtable llsd, object obj) | ||
78 | { | ||
79 | Type myType = obj.GetType(); | ||
80 | LLSDType[] llsdattributes = (LLSDType[])myType.GetCustomAttributes(typeof(LLSDType), false); | ||
81 | if (llsdattributes.Length > 0) | ||
82 | { | ||
83 | switch (llsdattributes[0].ObjectType) | ||
84 | { | ||
85 | case "MAP": | ||
86 | IDictionaryEnumerator enumerator = llsd.GetEnumerator(); | ||
87 | while (enumerator.MoveNext()) | ||
88 | { | ||
89 | System.Reflection.FieldInfo field = myType.GetField((string)enumerator.Key); | ||
90 | if (field != null) | ||
91 | { | ||
92 | if (enumerator.Value is Hashtable) | ||
93 | { | ||
94 | object fieldValue = field.GetValue(obj); | ||
95 | DeserialiseLLSDMap((Hashtable) enumerator.Value, fieldValue); | ||
96 | } | ||
97 | else if (enumerator.Value is ArrayList) | ||
98 | { | ||
99 | object fieldValue = field.GetValue(obj); | ||
100 | fieldValue.GetType().GetField("Array").SetValue(fieldValue, enumerator.Value); | ||
101 | //TODO | ||
102 | // the LLSD map/array types in the array need to be deserialised | ||
103 | // but first we need to know the right class to deserialise them into. | ||
104 | } | ||
105 | else | ||
106 | { | ||
107 | field.SetValue(obj, enumerator.Value); | ||
108 | } | ||
109 | } | ||
110 | } | ||
111 | break; | ||
112 | } | ||
113 | } | ||
114 | return obj; | ||
115 | } | ||
116 | } | ||
117 | |||
118 | [LLSDType("MAP")] | ||
119 | public class LLSDMapLayerResponse | ||
120 | { | ||
121 | public LLSDMapRequest AgentData = new LLSDMapRequest(); | ||
122 | public LLSDArray LayerData = new LLSDArray(); | ||
123 | |||
124 | public LLSDMapLayerResponse() | ||
125 | { | ||
126 | |||
127 | } | ||
128 | } | ||
129 | |||
130 | [LLSDType("MAP")] | ||
131 | public class LLSDCapsDetails | ||
132 | { | ||
133 | public string MapLayer = ""; | ||
134 | public string NewFileAgentInventory = ""; | ||
135 | //public string EventQueueGet = ""; | ||
136 | |||
137 | public LLSDCapsDetails() | ||
138 | { | ||
139 | |||
140 | } | ||
141 | } | ||
142 | |||
143 | [LLSDType("MAP")] | ||
144 | public class LLSDMapLayer | ||
145 | { | ||
146 | public int Left = 0; | ||
147 | public int Right = 0; | ||
148 | public int Top = 0; | ||
149 | public int Bottom = 0; | ||
150 | public LLUUID ImageID = LLUUID.Zero; | ||
151 | |||
152 | public LLSDMapLayer() | ||
153 | { | ||
154 | |||
155 | } | ||
156 | } | ||
157 | |||
158 | [LLSDType("ARRAY")] | ||
159 | public class LLSDArray | ||
160 | { | ||
161 | public ArrayList Array = new ArrayList(); | ||
162 | |||
163 | public LLSDArray() | ||
164 | { | ||
165 | |||
166 | } | ||
167 | } | ||
168 | |||
169 | [LLSDType("MAP")] | ||
170 | public class LLSDMapRequest | ||
171 | { | ||
172 | public int Flags = 0; | ||
173 | |||
174 | public LLSDMapRequest() | ||
175 | { | ||
176 | |||
177 | } | ||
178 | } | ||
179 | |||
180 | [LLSDType("MAP")] | ||
181 | public class LLSDUploadReply | ||
182 | { | ||
183 | public string new_asset = ""; | ||
184 | public LLUUID new_inventory_item = LLUUID.Zero; | ||
185 | public string state = ""; | ||
186 | |||
187 | public LLSDUploadReply() | ||
188 | { | ||
189 | |||
190 | } | ||
191 | } | ||
192 | |||
193 | [LLSDType("MAP")] | ||
194 | public class LLSDCapEvent | ||
195 | { | ||
196 | public int id = 0; | ||
197 | public LLSDArray events = new LLSDArray(); | ||
198 | |||
199 | public LLSDCapEvent() | ||
200 | { | ||
201 | |||
202 | } | ||
203 | } | ||
204 | |||
205 | [LLSDType("MAP")] | ||
206 | public class LLSDEmpty | ||
207 | { | ||
208 | public LLSDEmpty() | ||
209 | { | ||
210 | |||
211 | } | ||
212 | } | ||
213 | |||
214 | [LLSDType("MAP")] | ||
215 | public class LLSDTest | ||
216 | { | ||
217 | public int Test1 = 20; | ||
218 | public int Test2 = 10; | ||
219 | |||
220 | public LLSDTest() | ||
221 | { | ||
222 | |||
223 | } | ||
224 | } | ||
225 | |||
226 | |||
227 | [AttributeUsage(AttributeTargets.Class)] | ||
228 | public class LLSDType : Attribute | ||
229 | { | ||
230 | private string myType; | ||
231 | |||
232 | public LLSDType(string type) | ||
233 | { | ||
234 | myType = type; | ||
235 | |||
236 | } | ||
237 | |||
238 | public string ObjectType | ||
239 | { | ||
240 | get | ||
241 | { | ||
242 | return myType; | ||
243 | } | ||
244 | } | ||
245 | } | ||
246 | } | ||