SceneObjectGroup.Inventory.cs 16 KB

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