SceneObjectPartInventory.cs 51 KB

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