SceneObjectPartInventory.cs 59 KB

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