SceneObjectGroup.Inventory.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 OpenSim 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.Environment.Interfaces;
  34. using System.Collections.Generic;
  35. using System.Xml;
  36. namespace OpenSim.Region.Environment.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. public void RemoveScriptInstances()
  73. {
  74. lock (m_parts)
  75. {
  76. foreach (SceneObjectPart part in m_parts.Values)
  77. {
  78. part.Inventory.RemoveScriptInstances();
  79. }
  80. }
  81. }
  82. /// <summary>
  83. ///
  84. /// </summary>
  85. /// <param name="remoteClient"></param>
  86. /// <param name="localID"></param>
  87. public bool GetPartInventoryFileName(IClientAPI remoteClient, uint localID)
  88. {
  89. SceneObjectPart part = GetChildPart(localID);
  90. if (part != null)
  91. {
  92. return part.Inventory.GetInventoryFileName(remoteClient, localID);
  93. }
  94. else
  95. {
  96. m_log.ErrorFormat(
  97. "[PRIM INVENTORY]: " +
  98. "Couldn't find part {0} in object group {1}, {2} to retreive prim inventory",
  99. localID, Name, UUID);
  100. }
  101. return false;
  102. }
  103. /// <summary>
  104. /// Return serialized inventory metadata for the given constituent prim
  105. /// </summary>
  106. /// <param name="localID"></param>
  107. /// <param name="xferManager"></param>
  108. public void RequestInventoryFile(IClientAPI client, uint localID, IXfer xferManager)
  109. {
  110. SceneObjectPart part = GetChildPart(localID);
  111. if (part != null)
  112. {
  113. part.Inventory.RequestInventoryFile(client, xferManager);
  114. }
  115. else
  116. {
  117. m_log.ErrorFormat(
  118. "[PRIM INVENTORY]: " +
  119. "Couldn't find part {0} in object group {1}, {2} to request inventory data",
  120. localID, Name, UUID);
  121. }
  122. }
  123. /// <summary>
  124. /// Add an inventory item to a prim in this group.
  125. /// </summary>
  126. /// <param name="remoteClient"></param>
  127. /// <param name="localID"></param>
  128. /// <param name="item"></param>
  129. /// <param name="copyItemID">The item UUID that should be used by the new item.</param>
  130. /// <returns></returns>
  131. public bool AddInventoryItem(IClientAPI remoteClient, uint localID,
  132. InventoryItemBase item, UUID copyItemID)
  133. {
  134. UUID newItemId = (copyItemID != UUID.Zero) ? copyItemID : item.ID;
  135. SceneObjectPart part = GetChildPart(localID);
  136. if (part != null)
  137. {
  138. TaskInventoryItem taskItem = new TaskInventoryItem();
  139. taskItem.ItemID = newItemId;
  140. taskItem.AssetID = item.AssetID;
  141. taskItem.Name = item.Name;
  142. taskItem.Description = item.Description;
  143. taskItem.OwnerID = part.OwnerID; // Transfer ownership
  144. taskItem.CreatorID = item.Creator;
  145. taskItem.Type = item.AssetType;
  146. taskItem.InvType = item.InvType;
  147. if (remoteClient != null &&
  148. remoteClient.AgentId != part.OwnerID &&
  149. m_scene.Permissions.PropagatePermissions())
  150. {
  151. taskItem.BasePermissions = item.BasePermissions &
  152. item.NextPermissions;
  153. taskItem.CurrentPermissions = item.CurrentPermissions &
  154. item.NextPermissions;
  155. taskItem.EveryonePermissions = item.EveryOnePermissions &
  156. item.NextPermissions;
  157. taskItem.GroupPermissions = item.GroupPermissions &
  158. item.NextPermissions;
  159. taskItem.NextPermissions = item.NextPermissions;
  160. taskItem.CurrentPermissions |= 8;
  161. } else {
  162. taskItem.BasePermissions = item.BasePermissions;
  163. taskItem.CurrentPermissions = item.CurrentPermissions;
  164. taskItem.CurrentPermissions |= 8;
  165. taskItem.EveryonePermissions = item.EveryOnePermissions;
  166. taskItem.GroupPermissions = item.GroupPermissions;
  167. taskItem.NextPermissions = item.NextPermissions;
  168. }
  169. taskItem.Flags = item.Flags;
  170. // TODO: These are pending addition of those fields to TaskInventoryItem
  171. // taskItem.SalePrice = item.SalePrice;
  172. // taskItem.SaleType = item.SaleType;
  173. taskItem.CreationDate = (uint)item.CreationDate;
  174. bool addFromAllowedDrop = false;
  175. if (remoteClient!=null)
  176. {
  177. addFromAllowedDrop = remoteClient.AgentId != part.OwnerID;
  178. }
  179. part.Inventory.AddInventoryItem(taskItem, addFromAllowedDrop);
  180. return true;
  181. }
  182. else
  183. {
  184. m_log.ErrorFormat(
  185. "[PRIM INVENTORY]: " +
  186. "Couldn't find prim local ID {0} in group {1}, {2} to add inventory item ID {3}",
  187. localID, Name, UUID, newItemId);
  188. }
  189. return false;
  190. }
  191. /// <summary>
  192. /// Returns an existing inventory item. Returns the original, so any changes will be live.
  193. /// </summary>
  194. /// <param name="primID"></param>
  195. /// <param name="itemID"></param>
  196. /// <returns>null if the item does not exist</returns>
  197. public TaskInventoryItem GetInventoryItem(uint primID, UUID itemID)
  198. {
  199. SceneObjectPart part = GetChildPart(primID);
  200. if (part != null)
  201. {
  202. return part.Inventory.GetInventoryItem(itemID);
  203. }
  204. else
  205. {
  206. m_log.ErrorFormat(
  207. "[PRIM INVENTORY]: " +
  208. "Couldn't find prim local ID {0} in prim {1}, {2} to get inventory item ID {3}",
  209. primID, part.Name, part.UUID, itemID);
  210. }
  211. return null;
  212. }
  213. /// <summary>
  214. /// Update an existing inventory item.
  215. /// </summary>
  216. /// <param name="item">The updated item. An item with the same id must already exist
  217. /// in this prim's inventory</param>
  218. /// <returns>false if the item did not exist, true if the update occurred succesfully</returns>
  219. public bool UpdateInventoryItem(TaskInventoryItem item)
  220. {
  221. SceneObjectPart part = GetChildPart(item.ParentPartID);
  222. if (part != null)
  223. {
  224. part.Inventory.UpdateInventoryItem(item);
  225. return true;
  226. }
  227. else
  228. {
  229. m_log.ErrorFormat(
  230. "[PRIM INVENTORY]: " +
  231. "Couldn't find prim ID {0} to update item {1}, {2}",
  232. item.ParentPartID, item.Name, item.ItemID);
  233. }
  234. return false;
  235. }
  236. public int RemoveInventoryItem(uint localID, UUID itemID)
  237. {
  238. SceneObjectPart part = GetChildPart(localID);
  239. if (part != null)
  240. {
  241. int type = part.Inventory.RemoveInventoryItem(itemID);
  242. return type;
  243. }
  244. return -1;
  245. }
  246. public uint GetEffectivePermissions()
  247. {
  248. uint perms=(uint)(PermissionMask.Modify |
  249. PermissionMask.Copy |
  250. PermissionMask.Move |
  251. PermissionMask.Transfer) | 7;
  252. uint ownerMask = 0x7ffffff;
  253. foreach (SceneObjectPart part in m_parts.Values)
  254. {
  255. ownerMask &= part.OwnerMask;
  256. perms &= part.Inventory.MaskEffectivePermissions();
  257. }
  258. if ((ownerMask & (uint)PermissionMask.Modify) == 0)
  259. perms &= ~(uint)PermissionMask.Modify;
  260. if ((ownerMask & (uint)PermissionMask.Copy) == 0)
  261. perms &= ~(uint)PermissionMask.Copy;
  262. if ((ownerMask & (uint)PermissionMask.Transfer) == 0)
  263. perms &= ~(uint)PermissionMask.Transfer;
  264. if ((ownerMask & RootPart.NextOwnerMask & (uint)PermissionMask.Modify) == 0)
  265. perms &= ~((uint)PermissionMask.Modify >> 13);
  266. if ((ownerMask & RootPart.NextOwnerMask & (uint)PermissionMask.Copy) == 0)
  267. perms &= ~((uint)PermissionMask.Copy >> 13);
  268. if ((ownerMask & RootPart.NextOwnerMask & (uint)PermissionMask.Transfer) == 0)
  269. perms &= ~((uint)PermissionMask.Transfer >> 13);
  270. return perms;
  271. }
  272. public void ApplyNextOwnerPermissions()
  273. {
  274. foreach (SceneObjectPart part in m_parts.Values)
  275. {
  276. part.ApplyNextOwnerPermissions();
  277. }
  278. }
  279. public string GetStateSnapshot()
  280. {
  281. List<string> assemblies = new List<string>();
  282. Dictionary<UUID, string> states = new Dictionary<UUID, string>();
  283. foreach (SceneObjectPart part in m_parts.Values)
  284. {
  285. foreach (string a in part.Inventory.GetScriptAssemblies())
  286. {
  287. if (a != "" && !assemblies.Contains(a))
  288. assemblies.Add(a);
  289. }
  290. foreach (KeyValuePair<UUID, string> s in part.Inventory.GetScriptStates())
  291. {
  292. states[s.Key] = s.Value;
  293. }
  294. }
  295. if (states.Count < 1 || assemblies.Count < 1)
  296. return "";
  297. XmlDocument xmldoc = new XmlDocument();
  298. XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration,
  299. "", "");
  300. xmldoc.AppendChild(xmlnode);
  301. XmlElement rootElement = xmldoc.CreateElement("", "ScriptData",
  302. "");
  303. xmldoc.AppendChild(rootElement);
  304. XmlElement wrapper = xmldoc.CreateElement("", "Assemblies",
  305. "");
  306. rootElement.AppendChild(wrapper);
  307. foreach (string assembly in assemblies)
  308. {
  309. string fn = Path.GetFileName(assembly);
  310. if (fn == String.Empty)
  311. continue;
  312. FileInfo fi = new FileInfo(assembly);
  313. if (fi == null)
  314. continue;
  315. Byte[] data = new Byte[fi.Length];
  316. FileStream fs = File.Open(assembly, FileMode.Open, FileAccess.Read);
  317. fs.Read(data, 0, data.Length);
  318. fs.Close();
  319. XmlElement assemblyData = xmldoc.CreateElement("", "Assembly", "");
  320. XmlAttribute assemblyName = xmldoc.CreateAttribute("", "Filename", "");
  321. assemblyName.Value = fn;
  322. assemblyData.Attributes.Append(assemblyName);
  323. assemblyData.InnerText = System.Convert.ToBase64String(data);
  324. wrapper.AppendChild(assemblyData);
  325. }
  326. wrapper = xmldoc.CreateElement("", "ScriptStates",
  327. "");
  328. rootElement.AppendChild(wrapper);
  329. foreach (KeyValuePair<UUID, string> state in states)
  330. {
  331. XmlElement stateData = xmldoc.CreateElement("", "State", "");
  332. XmlAttribute stateID = xmldoc.CreateAttribute("", "UUID", "");
  333. stateID.Value = state.Key.ToString();
  334. stateData.Attributes.Append(stateID);
  335. XmlDocument sdoc = new XmlDocument();
  336. sdoc.LoadXml(state.Value);
  337. XmlNodeList rootL = sdoc.GetElementsByTagName("ScriptState");
  338. XmlNode rootNode = rootL[0];
  339. XmlNode newNode = xmldoc.ImportNode(rootNode, true);
  340. stateData.AppendChild(newNode);
  341. wrapper.AppendChild(stateData);
  342. }
  343. return xmldoc.InnerXml;
  344. }
  345. }
  346. }