InventoryServiceBase.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Threading;
  31. using libsecondlife;
  32. using log4net;
  33. namespace OpenSim.Framework.Communications
  34. {
  35. /// <summary>
  36. /// Abstract base class used by local and grid implementations of an inventory service.
  37. /// </summary>
  38. public abstract class InventoryServiceBase : IInventoryServices
  39. {
  40. private static readonly ILog m_log
  41. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. protected Dictionary<string, IInventoryData> m_plugins = new Dictionary<string, IInventoryData>();
  43. #region Plugin methods
  44. /// <summary>
  45. /// Adds a new user server plugin - plugins will be requested in the order they were loaded.
  46. /// </summary>
  47. /// <param name="FileName">The filename to the user server plugin DLL</param>
  48. public void AddPlugin(string FileName, string connect)
  49. {
  50. if (!String.IsNullOrEmpty(FileName))
  51. {
  52. m_log.Info("[AGENT INVENTORY]: Inventory storage: Attempting to load " + FileName);
  53. Assembly pluginAssembly = Assembly.LoadFrom(FileName);
  54. foreach (Type pluginType in pluginAssembly.GetTypes())
  55. {
  56. if (!pluginType.IsAbstract)
  57. {
  58. Type typeInterface = pluginType.GetInterface("IInventoryData", true);
  59. if (typeInterface != null)
  60. {
  61. IInventoryData plug =
  62. (IInventoryData) Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  63. plug.Initialise(connect);
  64. m_plugins.Add(plug.getName(), plug);
  65. m_log.Info("[AGENTINVENTORY]: Added IInventoryData Interface");
  66. }
  67. }
  68. }
  69. }
  70. }
  71. #endregion
  72. #region IInventoryServices methods
  73. // See IInventoryServices
  74. public List<InventoryFolderBase> GetInventorySkeleton(LLUUID userId)
  75. {
  76. // m_log.DebugFormat("[AGENT INVENTORY]: Getting inventory skeleton for {0}", userId);
  77. InventoryFolderBase rootFolder = RequestRootFolder(userId);
  78. // Agent has no inventory structure yet.
  79. if (null == rootFolder)
  80. {
  81. return null;
  82. }
  83. List<InventoryFolderBase> userFolders = new List<InventoryFolderBase>();
  84. userFolders.Add(rootFolder);
  85. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  86. {
  87. IList<InventoryFolderBase> folders = plugin.Value.getFolderHierarchy(rootFolder.ID);
  88. userFolders.AddRange(folders);
  89. }
  90. // foreach (InventoryFolderBase folder in userFolders)
  91. // {
  92. // m_log.DebugFormat("[AGENT INVENTORY]: Got folder {0} {1}", folder.name, folder.folderID);
  93. // }
  94. return userFolders;
  95. }
  96. // See IInventoryServices
  97. public virtual bool HasInventoryForUser(LLUUID userID)
  98. {
  99. return false;
  100. }
  101. // See IInventoryServices
  102. public InventoryFolderBase RequestRootFolder(LLUUID userID)
  103. {
  104. // FIXME: Probably doesn't do what was originally intended - only ever queries the first plugin
  105. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  106. {
  107. return plugin.Value.getUserRootFolder(userID);
  108. }
  109. return null;
  110. }
  111. // See IInventoryServices
  112. public bool CreateNewUserInventory(LLUUID user)
  113. {
  114. InventoryFolderBase existingRootFolder = RequestRootFolder(user);
  115. if (null != existingRootFolder)
  116. {
  117. m_log.WarnFormat(
  118. "[AGENT INVENTORY]: Did not create a new inventory for user {0} since they already have "
  119. + "a root inventory folder with id {1}",
  120. user, existingRootFolder.ID);
  121. }
  122. else
  123. {
  124. UsersInventory inven = new UsersInventory();
  125. inven.CreateNewInventorySet(user);
  126. AddNewInventorySet(inven);
  127. return true;
  128. }
  129. return false;
  130. }
  131. // See IInventoryServices
  132. public abstract void RequestInventoryForUser(LLUUID userID, InventoryReceiptCallback callback);
  133. #endregion
  134. #region Methods used by GridInventoryService
  135. public List<InventoryFolderBase> RequestSubFolders(LLUUID parentFolderID)
  136. {
  137. List<InventoryFolderBase> inventoryList = new List<InventoryFolderBase>();
  138. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  139. {
  140. return plugin.Value.getInventoryFolders(parentFolderID);
  141. }
  142. return inventoryList;
  143. }
  144. public List<InventoryItemBase> RequestFolderItems(LLUUID folderID)
  145. {
  146. List<InventoryItemBase> itemsList = new List<InventoryItemBase>();
  147. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  148. {
  149. itemsList = plugin.Value.getInventoryInFolder(folderID);
  150. return itemsList;
  151. }
  152. return itemsList;
  153. }
  154. #endregion
  155. // See IInventoryServices
  156. public bool AddFolder(InventoryFolderBase folder)
  157. {
  158. m_log.DebugFormat(
  159. "[AGENT INVENTORY]: Adding folder {0} {1} to folder {2}", folder.Name, folder.ID, folder.ParentID);
  160. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  161. {
  162. plugin.Value.addInventoryFolder(folder);
  163. }
  164. // FIXME: Should return false on failure
  165. return true;
  166. }
  167. // See IInventoryServices
  168. public bool MoveFolder(InventoryFolderBase folder)
  169. {
  170. m_log.DebugFormat(
  171. "[AGENT INVENTORY]: Moving folder {0} {1} to folder {2}", folder.Name, folder.ID, folder.ParentID);
  172. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  173. {
  174. plugin.Value.moveInventoryFolder(folder);
  175. }
  176. // FIXME: Should return false on failure
  177. return true;
  178. }
  179. // See IInventoryServices
  180. public bool AddItem(InventoryItemBase item)
  181. {
  182. m_log.DebugFormat(
  183. "[AGENT INVENTORY]: Adding item {0} {1} to folder {2}", item.Name, item.ID, item.Folder);
  184. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  185. {
  186. plugin.Value.addInventoryItem(item);
  187. }
  188. // FIXME: Should return false on failure
  189. return true;
  190. }
  191. // See IInventoryServices
  192. public bool UpdateItem(InventoryItemBase item)
  193. {
  194. m_log.InfoFormat(
  195. "[AGENT INVENTORY]: Updating item {0} {1} in folder {2}", item.Name, item.ID, item.Folder);
  196. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  197. {
  198. plugin.Value.updateInventoryItem(item);
  199. }
  200. // FIXME: Should return false on failure
  201. return true;
  202. }
  203. // See IInventoryServices
  204. public bool DeleteItem(InventoryItemBase item)
  205. {
  206. m_log.InfoFormat(
  207. "[AGENT INVENTORY]: Deleting item {0} {1} from folder {2}", item.Name, item.ID, item.Folder);
  208. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  209. {
  210. plugin.Value.deleteInventoryItem(item.ID);
  211. }
  212. // FIXME: Should return false on failure
  213. return true;
  214. }
  215. /// <summary>
  216. /// Purge a folder of all items items and subfolders.
  217. ///
  218. /// FIXME: Really nasty in a sense, because we have to query the database to get information we may
  219. /// already know... Needs heavy refactoring.
  220. /// </summary>
  221. /// <param name="folder"></param>
  222. public bool PurgeFolder(InventoryFolderBase folder)
  223. {
  224. m_log.DebugFormat(
  225. "[AGENT INVENTORY]: Purging folder {0} {1} of its contents", folder.Name, folder.ID);
  226. List<InventoryFolderBase> subFolders = RequestSubFolders(folder.ID);
  227. foreach (InventoryFolderBase subFolder in subFolders)
  228. {
  229. // m_log.DebugFormat("[AGENT INVENTORY]: Deleting folder {0} {1}", subFolder.Name, subFolder.ID);
  230. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  231. {
  232. plugin.Value.deleteInventoryFolder(subFolder.ID);
  233. }
  234. }
  235. List<InventoryItemBase> items = RequestFolderItems(folder.ID);
  236. foreach (InventoryItemBase item in items)
  237. {
  238. DeleteItem(item);
  239. }
  240. // FIXME: Should return false on failure
  241. return true;
  242. }
  243. private void AddNewInventorySet(UsersInventory inventory)
  244. {
  245. foreach (InventoryFolderBase folder in inventory.Folders.Values)
  246. {
  247. AddFolder(folder);
  248. }
  249. }
  250. /// <summary>
  251. /// Used to create a new user inventory.
  252. /// </summary>
  253. private class UsersInventory
  254. {
  255. public Dictionary<LLUUID, InventoryFolderBase> Folders = new Dictionary<LLUUID, InventoryFolderBase>();
  256. public Dictionary<LLUUID, InventoryItemBase> Items = new Dictionary<LLUUID, InventoryItemBase>();
  257. public virtual void CreateNewInventorySet(LLUUID user)
  258. {
  259. InventoryFolderBase folder = new InventoryFolderBase();
  260. folder.ParentID = LLUUID.Zero;
  261. folder.Owner = user;
  262. folder.ID = LLUUID.Random();
  263. folder.Name = "My Inventory";
  264. folder.Type = (short)AssetType.Folder;
  265. folder.Version = 1;
  266. Folders.Add(folder.ID, folder);
  267. LLUUID rootFolder = folder.ID;
  268. folder = new InventoryFolderBase();
  269. folder.ParentID = rootFolder;
  270. folder.Owner = user;
  271. folder.ID = LLUUID.Random();
  272. folder.Name = "Animations";
  273. folder.Type = (short)AssetType.Animation;
  274. folder.Version = 1;
  275. Folders.Add(folder.ID, folder);
  276. folder = new InventoryFolderBase();
  277. folder.ParentID = rootFolder;
  278. folder.Owner = user;
  279. folder.ID = LLUUID.Random();
  280. folder.Name = "Body Parts";
  281. folder.Type = (short)AssetType.Bodypart;
  282. folder.Version = 1;
  283. Folders.Add(folder.ID, folder);
  284. folder = new InventoryFolderBase();
  285. folder.ParentID = rootFolder;
  286. folder.Owner = user;
  287. folder.ID = LLUUID.Random();
  288. folder.Name = "Calling Cards";
  289. folder.Type = (short)AssetType.CallingCard;
  290. folder.Version = 1;
  291. Folders.Add(folder.ID, folder);
  292. folder = new InventoryFolderBase();
  293. folder.ParentID = rootFolder;
  294. folder.Owner = user;
  295. folder.ID = LLUUID.Random();
  296. folder.Name = "Clothing";
  297. folder.Type = (short)AssetType.Clothing;
  298. folder.Version = 1;
  299. Folders.Add(folder.ID, folder);
  300. folder = new InventoryFolderBase();
  301. folder.ParentID = rootFolder;
  302. folder.Owner = user;
  303. folder.ID = LLUUID.Random();
  304. folder.Name = "Gestures";
  305. folder.Type = (short)AssetType.Gesture;
  306. folder.Version = 1;
  307. Folders.Add(folder.ID, folder);
  308. folder = new InventoryFolderBase();
  309. folder.ParentID = rootFolder;
  310. folder.Owner = user;
  311. folder.ID = LLUUID.Random();
  312. folder.Name = "Landmarks";
  313. folder.Type = (short)AssetType.Landmark;
  314. folder.Version = 1;
  315. Folders.Add(folder.ID, folder);
  316. folder = new InventoryFolderBase();
  317. folder.ParentID = rootFolder;
  318. folder.Owner = user;
  319. folder.ID = LLUUID.Random();
  320. folder.Name = "Lost And Found";
  321. folder.Type = (short)AssetType.LostAndFoundFolder;
  322. folder.Version = 1;
  323. Folders.Add(folder.ID, folder);
  324. folder = new InventoryFolderBase();
  325. folder.ParentID = rootFolder;
  326. folder.Owner = user;
  327. folder.ID = LLUUID.Random();
  328. folder.Name = "Notecards";
  329. folder.Type = (short)AssetType.Notecard;
  330. folder.Version = 1;
  331. Folders.Add(folder.ID, folder);
  332. folder = new InventoryFolderBase();
  333. folder.ParentID = rootFolder;
  334. folder.Owner = user;
  335. folder.ID = LLUUID.Random();
  336. folder.Name = "Objects";
  337. folder.Type = (short)AssetType.Object;
  338. folder.Version = 1;
  339. Folders.Add(folder.ID, folder);
  340. folder = new InventoryFolderBase();
  341. folder.ParentID = rootFolder;
  342. folder.Owner = user;
  343. folder.ID = LLUUID.Random();
  344. folder.Name = "Photo Album";
  345. folder.Type = (short)AssetType.SnapshotFolder;
  346. folder.Version = 1;
  347. Folders.Add(folder.ID, folder);
  348. folder = new InventoryFolderBase();
  349. folder.ParentID = rootFolder;
  350. folder.Owner = user;
  351. folder.ID = LLUUID.Random();
  352. folder.Name = "Scripts";
  353. folder.Type = (short)AssetType.LSLText;
  354. folder.Version = 1;
  355. Folders.Add(folder.ID, folder);
  356. folder = new InventoryFolderBase();
  357. folder.ParentID = rootFolder;
  358. folder.Owner = user;
  359. folder.ID = LLUUID.Random();
  360. folder.Name = "Sounds";
  361. folder.Type = (short)AssetType.Sound;
  362. folder.Version = 1;
  363. Folders.Add(folder.ID, folder);
  364. folder = new InventoryFolderBase();
  365. folder.ParentID = rootFolder;
  366. folder.Owner = user;
  367. folder.ID = LLUUID.Random();
  368. folder.Name = "Textures";
  369. folder.Type = (short)AssetType.Texture;
  370. folder.Version = 1;
  371. Folders.Add(folder.ID, folder);
  372. folder = new InventoryFolderBase();
  373. folder.ParentID = rootFolder;
  374. folder.Owner = user;
  375. folder.ID = LLUUID.Random();
  376. folder.Name = "Trash";
  377. folder.Type = (short)AssetType.TrashFolder;
  378. folder.Version = 1;
  379. Folders.Add(folder.ID, folder);
  380. }
  381. }
  382. }
  383. }