aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Grid/InventoryServer/InventoryManager.cs
blob: 462dd4aa5a64c78c0c88ff1e99112554e99d0109 (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
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright (c) Contributors, http://opensimulator.org/
* See CONTRIBUTORS.TXT for a full list of copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the OpenSim Project nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* 
*/
using System;
using System.IO;
using System.Text;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using libsecondlife;

using OpenSim.Framework;
using OpenSim.Framework.Console;
using OpenSim.Framework.Servers;

namespace OpenSim.Grid.InventoryServer
{

    public class InventoryManager
    {
        IInventoryData _databasePlugin;

        /// <summary>
        /// Adds a new inventory server plugin - user servers will be requested in the order they were loaded.
        /// </summary>
        /// <param name="FileName">The filename to the inventory server plugin DLL</param>
        public void AddDatabasePlugin(string FileName)
        {
            MainLog.Instance.Verbose(OpenInventory_Main.LogName, "Invenstorage: Attempting to load " + FileName);
            Assembly pluginAssembly = Assembly.LoadFrom(FileName);

            MainLog.Instance.Verbose(OpenInventory_Main.LogName, "Invenstorage: Found " + pluginAssembly.GetTypes().Length + " interfaces.");
            foreach (Type pluginType in pluginAssembly.GetTypes())
            {
                if (!pluginType.IsAbstract)
                {
                    Type typeInterface = pluginType.GetInterface("IInventoryData", true);

                    if (typeInterface != null)
                    {
                        IInventoryData plug = (IInventoryData)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
                        plug.Initialise();
                        _databasePlugin = plug;
                        MainLog.Instance.Verbose(OpenInventory_Main.LogName, "Invenstorage: Added IInventoryData Interface");
                        break;
                    }

                    typeInterface = null;
                }
            }

            pluginAssembly = null;
        }

        protected static SerializableInventory loadInventoryFromXmlFile(string fileName)
        {
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            XmlReader reader = new XmlTextReader(fs);
            XmlSerializer x = new XmlSerializer(typeof(SerializableInventory));
            SerializableInventory inventory = (SerializableInventory)x.Deserialize(reader);
            fs.Close();
            fs.Dispose();
            return inventory;
        }

        protected static void saveInventoryToStream(SerializableInventory inventory, Stream s)
        {
            XmlTextWriter writer = new XmlTextWriter(s, Encoding.UTF8);
            writer.Formatting = Formatting.Indented;
            XmlSerializer x = new XmlSerializer(typeof(SerializableInventory));
            x.Serialize(writer, inventory);
        }

        protected static bool fixupFolder(SerializableInventory.SerializableFolder f, SerializableInventory.SerializableFolder parent)
        {
            bool modified = false;

            // ensure we have a valid folder id
            if (f.folderID == LLUUID.Zero)
            {
                f.folderID = LLUUID.Random();
                modified = true;
            }

            // ensure we have  valid agent id 
            if (f.agentID == LLUUID.Zero)
            {
                if (parent != null)
                    f.agentID = parent.agentID;
                else
                    f.agentID = f.folderID;
                modified = true;
            }

            if (f.parentID == LLUUID.Zero && parent != null)
            {
                f.parentID = parent.folderID;
                modified = true;
            }


            foreach (SerializableInventory.SerializableFolder child in f.SubFolders)
            {
                modified |= fixupFolder(child, f);
            }

            return modified;
        }

        protected static bool fixupInventory(SerializableInventory inventory)
        {
            return fixupFolder(inventory.root, null);
        }

        public class GetInventory : BaseStreamHandler
        {
            private SerializableInventory _inventory;
            private InventoryManager _manager;
            public GetInventory(InventoryManager manager)
                : base("GET", "/inventory")
            {
                _manager = manager;

                _inventory = loadInventoryFromXmlFile("Inventory_Library.xml");
                if (fixupInventory(_inventory))
                {
                    FileStream fs = new FileStream("Inventory_Library.xml", FileMode.Truncate, FileAccess.Write);
                    saveInventoryToStream(_inventory, fs);
                    fs.Flush();
                    fs.Close();
                    MainLog.Instance.Debug(OpenInventory_Main.LogName, "Modified");
                }
            }

            private void CreateDefaultInventory(LLUUID userID)
            {
            }

            private byte[] GetUserInventory(LLUUID userID)
            {
                MainLog.Instance.Notice(OpenInventory_Main.LogName, "Getting Inventory for user {0}", userID.ToStringHyphenated());
                byte[] result = new byte[] { };

                InventoryFolderBase fb = _manager._databasePlugin.getUserRootFolder(userID);
                if (fb == null)
                {
                    MainLog.Instance.Notice(OpenInventory_Main.LogName, "Inventory not found for user {0}, creating new", userID.ToStringHyphenated());
                    CreateDefaultInventory(userID);
                }

                return result;
            }

            override public byte[] Handle(string path, Stream request)
            {
                byte[] result = new byte[] { };

                string[] parms = path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                if (parms.Length >= 1)
                {
                    if (string.Compare(parms[1], "library", true) == 0)
                    {

                        MemoryStream ms = new MemoryStream();
                        saveInventoryToStream(_inventory, ms);

                        result = ms.GetBuffer();
                        Array.Resize<byte>(ref result, (int)ms.Length);
                    }
                    else if (string.Compare(parms[1], "user", true) == 0)
                    {
                        if (parms.Length >= 2)
                        {
                            result = GetUserInventory(new LLUUID(parms[2]));
                        }
                    }
                }
                return result;
            }
        }
    
    }
}