clockwerk-opensim – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 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 OpenMetaverse; |
||
31 | using OpenSim.Framework; |
||
32 | using OpenSim.Region.CoreModules.Avatar.Inventory.Archiver; |
||
33 | using OpenSim.Region.Framework.Scenes; |
||
34 | using OpenSim.Services.Interfaces; |
||
35 | |||
36 | namespace OpenSim.Tests.Common |
||
37 | { |
||
38 | /// <summary> |
||
39 | /// Utility functions for carrying out user inventory tests. |
||
40 | /// </summary> |
||
41 | public static class UserInventoryHelpers |
||
42 | { |
||
43 | public static readonly string PATH_DELIMITER = "/"; |
||
44 | |||
45 | /// <summary> |
||
46 | /// Add an existing scene object as an item in the user's inventory. |
||
47 | /// </summary> |
||
48 | /// <remarks> |
||
49 | /// Will be added to the system Objects folder. |
||
50 | /// </remarks> |
||
51 | /// <param name='scene'></param> |
||
52 | /// <param name='so'></param> |
||
53 | /// <param name='inventoryIdTail'></param> |
||
54 | /// <param name='assetIdTail'></param> |
||
55 | /// <returns>The inventory item created.</returns> |
||
56 | public static InventoryItemBase AddInventoryItem( |
||
57 | Scene scene, SceneObjectGroup so, int inventoryIdTail, int assetIdTail) |
||
58 | { |
||
59 | return AddInventoryItem( |
||
60 | scene, |
||
61 | so.Name, |
||
62 | TestHelpers.ParseTail(inventoryIdTail), |
||
63 | InventoryType.Object, |
||
64 | AssetHelpers.CreateAsset(TestHelpers.ParseTail(assetIdTail), so), |
||
65 | so.OwnerID); |
||
66 | } |
||
67 | |||
68 | /// <summary> |
||
69 | /// Add an existing scene object as an item in the user's inventory at the given path. |
||
70 | /// </summary> |
||
71 | /// <param name='scene'></param> |
||
72 | /// <param name='so'></param> |
||
73 | /// <param name='inventoryIdTail'></param> |
||
74 | /// <param name='assetIdTail'></param> |
||
75 | /// <returns>The inventory item created.</returns> |
||
76 | public static InventoryItemBase AddInventoryItem( |
||
77 | Scene scene, SceneObjectGroup so, int inventoryIdTail, int assetIdTail, string path) |
||
78 | { |
||
79 | return AddInventoryItem( |
||
80 | scene, |
||
81 | so.Name, |
||
82 | TestHelpers.ParseTail(inventoryIdTail), |
||
83 | InventoryType.Object, |
||
84 | AssetHelpers.CreateAsset(TestHelpers.ParseTail(assetIdTail), so), |
||
85 | so.OwnerID, |
||
86 | path); |
||
87 | } |
||
88 | |||
89 | /// <summary> |
||
90 | /// Adds the given item to the existing system folder for its type (e.g. an object will go in the "Objects" |
||
91 | /// folder). |
||
92 | /// </summary> |
||
93 | /// <param name="scene"></param> |
||
94 | /// <param name="itemName"></param> |
||
95 | /// <param name="itemId"></param> |
||
96 | /// <param name="itemType"></param> |
||
97 | /// <param name="asset">The serialized asset for this item</param> |
||
98 | /// <param name="userId"></param> |
||
99 | /// <returns></returns> |
||
100 | private static InventoryItemBase AddInventoryItem( |
||
101 | Scene scene, string itemName, UUID itemId, InventoryType itemType, AssetBase asset, UUID userId) |
||
102 | { |
||
103 | return AddInventoryItem( |
||
104 | scene, itemName, itemId, itemType, asset, userId, |
||
105 | scene.InventoryService.GetFolderForType(userId, (AssetType)asset.Type).Name); |
||
106 | } |
||
107 | |||
108 | /// <summary> |
||
109 | /// Adds the given item to an inventory folder |
||
110 | /// </summary> |
||
111 | /// <param name="scene"></param> |
||
112 | /// <param name="itemName"></param> |
||
113 | /// <param name="itemId"></param> |
||
114 | /// <param name="itemType"></param> |
||
115 | /// <param name="asset">The serialized asset for this item</param> |
||
116 | /// <param name="userId"></param> |
||
117 | /// <param name="path">Existing inventory path at which to add.</param> |
||
118 | /// <returns></returns> |
||
119 | private static InventoryItemBase AddInventoryItem( |
||
120 | Scene scene, string itemName, UUID itemId, InventoryType itemType, AssetBase asset, UUID userId, string path) |
||
121 | { |
||
122 | scene.AssetService.Store(asset); |
||
123 | |||
124 | InventoryItemBase item = new InventoryItemBase(); |
||
125 | item.Name = itemName; |
||
126 | item.AssetID = asset.FullID; |
||
127 | item.ID = itemId; |
||
128 | item.Owner = userId; |
||
129 | item.AssetType = asset.Type; |
||
130 | item.InvType = (int)itemType; |
||
131 | |||
132 | InventoryFolderBase folder = InventoryArchiveUtils.FindFoldersByPath(scene.InventoryService, userId, path)[0]; |
||
133 | |||
134 | item.Folder = folder.ID; |
||
135 | scene.AddInventoryItem(item); |
||
136 | |||
137 | return item; |
||
138 | } |
||
139 | |||
140 | /// <summary> |
||
141 | /// Creates a notecard in the objects folder and specify an item id. |
||
142 | /// </summary> |
||
143 | /// <param name="scene"></param> |
||
144 | /// <param name="itemName"></param> |
||
145 | /// <param name="itemId"></param> |
||
146 | /// <param name="userId"></param> |
||
147 | /// <returns></returns> |
||
148 | public static InventoryItemBase CreateInventoryItem(Scene scene, string itemName, UUID userId) |
||
149 | { |
||
150 | return CreateInventoryItem(scene, itemName, UUID.Random(), UUID.Random(), userId, InventoryType.Notecard); |
||
151 | } |
||
152 | |||
153 | /// <summary> |
||
154 | /// Creates an item of the given type with an accompanying asset. |
||
155 | /// </summary> |
||
156 | /// <param name="scene"></param> |
||
157 | /// <param name="itemName"></param> |
||
158 | /// <param name="itemId"></param> |
||
159 | /// <param name="userId"></param> |
||
160 | /// <param name="type">Type of item to create</param> |
||
161 | /// <returns></returns> |
||
162 | public static InventoryItemBase CreateInventoryItem( |
||
163 | Scene scene, string itemName, UUID userId, InventoryType type) |
||
164 | { |
||
165 | return CreateInventoryItem(scene, itemName, UUID.Random(), UUID.Random(), userId, type); |
||
166 | } |
||
167 | |||
168 | /// <summary> |
||
169 | /// Creates a notecard in the objects folder and specify an item id. |
||
170 | /// </summary> |
||
171 | /// <param name="scene"></param> |
||
172 | /// <param name="itemName"></param> |
||
173 | /// <param name="itemId"></param> |
||
174 | /// <param name="assetId"></param> |
||
175 | /// <param name="userId"></param> |
||
176 | /// <param name="type">Type of item to create</param> |
||
177 | /// <returns></returns> |
||
178 | public static InventoryItemBase CreateInventoryItem( |
||
179 | Scene scene, string itemName, UUID itemId, UUID assetId, UUID userId, InventoryType itemType) |
||
180 | { |
||
181 | AssetBase asset = null; |
||
182 | |||
183 | if (itemType == InventoryType.Notecard) |
||
184 | { |
||
185 | asset = AssetHelpers.CreateNotecardAsset(); |
||
186 | asset.CreatorID = userId.ToString(); |
||
187 | } |
||
188 | else if (itemType == InventoryType.Object) |
||
189 | { |
||
190 | asset = AssetHelpers.CreateAsset(assetId, SceneHelpers.CreateSceneObject(1, userId)); |
||
191 | } |
||
192 | else |
||
193 | { |
||
194 | throw new Exception(string.Format("Inventory type {0} not supported", itemType)); |
||
195 | } |
||
196 | |||
197 | return AddInventoryItem(scene, itemName, itemId, itemType, asset, userId); |
||
198 | } |
||
199 | |||
200 | /// <summary> |
||
201 | /// Create inventory folders starting from the user's root folder. |
||
202 | /// </summary> |
||
203 | /// <param name="inventoryService"></param> |
||
204 | /// <param name="userId"></param> |
||
205 | /// <param name="path"> |
||
206 | /// The folders to create. Multiple folders can be specified on a path delimited by the PATH_DELIMITER |
||
207 | /// </param> |
||
208 | /// <param name="useExistingFolders"> |
||
209 | /// If true, then folders in the path which already the same name are |
||
210 | /// used. This applies to the terminal folder as well. |
||
211 | /// If false, then all folders in the path are created, even if there is already a folder at a particular |
||
212 | /// level with the same name. |
||
213 | /// </param> |
||
214 | /// <returns> |
||
215 | /// The folder created. If the path contains multiple folders then the last one created is returned. |
||
216 | /// Will return null if the root folder could not be found. |
||
217 | /// </returns> |
||
218 | public static InventoryFolderBase CreateInventoryFolder( |
||
219 | IInventoryService inventoryService, UUID userId, string path, bool useExistingFolders) |
||
220 | { |
||
221 | return CreateInventoryFolder(inventoryService, userId, UUID.Random(), path, useExistingFolders); |
||
222 | } |
||
223 | |||
224 | /// <summary> |
||
225 | /// Create inventory folders starting from the user's root folder. |
||
226 | /// </summary> |
||
227 | /// <param name="inventoryService"></param> |
||
228 | /// <param name="userId"></param> |
||
229 | /// <param name="folderId"></param> |
||
230 | /// <param name="path"> |
||
231 | /// The folders to create. Multiple folders can be specified on a path delimited by the PATH_DELIMITER |
||
232 | /// </param> |
||
233 | /// <param name="useExistingFolders"> |
||
234 | /// If true, then folders in the path which already the same name are |
||
235 | /// used. This applies to the terminal folder as well. |
||
236 | /// If false, then all folders in the path are created, even if there is already a folder at a particular |
||
237 | /// level with the same name. |
||
238 | /// </param> |
||
239 | /// <returns> |
||
240 | /// The folder created. If the path contains multiple folders then the last one created is returned. |
||
241 | /// Will return null if the root folder could not be found. |
||
242 | /// </returns> |
||
243 | public static InventoryFolderBase CreateInventoryFolder( |
||
244 | IInventoryService inventoryService, UUID userId, UUID folderId, string path, bool useExistingFolders) |
||
245 | { |
||
246 | InventoryFolderBase rootFolder = inventoryService.GetRootFolder(userId); |
||
247 | |||
248 | if (null == rootFolder) |
||
249 | return null; |
||
250 | |||
251 | return CreateInventoryFolder(inventoryService, folderId, rootFolder, path, useExistingFolders); |
||
252 | } |
||
253 | |||
254 | /// <summary> |
||
255 | /// Create inventory folders starting from a given parent folder |
||
256 | /// </summary> |
||
257 | /// <remarks> |
||
258 | /// If any stem of the path names folders that already exist then these are not recreated. This includes the |
||
259 | /// final folder. |
||
260 | /// TODO: May need to make it an option to create duplicate folders. |
||
261 | /// </remarks> |
||
262 | /// <param name="inventoryService"></param> |
||
263 | /// <param name="folderId">ID of the folder to create</param> |
||
264 | /// <param name="parentFolder"></param> |
||
265 | /// <param name="path"> |
||
266 | /// The folder to create. |
||
267 | /// </param> |
||
268 | /// <param name="useExistingFolders"> |
||
269 | /// If true, then folders in the path which already the same name are |
||
270 | /// used. This applies to the terminal folder as well. |
||
271 | /// If false, then all folders in the path are created, even if there is already a folder at a particular |
||
272 | /// level with the same name. |
||
273 | /// </param> |
||
274 | /// <returns> |
||
275 | /// The folder created. If the path contains multiple folders then the last one created is returned. |
||
276 | /// </returns> |
||
277 | public static InventoryFolderBase CreateInventoryFolder( |
||
278 | IInventoryService inventoryService, UUID folderId, InventoryFolderBase parentFolder, string path, bool useExistingFolders) |
||
279 | { |
||
280 | string[] components = path.Split(new string[] { PATH_DELIMITER }, 2, StringSplitOptions.None); |
||
281 | |||
282 | InventoryFolderBase folder = null; |
||
283 | |||
284 | if (useExistingFolders) |
||
285 | folder = InventoryArchiveUtils.FindFolderByPath(inventoryService, parentFolder, components[0]); |
||
286 | |||
287 | if (folder == null) |
||
288 | { |
||
289 | // Console.WriteLine("Creating folder {0} at {1}", components[0], parentFolder.Name); |
||
290 | |||
291 | UUID folderIdForCreate; |
||
292 | |||
293 | if (components.Length > 1) |
||
294 | folderIdForCreate = UUID.Random(); |
||
295 | else |
||
296 | folderIdForCreate = folderId; |
||
297 | |||
298 | folder |
||
299 | = new InventoryFolderBase( |
||
300 | folderIdForCreate, components[0], parentFolder.Owner, (short)AssetType.Unknown, parentFolder.ID, 0); |
||
301 | |||
302 | inventoryService.AddFolder(folder); |
||
303 | } |
||
304 | // else |
||
305 | // { |
||
306 | // Console.WriteLine("Found existing folder {0}", folder.Name); |
||
307 | // } |
||
308 | |||
309 | if (components.Length > 1) |
||
310 | return CreateInventoryFolder(inventoryService, folderId, folder, components[1], useExistingFolders); |
||
311 | else |
||
312 | return folder; |
||
313 | } |
||
314 | |||
315 | /// <summary> |
||
316 | /// Get the inventory folder that matches the path name. If there are multiple folders then only the first |
||
317 | /// is returned. |
||
318 | /// </summary> |
||
319 | /// <param name="inventoryService"></param> |
||
320 | /// <param name="userId"></param> |
||
321 | /// <param name="path"></param> |
||
322 | /// <returns>null if no folder matching the path was found</returns> |
||
323 | public static InventoryFolderBase GetInventoryFolder(IInventoryService inventoryService, UUID userId, string path) |
||
324 | { |
||
325 | List<InventoryFolderBase> folders = GetInventoryFolders(inventoryService, userId, path); |
||
326 | |||
327 | if (folders.Count != 0) |
||
328 | return folders[0]; |
||
329 | else |
||
330 | return null; |
||
331 | } |
||
332 | |||
333 | /// <summary> |
||
334 | /// Get the inventory folders that match the path name. |
||
335 | /// </summary> |
||
336 | /// <param name="inventoryService"></param> |
||
337 | /// <param name="userId"></param> |
||
338 | /// <param name="path"></param> |
||
339 | /// <returns>An empty list if no matching folders were found</returns> |
||
340 | public static List<InventoryFolderBase> GetInventoryFolders(IInventoryService inventoryService, UUID userId, string path) |
||
341 | { |
||
342 | return InventoryArchiveUtils.FindFoldersByPath(inventoryService, userId, path); |
||
343 | } |
||
344 | |||
345 | /// <summary> |
||
346 | /// Get the inventory item that matches the path name. If there are multiple items then only the first |
||
347 | /// is returned. |
||
348 | /// </summary> |
||
349 | /// <param name="inventoryService"></param> |
||
350 | /// <param name="userId"></param> |
||
351 | /// <param name="path"></param> |
||
352 | /// <returns>null if no item matching the path was found</returns> |
||
353 | public static InventoryItemBase GetInventoryItem(IInventoryService inventoryService, UUID userId, string path) |
||
354 | { |
||
355 | return InventoryArchiveUtils.FindItemByPath(inventoryService, userId, path); |
||
356 | } |
||
357 | |||
358 | /// <summary> |
||
359 | /// Get the inventory items that match the path name. |
||
360 | /// </summary> |
||
361 | /// <param name="inventoryService"></param> |
||
362 | /// <param name="userId"></param> |
||
363 | /// <param name="path"></param> |
||
364 | /// <returns>An empty list if no matching items were found.</returns> |
||
365 | public static List<InventoryItemBase> GetInventoryItems(IInventoryService inventoryService, UUID userId, string path) |
||
366 | { |
||
367 | return InventoryArchiveUtils.FindItemsByPath(inventoryService, userId, path); |
||
368 | } |
||
369 | } |
||
370 | } |