diff options
Diffstat (limited to 'Common/OpenSim.Framework/LLSDHelpers.cs')
-rw-r--r-- | Common/OpenSim.Framework/LLSDHelpers.cs | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/Common/OpenSim.Framework/LLSDHelpers.cs b/Common/OpenSim.Framework/LLSDHelpers.cs new file mode 100644 index 0000000..5b73483 --- /dev/null +++ b/Common/OpenSim.Framework/LLSDHelpers.cs | |||
@@ -0,0 +1,173 @@ | |||
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 | field.SetValue(obj, enumerator.Value); | ||
93 | } | ||
94 | } | ||
95 | break; | ||
96 | } | ||
97 | } | ||
98 | return obj; | ||
99 | } | ||
100 | } | ||
101 | |||
102 | [LLSDType("MAP")] | ||
103 | public class LLSDMapLayer | ||
104 | { | ||
105 | public int Left = 0; | ||
106 | public int Right = 0; | ||
107 | public int Top = 0; | ||
108 | public int Bottom = 0; | ||
109 | public LLUUID ImageID = LLUUID.Zero; | ||
110 | |||
111 | public LLSDArray TestArray = new LLSDArray(); | ||
112 | public LLSDMapLayer() | ||
113 | { | ||
114 | |||
115 | } | ||
116 | } | ||
117 | |||
118 | [LLSDType("ARRAY")] | ||
119 | public class LLSDArray | ||
120 | { | ||
121 | public ArrayList Array = new ArrayList(); | ||
122 | |||
123 | public LLSDArray() | ||
124 | { | ||
125 | |||
126 | } | ||
127 | } | ||
128 | |||
129 | [LLSDType("MAP")] | ||
130 | public class LLSDMapRequest | ||
131 | { | ||
132 | public int Flags = 0; | ||
133 | |||
134 | public LLSDMapRequest() | ||
135 | { | ||
136 | |||
137 | } | ||
138 | } | ||
139 | |||
140 | [LLSDType("MAP")] | ||
141 | public class LLSDTest | ||
142 | { | ||
143 | public int Test1 = 20; | ||
144 | public int Test2 = 10; | ||
145 | |||
146 | public LLSDTest() | ||
147 | { | ||
148 | |||
149 | } | ||
150 | } | ||
151 | |||
152 | |||
153 | [AttributeUsage(AttributeTargets.Class)] | ||
154 | public class LLSDType : Attribute | ||
155 | { | ||
156 | private string myHandler; | ||
157 | |||
158 | |||
159 | public LLSDType(string type) | ||
160 | { | ||
161 | myHandler = type; | ||
162 | |||
163 | } | ||
164 | |||
165 | public string ObjectType | ||
166 | { | ||
167 | get | ||
168 | { | ||
169 | return myHandler; | ||
170 | } | ||
171 | } | ||
172 | } | ||
173 | } | ||