SceneObjectGroup.Inventory.cs 17 KB

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