SceneObjectGroup.Inventory.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. // We're adding this to a prim we don't own. Force
  165. // owner change
  166. taskItem.CurrentPermissions |= 16; // Slam
  167. }
  168. else
  169. {
  170. taskItem.BasePermissions = item.BasePermissions;
  171. taskItem.CurrentPermissions = item.CurrentPermissions;
  172. taskItem.EveryonePermissions = item.EveryOnePermissions;
  173. taskItem.GroupPermissions = item.GroupPermissions;
  174. taskItem.NextPermissions = item.NextPermissions;
  175. }
  176. taskItem.Flags = item.Flags;
  177. // TODO: These are pending addition of those fields to TaskInventoryItem
  178. // taskItem.SalePrice = item.SalePrice;
  179. // taskItem.SaleType = item.SaleType;
  180. taskItem.CreationDate = (uint)item.CreationDate;
  181. bool addFromAllowedDrop = false;
  182. if (remoteClient!=null)
  183. {
  184. addFromAllowedDrop = remoteClient.AgentId != part.OwnerID;
  185. }
  186. part.Inventory.AddInventoryItem(taskItem, addFromAllowedDrop);
  187. return true;
  188. }
  189. else
  190. {
  191. m_log.ErrorFormat(
  192. "[PRIM INVENTORY]: " +
  193. "Couldn't find prim local ID {0} in group {1}, {2} to add inventory item ID {3}",
  194. localID, Name, UUID, newItemId);
  195. }
  196. return false;
  197. }
  198. /// <summary>
  199. /// Returns an existing inventory item. Returns the original, so any changes will be live.
  200. /// </summary>
  201. /// <param name="primID"></param>
  202. /// <param name="itemID"></param>
  203. /// <returns>null if the item does not exist</returns>
  204. public TaskInventoryItem GetInventoryItem(uint primID, UUID itemID)
  205. {
  206. SceneObjectPart part = GetChildPart(primID);
  207. if (part != null)
  208. {
  209. return part.Inventory.GetInventoryItem(itemID);
  210. }
  211. else
  212. {
  213. m_log.ErrorFormat(
  214. "[PRIM INVENTORY]: " +
  215. "Couldn't find prim local ID {0} in prim {1}, {2} to get inventory item ID {3}",
  216. primID, part.Name, part.UUID, itemID);
  217. }
  218. return null;
  219. }
  220. /// <summary>
  221. /// Update an existing inventory item.
  222. /// </summary>
  223. /// <param name="item">The updated item. An item with the same id must already exist
  224. /// in this prim's inventory</param>
  225. /// <returns>false if the item did not exist, true if the update occurred succesfully</returns>
  226. public bool UpdateInventoryItem(TaskInventoryItem item)
  227. {
  228. SceneObjectPart part = GetChildPart(item.ParentPartID);
  229. if (part != null)
  230. {
  231. part.Inventory.UpdateInventoryItem(item);
  232. return true;
  233. }
  234. else
  235. {
  236. m_log.ErrorFormat(
  237. "[PRIM INVENTORY]: " +
  238. "Couldn't find prim ID {0} to update item {1}, {2}",
  239. item.ParentPartID, item.Name, item.ItemID);
  240. }
  241. return false;
  242. }
  243. public int RemoveInventoryItem(uint localID, UUID itemID)
  244. {
  245. SceneObjectPart part = GetChildPart(localID);
  246. if (part != null)
  247. {
  248. int type = part.Inventory.RemoveInventoryItem(itemID);
  249. return type;
  250. }
  251. return -1;
  252. }
  253. public uint GetEffectivePermissions()
  254. {
  255. uint perms=(uint)(PermissionMask.Modify |
  256. PermissionMask.Copy |
  257. PermissionMask.Move |
  258. PermissionMask.Transfer) | 7;
  259. uint ownerMask = 0x7fffffff;
  260. foreach (SceneObjectPart part in m_parts.Values)
  261. {
  262. ownerMask &= part.OwnerMask;
  263. perms &= part.Inventory.MaskEffectivePermissions();
  264. }
  265. if ((ownerMask & (uint)PermissionMask.Modify) == 0)
  266. perms &= ~(uint)PermissionMask.Modify;
  267. if ((ownerMask & (uint)PermissionMask.Copy) == 0)
  268. perms &= ~(uint)PermissionMask.Copy;
  269. if ((ownerMask & (uint)PermissionMask.Transfer) == 0)
  270. perms &= ~(uint)PermissionMask.Transfer;
  271. // If root prim permissions are applied here, this would screw
  272. // with in-inventory manipulation of the next owner perms
  273. // in a major way. So, let's move this to the give itself.
  274. // Yes. I know. Evil.
  275. // if ((ownerMask & RootPart.NextOwnerMask & (uint)PermissionMask.Modify) == 0)
  276. // perms &= ~((uint)PermissionMask.Modify >> 13);
  277. // if ((ownerMask & RootPart.NextOwnerMask & (uint)PermissionMask.Copy) == 0)
  278. // perms &= ~((uint)PermissionMask.Copy >> 13);
  279. // if ((ownerMask & RootPart.NextOwnerMask & (uint)PermissionMask.Transfer) == 0)
  280. // perms &= ~((uint)PermissionMask.Transfer >> 13);
  281. return perms;
  282. }
  283. public void ApplyNextOwnerPermissions()
  284. {
  285. foreach (SceneObjectPart part in m_parts.Values)
  286. {
  287. part.ApplyNextOwnerPermissions();
  288. }
  289. }
  290. public string GetStateSnapshot()
  291. {
  292. Dictionary<UUID, string> states = new Dictionary<UUID, string>();
  293. foreach (SceneObjectPart part in m_parts.Values)
  294. {
  295. foreach (KeyValuePair<UUID, string> s in part.Inventory.GetScriptStates())
  296. states[s.Key] = s.Value;
  297. }
  298. if (states.Count < 1)
  299. return "";
  300. XmlDocument xmldoc = new XmlDocument();
  301. XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration,
  302. "", "");
  303. xmldoc.AppendChild(xmlnode);
  304. XmlElement rootElement = xmldoc.CreateElement("", "ScriptData",
  305. "");
  306. xmldoc.AppendChild(rootElement);
  307. XmlElement wrapper = xmldoc.CreateElement("", "ScriptStates",
  308. "");
  309. rootElement.AppendChild(wrapper);
  310. foreach (KeyValuePair<UUID, string> state in states)
  311. {
  312. XmlDocument sdoc = new XmlDocument();
  313. sdoc.LoadXml(state.Value);
  314. XmlNodeList rootL = sdoc.GetElementsByTagName("State");
  315. XmlNode rootNode = rootL[0];
  316. XmlNode newNode = xmldoc.ImportNode(rootNode, true);
  317. wrapper.AppendChild(newNode);
  318. }
  319. return xmldoc.InnerXml;
  320. }
  321. public void SetState(string objXMLData, IScene ins)
  322. {
  323. if (!(ins is Scene))
  324. return;
  325. Scene s = (Scene)ins;
  326. if (objXMLData == String.Empty)
  327. return;
  328. IScriptModule scriptModule = null;
  329. foreach (IScriptModule sm in s.RequestModuleInterfaces<IScriptModule>())
  330. {
  331. if (sm.ScriptEngineName == s.DefaultScriptEngine)
  332. scriptModule = sm;
  333. else if (scriptModule == null)
  334. scriptModule = sm;
  335. }
  336. if (scriptModule == null)
  337. return;
  338. XmlDocument doc = new XmlDocument();
  339. try
  340. {
  341. doc.LoadXml(objXMLData);
  342. }
  343. catch (Exception) // (System.Xml.XmlException)
  344. {
  345. // We will get here if the XML is invalid or in unit
  346. // tests. Really should determine which it is and either
  347. // fail silently or log it
  348. // Fail silently, for now.
  349. // TODO: Fix this
  350. //
  351. return;
  352. }
  353. XmlNodeList rootL = doc.GetElementsByTagName("ScriptData");
  354. if (rootL.Count != 1)
  355. return;
  356. XmlElement rootE = (XmlElement)rootL[0];
  357. XmlNodeList dataL = rootE.GetElementsByTagName("ScriptStates");
  358. if (dataL.Count != 1)
  359. return;
  360. XmlElement dataE = (XmlElement)dataL[0];
  361. foreach (XmlNode n in dataE.ChildNodes)
  362. {
  363. XmlElement stateE = (XmlElement)n;
  364. UUID itemID = new UUID(stateE.GetAttribute("UUID"));
  365. scriptModule.SetXMLState(itemID, n.OuterXml);
  366. }
  367. }
  368. public void ResumeScripts()
  369. {
  370. foreach (SceneObjectPart part in m_parts.Values)
  371. {
  372. part.Inventory.ResumeScripts();
  373. }
  374. }
  375. }
  376. }