UserInventoryHelpers.cs 16 KB

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