1
0

SceneObjectGroup.Inventory.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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.Reflection;
  29. using OpenMetaverse;
  30. using log4net;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.Framework.Interfaces;
  33. using System.Collections.Generic;
  34. using System.Xml;
  35. using PermissionMask = OpenSim.Framework.PermissionMask;
  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. if (m_scene == null)
  65. {
  66. m_log.DebugFormat("[PRIM INVENTORY]: m_scene is null. Unable to create script instances");
  67. return 0;
  68. }
  69. // Don't start scripts if they're turned off in the region!
  70. if (!m_scene.RegionInfo.RegionSettings.DisableScripts)
  71. {
  72. SceneObjectPart[] parts = m_parts.GetArray();
  73. for (int i = 0; i < parts.Length; i++)
  74. scriptsStarted += parts[i].Inventory.CreateScriptInstances(startParam, postOnRez, engine, stateSource);
  75. }
  76. return scriptsStarted;
  77. }
  78. /// <summary>
  79. /// Stop and remove the scripts contained in all the prims in this group
  80. /// </summary>
  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. SceneObjectPart[] parts = m_parts.GetArray();
  93. for(int i = 0; i < parts.Length; ++i)
  94. parts[i]?.Inventory?.StopScriptInstances();
  95. }
  96. public void SendReleaseScriptsControl()
  97. {
  98. SceneObjectPart[] parts = m_parts.GetArray();
  99. for (int i = 0; i < parts.Length; i++)
  100. parts[i].Inventory.SendReleaseScriptsControl();
  101. }
  102. public void RemoveScriptsPermissions(int permissions)
  103. {
  104. SceneObjectPart[] parts = m_parts.GetArray();
  105. for (int i = 0; i < parts.Length; i++)
  106. parts[i].Inventory.RemoveScriptsPermissions(permissions);
  107. }
  108. public void RemoveScriptsPermissions(ScenePresence sp, int permissions)
  109. {
  110. SceneObjectPart[] parts = m_parts.GetArray();
  111. for (int i = 0; i < parts.Length; i++)
  112. parts[i].Inventory.RemoveScriptsPermissions(sp, permissions);
  113. }
  114. /// <summary>
  115. /// Add an inventory item from a user's inventory to a prim in this scene object.
  116. /// </summary>
  117. /// <param name="agentID">The agent adding the item.</param>
  118. /// <param name="localID">The local ID of the part receiving the add.</param>
  119. /// <param name="item">The user inventory item being added.</param>
  120. /// <param name="copyItemID">The item UUID that should be used by the new item.</param>
  121. /// <returns></returns>
  122. public bool AddInventoryItem(UUID agentID, uint localID, InventoryItemBase item, UUID copyItemID, bool withModRights = true)
  123. {
  124. //m_log.DebugFormat(
  125. // "[PRIM INVENTORY]: Adding inventory item {0} from {1} to part with local ID {2}",
  126. // item.Name, remoteClient.Name, localID);
  127. UUID newItemId = copyItemID.IsZero() ? item.ID : copyItemID;
  128. SceneObjectPart part = GetPart(localID);
  129. if (part is null)
  130. {
  131. m_log.Error(
  132. $"[PRIM INVENTORY]: Couldn't find prim local ID {localID} in group {Name}, {UUID} to add inventory item ID {newItemId}");
  133. return false;
  134. }
  135. TaskInventoryItem taskItem = new()
  136. {
  137. ItemID = newItemId,
  138. AssetID = item.AssetID,
  139. Name = item.Name,
  140. //CreationDate = (uint)Util.UnixTimeSinceEpoch(),
  141. Description = item.Description,
  142. OwnerID = part.OwnerID, // Transfer ownership
  143. CreatorID = item.CreatorIdAsUuid,
  144. Type = item.AssetType,
  145. InvType = item.InvType,
  146. Flags = item.Flags
  147. };
  148. if (agentID.NotEqual(part.OwnerID) && m_scene.Permissions.PropagatePermissions())
  149. {
  150. taskItem.BasePermissions = item.BasePermissions & item.NextPermissions;
  151. taskItem.CurrentPermissions = item.CurrentPermissions & item.NextPermissions;
  152. taskItem.EveryonePermissions = item.EveryOnePermissions & item.NextPermissions;
  153. taskItem.GroupPermissions = item.GroupPermissions & item.NextPermissions;
  154. taskItem.NextPermissions = item.NextPermissions;
  155. // We're adding this to a prim we don't own. Force
  156. // owner change
  157. taskItem.Flags |= (uint)InventoryItemFlags.ObjectSlamPerm;
  158. taskItem.LastOwnerID = item.Owner;
  159. }
  160. else
  161. {
  162. taskItem.BasePermissions = item.BasePermissions;
  163. taskItem.CurrentPermissions = item.CurrentPermissions;
  164. taskItem.EveryonePermissions = item.EveryOnePermissions;
  165. taskItem.GroupPermissions = item.GroupPermissions;
  166. taskItem.NextPermissions = item.NextPermissions;
  167. }
  168. // m_log.DebugFormat(
  169. // "[PRIM INVENTORY]: Flags are 0x{0:X} for item {1} added to part {2} by {3}",
  170. // taskItem.Flags, taskItem.Name, localID, remoteClient.Name);
  171. // TODO: These are pending addition of those fields to TaskInventoryItem
  172. // taskItem.SalePrice = item.SalePrice;
  173. // taskItem.SaleType = item.SaleType;
  174. bool addFromAllowedDrop;
  175. if(withModRights)
  176. addFromAllowedDrop = false;
  177. else
  178. addFromAllowedDrop = (part.ParentGroup.RootPart.GetEffectiveObjectFlags() & (uint)PrimFlags.AllowInventoryDrop) != 0;
  179. part.Inventory.AddInventoryItem(taskItem, addFromAllowedDrop);
  180. part.ParentGroup.InvalidateDeepEffectivePerms();
  181. return true;
  182. }
  183. /// <summary>
  184. /// Returns an existing inventory item. Returns the original, so any changes will be live.
  185. /// </summary>
  186. /// <param name="primID"></param>
  187. /// <param name="itemID"></param>
  188. /// <returns>null if the item does not exist</returns>
  189. public TaskInventoryItem GetInventoryItem(uint primID, UUID itemID)
  190. {
  191. SceneObjectPart part = GetPart(primID);
  192. if (part is not null)
  193. {
  194. return part.Inventory.GetInventoryItem(itemID);
  195. }
  196. else
  197. {
  198. m_log.ErrorFormat(
  199. "[PRIM INVENTORY]: " +
  200. "Couldn't find prim local ID {0} in prim {1}, {2} to get inventory item ID {3}",
  201. primID, part.Name, part.UUID, itemID);
  202. }
  203. return null;
  204. }
  205. /// <summary>
  206. /// Update an existing inventory item.
  207. /// </summary>
  208. /// <param name="item">The updated item. An item with the same id must already exist
  209. /// in this prim's inventory</param>
  210. /// <returns>false if the item did not exist, true if the update occurred succesfully</returns>
  211. public bool UpdateInventoryItem(TaskInventoryItem item)
  212. {
  213. SceneObjectPart part = GetPart(item.ParentPartID);
  214. if (part is not null)
  215. {
  216. part.Inventory.UpdateInventoryItem(item);
  217. return true;
  218. }
  219. else
  220. {
  221. m_log.ErrorFormat(
  222. "[PRIM INVENTORY]: " +
  223. "Couldn't find prim ID {0} to update item {1}, {2}",
  224. item.ParentPartID, item.Name, item.ItemID);
  225. }
  226. return false;
  227. }
  228. public int RemoveInventoryItem(uint localID, UUID itemID)
  229. {
  230. SceneObjectPart part = GetPart(localID);
  231. if (part is not null)
  232. {
  233. int type = part.Inventory.RemoveInventoryItem(itemID);
  234. return type;
  235. }
  236. return -1;
  237. }
  238. // new test code, to place in better place later
  239. private readonly object m_PermissionsLock = new();
  240. private bool m_EffectivePermsInvalid = true;
  241. private bool m_DeepEffectivePermsInvalid = true;
  242. // should called when parts chanced by their contents did not, so we know their cacche is valid
  243. // in case of doubt call InvalidateDeepEffectivePerms(), it only costs a bit more cpu time
  244. public void InvalidateEffectivePerms()
  245. {
  246. lock(m_PermissionsLock)
  247. m_EffectivePermsInvalid = true;
  248. }
  249. // should called when parts chanced and their contents where accounted for
  250. public void InvalidateDeepEffectivePerms()
  251. {
  252. lock(m_PermissionsLock)
  253. {
  254. m_DeepEffectivePermsInvalid = true;
  255. m_EffectivePermsInvalid = true;
  256. }
  257. }
  258. private uint m_EffectiveEveryOnePerms;
  259. public uint EffectiveEveryOnePerms
  260. {
  261. get
  262. {
  263. lock(m_PermissionsLock)
  264. {
  265. if(m_EffectivePermsInvalid)
  266. AggregatePerms();
  267. return m_EffectiveEveryOnePerms;
  268. }
  269. }
  270. }
  271. private uint m_EffectiveGroupPerms;
  272. public uint EffectiveGroupPerms
  273. {
  274. get
  275. {
  276. lock(m_PermissionsLock)
  277. {
  278. if(m_EffectivePermsInvalid)
  279. AggregatePerms();
  280. return m_EffectiveGroupPerms;
  281. }
  282. }
  283. }
  284. private uint m_EffectiveGroupOrEveryOnePerms;
  285. public uint EffectiveGroupOrEveryOnePerms
  286. {
  287. get
  288. {
  289. lock(m_PermissionsLock)
  290. {
  291. if(m_EffectivePermsInvalid)
  292. AggregatePerms();
  293. return m_EffectiveGroupOrEveryOnePerms;
  294. }
  295. }
  296. }
  297. private uint m_EffectiveOwnerPerms;
  298. public uint EffectiveOwnerPerms
  299. {
  300. get
  301. {
  302. lock(m_PermissionsLock)
  303. {
  304. if(m_EffectivePermsInvalid)
  305. AggregatePerms();
  306. return m_EffectiveOwnerPerms;
  307. }
  308. }
  309. }
  310. public void AggregatePerms()
  311. {
  312. lock(m_PermissionsLock)
  313. {
  314. // aux
  315. const uint allmask = (uint)PermissionMask.AllEffective;
  316. const uint movemodmask = (uint)(PermissionMask.Move | PermissionMask.Modify);
  317. const uint copytransfermast = (uint)(PermissionMask.Copy | PermissionMask.Transfer);
  318. uint basePerms = (RootPart.BaseMask & allmask) | (uint)PermissionMask.Move;
  319. bool noBaseTransfer = (basePerms & (uint)PermissionMask.Transfer) == 0;
  320. uint rootOwnerPerms = RootPart.OwnerMask;
  321. uint owner = rootOwnerPerms;
  322. uint rootGroupPerms = RootPart.GroupMask;
  323. uint group = rootGroupPerms;
  324. uint rootEveryonePerms = RootPart.EveryoneMask;
  325. uint everyone = rootEveryonePerms;
  326. bool needUpdate = false;
  327. // date is time of writing april 30th 2017
  328. bool newobj = (RootPart.CreationDate == 0 || RootPart.CreationDate > 1493574994);
  329. SceneObjectPart[] parts = m_parts.GetArray();
  330. for (int i = 0; i < parts.Length; i++)
  331. {
  332. SceneObjectPart part = parts[i];
  333. if(m_DeepEffectivePermsInvalid)
  334. part.AggregatedInnerPermsForGroup();
  335. owner &= part.AggregatedInnerOwnerPerms;
  336. group &= part.AggregatedInnerGroupPerms;
  337. if(newobj)
  338. group &= part.AggregatedInnerGroupPerms;
  339. if(newobj)
  340. everyone &= part.AggregatedInnerEveryonePerms;
  341. }
  342. // recover modify and move
  343. rootOwnerPerms &= movemodmask;
  344. owner |= rootOwnerPerms;
  345. if((owner & copytransfermast) == 0)
  346. owner |= (uint)PermissionMask.Transfer;
  347. owner &= basePerms;
  348. if(owner != m_EffectiveOwnerPerms)
  349. {
  350. needUpdate = true;
  351. m_EffectiveOwnerPerms = owner;
  352. }
  353. uint ownertransfermask = owner & (uint)PermissionMask.Transfer;
  354. // recover modify and move
  355. rootGroupPerms &= movemodmask;
  356. group |= rootGroupPerms;
  357. if(noBaseTransfer)
  358. group &=~(uint)PermissionMask.Copy;
  359. else
  360. group |= ownertransfermask;
  361. uint groupOrEveryone = group;
  362. uint tmpPerms = group & owner;
  363. if(tmpPerms != m_EffectiveGroupPerms)
  364. {
  365. needUpdate = true;
  366. m_EffectiveGroupPerms = tmpPerms;
  367. }
  368. // recover move
  369. rootEveryonePerms &= (uint)PermissionMask.Move;
  370. everyone |= rootEveryonePerms;
  371. everyone &= ~(uint)PermissionMask.Modify;
  372. if(noBaseTransfer)
  373. everyone &=~(uint)PermissionMask.Copy;
  374. else
  375. everyone |= ownertransfermask;
  376. groupOrEveryone |= everyone;
  377. tmpPerms = everyone & owner;
  378. if(tmpPerms != m_EffectiveEveryOnePerms)
  379. {
  380. needUpdate = true;
  381. m_EffectiveEveryOnePerms = tmpPerms;
  382. }
  383. tmpPerms = groupOrEveryone & owner;
  384. if(tmpPerms != m_EffectiveGroupOrEveryOnePerms)
  385. {
  386. needUpdate = true;
  387. m_EffectiveGroupOrEveryOnePerms = tmpPerms;
  388. }
  389. m_DeepEffectivePermsInvalid = false;
  390. m_EffectivePermsInvalid = false;
  391. if(needUpdate)
  392. RootPart.ScheduleFullUpdate();
  393. }
  394. }
  395. public uint CurrentAndFoldedNextPermissions()
  396. {
  397. uint perms=(uint)(PermissionMask.Modify |
  398. PermissionMask.Copy |
  399. PermissionMask.Move |
  400. PermissionMask.Transfer |
  401. PermissionMask.FoldedMask);
  402. uint ownerMask = RootPart.OwnerMask;
  403. SceneObjectPart[] parts = m_parts.GetArray();
  404. for (int i = 0; i < parts.Length; i++)
  405. {
  406. SceneObjectPart part = parts[i];
  407. ownerMask &= part.BaseMask;
  408. perms &= part.Inventory.MaskEffectivePermissions();
  409. }
  410. if ((ownerMask & (uint)PermissionMask.Modify) == 0)
  411. perms &= ~(uint)PermissionMask.Modify;
  412. if ((ownerMask & (uint)PermissionMask.Copy) == 0)
  413. perms &= ~(uint)PermissionMask.Copy;
  414. if ((ownerMask & (uint)PermissionMask.Transfer) == 0)
  415. perms &= ~(uint)PermissionMask.Transfer;
  416. if ((ownerMask & (uint)PermissionMask.Export) == 0)
  417. perms &= ~(uint)PermissionMask.Export;
  418. return perms;
  419. }
  420. public void ApplyNextOwnerPermissions()
  421. {
  422. // m_log.DebugFormat("[PRIM INVENTORY]: Applying next owner permissions to {0} {1}", Name, UUID);
  423. SceneObjectPart[] parts = m_parts.GetArray();
  424. for (int i = 0; i < parts.Length; i++)
  425. parts[i].ApplyNextOwnerPermissions();
  426. }
  427. public string GetStateSnapshot()
  428. {
  429. Dictionary<UUID, string> states = new();
  430. SceneObjectPart[] parts = m_parts.GetArray();
  431. for (int i = 0; i < parts.Length; i++)
  432. {
  433. SceneObjectPart part = parts[i];
  434. foreach (KeyValuePair<UUID, string> s in part.Inventory.GetScriptStates())
  435. states[s.Key] = s.Value;
  436. }
  437. if (states.Count < 1)
  438. return string.Empty;
  439. XmlDocument xmldoc = new();
  440. XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, string.Empty, string.Empty);
  441. xmldoc.AppendChild(xmlnode);
  442. XmlElement rootElement = xmldoc.CreateElement("", "ScriptData", string.Empty);
  443. xmldoc.AppendChild(rootElement);
  444. XmlElement wrapper = xmldoc.CreateElement("", "ScriptStates", string.Empty);
  445. rootElement.AppendChild(wrapper);
  446. foreach (KeyValuePair<UUID, string> state in states)
  447. {
  448. XmlDocument sdoc = new();
  449. sdoc.LoadXml(state.Value);
  450. XmlNodeList rootL = sdoc.GetElementsByTagName("State");
  451. XmlNode rootNode = rootL[0];
  452. XmlNode newNode = xmldoc.ImportNode(rootNode, true);
  453. wrapper.AppendChild(newNode);
  454. }
  455. return xmldoc.InnerXml;
  456. }
  457. public void SetState(string objXMLData, IScene ins)
  458. {
  459. if (ins is not Scene s)
  460. return;
  461. if (objXMLData.Length == 0)
  462. return;
  463. IScriptModule scriptModule = null;
  464. foreach (IScriptModule sm in s.RequestModuleInterfaces<IScriptModule>())
  465. {
  466. if (sm.ScriptEngineName == s.DefaultScriptEngine)
  467. scriptModule = sm;
  468. else
  469. scriptModule ??= sm;
  470. }
  471. if (scriptModule is null)
  472. return;
  473. XmlDocument doc = new();
  474. try
  475. {
  476. doc.LoadXml(objXMLData);
  477. }
  478. catch (Exception) // (System.Xml.XmlException)
  479. {
  480. // We will get here if the XML is invalid or in unit
  481. // tests. Really should determine which it is and either
  482. // fail silently or log it
  483. // Fail silently, for now.
  484. // TODO: Fix this
  485. //
  486. return;
  487. }
  488. XmlNodeList rootL = doc.GetElementsByTagName("ScriptData");
  489. if (rootL.Count != 1)
  490. return;
  491. XmlElement rootE = (XmlElement)rootL[0];
  492. XmlNodeList dataL = rootE.GetElementsByTagName("ScriptStates");
  493. if (dataL.Count != 1)
  494. return;
  495. XmlElement dataE = (XmlElement)dataL[0];
  496. foreach (XmlNode n in dataE.ChildNodes)
  497. {
  498. XmlElement stateE = (XmlElement)n;
  499. UUID itemID = new(stateE.GetAttribute("UUID"));
  500. scriptModule.SetXMLState(itemID, n.OuterXml);
  501. }
  502. }
  503. public void ResumeScripts()
  504. {
  505. if (m_scene.RegionInfo.RegionSettings.DisableScripts)
  506. return;
  507. SceneObjectPart[] parts = m_parts.GetArray();
  508. for (int i = 0; i < parts.Length; i++)
  509. parts[i].Inventory.ResumeScripts();
  510. }
  511. /// <summary>
  512. /// Returns true if any part in the scene object contains scripts, false otherwise.
  513. /// </summary>
  514. /// <returns></returns>
  515. public bool ContainsScripts()
  516. {
  517. foreach (SceneObjectPart part in Parts)
  518. if (part.Inventory.ContainsScripts())
  519. return true;
  520. return false;
  521. }
  522. }
  523. }