InventoryServiceBase.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 libsecondlife;
  31. using OpenSim.Framework.Console;
  32. namespace OpenSim.Framework.Communications
  33. {
  34. public abstract class InventoryServiceBase : IInventoryServices
  35. {
  36. private static readonly log4net.ILog m_log
  37. = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  38. protected Dictionary<string, IInventoryData> m_plugins = new Dictionary<string, IInventoryData>();
  39. #region Plugin methods
  40. /// <summary>
  41. /// Adds a new user server plugin - plugins will be requested in the order they were loaded.
  42. /// </summary>
  43. /// <param name="FileName">The filename to the user server plugin DLL</param>
  44. public void AddPlugin(string FileName)
  45. {
  46. if (!String.IsNullOrEmpty(FileName))
  47. {
  48. m_log.Info("[AGENTINVENTORY]: Inventory storage: Attempting to load " + FileName);
  49. Assembly pluginAssembly = Assembly.LoadFrom(FileName);
  50. foreach (Type pluginType in pluginAssembly.GetTypes())
  51. {
  52. if (!pluginType.IsAbstract)
  53. {
  54. Type typeInterface = pluginType.GetInterface("IInventoryData", true);
  55. if (typeInterface != null)
  56. {
  57. IInventoryData plug =
  58. (IInventoryData) Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  59. plug.Initialise();
  60. m_plugins.Add(plug.getName(), plug);
  61. m_log.Info("[AGENTINVENTORY]: Added IInventoryData Interface");
  62. }
  63. }
  64. }
  65. }
  66. }
  67. #endregion
  68. #region IInventoryServices methods
  69. // See IInventoryServices
  70. public List<InventoryFolderBase> GetInventorySkeleton(LLUUID userId)
  71. {
  72. //m_log.DebugFormat("[AGENT INVENTORY]: Getting inventory skeleton for {0}", userId);
  73. List<InventoryFolderBase> userFolders = new List<InventoryFolderBase>();
  74. InventoryFolderBase rootFolder = RequestRootFolder(userId);
  75. userFolders.Add(rootFolder);
  76. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  77. {
  78. IList<InventoryFolderBase> folders = plugin.Value.getFolderHierarchy(rootFolder.folderID);
  79. userFolders.AddRange(folders);
  80. }
  81. // foreach (InventoryFolderBase folder in userFolders)
  82. // {
  83. // m_log.DebugFormat("[AGENT INVENTORY]: Got folder {0} {1}", folder.name, folder.folderID);
  84. // }
  85. return userFolders;
  86. }
  87. // See IInventoryServices
  88. public void MoveInventoryFolder(LLUUID userID, InventoryFolderBase folder)
  89. {
  90. // FIXME: Probably doesn't do what was originally intended - only ever queries the first plugin
  91. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  92. {
  93. plugin.Value.moveInventoryFolder(folder);
  94. }
  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 void CreateNewUserInventory(LLUUID user)
  113. {
  114. InventoryFolderBase existingRootFolder = RequestRootFolder(user);
  115. if (null != existingRootFolder)
  116. {
  117. m_log.ErrorFormat("[AGENT INVENTORY]: " +
  118. "Did not create a new inventory for user {0} since they already have "
  119. + "a root inventory folder with id {1}", user, existingRootFolder);
  120. }
  121. else
  122. {
  123. UsersInventory inven = new UsersInventory();
  124. inven.CreateNewInventorySet(user);
  125. AddNewInventorySet(inven);
  126. }
  127. }
  128. public abstract void RequestInventoryForUser(LLUUID userID, InventoryFolderInfo folderCallBack,
  129. InventoryItemInfo itemCallBack);
  130. public abstract void AddNewInventoryFolder(LLUUID userID, InventoryFolderBase folder);
  131. public abstract void MoveExistingInventoryFolder(InventoryFolderBase folder);
  132. public abstract void AddNewInventoryItem(LLUUID userID, InventoryItemBase item);
  133. public abstract void DeleteInventoryItem(LLUUID userID, InventoryItemBase item);
  134. #endregion
  135. #region Methods used by GridInventoryService
  136. public List<InventoryFolderBase> RequestSubFolders(LLUUID parentFolderID)
  137. {
  138. List<InventoryFolderBase> inventoryList = new List<InventoryFolderBase>();
  139. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  140. {
  141. return plugin.Value.getInventoryFolders(parentFolderID);
  142. }
  143. return inventoryList;
  144. }
  145. public List<InventoryItemBase> RequestFolderItems(LLUUID folderID)
  146. {
  147. List<InventoryItemBase> itemsList = new List<InventoryItemBase>();
  148. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  149. {
  150. itemsList = plugin.Value.getInventoryInFolder(folderID);
  151. return itemsList;
  152. }
  153. return itemsList;
  154. }
  155. #endregion
  156. protected void AddFolder(InventoryFolderBase folder)
  157. {
  158. m_log.DebugFormat(
  159. "[INVENTORY SERVICE BASE]: Adding folder {0}, {1} to {2}", folder.name, folder.folderID, folder.parentID);
  160. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  161. {
  162. plugin.Value.addInventoryFolder(folder);
  163. }
  164. }
  165. protected void MoveFolder(InventoryFolderBase folder)
  166. {
  167. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  168. {
  169. plugin.Value.moveInventoryFolder(folder);
  170. }
  171. }
  172. protected void AddItem(InventoryItemBase item)
  173. {
  174. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  175. {
  176. plugin.Value.addInventoryItem(item);
  177. }
  178. }
  179. protected void DeleteItem(InventoryItemBase item)
  180. {
  181. foreach (KeyValuePair<string, IInventoryData> plugin in m_plugins)
  182. {
  183. plugin.Value.deleteInventoryItem(item.inventoryID);
  184. }
  185. }
  186. private void AddNewInventorySet(UsersInventory inventory)
  187. {
  188. foreach (InventoryFolderBase folder in inventory.Folders.Values)
  189. {
  190. AddFolder(folder);
  191. }
  192. }
  193. private class UsersInventory
  194. {
  195. public Dictionary<LLUUID, InventoryFolderBase> Folders = new Dictionary<LLUUID, InventoryFolderBase>();
  196. public Dictionary<LLUUID, InventoryItemBase> Items = new Dictionary<LLUUID, InventoryItemBase>();
  197. public virtual void CreateNewInventorySet(LLUUID user)
  198. {
  199. InventoryFolderBase folder = new InventoryFolderBase();
  200. folder.parentID = LLUUID.Zero;
  201. folder.agentID = user;
  202. folder.folderID = LLUUID.Random();
  203. folder.name = "My Inventory";
  204. folder.type = (short)AssetType.Folder;
  205. folder.version = 1;
  206. Folders.Add(folder.folderID, folder);
  207. LLUUID rootFolder = folder.folderID;
  208. folder = new InventoryFolderBase();
  209. folder.parentID = rootFolder;
  210. folder.agentID = user;
  211. folder.folderID = LLUUID.Random();
  212. folder.name = "Animations";
  213. folder.type = (short)AssetType.Animation;
  214. folder.version = 1;
  215. Folders.Add(folder.folderID, folder);
  216. folder = new InventoryFolderBase();
  217. folder.parentID = rootFolder;
  218. folder.agentID = user;
  219. folder.folderID = LLUUID.Random();
  220. folder.name = "Body Parts";
  221. folder.type = (short)AssetType.Bodypart;
  222. folder.version = 1;
  223. Folders.Add(folder.folderID, folder);
  224. folder = new InventoryFolderBase();
  225. folder.parentID = rootFolder;
  226. folder.agentID = user;
  227. folder.folderID = LLUUID.Random();
  228. folder.name = "Calling Cards";
  229. folder.type = (short)AssetType.CallingCard;
  230. folder.version = 1;
  231. Folders.Add(folder.folderID, folder);
  232. folder = new InventoryFolderBase();
  233. folder.parentID = rootFolder;
  234. folder.agentID = user;
  235. folder.folderID = LLUUID.Random();
  236. folder.name = "Clothing";
  237. folder.type = (short)AssetType.Clothing;
  238. folder.version = 1;
  239. Folders.Add(folder.folderID, folder);
  240. folder = new InventoryFolderBase();
  241. folder.parentID = rootFolder;
  242. folder.agentID = user;
  243. folder.folderID = LLUUID.Random();
  244. folder.name = "Gestures";
  245. folder.type = (short)AssetType.Gesture;
  246. folder.version = 1;
  247. Folders.Add(folder.folderID, folder);
  248. folder = new InventoryFolderBase();
  249. folder.parentID = rootFolder;
  250. folder.agentID = user;
  251. folder.folderID = LLUUID.Random();
  252. folder.name = "Landmarks";
  253. folder.type = (short)AssetType.Landmark;
  254. folder.version = 1;
  255. Folders.Add(folder.folderID, folder);
  256. folder = new InventoryFolderBase();
  257. folder.parentID = rootFolder;
  258. folder.agentID = user;
  259. folder.folderID = LLUUID.Random();
  260. folder.name = "Lost And Found";
  261. folder.type = (short)AssetType.LostAndFoundFolder;
  262. folder.version = 1;
  263. Folders.Add(folder.folderID, folder);
  264. folder = new InventoryFolderBase();
  265. folder.parentID = rootFolder;
  266. folder.agentID = user;
  267. folder.folderID = LLUUID.Random();
  268. folder.name = "Notecards";
  269. folder.type = (short)AssetType.Notecard;
  270. folder.version = 1;
  271. Folders.Add(folder.folderID, folder);
  272. folder = new InventoryFolderBase();
  273. folder.parentID = rootFolder;
  274. folder.agentID = user;
  275. folder.folderID = LLUUID.Random();
  276. folder.name = "Objects";
  277. folder.type = (short)AssetType.Primitive;
  278. folder.version = 1;
  279. Folders.Add(folder.folderID, folder);
  280. folder = new InventoryFolderBase();
  281. folder.parentID = rootFolder;
  282. folder.agentID = user;
  283. folder.folderID = LLUUID.Random();
  284. folder.name = "Photo Album";
  285. folder.type = (short)AssetType.SnapshotFolder;
  286. folder.version = 1;
  287. Folders.Add(folder.folderID, folder);
  288. folder = new InventoryFolderBase();
  289. folder.parentID = rootFolder;
  290. folder.agentID = user;
  291. folder.folderID = LLUUID.Random();
  292. folder.name = "Scripts";
  293. folder.type = (short)AssetType.LSLText;
  294. folder.version = 1;
  295. Folders.Add(folder.folderID, folder);
  296. folder = new InventoryFolderBase();
  297. folder.parentID = rootFolder;
  298. folder.agentID = user;
  299. folder.folderID = LLUUID.Random();
  300. folder.name = "Sounds";
  301. folder.type = (short)AssetType.Sound;
  302. folder.version = 1;
  303. Folders.Add(folder.folderID, folder);
  304. folder = new InventoryFolderBase();
  305. folder.parentID = rootFolder;
  306. folder.agentID = user;
  307. folder.folderID = LLUUID.Random();
  308. folder.name = "Textures";
  309. folder.type = (short)AssetType.Texture;
  310. folder.version = 1;
  311. Folders.Add(folder.folderID, folder);
  312. folder = new InventoryFolderBase();
  313. folder.parentID = rootFolder;
  314. folder.agentID = user;
  315. folder.folderID = LLUUID.Random();
  316. folder.name = "Trash";
  317. folder.type = (short)AssetType.TrashFolder;
  318. folder.version = 1;
  319. Folders.Add(folder.folderID, folder);
  320. }
  321. }
  322. }
  323. }