SceneObjectPartInventory.cs 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  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.Text;
  29. using System.Xml;
  30. using System.IO;
  31. using System.Collections.Generic;
  32. using System.Collections;
  33. using System.Reflection;
  34. using System.Threading;
  35. using OpenMetaverse;
  36. using log4net;
  37. using OpenSim.Framework;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes.Scripting;
  40. using OpenSim.Region.Framework.Scenes.Serialization;
  41. using PermissionMask = OpenSim.Framework.PermissionMask;
  42. namespace OpenSim.Region.Framework.Scenes
  43. {
  44. public class SceneObjectPartInventory : IEntityInventory , IDisposable
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private byte[] m_inventoryFileData = new byte[0];
  48. private byte[] m_inventoryFileNameBytes = new byte[0];
  49. private string m_inventoryFileName = "";
  50. private uint m_inventoryFileNameSerial = 0;
  51. private bool m_inventoryPrivileged = false;
  52. private object m_inventoryFileLock = new object();
  53. private Dictionary<UUID, ArrayList> m_scriptErrors = new Dictionary<UUID, ArrayList>();
  54. /// <value>
  55. /// The part to which the inventory belongs.
  56. /// </value>
  57. private SceneObjectPart m_part;
  58. /// <summary>
  59. /// Serial count for inventory file , used to tell if inventory has changed
  60. /// no need for this to be part of Database backup
  61. /// </summary>
  62. protected uint m_inventorySerial = 0;
  63. /// <summary>
  64. /// Holds in memory prim inventory
  65. /// </summary>
  66. protected TaskInventoryDictionary m_items = new TaskInventoryDictionary();
  67. protected Dictionary<UUID, TaskInventoryItem> m_scripts = null;
  68. /// <summary>
  69. /// Tracks whether inventory has changed since the last persistent backup
  70. /// </summary>
  71. internal bool HasInventoryChanged;
  72. /// <value>
  73. /// Inventory serial number
  74. /// </value>
  75. protected internal uint Serial
  76. {
  77. get { return m_inventorySerial; }
  78. set { m_inventorySerial = value; }
  79. }
  80. /// <value>
  81. /// Raw inventory data
  82. /// </value>
  83. protected internal TaskInventoryDictionary Items
  84. {
  85. get
  86. {
  87. return m_items;
  88. }
  89. set
  90. {
  91. m_items = value;
  92. m_inventorySerial++;
  93. gatherScriptsAndQueryStates();
  94. }
  95. }
  96. public int Count
  97. {
  98. get
  99. {
  100. m_items.LockItemsForRead(true);
  101. int c = m_items.Count;
  102. m_items.LockItemsForRead(false);
  103. return c;
  104. }
  105. }
  106. /// <summary>
  107. /// Constructor
  108. /// </summary>
  109. /// <param name="part">
  110. /// A <see cref="SceneObjectPart"/>
  111. /// </param>
  112. public SceneObjectPartInventory(SceneObjectPart part)
  113. {
  114. m_part = part;
  115. }
  116. ~SceneObjectPartInventory()
  117. {
  118. Dispose(false);
  119. }
  120. private bool disposed = false;
  121. public void Dispose()
  122. {
  123. Dispose(true);
  124. GC.SuppressFinalize(this);
  125. }
  126. protected void Dispose(bool disposing)
  127. {
  128. // Check to see if Dispose has already been called.
  129. if (!disposed)
  130. {
  131. if (m_items != null)
  132. {
  133. m_items.Dispose();
  134. m_items = null;
  135. }
  136. disposed = true;
  137. }
  138. }
  139. /// <summary>
  140. /// Force the task inventory of this prim to persist at the next update sweep
  141. /// </summary>
  142. public void ForceInventoryPersistence()
  143. {
  144. HasInventoryChanged = true;
  145. }
  146. /// <summary>
  147. /// Reset UUIDs for all the items in the prim's inventory.
  148. /// </summary>
  149. /// <remarks>
  150. /// This involves either generating
  151. /// new ones or setting existing UUIDs to the correct parent UUIDs.
  152. ///
  153. /// If this method is called and there are inventory items, then we regard the inventory as having changed.
  154. /// </remarks>
  155. public void ResetInventoryIDs()
  156. {
  157. if (m_part == null)
  158. return;
  159. m_items.LockItemsForWrite(true);
  160. if (m_items.Count == 0)
  161. {
  162. m_items.LockItemsForWrite(false);
  163. return;
  164. }
  165. UUID partID = m_part.UUID;
  166. IList<TaskInventoryItem> items = new List<TaskInventoryItem>(m_items.Values);
  167. m_items.Clear();
  168. if(m_scripts == null)
  169. {
  170. for (int i = 0; i < items.Count; ++i)
  171. {
  172. TaskInventoryItem item = items[i];
  173. item.ResetIDs(partID);
  174. m_items.Add(item.ItemID, item);
  175. }
  176. }
  177. else
  178. {
  179. m_scripts.Clear();
  180. for (int i = 0; i < items.Count; ++i)
  181. {
  182. TaskInventoryItem item = items[i];
  183. item.ResetIDs(partID);
  184. m_items.Add(item.ItemID, item);
  185. if (item.InvType == (int)InventoryType.LSL)
  186. m_scripts.Add(item.ItemID, item);
  187. }
  188. }
  189. m_inventorySerial++;
  190. m_items.LockItemsForWrite(false);
  191. }
  192. public void ResetObjectID()
  193. {
  194. if (m_part == null)
  195. return;
  196. UUID partID = m_part.UUID;
  197. m_items.LockItemsForWrite(true);
  198. if (m_items.Count == 0)
  199. {
  200. m_items.LockItemsForWrite(false);
  201. return;
  202. }
  203. foreach(TaskInventoryItem item in m_items.Values)
  204. {
  205. item.ParentPartID = partID;
  206. item.ParentID = partID;
  207. }
  208. m_inventorySerial++;
  209. m_items.LockItemsForWrite(false);
  210. }
  211. /// <summary>
  212. /// Change every item in this inventory to a new owner.
  213. /// </summary>
  214. /// <param name="ownerId"></param>
  215. public void ChangeInventoryOwner(UUID ownerId)
  216. {
  217. if(m_part == null)
  218. return;
  219. m_items.LockItemsForWrite(true);
  220. if (m_items.Count == 0)
  221. {
  222. m_items.LockItemsForWrite(false);
  223. return;
  224. }
  225. foreach (TaskInventoryItem item in m_items.Values)
  226. {
  227. if (ownerId != item.OwnerID)
  228. item.LastOwnerID = item.OwnerID;
  229. item.OwnerID = ownerId;
  230. item.PermsMask = 0;
  231. item.PermsGranter = UUID.Zero;
  232. item.OwnerChanged = true;
  233. }
  234. HasInventoryChanged = true;
  235. m_part.ParentGroup.HasGroupChanged = true;
  236. m_inventorySerial++;
  237. m_items.LockItemsForWrite(false);
  238. }
  239. /// <summary>
  240. /// Change every item in this inventory to a new group.
  241. /// </summary>
  242. /// <param name="groupID"></param>
  243. public void ChangeInventoryGroup(UUID groupID)
  244. {
  245. if(m_part == null)
  246. return;
  247. m_items.LockItemsForWrite(true);
  248. if (m_items.Count == 0)
  249. {
  250. m_items.LockItemsForWrite(false);
  251. return;
  252. }
  253. m_inventorySerial++;
  254. // Don't let this set the HasGroupChanged flag for attachments
  255. // as this happens during rez and we don't want a new asset
  256. // for each attachment each time
  257. if (!m_part.ParentGroup.IsAttachment)
  258. {
  259. HasInventoryChanged = true;
  260. m_part.ParentGroup.HasGroupChanged = true;
  261. }
  262. foreach (TaskInventoryItem item in m_items.Values)
  263. item.GroupID = groupID;
  264. m_items.LockItemsForWrite(false);
  265. }
  266. private void gatherScriptsAndQueryStates()
  267. {
  268. m_items.LockItemsForWrite(true);
  269. m_scripts = new Dictionary<UUID, TaskInventoryItem>();
  270. foreach (TaskInventoryItem item in m_items.Values)
  271. {
  272. if (item.InvType == (int)InventoryType.LSL)
  273. m_scripts[item.ItemID] = item;
  274. }
  275. if (m_scripts.Count == 0)
  276. {
  277. m_items.LockItemsForWrite(false);
  278. m_scripts = null;
  279. return;
  280. }
  281. m_items.LockItemsForWrite(false);
  282. if (m_part.ParentGroup == null || m_part.ParentGroup.Scene == null)
  283. return;
  284. IScriptModule[] scriptEngines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  285. if (scriptEngines.Length == 0) // No engine at all
  286. return;
  287. bool running;
  288. m_items.LockItemsForRead(true);
  289. foreach (TaskInventoryItem item in m_scripts.Values)
  290. {
  291. //running = false;
  292. foreach (IScriptModule e in scriptEngines)
  293. {
  294. if (e.HasScript(item.ItemID, out running))
  295. {
  296. item.ScriptRunning = running;
  297. break;
  298. }
  299. }
  300. //item.ScriptRunning = running;
  301. }
  302. m_items.LockItemsForRead(false);
  303. }
  304. public bool TryGetScriptInstanceRunning(UUID itemId, out bool running)
  305. {
  306. running = false;
  307. TaskInventoryItem item = GetInventoryItem(itemId);
  308. if (item == null)
  309. return false;
  310. return TryGetScriptInstanceRunning(m_part.ParentGroup.Scene, item, out running);
  311. }
  312. public static bool TryGetScriptInstanceRunning(Scene scene, TaskInventoryItem item, out bool running)
  313. {
  314. running = false;
  315. if (item.InvType != (int)InventoryType.LSL)
  316. return false;
  317. IScriptModule[] scriptEngines = scene.RequestModuleInterfaces<IScriptModule>();
  318. if (scriptEngines.Length == 0) // No engine at all
  319. return false;
  320. foreach (IScriptModule e in scriptEngines)
  321. {
  322. if (e.HasScript(item.ItemID, out running))
  323. return true;
  324. }
  325. return false;
  326. }
  327. public int CreateScriptInstances(int startParam, bool postOnRez, string engine, int stateSource)
  328. {
  329. m_items.LockItemsForRead(true);
  330. if(m_scripts == null || m_scripts.Count == 0)
  331. {
  332. m_items.LockItemsForRead(false);
  333. return 0;
  334. }
  335. List<TaskInventoryItem> scripts = new List<TaskInventoryItem>(m_scripts.Values);
  336. m_items.LockItemsForRead(false);
  337. int scriptsValidForStarting = 0;
  338. for (int i = 0; i < scripts.Count; ++i)
  339. {
  340. if (CreateScriptInstance(scripts[i], startParam, postOnRez, engine, stateSource))
  341. scriptsValidForStarting++;
  342. }
  343. return scriptsValidForStarting;
  344. }
  345. public ArrayList GetScriptErrors(UUID itemID)
  346. {
  347. IScriptModule[] scriptEngines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  348. ArrayList ret = new ArrayList();
  349. foreach (IScriptModule e in scriptEngines)
  350. {
  351. if (e != null)
  352. {
  353. ArrayList errors = e.GetScriptErrors(itemID);
  354. foreach (Object line in errors)
  355. ret.Add(line);
  356. }
  357. }
  358. return ret;
  359. }
  360. /// <summary>
  361. /// Stop and remove all the scripts in this prim.
  362. /// </summary>
  363. /// <param name="sceneObjectBeingDeleted">
  364. /// Should be true if these scripts are being removed because the scene
  365. /// object is being deleted. This will prevent spurious updates to the client.
  366. /// </param>
  367. public void RemoveScriptInstances(bool sceneObjectBeingDeleted)
  368. {
  369. m_items.LockItemsForRead(true);
  370. if (m_scripts == null || m_scripts.Count == 0)
  371. {
  372. m_items.LockItemsForRead(false);
  373. return;
  374. }
  375. List<TaskInventoryItem> scripts = new List<TaskInventoryItem>(m_scripts.Values);
  376. m_items.LockItemsForRead(false);
  377. foreach (TaskInventoryItem item in scripts)
  378. {
  379. RemoveScriptInstance(item.ItemID, sceneObjectBeingDeleted);
  380. m_part.RemoveScriptEvents(item.ItemID);
  381. }
  382. }
  383. /// <summary>
  384. /// Stop all the scripts in this prim.
  385. /// </summary>
  386. public void StopScriptInstances()
  387. {
  388. m_items.LockItemsForRead(true);
  389. if (m_scripts == null || m_scripts.Count == 0)
  390. {
  391. m_items.LockItemsForRead(false);
  392. return;
  393. }
  394. List<TaskInventoryItem> scripts = new List<TaskInventoryItem>(m_scripts.Values);
  395. m_items.LockItemsForRead(false);
  396. foreach (TaskInventoryItem item in scripts)
  397. StopScriptInstance(item);
  398. }
  399. /// <summary>
  400. /// Start a script which is in this prim's inventory.
  401. /// </summary>
  402. /// <param name="item"></param>
  403. /// <returns>true if the script instance was created, false otherwise</returns>
  404. public bool CreateScriptInstance(TaskInventoryItem item, int startParam, bool postOnRez, string engine, int stateSource)
  405. {
  406. //m_log.DebugFormat("[PRIM INVENTORY]: Starting script {0} {1} in prim {2} {3} in {4}",
  407. // item.Name, item.ItemID, m_part.Name, m_part.UUID, m_part.ParentGroup.Scene.RegionInfo.RegionName);
  408. if (!m_part.ParentGroup.Scene.Permissions.CanRunScript(item, m_part))
  409. {
  410. StoreScriptError(item.ItemID, "no permission");
  411. return false;
  412. }
  413. m_part.AddFlag(PrimFlags.Scripted);
  414. if (m_part.ParentGroup.Scene.RegionInfo.RegionSettings.DisableScripts)
  415. return false;
  416. UUID itemID = item.ItemID;
  417. m_items.LockItemsForRead(true);
  418. if (!m_items.TryGetValue(item.ItemID, out TaskInventoryItem it))
  419. {
  420. m_items.LockItemsForRead(false);
  421. StoreScriptError(itemID, String.Format("TaskItem ID {0} could not be found", item.ItemID));
  422. m_log.ErrorFormat(
  423. "[PRIM INVENTORY]: Couldn't start script {0}, {1} at {2} in {3} since taskitem ID {4} could not be found",
  424. item.Name, item.ItemID, m_part.AbsolutePosition,
  425. m_part.ParentGroup.Scene.RegionInfo.RegionName, item.ItemID);
  426. return false;
  427. }
  428. m_items.LockItemsForRead(false);
  429. if (stateSource == 2 && m_part.ParentGroup.Scene.m_trustBinaries)
  430. {
  431. // Prim crossing
  432. m_items.LockItemsForWrite(true);
  433. it.PermsMask = 0;
  434. it.PermsGranter = UUID.Zero;
  435. m_items.LockItemsForWrite(false);
  436. m_part.ParentGroup.Scene.EventManager.TriggerRezScript(
  437. m_part.LocalId, itemID, String.Empty, startParam, postOnRez, engine, stateSource);
  438. StoreScriptErrors(itemID, null);
  439. m_part.ParentGroup.AddActiveScriptCount(1);
  440. m_part.ScheduleFullUpdate();
  441. return true;
  442. }
  443. AssetBase asset = m_part.ParentGroup.Scene.AssetService.Get(item.AssetID.ToString());
  444. if (asset == null)
  445. {
  446. StoreScriptError(itemID, String.Format("asset ID {0} could not be found", item.AssetID));
  447. m_log.ErrorFormat(
  448. "[PRIM INVENTORY]: Couldn't start script {0}, {1} at {2} in {3} since asset ID {4} could not be found",
  449. item.Name, item.ItemID, m_part.AbsolutePosition,
  450. m_part.ParentGroup.Scene.RegionInfo.RegionName, item.AssetID);
  451. return false;
  452. }
  453. if (m_part.ParentGroup.m_savedScriptState != null)
  454. item.OldItemID = RestoreSavedScriptState(item.LoadedItemID, item.OldItemID, itemID);
  455. m_items.LockItemsForWrite(true);
  456. it.OldItemID = item.OldItemID;
  457. it.PermsMask = 0;
  458. it.PermsGranter = UUID.Zero;
  459. m_items.LockItemsForWrite(false);
  460. string script = Utils.BytesToString(asset.Data);
  461. m_part.ParentGroup.Scene.EventManager.TriggerRezScript(
  462. m_part.LocalId, itemID, script, startParam, postOnRez, engine, stateSource);
  463. StoreScriptErrors(itemID, null);
  464. if (!item.ScriptRunning)
  465. m_part.ParentGroup.Scene.EventManager.TriggerStopScript(m_part.LocalId, itemID);
  466. m_part.ParentGroup.AddActiveScriptCount(1);
  467. m_part.ScheduleFullUpdate();
  468. return true;
  469. }
  470. private UUID RestoreSavedScriptState(UUID loadedID, UUID oldID, UUID newID)
  471. {
  472. //m_log.DebugFormat(
  473. // "[PRIM INVENTORY]: Restoring scripted state for item {0}, oldID {1}, loadedID {2}",
  474. // newID, oldID, loadedID);
  475. IScriptModule[] scriptEngines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  476. if (scriptEngines.Length == 0) // No engine at all
  477. return oldID;
  478. UUID stateID = oldID;
  479. if (!m_part.ParentGroup.m_savedScriptState.ContainsKey(oldID))
  480. stateID = loadedID;
  481. if (m_part.ParentGroup.m_savedScriptState.ContainsKey(stateID))
  482. {
  483. XmlDocument doc = new XmlDocument();
  484. doc.LoadXml(m_part.ParentGroup.m_savedScriptState[stateID]);
  485. ////////// CRUFT WARNING ///////////////////////////////////
  486. //
  487. // Old objects will have <ScriptState><State> ...
  488. // This format is XEngine ONLY
  489. //
  490. // New objects have <State Engine="...." ...><ScriptState>...
  491. // This can be passed to any engine
  492. //
  493. XmlNode n = doc.SelectSingleNode("ScriptState");
  494. if (n != null) // Old format data
  495. {
  496. XmlDocument newDoc = new XmlDocument();
  497. XmlElement rootN = newDoc.CreateElement("", "State", "");
  498. XmlAttribute uuidA = newDoc.CreateAttribute("", "UUID", "");
  499. uuidA.Value = stateID.ToString();
  500. rootN.Attributes.Append(uuidA);
  501. XmlAttribute engineA = newDoc.CreateAttribute("", "Engine", "");
  502. engineA.Value = "XEngine";
  503. rootN.Attributes.Append(engineA);
  504. newDoc.AppendChild(rootN);
  505. XmlNode stateN = newDoc.ImportNode(n, true);
  506. rootN.AppendChild(stateN);
  507. // This created document has only the minimun data
  508. // necessary for XEngine to parse it successfully
  509. //m_log.DebugFormat("[PRIM INVENTORY]: Adding legacy state {0} in {1}", stateID, newID);
  510. m_part.ParentGroup.m_savedScriptState[stateID] = newDoc.OuterXml;
  511. }
  512. foreach (IScriptModule e in scriptEngines)
  513. {
  514. if (e != null)
  515. {
  516. if (e.SetXMLState(newID, m_part.ParentGroup.m_savedScriptState[stateID]))
  517. break;
  518. }
  519. }
  520. m_part.ParentGroup.m_savedScriptState.Remove(stateID);
  521. }
  522. return stateID;
  523. }
  524. /// <summary>
  525. /// Start a script which is in this prim's inventory.
  526. /// Some processing may occur in the background, but this routine returns asap.
  527. /// </summary>
  528. /// <param name="itemId">
  529. /// A <see cref="UUID"/>
  530. /// </param>
  531. public bool CreateScriptInstance(UUID itemId, int startParam, bool postOnRez, string engine, int stateSource)
  532. {
  533. lock (m_scriptErrors)
  534. {
  535. // Indicate to CreateScriptInstanceInternal() we don't want it to wait for completion
  536. m_scriptErrors.Remove(itemId);
  537. }
  538. CreateScriptInstanceInternal(itemId, startParam, postOnRez, engine, stateSource);
  539. return true;
  540. }
  541. private void CreateScriptInstanceInternal(UUID itemId, int startParam, bool postOnRez, string engine, int stateSource)
  542. {
  543. m_items.LockItemsForRead(true);
  544. if (m_items.TryGetValue(itemId, out TaskInventoryItem it))
  545. {
  546. m_items.LockItemsForRead(false);
  547. CreateScriptInstance(it, startParam, postOnRez, engine, stateSource);
  548. }
  549. else
  550. {
  551. m_items.LockItemsForRead(false);
  552. string msg = String.Format("couldn't be found for prim {0}, {1} at {2} in {3}", m_part.Name, m_part.UUID,
  553. m_part.AbsolutePosition, m_part.ParentGroup.Scene.RegionInfo.RegionName);
  554. StoreScriptError(itemId, msg);
  555. m_log.ErrorFormat(
  556. "[PRIM INVENTORY]: " +
  557. "Couldn't start script with ID {0} since it {1}", itemId, msg);
  558. }
  559. }
  560. /// <summary>
  561. /// Start a script which is in this prim's inventory and return any compilation error messages.
  562. /// </summary>
  563. /// <param name="itemId">
  564. /// A <see cref="UUID"/>
  565. /// </param>
  566. public ArrayList CreateScriptInstanceEr(UUID itemId, int startParam, bool postOnRez, string engine, int stateSource)
  567. {
  568. ArrayList errors;
  569. // Indicate to CreateScriptInstanceInternal() we want it to
  570. // post any compilation/loading error messages
  571. lock (m_scriptErrors)
  572. {
  573. m_scriptErrors[itemId] = null;
  574. }
  575. // Perform compilation/loading
  576. CreateScriptInstanceInternal(itemId, startParam, postOnRez, engine, stateSource);
  577. // Wait for and retrieve any errors
  578. lock (m_scriptErrors)
  579. {
  580. while ((errors = m_scriptErrors[itemId]) == null)
  581. {
  582. if (!System.Threading.Monitor.Wait(m_scriptErrors, 15000))
  583. {
  584. m_log.ErrorFormat(
  585. "[PRIM INVENTORY]: " +
  586. "timedout waiting for script {0} errors", itemId);
  587. errors = m_scriptErrors[itemId];
  588. if (errors == null)
  589. {
  590. errors = new ArrayList(1);
  591. errors.Add("timedout waiting for errors");
  592. }
  593. break;
  594. }
  595. }
  596. m_scriptErrors.Remove(itemId);
  597. }
  598. return errors;
  599. }
  600. // Signal to CreateScriptInstanceEr() that compilation/loading is complete
  601. private void StoreScriptErrors(UUID itemId, ArrayList errors)
  602. {
  603. lock (m_scriptErrors)
  604. {
  605. // If compilation/loading initiated via CreateScriptInstance(),
  606. // it does not want the errors, so just get out
  607. if (!m_scriptErrors.ContainsKey(itemId))
  608. {
  609. return;
  610. }
  611. // Initiated via CreateScriptInstanceEr(), if we know what the
  612. // errors are, save them and wake CreateScriptInstanceEr().
  613. if (errors != null)
  614. {
  615. m_scriptErrors[itemId] = errors;
  616. System.Threading.Monitor.PulseAll(m_scriptErrors);
  617. return;
  618. }
  619. }
  620. // Initiated via CreateScriptInstanceEr() but we don't know what
  621. // the errors are yet, so retrieve them from the script engine.
  622. // This may involve some waiting internal to GetScriptErrors().
  623. errors = GetScriptErrors(itemId);
  624. // Get a default non-null value to indicate success.
  625. if (errors == null)
  626. {
  627. errors = new ArrayList();
  628. }
  629. // Post to CreateScriptInstanceEr() and wake it up
  630. lock (m_scriptErrors)
  631. {
  632. m_scriptErrors[itemId] = errors;
  633. System.Threading.Monitor.PulseAll(m_scriptErrors);
  634. }
  635. }
  636. // Like StoreScriptErrors(), but just posts a single string message
  637. private void StoreScriptError(UUID itemId, string message)
  638. {
  639. ArrayList errors = new ArrayList(1);
  640. errors.Add(message);
  641. StoreScriptErrors(itemId, errors);
  642. }
  643. /// <summary>
  644. /// Stop and remove a script which is in this prim's inventory.
  645. /// </summary>
  646. /// <param name="itemId"></param>
  647. /// <param name="sceneObjectBeingDeleted">
  648. /// Should be true if this script is being removed because the scene
  649. /// object is being deleted. This will prevent spurious updates to the client.
  650. /// </param>
  651. public void RemoveScriptInstance(UUID itemId, bool sceneObjectBeingDeleted)
  652. {
  653. if (m_items.ContainsKey(itemId))
  654. {
  655. if (!sceneObjectBeingDeleted)
  656. m_part.RemoveScriptEvents(itemId);
  657. m_part.ParentGroup.Scene.EventManager.TriggerRemoveScript(m_part.LocalId, itemId);
  658. m_part.ParentGroup.AddActiveScriptCount(-1);
  659. }
  660. else
  661. {
  662. m_log.WarnFormat(
  663. "[PRIM INVENTORY]: " +
  664. "Couldn't stop script with ID {0} since it couldn't be found for prim {1}, {2} at {3} in {4}",
  665. itemId, m_part.Name, m_part.UUID,
  666. m_part.AbsolutePosition, m_part.ParentGroup.Scene.RegionInfo.RegionName);
  667. }
  668. }
  669. /// <summary>
  670. /// Stop a script which is in this prim's inventory.
  671. /// </summary>
  672. /// <param name="itemId"></param>
  673. /// <param name="sceneObjectBeingDeleted">
  674. /// Should be true if this script is being removed because the scene
  675. /// object is being deleted. This will prevent spurious updates to the client.
  676. /// </param>
  677. public void StopScriptInstance(UUID itemId)
  678. {
  679. m_items.LockItemsForRead(true);
  680. m_items.TryGetValue(itemId, out TaskInventoryItem scriptItem);
  681. m_items.LockItemsForRead(false);
  682. if (scriptItem != null)
  683. {
  684. StopScriptInstance(scriptItem);
  685. }
  686. else
  687. {
  688. m_log.WarnFormat(
  689. "[PRIM INVENTORY]: " +
  690. "Couldn't stop script with ID {0} since it couldn't be found for prim {1}, {2} at {3} in {4}",
  691. itemId, m_part.Name, m_part.UUID,
  692. m_part.AbsolutePosition, m_part.ParentGroup.Scene.RegionInfo.RegionName);
  693. }
  694. }
  695. /// <summary>
  696. /// Stop a script which is in this prim's inventory.
  697. /// </summary>
  698. /// <param name="itemId"></param>
  699. /// <param name="sceneObjectBeingDeleted">
  700. /// Should be true if this script is being removed because the scene
  701. /// object is being deleted. This will prevent spurious updates to the client.
  702. /// </param>
  703. public void StopScriptInstance(TaskInventoryItem item)
  704. {
  705. if (m_part.ParentGroup.Scene != null)
  706. m_part.ParentGroup.Scene.EventManager.TriggerStopScript(m_part.LocalId, item.ItemID);
  707. // At the moment, even stopped scripts are counted as active, which is probably wrong.
  708. // m_part.ParentGroup.AddActiveScriptCount(-1);
  709. }
  710. public void SendReleaseScriptsControl()
  711. {
  712. m_items.LockItemsForRead(true);
  713. if (m_scripts == null || m_scripts.Count == 0)
  714. {
  715. m_items.LockItemsForRead(false);
  716. return;
  717. }
  718. List<UUID> grants = new List<UUID>();
  719. List<UUID> items = new List<UUID>();
  720. foreach (TaskInventoryItem item in m_scripts.Values)
  721. {
  722. if (((item.PermsMask & 4) == 0))
  723. continue;
  724. grants.Add(item.PermsGranter);
  725. items.Add(item.ItemID);
  726. }
  727. m_items.LockItemsForRead(false);
  728. if (grants.Count > 0)
  729. {
  730. for (int i = 0; i < grants.Count; ++i)
  731. {
  732. ScenePresence presence = m_part.ParentGroup.Scene.GetScenePresence(grants[i]);
  733. if (presence != null && !presence.IsDeleted && presence.ParentPart != m_part) // last check mb needed for vehicle crossing ???
  734. presence.UnRegisterControlEventsToScript(m_part.LocalId, items[i]);
  735. }
  736. }
  737. }
  738. public void RemoveScriptsPermissions(int permissions)
  739. {
  740. m_items.LockItemsForWrite(true);
  741. if (m_scripts == null || m_scripts.Count == 0)
  742. {
  743. m_items.LockItemsForWrite(false);
  744. return;
  745. }
  746. bool removeControl = ((permissions & 4) != 0); //takecontrol
  747. List<UUID> grants = new List<UUID>();
  748. List<UUID> items = new List<UUID>();
  749. permissions = ~permissions;
  750. foreach (TaskInventoryItem item in m_scripts.Values)
  751. {
  752. int curmask = item.PermsMask;
  753. UUID curGrant = item.PermsGranter;
  754. if (removeControl && ((curmask & 4) != 0))
  755. {
  756. grants.Add(curGrant);
  757. items.Add(item.ItemID);
  758. }
  759. curmask &= permissions;
  760. item.PermsMask = curmask;
  761. if(curmask == 0)
  762. item.PermsGranter = UUID.Zero;
  763. }
  764. m_items.LockItemsForWrite(false);
  765. if(grants.Count > 0)
  766. {
  767. for(int i = 0; i< grants.Count;++i)
  768. {
  769. ScenePresence presence = m_part.ParentGroup.Scene.GetScenePresence(grants[i]);
  770. if (presence != null && !presence.IsDeleted)
  771. presence.UnRegisterControlEventsToScript(m_part.LocalId, items[i]);
  772. }
  773. }
  774. }
  775. public void RemoveScriptsPermissions(ScenePresence sp, int permissions)
  776. {
  777. m_items.LockItemsForWrite(true);
  778. if (m_scripts == null || m_scripts.Count == 0)
  779. {
  780. m_items.LockItemsForWrite(false);
  781. return;
  782. }
  783. bool removeControl = ((permissions & 4) != 0); //takecontrol
  784. UUID grant = sp.UUID;
  785. List<UUID> items = new List<UUID>();
  786. permissions = ~permissions;
  787. foreach (TaskInventoryItem item in m_scripts.Values)
  788. {
  789. if(grant != item.PermsGranter)
  790. continue;
  791. int curmask = item.PermsMask;
  792. if (removeControl && ((curmask & 4) != 0))
  793. items.Add(item.ItemID);
  794. curmask &= permissions;
  795. item.PermsMask = curmask;
  796. if(curmask == 0)
  797. item.PermsGranter = UUID.Zero;
  798. }
  799. m_items.LockItemsForWrite(false);
  800. if(items.Count > 0)
  801. {
  802. for(int i = 0; i < items.Count; ++i)
  803. {
  804. if (!sp.IsDeleted)
  805. sp.UnRegisterControlEventsToScript(m_part.LocalId, items[i]);
  806. }
  807. }
  808. }
  809. /// <summary>
  810. /// Check if the inventory holds an item with a given name.
  811. /// </summary>
  812. /// <param name="name"></param>
  813. /// <returns></returns>
  814. private bool InventoryContainsName(string name)
  815. {
  816. m_items.LockItemsForRead(true);
  817. foreach (TaskInventoryItem item in m_items.Values)
  818. {
  819. if (item.Name == name)
  820. {
  821. m_items.LockItemsForRead(false);
  822. return true;
  823. }
  824. }
  825. m_items.LockItemsForRead(false);
  826. return false;
  827. }
  828. /// <summary>
  829. /// For a given item name, return that name if it is available. Otherwise, return the next available
  830. /// similar name (which is currently the original name with the next available numeric suffix).
  831. /// </summary>
  832. /// <param name="name"></param>
  833. /// <returns></returns>
  834. private string FindAvailableInventoryName(string name)
  835. {
  836. if (!InventoryContainsName(name))
  837. return name;
  838. int suffix=1;
  839. while (suffix < 256)
  840. {
  841. string tryName=String.Format("{0} {1}", name, suffix);
  842. if (!InventoryContainsName(tryName))
  843. return tryName;
  844. suffix++;
  845. }
  846. return String.Empty;
  847. }
  848. /// <summary>
  849. /// Add an item to this prim's inventory. If an item with the same name already exists, then an alternative
  850. /// name is chosen.
  851. /// </summary>
  852. /// <param name="item"></param>
  853. public void AddInventoryItem(TaskInventoryItem item, bool allowedDrop)
  854. {
  855. AddInventoryItem(item.Name, item, allowedDrop);
  856. }
  857. /// <summary>
  858. /// Add an item to this prim's inventory. If an item with the same name already exists, it is replaced.
  859. /// </summary>
  860. /// <param name="item"></param>
  861. public void AddInventoryItemExclusive(TaskInventoryItem item, bool allowedDrop)
  862. {
  863. m_items.LockItemsForRead(true);
  864. List<TaskInventoryItem> il = new List<TaskInventoryItem>(m_items.Values);
  865. m_items.LockItemsForRead(false);
  866. foreach (TaskInventoryItem i in il)
  867. {
  868. if (i.Name == item.Name)
  869. {
  870. if (i.InvType == (int)InventoryType.LSL)
  871. RemoveScriptInstance(i.ItemID, false);
  872. RemoveInventoryItem(i.ItemID);
  873. break;
  874. }
  875. }
  876. AddInventoryItem(item.Name, item, allowedDrop);
  877. }
  878. /// <summary>
  879. /// Add an item to this prim's inventory.
  880. /// </summary>
  881. /// <param name="name">The name that the new item should have.</param>
  882. /// <param name="item">
  883. /// The item itself. The name within this structure is ignored in favour of the name
  884. /// given in this method's arguments
  885. /// </param>
  886. /// <param name="allowedDrop">
  887. /// Item was only added to inventory because AllowedDrop is set
  888. /// </param>
  889. protected void AddInventoryItem(string name, TaskInventoryItem item, bool allowedDrop)
  890. {
  891. name = FindAvailableInventoryName(name);
  892. if (name == String.Empty)
  893. return;
  894. item.ParentID = m_part.UUID;
  895. item.ParentPartID = m_part.UUID;
  896. item.Name = name;
  897. item.GroupID = m_part.GroupID;
  898. m_items.LockItemsForWrite(true);
  899. m_items.Add(item.ItemID, item);
  900. if (item.InvType == (int)InventoryType.LSL)
  901. {
  902. if (m_scripts == null)
  903. m_scripts = new Dictionary<UUID, TaskInventoryItem>();
  904. m_scripts.Add(item.ItemID, item);
  905. }
  906. m_items.LockItemsForWrite(false);
  907. if (allowedDrop)
  908. m_part.TriggerScriptChangedEvent(Changed.ALLOWED_DROP, item.ItemID);
  909. else
  910. m_part.TriggerScriptChangedEvent(Changed.INVENTORY);
  911. m_part.AggregateInnerPerms();
  912. m_inventorySerial++;
  913. HasInventoryChanged = true;
  914. m_part.ParentGroup.HasGroupChanged = true;
  915. }
  916. /// <summary>
  917. /// Restore a whole collection of items to the prim's inventory at once.
  918. /// We assume that the items already have all their fields correctly filled out.
  919. /// The items are not flagged for persistence to the database, since they are being restored
  920. /// from persistence rather than being newly added.
  921. /// </summary>
  922. /// <param name="items"></param>
  923. public void RestoreInventoryItems(ICollection<TaskInventoryItem> items)
  924. {
  925. m_items.LockItemsForWrite(true);
  926. foreach (TaskInventoryItem item in items)
  927. {
  928. m_items.Add(item.ItemID, item);
  929. if (item.InvType == (int)InventoryType.LSL)
  930. {
  931. if (m_scripts == null)
  932. m_scripts = new Dictionary<UUID, TaskInventoryItem>();
  933. m_scripts.Add(item.ItemID, item);
  934. }
  935. }
  936. m_items.LockItemsForWrite(false);
  937. m_part.AggregateInnerPerms();
  938. m_inventorySerial++;
  939. }
  940. /// <summary>
  941. /// Returns an existing inventory item. Returns the original, so any changes will be live.
  942. /// </summary>
  943. /// <param name="itemID"></param>
  944. /// <returns>null if the item does not exist</returns>
  945. public TaskInventoryItem GetInventoryItem(UUID itemId)
  946. {
  947. TaskInventoryItem item;
  948. m_items.LockItemsForRead(true);
  949. m_items.TryGetValue(itemId, out item);
  950. m_items.LockItemsForRead(false);
  951. return item;
  952. }
  953. public TaskInventoryItem GetInventoryItem(string name)
  954. {
  955. m_items.LockItemsForRead(true);
  956. foreach (TaskInventoryItem item in m_items.Values)
  957. {
  958. if (item.Name == name)
  959. {
  960. m_items.LockItemsForRead(false);
  961. return item;
  962. }
  963. }
  964. m_items.LockItemsForRead(false);
  965. return null;
  966. }
  967. public List<TaskInventoryItem> GetInventoryItems(string name)
  968. {
  969. List<TaskInventoryItem> items = new List<TaskInventoryItem>();
  970. m_items.LockItemsForRead(true);
  971. foreach (TaskInventoryItem item in m_items.Values)
  972. {
  973. if (item.Name == name)
  974. items.Add(item);
  975. }
  976. m_items.LockItemsForRead(false);
  977. return items;
  978. }
  979. public bool GetRezReadySceneObjects(TaskInventoryItem item, out List<SceneObjectGroup> objlist, out List<Vector3> veclist, out Vector3 bbox, out float offsetHeight)
  980. {
  981. AssetBase rezAsset = m_part.ParentGroup.Scene.AssetService.Get(item.AssetID.ToString());
  982. if (null == rezAsset)
  983. {
  984. m_log.WarnFormat(
  985. "[PRIM INVENTORY]: Could not find asset {0} for inventory item {1} in {2}",
  986. item.AssetID, item.Name, m_part.Name);
  987. objlist = null;
  988. veclist = null;
  989. bbox = Vector3.Zero;
  990. offsetHeight = 0;
  991. return false;
  992. }
  993. bool single = m_part.ParentGroup.Scene.GetObjectsToRez(rezAsset.Data, false, out objlist, out veclist, out bbox, out offsetHeight);
  994. for (int i = 0; i < objlist.Count; i++)
  995. {
  996. SceneObjectGroup group = objlist[i];
  997. /*
  998. group.RootPart.AttachPoint = group.RootPart.Shape.State;
  999. group.RootPart.AttachedPos = group.AbsolutePosition;
  1000. group.RootPart.AttachRotation = group.GroupRotation;
  1001. */
  1002. group.ResetIDs();
  1003. SceneObjectPart rootPart = group.GetPart(group.UUID);
  1004. // Since renaming the item in the inventory does not affect the name stored
  1005. // in the serialization, transfer the correct name from the inventory to the
  1006. // object itself before we rez.
  1007. // Only do these for the first object if we are rezzing a coalescence.
  1008. // nahh dont mess with coalescence objects,
  1009. // the name in inventory can be change for inventory purpuses only
  1010. if (objlist.Count == 1)
  1011. {
  1012. rootPart.Name = item.Name;
  1013. rootPart.Description = item.Description;
  1014. }
  1015. /* reverted to old code till part.ApplyPermissionsOnRez is better reviewed/fixed
  1016. group.SetGroup(m_part.GroupID, null);
  1017. foreach (SceneObjectPart part in group.Parts)
  1018. {
  1019. // Convert between InventoryItem classes. You can never have too many similar but slightly different classes :)
  1020. InventoryItemBase dest = new InventoryItemBase(item.ItemID, item.OwnerID);
  1021. dest.BasePermissions = item.BasePermissions;
  1022. dest.CurrentPermissions = item.CurrentPermissions;
  1023. dest.EveryOnePermissions = item.EveryonePermissions;
  1024. dest.GroupPermissions = item.GroupPermissions;
  1025. dest.NextPermissions = item.NextPermissions;
  1026. dest.Flags = item.Flags;
  1027. part.ApplyPermissionsOnRez(dest, false, m_part.ParentGroup.Scene);
  1028. }
  1029. */
  1030. // old code start
  1031. SceneObjectPart[] partList = group.Parts;
  1032. group.SetGroup(m_part.GroupID, null);
  1033. if ((rootPart.OwnerID != item.OwnerID) || (item.CurrentPermissions & (uint)PermissionMask.Slam) != 0 || (item.Flags & (uint)InventoryItemFlags.ObjectSlamPerm) != 0)
  1034. {
  1035. if (m_part.ParentGroup.Scene.Permissions.PropagatePermissions())
  1036. {
  1037. foreach (SceneObjectPart part in partList)
  1038. {
  1039. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteEveryone) != 0)
  1040. part.EveryoneMask = item.EveryonePermissions;
  1041. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteNextOwner) != 0)
  1042. part.NextOwnerMask = item.NextPermissions;
  1043. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteGroup) != 0)
  1044. part.GroupMask = item.GroupPermissions;
  1045. }
  1046. group.ApplyNextOwnerPermissions();
  1047. }
  1048. }
  1049. foreach (SceneObjectPart part in partList)
  1050. {
  1051. if ((part.OwnerID != item.OwnerID) || (item.CurrentPermissions & (uint)PermissionMask.Slam) != 0 || (item.Flags & (uint)InventoryItemFlags.ObjectSlamPerm) != 0)
  1052. {
  1053. if(part.GroupID != part.OwnerID)
  1054. part.LastOwnerID = part.OwnerID;
  1055. part.OwnerID = item.OwnerID;
  1056. part.Inventory.ChangeInventoryOwner(item.OwnerID);
  1057. }
  1058. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteEveryone) != 0)
  1059. part.EveryoneMask = item.EveryonePermissions;
  1060. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteNextOwner) != 0)
  1061. part.NextOwnerMask = item.NextPermissions;
  1062. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteGroup) != 0)
  1063. part.GroupMask = item.GroupPermissions;
  1064. }
  1065. // old code end
  1066. rootPart.TrimPermissions();
  1067. group.InvalidateDeepEffectivePerms();
  1068. }
  1069. return true;
  1070. }
  1071. /// <summary>
  1072. /// Update an existing inventory item.
  1073. /// </summary>
  1074. /// <param name="item">The updated item. An item with the same id must already exist
  1075. /// in this prim's inventory.</param>
  1076. /// <returns>false if the item did not exist, true if the update occurred successfully</returns>
  1077. public bool UpdateInventoryItem(TaskInventoryItem item)
  1078. {
  1079. return UpdateInventoryItem(item, true, true);
  1080. }
  1081. public bool UpdateInventoryItem(TaskInventoryItem item, bool fireScriptEvents)
  1082. {
  1083. return UpdateInventoryItem(item, fireScriptEvents, true);
  1084. }
  1085. public bool UpdateInventoryItem(TaskInventoryItem item, bool fireScriptEvents, bool considerChanged)
  1086. {
  1087. m_items.LockItemsForWrite(true);
  1088. if (m_items.ContainsKey(item.ItemID))
  1089. {
  1090. //m_log.DebugFormat("[PRIM INVENTORY]: Updating item {0} in {1}", item.Name, m_part.Name);
  1091. item.ParentID = m_part.UUID;
  1092. item.ParentPartID = m_part.UUID;
  1093. // If group permissions have been set on, check that the groupID is up to date in case it has
  1094. // changed since permissions were last set.
  1095. if (item.GroupPermissions != (uint)PermissionMask.None)
  1096. item.GroupID = m_part.GroupID;
  1097. if(item.OwnerID == UUID.Zero) // viewer to internal enconding of group owned
  1098. item.OwnerID = item.GroupID;
  1099. if (item.AssetID == UUID.Zero)
  1100. item.AssetID = m_items[item.ItemID].AssetID;
  1101. m_items[item.ItemID] = item;
  1102. if(item.InvType == (int)InventoryType.LSL)
  1103. {
  1104. if(m_scripts == null)
  1105. m_scripts = new Dictionary<UUID, TaskInventoryItem>();
  1106. m_scripts[item.ItemID] = item;
  1107. }
  1108. m_inventorySerial++;
  1109. if (fireScriptEvents)
  1110. m_part.TriggerScriptChangedEvent(Changed.INVENTORY);
  1111. if (considerChanged)
  1112. {
  1113. m_part.ParentGroup.InvalidateDeepEffectivePerms();
  1114. HasInventoryChanged = true;
  1115. m_part.ParentGroup.HasGroupChanged = true;
  1116. }
  1117. m_items.LockItemsForWrite(false);
  1118. return true;
  1119. }
  1120. else
  1121. {
  1122. m_log.ErrorFormat(
  1123. "[PRIM INVENTORY]: " +
  1124. "Tried to retrieve item ID {0} from prim {1}, {2} at {3} in {4} but the item does not exist in this inventory",
  1125. item.ItemID, m_part.Name, m_part.UUID,
  1126. m_part.AbsolutePosition, m_part.ParentGroup.Scene.RegionInfo.RegionName);
  1127. }
  1128. m_items.LockItemsForWrite(false);
  1129. return false;
  1130. }
  1131. /// <summary>
  1132. /// Remove an item from this prim's inventory
  1133. /// </summary>
  1134. /// <param name="itemID"></param>
  1135. /// <returns>Numeric asset type of the item removed. Returns -1 if the item did not exist
  1136. /// in this prim's inventory.</returns>
  1137. public int RemoveInventoryItem(UUID itemID)
  1138. {
  1139. m_items.LockItemsForRead(true);
  1140. if (m_items.ContainsKey(itemID))
  1141. {
  1142. int type = m_items[itemID].InvType;
  1143. m_items.LockItemsForRead(false);
  1144. if (type == (int)InventoryType.LSL) // Script
  1145. {
  1146. m_part.ParentGroup.Scene.EventManager.TriggerRemoveScript(m_part.LocalId, itemID);
  1147. }
  1148. m_items.LockItemsForWrite(true);
  1149. m_items.Remove(itemID);
  1150. if(m_scripts != null)
  1151. {
  1152. m_scripts.Remove(itemID);
  1153. if(m_scripts.Count == 0)
  1154. m_scripts = null;
  1155. }
  1156. if (m_scripts == null)
  1157. {
  1158. m_part.RemFlag(PrimFlags.Scripted);
  1159. }
  1160. m_inventorySerial++;
  1161. m_items.LockItemsForWrite(false);
  1162. m_part.ParentGroup.InvalidateDeepEffectivePerms();
  1163. HasInventoryChanged = true;
  1164. m_part.ParentGroup.HasGroupChanged = true;
  1165. m_part.ScheduleFullUpdate();
  1166. m_part.TriggerScriptChangedEvent(Changed.INVENTORY);
  1167. return type;
  1168. }
  1169. else
  1170. {
  1171. m_items.LockItemsForRead(false);
  1172. m_log.ErrorFormat(
  1173. "[PRIM INVENTORY]: " +
  1174. "Tried to remove item ID {0} from prim {1}, {2} but the item does not exist in this inventory",
  1175. itemID, m_part.Name, m_part.UUID);
  1176. }
  1177. return -1;
  1178. }
  1179. /// <summary>
  1180. /// Serialize all the metadata for the items in this prim's inventory ready for sending to the client
  1181. /// </summary>
  1182. /// <param name="xferManager"></param>
  1183. public void RequestInventoryFile(IClientAPI client, IXfer xferManager)
  1184. {
  1185. lock (m_inventoryFileLock)
  1186. {
  1187. bool changed = false;
  1188. m_items.LockItemsForRead(true);
  1189. if (m_inventorySerial == 0) // No inventory
  1190. {
  1191. m_items.LockItemsForRead(false);
  1192. client.SendTaskInventory(m_part.UUID, 0, new byte[0]);
  1193. return;
  1194. }
  1195. if (m_items.Count == 0) // No inventory
  1196. {
  1197. m_items.LockItemsForRead(false);
  1198. client.SendTaskInventory(m_part.UUID, 0, new byte[0]);
  1199. return;
  1200. }
  1201. if (m_inventoryFileNameSerial != m_inventorySerial)
  1202. {
  1203. m_inventoryFileNameSerial = m_inventorySerial;
  1204. changed = true;
  1205. }
  1206. m_items.LockItemsForRead(false);
  1207. if (m_inventoryFileData.Length < 2)
  1208. changed = true;
  1209. bool includeAssets = false;
  1210. if (m_part.ParentGroup.Scene.Permissions.CanEditObjectInventory(m_part.UUID, client.AgentId))
  1211. includeAssets = true;
  1212. if (m_inventoryPrivileged != includeAssets)
  1213. changed = true;
  1214. if (!changed)
  1215. {
  1216. xferManager.AddNewFile(m_inventoryFileName, m_inventoryFileData);
  1217. client.SendTaskInventory(m_part.UUID, (short)m_inventoryFileNameSerial,
  1218. m_inventoryFileNameBytes);
  1219. return;
  1220. }
  1221. m_inventoryPrivileged = includeAssets;
  1222. InventoryStringBuilder invString = new InventoryStringBuilder(m_part.UUID, UUID.Zero);
  1223. m_items.LockItemsForRead(true);
  1224. foreach (TaskInventoryItem item in m_items.Values)
  1225. {
  1226. UUID ownerID = item.OwnerID;
  1227. UUID groupID = item.GroupID;
  1228. uint everyoneMask = item.EveryonePermissions;
  1229. uint baseMask = item.BasePermissions;
  1230. uint ownerMask = item.CurrentPermissions;
  1231. uint groupMask = item.GroupPermissions;
  1232. invString.AddItemStart();
  1233. invString.AddNameValueLine("item_id", item.ItemID.ToString());
  1234. invString.AddNameValueLine("parent_id", m_part.UUID.ToString());
  1235. invString.AddPermissionsStart();
  1236. invString.AddNameValueLine("base_mask", Utils.UIntToHexString(baseMask));
  1237. invString.AddNameValueLine("owner_mask", Utils.UIntToHexString(ownerMask));
  1238. invString.AddNameValueLine("group_mask", Utils.UIntToHexString(groupMask));
  1239. invString.AddNameValueLine("everyone_mask", Utils.UIntToHexString(everyoneMask));
  1240. invString.AddNameValueLine("next_owner_mask", Utils.UIntToHexString(item.NextPermissions));
  1241. invString.AddNameValueLine("creator_id", item.CreatorID.ToString());
  1242. invString.AddNameValueLine("last_owner_id", item.LastOwnerID.ToString());
  1243. invString.AddNameValueLine("group_id",groupID.ToString());
  1244. if(groupID != UUID.Zero && ownerID == groupID)
  1245. {
  1246. invString.AddNameValueLine("owner_id", UUID.Zero.ToString());
  1247. invString.AddNameValueLine("group_owned","1");
  1248. }
  1249. else
  1250. {
  1251. invString.AddNameValueLine("owner_id", ownerID.ToString());
  1252. invString.AddNameValueLine("group_owned","0");
  1253. }
  1254. invString.AddSectionEnd();
  1255. if (includeAssets)
  1256. invString.AddNameValueLine("asset_id", item.AssetID.ToString());
  1257. else
  1258. invString.AddNameValueLine("asset_id", UUID.Zero.ToString());
  1259. invString.AddNameValueLine("type", Utils.AssetTypeToString((AssetType)item.Type));
  1260. invString.AddNameValueLine("inv_type", Utils.InventoryTypeToString((InventoryType)item.InvType));
  1261. invString.AddNameValueLine("flags", Utils.UIntToHexString(item.Flags));
  1262. invString.AddSaleStart();
  1263. invString.AddNameValueLine("sale_type", "not");
  1264. invString.AddNameValueLine("sale_price", "0");
  1265. invString.AddSectionEnd();
  1266. invString.AddNameValueLine("name", item.Name + "|");
  1267. invString.AddNameValueLine("desc", item.Description + "|");
  1268. invString.AddNameValueLine("creation_date", item.CreationDate.ToString());
  1269. invString.AddSectionEnd();
  1270. }
  1271. m_items.LockItemsForRead(false);
  1272. m_inventoryFileData = Utils.StringToBytes(invString.GetString());
  1273. if (m_inventoryFileData.Length > 2)
  1274. {
  1275. m_inventoryFileName = "inventory_" + UUID.Random().ToString() + ".tmp";
  1276. m_inventoryFileNameBytes = Util.StringToBytes256(m_inventoryFileName);
  1277. xferManager.AddNewFile(m_inventoryFileName, m_inventoryFileData);
  1278. client.SendTaskInventory(m_part.UUID, (short)m_inventoryFileNameSerial,m_inventoryFileNameBytes);
  1279. return;
  1280. }
  1281. client.SendTaskInventory(m_part.UUID, 0, new byte[0]);
  1282. }
  1283. }
  1284. /// <summary>
  1285. /// Process inventory backup
  1286. /// </summary>
  1287. /// <param name="datastore"></param>
  1288. public void ProcessInventoryBackup(ISimulationDataService datastore)
  1289. {
  1290. // Removed this because linking will cause an immediate delete of the new
  1291. // child prim from the database and the subsequent storing of the prim sees
  1292. // the inventory of it as unchanged and doesn't store it at all. The overhead
  1293. // of storing prim inventory needlessly is much less than the aggravation
  1294. // of prim inventory loss.
  1295. // if (HasInventoryChanged)
  1296. // {
  1297. m_items.LockItemsForRead(true);
  1298. ICollection<TaskInventoryItem> itemsvalues = m_items.Values;
  1299. HasInventoryChanged = false;
  1300. m_items.LockItemsForRead(false);
  1301. try
  1302. {
  1303. datastore.StorePrimInventory(m_part.UUID, itemsvalues);
  1304. }
  1305. catch {}
  1306. // }
  1307. }
  1308. public class InventoryStringBuilder
  1309. {
  1310. private StringBuilder BuildString = new StringBuilder(1024);
  1311. public InventoryStringBuilder(UUID folderID, UUID parentID)
  1312. {
  1313. BuildString.Append("\tinv_object\t0\n\t{\n");
  1314. AddNameValueLine("obj_id", folderID.ToString());
  1315. AddNameValueLine("parent_id", parentID.ToString());
  1316. AddNameValueLine("type", "category");
  1317. AddNameValueLine("name", "Contents|\n\t}");
  1318. }
  1319. public void AddItemStart()
  1320. {
  1321. BuildString.Append("\tinv_item\t0\n\t{\n");
  1322. }
  1323. public void AddPermissionsStart()
  1324. {
  1325. BuildString.Append("\tpermissions 0\n\t{\n");
  1326. }
  1327. public void AddSaleStart()
  1328. {
  1329. BuildString.Append("\tsale_info\t0\n\t{\n");
  1330. }
  1331. protected void AddSectionStart()
  1332. {
  1333. BuildString.Append("\t{\n");
  1334. }
  1335. public void AddSectionEnd()
  1336. {
  1337. BuildString.Append("\t}\n");
  1338. }
  1339. public void AddLine(string addLine)
  1340. {
  1341. BuildString.Append(addLine);
  1342. }
  1343. public void AddNameValueLine(string name, string value)
  1344. {
  1345. BuildString.Append("\t\t");
  1346. BuildString.Append(name);
  1347. BuildString.Append("\t");
  1348. BuildString.Append(value);
  1349. BuildString.Append("\n");
  1350. }
  1351. public String GetString()
  1352. {
  1353. return BuildString.ToString();
  1354. }
  1355. public void Close()
  1356. {
  1357. }
  1358. }
  1359. public void AggregateInnerPerms(ref uint owner, ref uint group, ref uint everyone)
  1360. {
  1361. foreach (TaskInventoryItem item in m_items.Values)
  1362. {
  1363. if(item.InvType == (sbyte)InventoryType.Landmark)
  1364. continue;
  1365. owner &= item.CurrentPermissions;
  1366. group &= item.GroupPermissions;
  1367. everyone &= item.EveryonePermissions;
  1368. }
  1369. }
  1370. public uint MaskEffectivePermissions()
  1371. {
  1372. // used to propagate permissions restrictions outwards
  1373. // Modify does not propagate outwards.
  1374. uint mask=0x7fffffff;
  1375. foreach (TaskInventoryItem item in m_items.Values)
  1376. {
  1377. if(item.InvType == (sbyte)InventoryType.Landmark)
  1378. continue;
  1379. // apply current to normal permission bits
  1380. uint newperms = item.CurrentPermissions;
  1381. if ((newperms & (uint)PermissionMask.Copy) == 0)
  1382. mask &= ~(uint)PermissionMask.Copy;
  1383. if ((newperms & (uint)PermissionMask.Transfer) == 0)
  1384. mask &= ~(uint)PermissionMask.Transfer;
  1385. if ((newperms & (uint)PermissionMask.Export) == 0)
  1386. mask &= ~((uint)PermissionMask.Export);
  1387. // apply next owner restricted by current to folded bits
  1388. newperms &= item.NextPermissions;
  1389. if ((newperms & (uint)PermissionMask.Copy) == 0)
  1390. mask &= ~((uint)PermissionMask.FoldedCopy);
  1391. if ((newperms & (uint)PermissionMask.Transfer) == 0)
  1392. mask &= ~((uint)PermissionMask.FoldedTransfer);
  1393. if ((newperms & (uint)PermissionMask.Export) == 0)
  1394. mask &= ~((uint)PermissionMask.FoldedExport);
  1395. }
  1396. return mask;
  1397. }
  1398. public void ApplyNextOwnerPermissions()
  1399. {
  1400. foreach (TaskInventoryItem item in m_items.Values)
  1401. {
  1402. item.CurrentPermissions &= item.NextPermissions;
  1403. item.BasePermissions &= item.NextPermissions;
  1404. item.EveryonePermissions &= item.NextPermissions;
  1405. item.OwnerChanged = true;
  1406. item.PermsMask = 0;
  1407. item.PermsGranter = UUID.Zero;
  1408. }
  1409. }
  1410. public void ApplyGodPermissions(uint perms)
  1411. {
  1412. foreach (TaskInventoryItem item in m_items.Values)
  1413. {
  1414. item.CurrentPermissions = perms;
  1415. item.BasePermissions = perms;
  1416. }
  1417. m_inventorySerial++;
  1418. HasInventoryChanged = true;
  1419. }
  1420. /// <summary>
  1421. /// Returns true if this part inventory contains any scripts. False otherwise.
  1422. /// </summary>
  1423. /// <returns></returns>
  1424. public bool ContainsScripts()
  1425. {
  1426. m_items.LockItemsForRead(true);
  1427. bool res = (m_scripts != null && m_scripts.Count >0);
  1428. m_items.LockItemsForRead(false);
  1429. return res;
  1430. }
  1431. /// <summary>
  1432. /// Returns the count of scripts in this parts inventory.
  1433. /// </summary>
  1434. /// <returns></returns>
  1435. public int ScriptCount()
  1436. {
  1437. int count = 0;
  1438. m_items.LockItemsForRead(true);
  1439. if(m_scripts != null)
  1440. count = m_scripts.Count;
  1441. m_items.LockItemsForRead(false);
  1442. return count;
  1443. }
  1444. /// <summary>
  1445. /// Returns the count of running scripts in this parts inventory.
  1446. /// </summary>
  1447. /// <returns></returns>
  1448. public int RunningScriptCount()
  1449. {
  1450. IScriptModule[] scriptEngines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  1451. if (scriptEngines.Length == 0)
  1452. return 0;
  1453. int count = 0;
  1454. m_items.LockItemsForRead(true);
  1455. if (m_scripts == null || m_scripts.Count == 0)
  1456. {
  1457. m_items.LockItemsForRead(false);
  1458. return 0;
  1459. }
  1460. List<TaskInventoryItem> scripts = new List<TaskInventoryItem>(m_scripts.Values);
  1461. m_items.LockItemsForRead(false);
  1462. foreach (TaskInventoryItem item in scripts)
  1463. {
  1464. foreach (IScriptModule engine in scriptEngines)
  1465. {
  1466. if (engine != null)
  1467. {
  1468. if (engine.HasScript(item.ItemID, out bool running))
  1469. {
  1470. if(running)
  1471. count++;
  1472. break;
  1473. }
  1474. }
  1475. }
  1476. }
  1477. return count;
  1478. }
  1479. public List<UUID> GetInventoryList()
  1480. {
  1481. m_items.LockItemsForRead(true);
  1482. List<UUID> ret = new List<UUID>(m_items.Count);
  1483. foreach (TaskInventoryItem item in m_items.Values)
  1484. ret.Add(item.ItemID);
  1485. m_items.LockItemsForRead(false);
  1486. return ret;
  1487. }
  1488. public List<TaskInventoryItem> GetInventoryItems()
  1489. {
  1490. m_items.LockItemsForRead(true);
  1491. List<TaskInventoryItem> ret = new List<TaskInventoryItem>(m_items.Values);
  1492. m_items.LockItemsForRead(false);
  1493. return ret;
  1494. }
  1495. public List<TaskInventoryItem> GetInventoryItems(InventoryType type)
  1496. {
  1497. m_items.LockItemsForRead(true);
  1498. List<TaskInventoryItem> ret = new List<TaskInventoryItem>(m_items.Count);
  1499. foreach (TaskInventoryItem item in m_items.Values)
  1500. if (item.InvType == (int)type)
  1501. ret.Add(item);
  1502. m_items.LockItemsForRead(false);
  1503. return ret;
  1504. }
  1505. public Dictionary<UUID, string> GetScriptStates()
  1506. {
  1507. return GetScriptStates(false);
  1508. }
  1509. public Dictionary<UUID, string> GetScriptStates(bool oldIDs)
  1510. {
  1511. Dictionary<UUID, string> ret = new Dictionary<UUID, string>();
  1512. if (m_part.ParentGroup.Scene == null) // Group not in a scene
  1513. return ret;
  1514. IScriptModule[] scriptEngines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  1515. if (scriptEngines.Length == 0) // No engine at all
  1516. return ret;
  1517. m_items.LockItemsForRead(true);
  1518. if (m_scripts == null || m_scripts.Count == 0)
  1519. {
  1520. m_items.LockItemsForRead(false);
  1521. return ret;
  1522. }
  1523. List<TaskInventoryItem> scripts = new List<TaskInventoryItem>(m_scripts.Values);
  1524. m_items.LockItemsForRead(false);
  1525. foreach (TaskInventoryItem item in scripts)
  1526. {
  1527. foreach (IScriptModule e in scriptEngines)
  1528. {
  1529. if (e != null)
  1530. {
  1531. //m_log.DebugFormat(
  1532. // "[PRIM INVENTORY]: Getting script state from engine {0} for {1} in part {2} in group {3} in {4}",
  1533. // e.Name, item.Name, m_part.Name, m_part.ParentGroup.Name, m_part.ParentGroup.Scene.Name);
  1534. string n = e.GetXMLState(item.ItemID);
  1535. if (n != String.Empty)
  1536. {
  1537. if (oldIDs)
  1538. {
  1539. if (!ret.ContainsKey(item.OldItemID))
  1540. ret[item.OldItemID] = n;
  1541. }
  1542. else
  1543. {
  1544. if (!ret.ContainsKey(item.ItemID))
  1545. ret[item.ItemID] = n;
  1546. }
  1547. break;
  1548. }
  1549. }
  1550. }
  1551. }
  1552. return ret;
  1553. }
  1554. public void ResumeScripts()
  1555. {
  1556. IScriptModule[] scriptEngines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  1557. if (scriptEngines.Length == 0)
  1558. return;
  1559. m_items.LockItemsForRead(true);
  1560. if (m_scripts == null || m_scripts.Count == 0)
  1561. {
  1562. m_items.LockItemsForRead(false);
  1563. return;
  1564. }
  1565. List<TaskInventoryItem> scripts = new List<TaskInventoryItem>(m_scripts.Values);
  1566. m_items.LockItemsForRead(false);
  1567. foreach (TaskInventoryItem item in scripts)
  1568. {
  1569. foreach (IScriptModule engine in scriptEngines)
  1570. {
  1571. if (engine != null)
  1572. {
  1573. //m_log.DebugFormat(
  1574. // "[PRIM INVENTORY]: Resuming script {0} {1} for {2}, OwnerChanged {3}",
  1575. // item.Name, item.ItemID, item.OwnerID, item.OwnerChanged);
  1576. if(!engine.ResumeScript(item.ItemID))
  1577. continue;
  1578. if (item.OwnerChanged)
  1579. engine.PostScriptEvent(item.ItemID, "changed", new Object[] { (int)Changed.OWNER });
  1580. item.OwnerChanged = false;
  1581. }
  1582. }
  1583. }
  1584. }
  1585. }
  1586. }