aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Grid/NewAssetServer/Metadata.cs
blob: 247a3e84a4af474955c8182570ba1d7d3bfa4055 (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
using System;
using System.Collections.Generic;
using System.Xml;
using OpenMetaverse;
using OpenMetaverse.StructuredData;

namespace AssetServer
{
    public class Metadata
    {
        public UUID ID;
        public string Name;
        public string Description;
        public DateTime CreationDate;
        public string ContentType;
        public byte[] SHA1;
        public bool Temporary;
        public Dictionary<string, Uri> Methods = new Dictionary<string, Uri>();
        public OSDMap ExtraData;

        public OSDMap SerializeToOSD()
        {
            OSDMap osdata = new OSDMap();

            if (ID != UUID.Zero) osdata["id"] = OSD.FromUUID(ID);
            osdata["name"] = OSD.FromString(Name);
            osdata["description"] = OSD.FromString(Description);
            osdata["creation_date"] = OSD.FromDate(CreationDate);
            osdata["type"] = OSD.FromString(ContentType);
            osdata["sha1"] = OSD.FromBinary(SHA1);
            osdata["temporary"] = OSD.FromBoolean(Temporary);

            OSDMap methods = new OSDMap(Methods.Count);
            foreach (KeyValuePair<string, Uri> kvp in Methods)
                methods.Add(kvp.Key, OSD.FromUri(kvp.Value));
            osdata["methods"] = methods;

            if (ExtraData != null) osdata["extra_data"] = ExtraData;

            return osdata;
        }

        public byte[] SerializeToBytes()
        {
            LitJson.JsonData jsonData = OSDParser.SerializeJson(SerializeToOSD());
            return System.Text.Encoding.UTF8.GetBytes(jsonData.ToJson());
        }

        public void Deserialize(byte[] data)
        {
            OSD osdata = OSDParser.DeserializeJson(System.Text.Encoding.UTF8.GetString(data));
            Deserialize(osdata);
        }

        public void Deserialize(string data)
        {
            OSD osdata = OSDParser.DeserializeJson(data);
            Deserialize(osdata);
        }

        public void Deserialize(OSD osdata)
        {
            if (osdata.Type == OSDType.Map)
            {
                OSDMap map = (OSDMap)osdata;
                ID = map["id"].AsUUID();
                Name = map["name"].AsString();
                Description = map["description"].AsString();
                CreationDate = map["creation_date"].AsDate();
                ContentType = map["type"].AsString();
                SHA1 = map["sha1"].AsBinary();
                Temporary = map["temporary"].AsBoolean();

                OSDMap methods = map["methods"] as OSDMap;
                if (methods != null)
                {
                    foreach (KeyValuePair<string, OSD> kvp in methods)
                        Methods.Add(kvp.Key, kvp.Value.AsUri());
                }

                ExtraData = map["extra_data"] as OSDMap;
            }
        }
    }
}