aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Common/OpenSim.Framework/LLSDHelpers.cs
blob: 3b044a3e82d9502b2a144224c2322b2f5b6dcf9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using libsecondlife;

namespace OpenSim.Framework
{
    public class LLSDHelpers
    {
        public static string SerialiseLLSDReply(object obj)
        {
            StringWriter sw = new StringWriter();
            XmlTextWriter writer = new XmlTextWriter(sw);
            writer.Formatting = Formatting.None;
            writer.WriteStartElement(String.Empty, "llsd", String.Empty);
            LLSDHelpers.SerializeLLSDType(writer, obj);
            writer.WriteEndElement();
            writer.Close();
            return sw.ToString();
        }

        public static void SerializeLLSDType(XmlTextWriter writer, object obj)
        {
            Type myType = obj.GetType();
            LLSDType[] llsdattributes = (LLSDType[])myType.GetCustomAttributes(typeof(LLSDType), false);
            if (llsdattributes.Length > 0)
            {
                switch (llsdattributes[0].ObjectType)
                {
                    case "MAP":
                        writer.WriteStartElement(String.Empty, "map", String.Empty);
                        System.Reflection.FieldInfo[] fields = myType.GetFields();
                        for (int i = 0; i < fields.Length; i++)
                        {
                            object fieldValue = fields[i].GetValue(obj);
                            LLSDType[] fieldAttributes = (LLSDType[])fieldValue.GetType().GetCustomAttributes(typeof(LLSDType), false);
                            if (fieldAttributes.Length > 0)
                            {
                                writer.WriteStartElement(String.Empty, "key", String.Empty);
                                writer.WriteString(fields[i].Name);
                                writer.WriteEndElement();
                                SerializeLLSDType(writer, fieldValue);
                            }
                            else
                            {
                                //Console.WriteLine("LLSD field name" + fields[i].Name + " , " + fields[i].GetValue(obj).GetType());
                                writer.WriteStartElement(String.Empty, "key", String.Empty);
                                writer.WriteString(fields[i].Name);
                                writer.WriteEndElement();
                                LLSD.LLSDWriteOne(writer, fieldValue);
                            }
                        }
                        writer.WriteEndElement();
                        break;
                    case "ARRAY":
                        // LLSDArray arrayObject = obj as LLSDArray;
                        // ArrayList a = arrayObject.Array;
                        ArrayList a = (ArrayList)obj.GetType().GetField("Array").GetValue(obj);
                        writer.WriteStartElement(String.Empty, "array", String.Empty);
                        foreach (object item in a)
                        {
                            SerializeLLSDType(writer, item);
                        }
                        writer.WriteEndElement();
                        break;
                }
            }
            else
            {
                LLSD.LLSDWriteOne(writer, obj);
            }
        }

        public static object DeserialiseLLSDMap(Hashtable llsd, object obj)
        {
            Type myType = obj.GetType();
            LLSDType[] llsdattributes = (LLSDType[])myType.GetCustomAttributes(typeof(LLSDType), false);
            if (llsdattributes.Length > 0)
            {
                switch (llsdattributes[0].ObjectType)
                {
                    case "MAP":
                        IDictionaryEnumerator enumerator = llsd.GetEnumerator();
                        while (enumerator.MoveNext())
                        {
                            System.Reflection.FieldInfo field = myType.GetField((string)enumerator.Key);
                            if (field != null)
                            {
                                field.SetValue(obj, enumerator.Value);
                            }
                        }
                        break;
                }
            }
            return obj;
        }
    }

    [LLSDType("MAP")]
    public class LLSDMapLayerResponse
    {
        public LLSDMapRequest AgentData = new LLSDMapRequest();
        public LLSDArray LayerData = new LLSDArray();

        public LLSDMapLayerResponse()
        {

        }
    }

    [LLSDType("MAP")]
    public class LLSDCapsDetails
    {
        public string MapLayer = "";
        public string NewFileAgentInventory = "";
        //public string EventQueueGet = "";

        public LLSDCapsDetails()
        {

        }
    }

    [LLSDType("MAP")]
    public class LLSDMapLayer
    {
        public int Left = 0;
        public int Right = 0;
        public int Top = 0;
        public int Bottom = 0;
        public LLUUID ImageID = LLUUID.Zero;

        public LLSDMapLayer()
        {
           
        }
    }

    [LLSDType("ARRAY")]
    public class LLSDArray
    {
        public ArrayList Array = new ArrayList();

        public LLSDArray()
        {

        }
    }

    [LLSDType("MAP")]
    public class LLSDMapRequest
    {
        public int Flags = 0;

        public LLSDMapRequest()
        {

        }
    }


    [LLSDType("MAP")]
    public class LLSDTest
    {
        public int Test1 = 20;
        public int Test2 = 10;

        public LLSDTest()
        {

        }
    }


    [AttributeUsage(AttributeTargets.Class)]
    public class LLSDType : Attribute
    {
        private string myType;

        public LLSDType(string type)
        {
            myType = type;

        }

        public string ObjectType
        {
            get
            {
                return myType;
            }
        }
    }
}