UserInventoryHelpers.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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, (AssetType)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. InventoryFolderBase folder = InventoryArchiveUtils.FindFoldersByPath(scene.InventoryService, userId, path)[0];
  125. item.Folder = folder.ID;
  126. scene.AddInventoryItem(item);
  127. return item;
  128. }
  129. /// <summary>
  130. /// Creates a notecard in the objects folder and specify an item id.
  131. /// </summary>
  132. /// <param name="scene"></param>
  133. /// <param name="itemName"></param>
  134. /// <param name="itemId"></param>
  135. /// <param name="userId"></param>
  136. /// <returns></returns>
  137. public static InventoryItemBase CreateInventoryItem(Scene scene, string itemName, UUID userId)
  138. {
  139. return CreateInventoryItem(scene, itemName, UUID.Random(), UUID.Random(), userId, InventoryType.Notecard);
  140. }
  141. /// <summary>
  142. /// Creates an item of the given type with an accompanying asset.
  143. /// </summary>
  144. /// <param name="scene"></param>
  145. /// <param name="itemName"></param>
  146. /// <param name="itemId"></param>
  147. /// <param name="userId"></param>
  148. /// <param name="type">Type of item to create</param>
  149. /// <returns></returns>
  150. public static InventoryItemBase CreateInventoryItem(
  151. Scene scene, string itemName, UUID userId, InventoryType type)
  152. {
  153. return CreateInventoryItem(scene, itemName, UUID.Random(), UUID.Random(), userId, type);
  154. }
  155. /// <summary>
  156. /// Creates a notecard in the objects folder and specify an item id.
  157. /// </summary>
  158. /// <param name="scene"></param>
  159. /// <param name="itemName"></param>
  160. /// <param name="itemId"></param>
  161. /// <param name="assetId"></param>
  162. /// <param name="userId"></param>
  163. /// <param name="type">Type of item to create</param>
  164. /// <returns></returns>
  165. public static InventoryItemBase CreateInventoryItem(
  166. Scene scene, string itemName, UUID itemId, UUID assetId, UUID userId, InventoryType itemType)
  167. {
  168. AssetBase asset = null;
  169. if (itemType == InventoryType.Notecard)
  170. {
  171. asset = AssetHelpers.CreateNotecardAsset();
  172. asset.CreatorID = userId.ToString();
  173. }
  174. else if (itemType == InventoryType.Object)
  175. {
  176. asset = AssetHelpers.CreateAsset(assetId, SceneHelpers.CreateSceneObject(1, userId));
  177. }
  178. else
  179. {
  180. throw new Exception(string.Format("Inventory type {0} not supported", itemType));
  181. }
  182. return AddInventoryItem(scene, itemName, itemId, itemType, asset, userId);
  183. }
  184. /// <summary>
  185. /// Create inventory folders starting from the user's root folder.
  186. /// </summary>
  187. /// <param name="inventoryService"></param>
  188. /// <param name="userId"></param>
  189. /// <param name="path">
  190. /// The folders to create. Multiple folders can be specified on a path delimited by the PATH_DELIMITER
  191. /// </param>
  192. /// <param name="useExistingFolders">
  193. /// If true, then folders in the path which already the same name are
  194. /// used. This applies to the terminal folder as well.
  195. /// If false, then all folders in the path are created, even if there is already a folder at a particular
  196. /// level with the same name.
  197. /// </param>
  198. /// <returns>
  199. /// The folder created. If the path contains multiple folders then the last one created is returned.
  200. /// Will return null if the root folder could not be found.
  201. /// </returns>
  202. public static InventoryFolderBase CreateInventoryFolder(
  203. IInventoryService inventoryService, UUID userId, string path, bool useExistingFolders)
  204. {
  205. return CreateInventoryFolder(inventoryService, userId, UUID.Random(), path, useExistingFolders);
  206. }
  207. /// <summary>
  208. /// Create inventory folders starting from the user's root folder.
  209. /// </summary>
  210. /// <param name="inventoryService"></param>
  211. /// <param name="userId"></param>
  212. /// <param name="folderId"></param>
  213. /// <param name="path">
  214. /// The folders to create. Multiple folders can be specified on a path delimited by the PATH_DELIMITER
  215. /// </param>
  216. /// <param name="useExistingFolders">
  217. /// If true, then folders in the path which already the same name are
  218. /// used. This applies to the terminal folder as well.
  219. /// If false, then all folders in the path are created, even if there is already a folder at a particular
  220. /// level with the same name.
  221. /// </param>
  222. /// <returns>
  223. /// The folder created. If the path contains multiple folders then the last one created is returned.
  224. /// Will return null if the root folder could not be found.
  225. /// </returns>
  226. public static InventoryFolderBase CreateInventoryFolder(
  227. IInventoryService inventoryService, UUID userId, UUID folderId, string path, bool useExistingFolders)
  228. {
  229. InventoryFolderBase rootFolder = inventoryService.GetRootFolder(userId);
  230. if (null == rootFolder)
  231. return null;
  232. return CreateInventoryFolder(inventoryService, folderId, rootFolder, path, useExistingFolders);
  233. }
  234. /// <summary>
  235. /// Create inventory folders starting from a given parent folder
  236. /// </summary>
  237. /// <remarks>
  238. /// If any stem of the path names folders that already exist then these are not recreated. This includes the
  239. /// final folder.
  240. /// TODO: May need to make it an option to create duplicate folders.
  241. /// </remarks>
  242. /// <param name="inventoryService"></param>
  243. /// <param name="folderId">ID of the folder to create</param>
  244. /// <param name="parentFolder"></param>
  245. /// <param name="path">
  246. /// The folder to create.
  247. /// </param>
  248. /// <param name="useExistingFolders">
  249. /// If true, then folders in the path which already the same name are
  250. /// used. This applies to the terminal folder as well.
  251. /// If false, then all folders in the path are created, even if there is already a folder at a particular
  252. /// level with the same name.
  253. /// </param>
  254. /// <returns>
  255. /// The folder created. If the path contains multiple folders then the last one created is returned.
  256. /// </returns>
  257. public static InventoryFolderBase CreateInventoryFolder(
  258. IInventoryService inventoryService, UUID folderId, InventoryFolderBase parentFolder, string path, bool useExistingFolders)
  259. {
  260. string[] components = path.Split(new string[] { PATH_DELIMITER }, 2, StringSplitOptions.None);
  261. InventoryFolderBase folder = null;
  262. if (useExistingFolders)
  263. folder = InventoryArchiveUtils.FindFolderByPath(inventoryService, parentFolder, components[0]);
  264. if (folder == null)
  265. {
  266. // Console.WriteLine("Creating folder {0} at {1}", components[0], parentFolder.Name);
  267. UUID folderIdForCreate;
  268. if (components.Length > 1)
  269. folderIdForCreate = UUID.Random();
  270. else
  271. folderIdForCreate = folderId;
  272. folder
  273. = new InventoryFolderBase(
  274. folderIdForCreate, components[0], parentFolder.Owner, (short)AssetType.Unknown, parentFolder.ID, 0);
  275. inventoryService.AddFolder(folder);
  276. }
  277. // else
  278. // {
  279. // Console.WriteLine("Found existing folder {0}", folder.Name);
  280. // }
  281. if (components.Length > 1)
  282. return CreateInventoryFolder(inventoryService, folderId, folder, components[1], useExistingFolders);
  283. else
  284. return folder;
  285. }
  286. /// <summary>
  287. /// Get the inventory folder that matches the path name. If there are multiple folders then only the first
  288. /// is returned.
  289. /// </summary>
  290. /// <param name="inventoryService"></param>
  291. /// <param name="userId"></param>
  292. /// <param name="path"></param>
  293. /// <returns>null if no folder matching the path was found</returns>
  294. public static InventoryFolderBase GetInventoryFolder(IInventoryService inventoryService, UUID userId, string path)
  295. {
  296. List<InventoryFolderBase> folders = GetInventoryFolders(inventoryService, userId, path);
  297. if (folders.Count != 0)
  298. return folders[0];
  299. else
  300. return null;
  301. }
  302. /// <summary>
  303. /// Get the inventory folders that match the path name.
  304. /// </summary>
  305. /// <param name="inventoryService"></param>
  306. /// <param name="userId"></param>
  307. /// <param name="path"></param>
  308. /// <returns>An empty list if no matching folders were found</returns>
  309. public static List<InventoryFolderBase> GetInventoryFolders(IInventoryService inventoryService, UUID userId, string path)
  310. {
  311. return InventoryArchiveUtils.FindFoldersByPath(inventoryService, userId, path);
  312. }
  313. /// <summary>
  314. /// Get the inventory item that matches the path name. If there are multiple items then only the first
  315. /// is returned.
  316. /// </summary>
  317. /// <param name="inventoryService"></param>
  318. /// <param name="userId"></param>
  319. /// <param name="path"></param>
  320. /// <returns>null if no item matching the path was found</returns>
  321. public static InventoryItemBase GetInventoryItem(IInventoryService inventoryService, UUID userId, string path)
  322. {
  323. return InventoryArchiveUtils.FindItemByPath(inventoryService, userId, path);
  324. }
  325. /// <summary>
  326. /// Get the inventory items that match the path name.
  327. /// </summary>
  328. /// <param name="inventoryService"></param>
  329. /// <param name="userId"></param>
  330. /// <param name="path"></param>
  331. /// <returns>An empty list if no matching items were found.</returns>
  332. public static List<InventoryItemBase> GetInventoryItems(IInventoryService inventoryService, UUID userId, string path)
  333. {
  334. return InventoryArchiveUtils.FindItemsByPath(inventoryService, userId, path);
  335. }
  336. }
  337. }