SceneObjectPartInventory.cs 51 KB

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