SceneObjectGroup.Inventory.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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.IO;
  29. using System.Reflection;
  30. using OpenMetaverse;
  31. using log4net;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Framework.Interfaces;
  34. using System.Collections.Generic;
  35. using System.Xml;
  36. namespace OpenSim.Region.Framework.Scenes
  37. {
  38. public partial class SceneObjectGroup : EntityBase
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. /// <summary>
  42. /// Force all task inventories of prims in the scene object to persist
  43. /// </summary>
  44. public void ForceInventoryPersistence()
  45. {
  46. lock (m_parts)
  47. {
  48. foreach (SceneObjectPart part in m_parts.Values)
  49. {
  50. part.Inventory.ForceInventoryPersistence();
  51. }
  52. }
  53. }
  54. /// <summary>
  55. /// Start the scripts contained in all the prims in this group.
  56. /// </summary>
  57. public void CreateScriptInstances(int startParam, bool postOnRez,
  58. string engine, int stateSource)
  59. {
  60. // Don't start scripts if they're turned off in the region!
  61. if (!m_scene.RegionInfo.RegionSettings.DisableScripts)
  62. {
  63. foreach (SceneObjectPart part in m_parts.Values)
  64. {
  65. part.Inventory.CreateScriptInstances(startParam, postOnRez, engine, stateSource);
  66. }
  67. }
  68. }
  69. /// <summary>
  70. /// Stop the scripts contained in all the prims in this group
  71. /// </summary>
  72. /// <param name="sceneObjectBeingDeleted">
  73. /// Should be true if these scripts are being removed because the scene
  74. /// object is being deleted. This will prevent spurious updates to the client.
  75. /// </param>
  76. public void RemoveScriptInstances(bool sceneObjectBeingDeleted)
  77. {
  78. lock (m_parts)
  79. {
  80. foreach (SceneObjectPart part in m_parts.Values)
  81. {
  82. part.Inventory.RemoveScriptInstances(sceneObjectBeingDeleted);
  83. }
  84. }
  85. }
  86. /// <summary>
  87. ///
  88. /// </summary>
  89. /// <param name="remoteClient"></param>
  90. /// <param name="localID"></param>
  91. public bool GetPartInventoryFileName(IClientAPI remoteClient, uint localID)
  92. {
  93. SceneObjectPart part = GetChildPart(localID);
  94. if (part != null)
  95. {
  96. return part.Inventory.GetInventoryFileName(remoteClient, localID);
  97. }
  98. else
  99. {
  100. m_log.ErrorFormat(
  101. "[PRIM INVENTORY]: " +
  102. "Couldn't find part {0} in object group {1}, {2} to retreive prim inventory",
  103. localID, Name, UUID);
  104. }
  105. return false;
  106. }
  107. /// <summary>
  108. /// Return serialized inventory metadata for the given constituent prim
  109. /// </summary>
  110. /// <param name="localID"></param>
  111. /// <param name="xferManager"></param>
  112. public void RequestInventoryFile(IClientAPI client, uint localID, IXfer xferManager)
  113. {
  114. SceneObjectPart part = GetChildPart(localID);
  115. if (part != null)
  116. {
  117. part.Inventory.RequestInventoryFile(client, xferManager);
  118. }
  119. else
  120. {
  121. m_log.ErrorFormat(
  122. "[PRIM INVENTORY]: " +
  123. "Couldn't find part {0} in object group {1}, {2} to request inventory data",
  124. localID, Name, UUID);
  125. }
  126. }
  127. /// <summary>
  128. /// Add an inventory item to a prim in this group.
  129. /// </summary>
  130. /// <param name="remoteClient"></param>
  131. /// <param name="localID"></param>
  132. /// <param name="item"></param>
  133. /// <param name="copyItemID">The item UUID that should be used by the new item.</param>
  134. /// <returns></returns>
  135. public bool AddInventoryItem(IClientAPI remoteClient, uint localID,
  136. InventoryItemBase item, UUID copyItemID)
  137. {
  138. UUID newItemId = (copyItemID != UUID.Zero) ? copyItemID : item.ID;
  139. SceneObjectPart part = GetChildPart(localID);
  140. if (part != null)
  141. {
  142. TaskInventoryItem taskItem = new TaskInventoryItem();
  143. taskItem.ItemID = newItemId;
  144. taskItem.AssetID = item.AssetID;
  145. taskItem.Name = item.Name;
  146. taskItem.Description = item.Description;
  147. taskItem.OwnerID = part.OwnerID; // Transfer ownership
  148. taskItem.CreatorID = item.CreatorIdAsUuid;
  149. taskItem.Type = item.AssetType;
  150. taskItem.InvType = item.InvType;
  151. if (remoteClient != null &&
  152. remoteClient.AgentId != part.OwnerID &&
  153. m_scene.Permissions.PropagatePermissions())
  154. {
  155. taskItem.BasePermissions = item.BasePermissions &
  156. item.NextPermissions;
  157. taskItem.CurrentPermissions = item.CurrentPermissions &
  158. item.NextPermissions;
  159. taskItem.EveryonePermissions = item.EveryOnePermissions &
  160. item.NextPermissions;
  161. taskItem.GroupPermissions = item.GroupPermissions &
  162. item.NextPermissions;
  163. taskItem.NextPermissions = item.NextPermissions;
  164. taskItem.CurrentPermissions |= 8;
  165. }
  166. else
  167. {
  168. taskItem.BasePermissions = item.BasePermissions;
  169. taskItem.CurrentPermissions = item.CurrentPermissions;
  170. taskItem.CurrentPermissions |= 8;
  171. taskItem.EveryonePermissions = item.EveryOnePermissions;
  172. taskItem.GroupPermissions = item.GroupPermissions;
  173. taskItem.NextPermissions = item.NextPermissions;
  174. }
  175. taskItem.Flags = item.Flags;
  176. // TODO: These are pending addition of those fields to TaskInventoryItem
  177. // taskItem.SalePrice = item.SalePrice;
  178. // taskItem.SaleType = item.SaleType;
  179. taskItem.CreationDate = (uint)item.CreationDate;
  180. bool addFromAllowedDrop = false;
  181. if (remoteClient!=null)
  182. {
  183. addFromAllowedDrop = remoteClient.AgentId != part.OwnerID;
  184. }
  185. part.Inventory.AddInventoryItem(taskItem, addFromAllowedDrop);
  186. return true;
  187. }
  188. else
  189. {
  190. m_log.ErrorFormat(
  191. "[PRIM INVENTORY]: " +
  192. "Couldn't find prim local ID {0} in group {1}, {2} to add inventory item ID {3}",
  193. localID, Name, UUID, newItemId);
  194. }
  195. return false;
  196. }
  197. /// <summary>
  198. /// Returns an existing inventory item. Returns the original, so any changes will be live.
  199. /// </summary>
  200. /// <param name="primID"></param>
  201. /// <param name="itemID"></param>
  202. /// <returns>null if the item does not exist</returns>
  203. public TaskInventoryItem GetInventoryItem(uint primID, UUID itemID)
  204. {
  205. SceneObjectPart part = GetChildPart(primID);
  206. if (part != null)
  207. {
  208. return part.Inventory.GetInventoryItem(itemID);
  209. }
  210. else
  211. {
  212. m_log.ErrorFormat(
  213. "[PRIM INVENTORY]: " +
  214. "Couldn't find prim local ID {0} in prim {1}, {2} to get inventory item ID {3}",
  215. primID, part.Name, part.UUID, itemID);
  216. }
  217. return null;
  218. }
  219. /// <summary>
  220. /// Update an existing inventory item.
  221. /// </summary>
  222. /// <param name="item">The updated item. An item with the same id must already exist
  223. /// in this prim's inventory</param>
  224. /// <returns>false if the item did not exist, true if the update occurred succesfully</returns>
  225. public bool UpdateInventoryItem(TaskInventoryItem item)
  226. {
  227. SceneObjectPart part = GetChildPart(item.ParentPartID);
  228. if (part != null)
  229. {
  230. part.Inventory.UpdateInventoryItem(item);
  231. return true;
  232. }
  233. else
  234. {
  235. m_log.ErrorFormat(
  236. "[PRIM INVENTORY]: " +
  237. "Couldn't find prim ID {0} to update item {1}, {2}",
  238. item.ParentPartID, item.Name, item.ItemID);
  239. }
  240. return false;
  241. }
  242. public int RemoveInventoryItem(uint localID, UUID itemID)
  243. {
  244. SceneObjectPart part = GetChildPart(localID);
  245. if (part != null)
  246. {
  247. int type = part.Inventory.RemoveInventoryItem(itemID);
  248. return type;
  249. }
  250. return -1;
  251. }
  252. public uint GetEffectivePermissions()
  253. {
  254. uint perms=(uint)(PermissionMask.Modify |
  255. PermissionMask.Copy |
  256. PermissionMask.Move |
  257. PermissionMask.Transfer) | 7;
  258. uint ownerMask = 0x7fffffff;
  259. foreach (SceneObjectPart part in m_parts.Values)
  260. {
  261. ownerMask &= part.OwnerMask;
  262. perms &= part.Inventory.MaskEffectivePermissions();
  263. }
  264. if ((ownerMask & (uint)PermissionMask.Modify) == 0)
  265. perms &= ~(uint)PermissionMask.Modify;
  266. if ((ownerMask & (uint)PermissionMask.Copy) == 0)
  267. perms &= ~(uint)PermissionMask.Copy;
  268. if ((ownerMask & (uint)PermissionMask.Transfer) == 0)
  269. perms &= ~(uint)PermissionMask.Transfer;
  270. // If root prim permissions are applied here, this would screw
  271. // with in-inventory manipulation of the next owner perms
  272. // in a major way. So, let's move this to the give itself.
  273. // Yes. I know. Evil.
  274. // if ((ownerMask & RootPart.NextOwnerMask & (uint)PermissionMask.Modify) == 0)
  275. // perms &= ~((uint)PermissionMask.Modify >> 13);
  276. // if ((ownerMask & RootPart.NextOwnerMask & (uint)PermissionMask.Copy) == 0)
  277. // perms &= ~((uint)PermissionMask.Copy >> 13);
  278. // if ((ownerMask & RootPart.NextOwnerMask & (uint)PermissionMask.Transfer) == 0)
  279. // perms &= ~((uint)PermissionMask.Transfer >> 13);
  280. return perms;
  281. }
  282. public void ApplyNextOwnerPermissions()
  283. {
  284. foreach (SceneObjectPart part in m_parts.Values)
  285. {
  286. part.ApplyNextOwnerPermissions();
  287. }
  288. }
  289. public string GetStateSnapshot()
  290. {
  291. Dictionary<UUID, string> states = new Dictionary<UUID, string>();
  292. foreach (SceneObjectPart part in m_parts.Values)
  293. {
  294. foreach (KeyValuePair<UUID, string> s in part.Inventory.GetScriptStates())
  295. states[s.Key] = s.Value;
  296. }
  297. if (states.Count < 1)
  298. return "";
  299. XmlDocument xmldoc = new XmlDocument();
  300. XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration,
  301. "", "");
  302. xmldoc.AppendChild(xmlnode);
  303. XmlElement rootElement = xmldoc.CreateElement("", "ScriptData",
  304. "");
  305. xmldoc.AppendChild(rootElement);
  306. XmlElement wrapper = xmldoc.CreateElement("", "ScriptStates",
  307. "");
  308. rootElement.AppendChild(wrapper);
  309. foreach (KeyValuePair<UUID, string> state in states)
  310. {
  311. XmlDocument sdoc = new XmlDocument();
  312. sdoc.LoadXml(state.Value);
  313. XmlNodeList rootL = sdoc.GetElementsByTagName("State");
  314. XmlNode rootNode = rootL[0];
  315. XmlNode newNode = xmldoc.ImportNode(rootNode, true);
  316. wrapper.AppendChild(newNode);
  317. }
  318. return xmldoc.InnerXml;
  319. }
  320. public void SetState(string objXMLData, IScene ins)
  321. {
  322. if (!(ins is Scene))
  323. return;
  324. Scene s = (Scene)ins;
  325. if (objXMLData == String.Empty)
  326. return;
  327. IScriptModule scriptModule = null;
  328. foreach (IScriptModule sm in s.RequestModuleInterfaces<IScriptModule>())
  329. {
  330. if (sm.ScriptEngineName == s.DefaultScriptEngine)
  331. scriptModule = sm;
  332. else if (scriptModule == null)
  333. scriptModule = sm;
  334. }
  335. if (scriptModule == null)
  336. return;
  337. XmlDocument doc = new XmlDocument();
  338. try
  339. {
  340. doc.LoadXml(objXMLData);
  341. }
  342. catch (Exception) // (System.Xml.XmlException)
  343. {
  344. // We will get here if the XML is invalid or in unit
  345. // tests. Really should determine which it is and either
  346. // fail silently or log it
  347. // Fail silently, for now.
  348. // TODO: Fix this
  349. //
  350. return;
  351. }
  352. XmlNodeList rootL = doc.GetElementsByTagName("ScriptData");
  353. if (rootL.Count != 1)
  354. return;
  355. XmlElement rootE = (XmlElement)rootL[0];
  356. XmlNodeList dataL = rootE.GetElementsByTagName("ScriptStates");
  357. if (dataL.Count != 1)
  358. return;
  359. XmlElement dataE = (XmlElement)dataL[0];
  360. foreach (XmlNode n in dataE.ChildNodes)
  361. {
  362. XmlElement stateE = (XmlElement)n;
  363. UUID itemID = new UUID(stateE.GetAttribute("UUID"));
  364. scriptModule.SetXMLState(itemID, n.OuterXml);
  365. }
  366. }
  367. public void ResumeScripts()
  368. {
  369. foreach (SceneObjectPart part in m_parts.Values)
  370. {
  371. part.Inventory.ResumeScripts();
  372. }
  373. }
  374. }
  375. }