InventoryServiceBase.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 OpenSim 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.Reflection;
  31. using libsecondlife;
  32. using OpenSim.Framework.Console;
  33. namespace OpenSim.Framework.Communications
  34. {
  35. public abstract class InventoryServiceBase : IInventoryServices
  36. {
  37. protected Dictionary<string, IInventoryData> m_plugins = new Dictionary<string, IInventoryData>();
  38. //protected IAssetServer m_assetServer;
  39. public InventoryServiceBase()
  40. {
  41. //m_assetServer = assetServer;
  42. }
  43. /// <summary>
  44. /// Adds a new user server plugin - plugins will be requested in the order they were loaded.
  45. /// </summary>
  46. /// <param name="FileName">The filename to the user server plugin DLL</param>
  47. public void AddPlugin(string FileName)
  48. {
  49. if (!String.IsNullOrEmpty(FileName))
  50. {
  51. MainLog.Instance.Verbose("AGENTINVENTORY", "Inventorystorage: Attempting to load " + FileName);
  52. Assembly pluginAssembly = Assembly.LoadFrom(FileName);
  53. foreach (Type pluginType in pluginAssembly.GetTypes())
  54. {
  55. if (!pluginType.IsAbstract)
  56. {
  57. Type typeInterface = pluginType.GetInterface("IInventoryData", true);
  58. if (typeInterface != null)
  59. {
  60. IInventoryData plug =
  61. (IInventoryData) Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  62. plug.Initialise();
  63. m_plugins.Add(plug.getName(), plug);
  64. MainLog.Instance.Verbose("AGENTINVENTORY", "Added IInventoryData Interface");
  65. }
  66. }
  67. }
  68. }
  69. }
  70. public List<InventoryFolderBase> RequestFirstLevelFolders(Guid rawUserID)
  71. {
  72. LLUUID userID = new LLUUID(rawUserID);
  73. return RequestFirstLevelFolders(userID);
  74. }
  75. /// <summary>
  76. /// Returns the root folder plus any folders in root (so down one level in the Inventory folders tree)
  77. /// </summary>
  78. /// <param name="userID"></param>
  79. /// <returns></returns>
  80. public List<InventoryFolderBase> RequestFirstLevelFolders(LLUUID userID)
  81. {
  82. List<InventoryFolderBase> inventoryList = new List<InventoryFolderBase>();
  83. InventoryFolderBase rootFolder = null;
  84. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  85. {
  86. rootFolder = plugin.Value.getUserRootFolder(userID);
  87. if (rootFolder != null)
  88. {
  89. MainLog.Instance.Verbose(
  90. "INVENTORY",
  91. "Found root folder for user with ID " + userID + ". Retrieving inventory contents.");
  92. inventoryList = plugin.Value.getInventoryFolders(rootFolder.folderID);
  93. inventoryList.Insert(0, rootFolder);
  94. return inventoryList;
  95. }
  96. }
  97. MainLog.Instance.Warn(
  98. "INVENTORY", "Could not find a root folder belonging to user with ID " + userID);
  99. return inventoryList;
  100. }
  101. /// <summary>
  102. /// Get the root folder for a user
  103. /// </summary>
  104. /// <param name="userID"></param>
  105. /// <returns>null if no root folder was found</returns>
  106. public InventoryFolderBase RequestUsersRoot(LLUUID userID)
  107. {
  108. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  109. {
  110. return plugin.Value.getUserRootFolder(userID);
  111. }
  112. return null;
  113. }
  114. /// <summary>
  115. ///
  116. /// </summary>
  117. public void MoveInventoryFolder(LLUUID userID, InventoryFolderBase folder)
  118. {
  119. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  120. {
  121. plugin.Value.moveInventoryFolder(folder);
  122. }
  123. }
  124. /// <summary>
  125. ///
  126. /// </summary>
  127. /// <param name="parentFolderID"></param>
  128. /// <returns></returns>
  129. public List<InventoryFolderBase> RequestSubFolders(LLUUID parentFolderID)
  130. {
  131. List<InventoryFolderBase> inventoryList = new List<InventoryFolderBase>();
  132. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  133. {
  134. return plugin.Value.getInventoryFolders(parentFolderID);
  135. }
  136. return inventoryList;
  137. }
  138. public List<InventoryItemBase> RequestFolderItems(LLUUID folderID)
  139. {
  140. List<InventoryItemBase> itemsList = new List<InventoryItemBase>();
  141. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  142. {
  143. itemsList = plugin.Value.getInventoryInFolder(folderID);
  144. return itemsList;
  145. }
  146. return itemsList;
  147. }
  148. public void AddFolder(InventoryFolderBase folder)
  149. {
  150. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  151. {
  152. plugin.Value.addInventoryFolder(folder);
  153. }
  154. }
  155. public void MoveFolder(InventoryFolderBase folder)
  156. {
  157. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  158. {
  159. plugin.Value.moveInventoryFolder(folder);
  160. }
  161. }
  162. public void AddItem(InventoryItemBase item)
  163. {
  164. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  165. {
  166. plugin.Value.addInventoryItem(item);
  167. }
  168. }
  169. public void DeleteItem(InventoryItemBase item)
  170. {
  171. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  172. {
  173. plugin.Value.deleteInventoryItem(item.inventoryID);
  174. }
  175. }
  176. /// <summary>
  177. ///
  178. /// </summary>
  179. /// <param name="inventory"></param>
  180. public void AddNewInventorySet(UsersInventory inventory)
  181. {
  182. foreach (InventoryFolderBase folder in inventory.Folders.Values)
  183. {
  184. AddFolder(folder);
  185. }
  186. }
  187. /// <summary>
  188. /// Create a new set of inventory folders for the given user.
  189. /// </summary>
  190. /// <param name="user"></param>
  191. public void CreateNewUserInventory(LLUUID user)
  192. {
  193. InventoryFolderBase existingRootFolder = RequestUsersRoot(user);
  194. if (null != existingRootFolder)
  195. {
  196. MainLog.Instance.Error(
  197. "AGENTINVENTORY",
  198. "Did not create a new inventory for user {0} since they already have "
  199. + "a root inventory folder with id {1}", user, existingRootFolder);
  200. }
  201. else
  202. {
  203. UsersInventory inven = new UsersInventory();
  204. inven.CreateNewInventorySet(user);
  205. AddNewInventorySet(inven);
  206. }
  207. }
  208. public class UsersInventory
  209. {
  210. public Dictionary<LLUUID, InventoryFolderBase> Folders = new Dictionary<LLUUID, InventoryFolderBase>();
  211. public Dictionary<LLUUID, InventoryItemBase> Items = new Dictionary<LLUUID, InventoryItemBase>();
  212. public UsersInventory()
  213. {
  214. }
  215. public virtual void CreateNewInventorySet(LLUUID user)
  216. {
  217. InventoryFolderBase folder = new InventoryFolderBase();
  218. folder.parentID = LLUUID.Zero;
  219. folder.agentID = user;
  220. folder.folderID = LLUUID.Random();
  221. folder.name = "My Inventory";
  222. folder.type = 8;
  223. folder.version = 1;
  224. Folders.Add(folder.folderID, folder);
  225. LLUUID rootFolder = folder.folderID;
  226. folder = new InventoryFolderBase();
  227. folder.parentID = rootFolder;
  228. folder.agentID = user;
  229. folder.folderID = LLUUID.Random();
  230. folder.name = "Accessories";
  231. folder.type = 8;
  232. folder.version = 1;
  233. Folders.Add(folder.folderID, folder);
  234. folder = new InventoryFolderBase();
  235. folder.parentID = rootFolder;
  236. folder.agentID = user;
  237. folder.folderID = LLUUID.Random();
  238. folder.name = "Animations";
  239. folder.type = 20;
  240. folder.version = 1;
  241. Folders.Add(folder.folderID, folder);
  242. folder = new InventoryFolderBase();
  243. folder.parentID = rootFolder;
  244. folder.agentID = user;
  245. folder.folderID = LLUUID.Random();
  246. folder.name = "BodyParts";
  247. folder.type = 13;
  248. folder.version = 1;
  249. Folders.Add(folder.folderID, folder);
  250. folder = new InventoryFolderBase();
  251. folder.parentID = rootFolder;
  252. folder.agentID = user;
  253. folder.folderID = LLUUID.Random();
  254. folder.name = "Clothing";
  255. folder.type = 5;
  256. folder.version = 1;
  257. Folders.Add(folder.folderID, folder);
  258. folder = new InventoryFolderBase();
  259. folder.parentID = rootFolder;
  260. folder.agentID = user;
  261. folder.folderID = LLUUID.Random();
  262. folder.name = "Gestures";
  263. folder.type = 21;
  264. folder.version = 1;
  265. Folders.Add(folder.folderID, folder);
  266. folder = new InventoryFolderBase();
  267. folder.parentID = rootFolder;
  268. folder.agentID = user;
  269. folder.folderID = LLUUID.Random();
  270. folder.name = "Landmarks";
  271. folder.type = 3;
  272. folder.version = 1;
  273. Folders.Add(folder.folderID, folder);
  274. folder = new InventoryFolderBase();
  275. folder.parentID = rootFolder;
  276. folder.agentID = user;
  277. folder.folderID = LLUUID.Random();
  278. folder.name = "Lost And Found";
  279. folder.type = 3;
  280. folder.version = 1;
  281. Folders.Add(folder.folderID, folder);
  282. folder = new InventoryFolderBase();
  283. folder.parentID = rootFolder;
  284. folder.agentID = user;
  285. folder.folderID = LLUUID.Random();
  286. folder.name = "Notecards";
  287. folder.type = 7;
  288. folder.version = 1;
  289. Folders.Add(folder.folderID, folder);
  290. folder = new InventoryFolderBase();
  291. folder.parentID = rootFolder;
  292. folder.agentID = user;
  293. folder.folderID = LLUUID.Random();
  294. folder.name = "Objects";
  295. folder.type = 6;
  296. folder.version = 1;
  297. Folders.Add(folder.folderID, folder);
  298. folder = new InventoryFolderBase();
  299. folder.parentID = rootFolder;
  300. folder.agentID = user;
  301. folder.folderID = LLUUID.Random();
  302. folder.name = "Photo Album";
  303. folder.type = 15;
  304. folder.version = 1;
  305. Folders.Add(folder.folderID, folder);
  306. folder = new InventoryFolderBase();
  307. folder.parentID = rootFolder;
  308. folder.agentID = user;
  309. folder.folderID = LLUUID.Random();
  310. folder.name = "Scripts";
  311. folder.type = 10;
  312. folder.version = 1;
  313. Folders.Add(folder.folderID, folder);
  314. folder = new InventoryFolderBase();
  315. folder.parentID = rootFolder;
  316. folder.agentID = user;
  317. folder.folderID = LLUUID.Random();
  318. folder.name = "Sounds";
  319. folder.type = 1;
  320. folder.version = 1;
  321. Folders.Add(folder.folderID, folder);
  322. folder = new InventoryFolderBase();
  323. folder.parentID = rootFolder;
  324. folder.agentID = user;
  325. folder.folderID = LLUUID.Random();
  326. folder.name = "Textures";
  327. folder.type = 0;
  328. folder.version = 1;
  329. Folders.Add(folder.folderID, folder);
  330. folder = new InventoryFolderBase();
  331. folder.parentID = rootFolder;
  332. folder.agentID = user;
  333. folder.folderID = LLUUID.Random();
  334. folder.name = "Trash";
  335. folder.type = 14;
  336. folder.version = 1;
  337. Folders.Add(folder.folderID, folder);
  338. }
  339. }
  340. public abstract void RequestInventoryForUser(LLUUID userID, InventoryFolderInfo folderCallBack,
  341. InventoryItemInfo itemCallBack);
  342. public abstract void AddNewInventoryFolder(LLUUID userID, InventoryFolderBase folder);
  343. public abstract void MoveExistingInventoryFolder(InventoryFolderBase folder);
  344. public abstract void AddNewInventoryItem(LLUUID userID, InventoryItemBase item);
  345. public abstract void DeleteInventoryItem(LLUUID userID, InventoryItemBase item);
  346. }
  347. }