aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Capabilities/LLSDHelpers.cs
diff options
context:
space:
mode:
authormingchen2007-06-27 19:43:46 +0000
committermingchen2007-06-27 19:43:46 +0000
commit0232f01a58a3c0a88e95c22589efec21f502f081 (patch)
treeff210fa6750c23972086ebbf816ead0a0a2f412a /OpenSim/Region/Capabilities/LLSDHelpers.cs
parent*Moved VersionInfo.cs to its correct place in OpenSim.csproj (diff)
downloadopensim-SC_OLD-0232f01a58a3c0a88e95c22589efec21f502f081.zip
opensim-SC_OLD-0232f01a58a3c0a88e95c22589efec21f502f081.tar.gz
opensim-SC_OLD-0232f01a58a3c0a88e95c22589efec21f502f081.tar.bz2
opensim-SC_OLD-0232f01a58a3c0a88e95c22589efec21f502f081.tar.xz
*Moved all the classes into their own file from LLSDHelpers.cs
*Some folder renaming to follow project Name *Updated prebuild.xml
Diffstat (limited to 'OpenSim/Region/Capabilities/LLSDHelpers.cs')
-rw-r--r--OpenSim/Region/Capabilities/LLSDHelpers.cs163
1 files changed, 163 insertions, 0 deletions
diff --git a/OpenSim/Region/Capabilities/LLSDHelpers.cs b/OpenSim/Region/Capabilities/LLSDHelpers.cs
new file mode 100644
index 0000000..80916b4
--- /dev/null
+++ b/OpenSim/Region/Capabilities/LLSDHelpers.cs
@@ -0,0 +1,163 @@
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*/
28using System;
29using System.Collections;
30using System.Collections.Generic;
31using System.Text;
32using System.IO;
33using System.Xml;
34using libsecondlife;
35
36namespace 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 //Console.WriteLine("LLSD field name" + fields[i].Name + " , " + fields[i].GetValue(obj).GetType());
77 writer.WriteStartElement(String.Empty, "key", String.Empty);
78 writer.WriteString(fields[i].Name);
79 writer.WriteEndElement();
80 LLSD.LLSDWriteOne(writer, fieldValue);
81 }
82 }
83 writer.WriteEndElement();
84 break;
85 case "ARRAY":
86 // LLSDArray arrayObject = obj as LLSDArray;
87 // ArrayList a = arrayObject.Array;
88 ArrayList a = (ArrayList)obj.GetType().GetField("Array").GetValue(obj);
89 writer.WriteStartElement(String.Empty, "array", String.Empty);
90 foreach (object item in a)
91 {
92 SerializeLLSDType(writer, item);
93 }
94 writer.WriteEndElement();
95 break;
96 }
97 }
98 else
99 {
100 LLSD.LLSDWriteOne(writer, obj);
101 }
102 }
103
104 public static object DeserialiseLLSDMap(Hashtable llsd, object obj)
105 {
106 Type myType = obj.GetType();
107 LLSDType[] llsdattributes = (LLSDType[])myType.GetCustomAttributes(typeof(LLSDType), false);
108 if (llsdattributes.Length > 0)
109 {
110 switch (llsdattributes[0].ObjectType)
111 {
112 case "MAP":
113 IDictionaryEnumerator enumerator = llsd.GetEnumerator();
114 while (enumerator.MoveNext())
115 {
116 System.Reflection.FieldInfo field = myType.GetField((string)enumerator.Key);
117 if (field != null)
118 {
119 if (enumerator.Value is Hashtable)
120 {
121 object fieldValue = field.GetValue(obj);
122 DeserialiseLLSDMap((Hashtable) enumerator.Value, fieldValue);
123 }
124 else if (enumerator.Value is ArrayList)
125 {
126 object fieldValue = field.GetValue(obj);
127 fieldValue.GetType().GetField("Array").SetValue(fieldValue, enumerator.Value);
128 //TODO
129 // the LLSD map/array types in the array need to be deserialised
130 // but first we need to know the right class to deserialise them into.
131 }
132 else
133 {
134 field.SetValue(obj, enumerator.Value);
135 }
136 }
137 }
138 break;
139 }
140 }
141 return obj;
142 }
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163}