opensim – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | eva | 1 | /* |
2 | * Copyright (c) Contributors, http://opensimulator.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 OpenSimulator 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 | |||
28 | using System; |
||
29 | using System.Collections.Generic; |
||
30 | using System.IO; |
||
31 | using System.Reflection; |
||
32 | using System.Xml; |
||
33 | |||
34 | using OpenSim.Framework; |
||
35 | using OpenSim.Services.Base; |
||
36 | using OpenSim.Services.Interfaces; |
||
37 | |||
38 | using log4net; |
||
39 | using Nini.Config; |
||
40 | using OpenMetaverse; |
||
41 | using PermissionMask = OpenSim.Framework.PermissionMask; |
||
42 | |||
43 | namespace OpenSim.Services.InventoryService |
||
44 | { |
||
45 | /// <summary> |
||
46 | /// Basically a hack to give us a Inventory library while we don't have a inventory server |
||
47 | /// once the server is fully implemented then should read the data from that |
||
48 | /// </summary> |
||
49 | public class LibraryService : ServiceBase, ILibraryService |
||
50 | { |
||
51 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
||
52 | |||
53 | private InventoryFolderImpl m_LibraryRootFolder; |
||
54 | |||
55 | public InventoryFolderImpl LibraryRootFolder |
||
56 | { |
||
57 | get { return m_LibraryRootFolder; } |
||
58 | } |
||
59 | |||
60 | private UUID libOwner = new UUID("11111111-1111-0000-0000-000100bba000"); |
||
61 | |||
62 | /// <summary> |
||
63 | /// Holds the root library folder and all its descendents. This is really only used during inventory |
||
64 | /// setup so that we don't have to repeatedly search the tree of library folders. |
||
65 | /// </summary> |
||
66 | protected Dictionary<UUID, InventoryFolderImpl> libraryFolders |
||
67 | = new Dictionary<UUID, InventoryFolderImpl>(); |
||
68 | |||
69 | public LibraryService(IConfigSource config) |
||
70 | : base(config) |
||
71 | { |
||
72 | string pLibrariesLocation = Path.Combine("inventory", "Libraries.xml"); |
||
73 | string pLibName = "OpenSim Library"; |
||
74 | |||
75 | IConfig libConfig = config.Configs["LibraryService"]; |
||
76 | if (libConfig != null) |
||
77 | { |
||
78 | pLibrariesLocation = libConfig.GetString("DefaultLibrary", pLibrariesLocation); |
||
79 | pLibName = libConfig.GetString("LibraryName", pLibName); |
||
80 | } |
||
81 | |||
82 | m_log.Debug("[LIBRARY]: Starting library service..."); |
||
83 | |||
84 | m_LibraryRootFolder = new InventoryFolderImpl(); |
||
85 | m_LibraryRootFolder.Owner = libOwner; |
||
86 | m_LibraryRootFolder.ID = new UUID("00000112-000f-0000-0000-000100bba000"); |
||
87 | m_LibraryRootFolder.Name = pLibName; |
||
88 | m_LibraryRootFolder.ParentID = UUID.Zero; |
||
89 | m_LibraryRootFolder.Type = (short)8; |
||
90 | m_LibraryRootFolder.Version = (ushort)1; |
||
91 | |||
92 | libraryFolders.Add(m_LibraryRootFolder.ID, m_LibraryRootFolder); |
||
93 | |||
94 | LoadLibraries(pLibrariesLocation); |
||
95 | } |
||
96 | |||
97 | public InventoryItemBase CreateItem(UUID inventoryID, UUID assetID, string name, string description, |
||
98 | int assetType, int invType, UUID parentFolderID) |
||
99 | { |
||
100 | InventoryItemBase item = new InventoryItemBase(); |
||
101 | item.Owner = libOwner; |
||
102 | item.CreatorId = libOwner.ToString(); |
||
103 | item.ID = inventoryID; |
||
104 | item.AssetID = assetID; |
||
105 | item.Description = description; |
||
106 | item.Name = name; |
||
107 | item.AssetType = assetType; |
||
108 | item.InvType = invType; |
||
109 | item.Folder = parentFolderID; |
||
110 | item.BasePermissions = 0x7FFFFFFF; |
||
111 | item.EveryOnePermissions = 0x7FFFFFFF; |
||
112 | item.CurrentPermissions = 0x7FFFFFFF; |
||
113 | item.NextPermissions = 0x7FFFFFFF; |
||
114 | return item; |
||
115 | } |
||
116 | |||
117 | /// <summary> |
||
118 | /// Use the asset set information at path to load assets |
||
119 | /// </summary> |
||
120 | /// <param name="path"></param> |
||
121 | /// <param name="assets"></param> |
||
122 | protected void LoadLibraries(string librariesControlPath) |
||
123 | { |
||
124 | m_log.InfoFormat("[LIBRARY INVENTORY]: Loading library control file {0}", librariesControlPath); |
||
125 | LoadFromFile(librariesControlPath, "Libraries control", ReadLibraryFromConfig); |
||
126 | } |
||
127 | |||
128 | /// <summary> |
||
129 | /// Read a library set from config |
||
130 | /// </summary> |
||
131 | /// <param name="config"></param> |
||
132 | protected void ReadLibraryFromConfig(IConfig config, string path) |
||
133 | { |
||
134 | string basePath = Path.GetDirectoryName(path); |
||
135 | string foldersPath |
||
136 | = Path.Combine( |
||
137 | basePath, config.GetString("foldersFile", String.Empty)); |
||
138 | |||
139 | LoadFromFile(foldersPath, "Library folders", ReadFolderFromConfig); |
||
140 | |||
141 | string itemsPath |
||
142 | = Path.Combine( |
||
143 | basePath, config.GetString("itemsFile", String.Empty)); |
||
144 | |||
145 | LoadFromFile(itemsPath, "Library items", ReadItemFromConfig); |
||
146 | } |
||
147 | |||
148 | /// <summary> |
||
149 | /// Read a library inventory folder from a loaded configuration |
||
150 | /// </summary> |
||
151 | /// <param name="source"></param> |
||
152 | private void ReadFolderFromConfig(IConfig config, string path) |
||
153 | { |
||
154 | InventoryFolderImpl folderInfo = new InventoryFolderImpl(); |
||
155 | |||
156 | folderInfo.ID = new UUID(config.GetString("folderID", m_LibraryRootFolder.ID.ToString())); |
||
157 | folderInfo.Name = config.GetString("name", "unknown"); |
||
158 | folderInfo.ParentID = new UUID(config.GetString("parentFolderID", m_LibraryRootFolder.ID.ToString())); |
||
159 | folderInfo.Type = (short)config.GetInt("type", 8); |
||
160 | |||
161 | folderInfo.Owner = libOwner; |
||
162 | folderInfo.Version = 1; |
||
163 | |||
164 | if (libraryFolders.ContainsKey(folderInfo.ParentID)) |
||
165 | { |
||
166 | InventoryFolderImpl parentFolder = libraryFolders[folderInfo.ParentID]; |
||
167 | |||
168 | libraryFolders.Add(folderInfo.ID, folderInfo); |
||
169 | parentFolder.AddChildFolder(folderInfo); |
||
170 | |||
171 | // m_log.InfoFormat("[LIBRARY INVENTORY]: Adding folder {0} ({1})", folderInfo.name, folderInfo.folderID); |
||
172 | } |
||
173 | else |
||
174 | { |
||
175 | m_log.WarnFormat( |
||
176 | "[LIBRARY INVENTORY]: Couldn't add folder {0} ({1}) since parent folder with ID {2} does not exist!", |
||
177 | folderInfo.Name, folderInfo.ID, folderInfo.ParentID); |
||
178 | } |
||
179 | } |
||
180 | |||
181 | /// <summary> |
||
182 | /// Read a library inventory item metadata from a loaded configuration |
||
183 | /// </summary> |
||
184 | /// <param name="source"></param> |
||
185 | private void ReadItemFromConfig(IConfig config, string path) |
||
186 | { |
||
187 | InventoryItemBase item = new InventoryItemBase(); |
||
188 | item.Owner = libOwner; |
||
189 | item.CreatorId = libOwner.ToString(); |
||
190 | item.ID = new UUID(config.GetString("inventoryID", m_LibraryRootFolder.ID.ToString())); |
||
191 | item.AssetID = new UUID(config.GetString("assetID", item.ID.ToString())); |
||
192 | item.Folder = new UUID(config.GetString("folderID", m_LibraryRootFolder.ID.ToString())); |
||
193 | item.Name = config.GetString("name", String.Empty); |
||
194 | item.Description = config.GetString("description", item.Name); |
||
195 | item.InvType = config.GetInt("inventoryType", 0); |
||
196 | item.AssetType = config.GetInt("assetType", item.InvType); |
||
197 | item.CurrentPermissions = (uint)config.GetLong("currentPermissions", (uint)PermissionMask.All); |
||
198 | item.NextPermissions = (uint)config.GetLong("nextPermissions", (uint)PermissionMask.All); |
||
199 | item.EveryOnePermissions |
||
200 | = (uint)config.GetLong("everyonePermissions", (uint)PermissionMask.All - (uint)PermissionMask.Modify); |
||
201 | item.BasePermissions = (uint)config.GetLong("basePermissions", (uint)PermissionMask.All); |
||
202 | item.Flags = (uint)config.GetInt("flags", 0); |
||
203 | |||
204 | if (libraryFolders.ContainsKey(item.Folder)) |
||
205 | { |
||
206 | InventoryFolderImpl parentFolder = libraryFolders[item.Folder]; |
||
207 | try |
||
208 | { |
||
209 | parentFolder.Items.Add(item.ID, item); |
||
210 | } |
||
211 | catch (Exception) |
||
212 | { |
||
213 | m_log.WarnFormat("[LIBRARY INVENTORY] Item {1} [{0}] not added, duplicate item", item.ID, item.Name); |
||
214 | } |
||
215 | } |
||
216 | else |
||
217 | { |
||
218 | m_log.WarnFormat( |
||
219 | "[LIBRARY INVENTORY]: Couldn't add item {0} ({1}) since parent folder with ID {2} does not exist!", |
||
220 | item.Name, item.ID, item.Folder); |
||
221 | } |
||
222 | } |
||
223 | |||
224 | private delegate void ConfigAction(IConfig config, string path); |
||
225 | |||
226 | /// <summary> |
||
227 | /// Load the given configuration at a path and perform an action on each Config contained within it |
||
228 | /// </summary> |
||
229 | /// <param name="path"></param> |
||
230 | /// <param name="fileDescription"></param> |
||
231 | /// <param name="action"></param> |
||
232 | private static void LoadFromFile(string path, string fileDescription, ConfigAction action) |
||
233 | { |
||
234 | if (File.Exists(path)) |
||
235 | { |
||
236 | try |
||
237 | { |
||
238 | XmlConfigSource source = new XmlConfigSource(path); |
||
239 | |||
240 | for (int i = 0; i < source.Configs.Count; i++) |
||
241 | { |
||
242 | action(source.Configs[i], path); |
||
243 | } |
||
244 | } |
||
245 | catch (XmlException e) |
||
246 | { |
||
247 | m_log.ErrorFormat("[LIBRARY INVENTORY]: Error loading {0} : {1}", path, e); |
||
248 | } |
||
249 | } |
||
250 | else |
||
251 | { |
||
252 | m_log.ErrorFormat("[LIBRARY INVENTORY]: {0} file {1} does not exist!", fileDescription, path); |
||
253 | } |
||
254 | } |
||
255 | |||
256 | /// <summary> |
||
257 | /// Looks like a simple getter, but is written like this for some consistency with the other Request |
||
258 | /// methods in the superclass |
||
259 | /// </summary> |
||
260 | /// <returns></returns> |
||
261 | public Dictionary<UUID, InventoryFolderImpl> GetAllFolders() |
||
262 | { |
||
263 | Dictionary<UUID, InventoryFolderImpl> fs = new Dictionary<UUID, InventoryFolderImpl>(); |
||
264 | fs.Add(m_LibraryRootFolder.ID, m_LibraryRootFolder); |
||
265 | List<InventoryFolderImpl> fis = TraverseFolder(m_LibraryRootFolder); |
||
266 | foreach (InventoryFolderImpl f in fis) |
||
267 | { |
||
268 | fs.Add(f.ID, f); |
||
269 | } |
||
270 | //return libraryFolders; |
||
271 | return fs; |
||
272 | } |
||
273 | |||
274 | private List<InventoryFolderImpl> TraverseFolder(InventoryFolderImpl node) |
||
275 | { |
||
276 | List<InventoryFolderImpl> folders = node.RequestListOfFolderImpls(); |
||
277 | List<InventoryFolderImpl> subs = new List<InventoryFolderImpl>(); |
||
278 | foreach (InventoryFolderImpl f in folders) |
||
279 | subs.AddRange(TraverseFolder(f)); |
||
280 | |||
281 | folders.AddRange(subs); |
||
282 | return folders; |
||
283 | } |
||
284 | } |
||
285 | } |