SceneObjectGroup.Inventory.cs 16 KB

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