SceneObjectPartInventory.cs 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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 bool GetRezReadySceneObjects(TaskInventoryItem item, out List<SceneObjectGroup> objlist, out List<Vector3> veclist)
  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. objlist = null;
  645. veclist = null;
  646. return false;
  647. }
  648. Vector3 bbox;
  649. float offsetHeight;
  650. bool single = m_part.ParentGroup.Scene.GetObjectsToRez(rezAsset.Data, false, out objlist, out veclist, out bbox, out offsetHeight);
  651. for (int i = 0; i < objlist.Count; i++)
  652. {
  653. SceneObjectGroup group = objlist[i];
  654. group.ResetIDs();
  655. SceneObjectPart rootPart = group.GetPart(group.UUID);
  656. // Since renaming the item in the inventory does not affect the name stored
  657. // in the serialization, transfer the correct name from the inventory to the
  658. // object itself before we rez.
  659. rootPart.Name = item.Name;
  660. rootPart.Description = item.Description;
  661. SceneObjectPart[] partList = group.Parts;
  662. group.SetGroup(m_part.GroupID, null);
  663. // TODO: Remove magic number badness
  664. if ((rootPart.OwnerID != item.OwnerID) || (item.CurrentPermissions & 16) != 0 || (item.Flags & (uint)InventoryItemFlags.ObjectSlamPerm) != 0) // Magic number
  665. {
  666. if (m_part.ParentGroup.Scene.Permissions.PropagatePermissions())
  667. {
  668. foreach (SceneObjectPart part in partList)
  669. {
  670. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteEveryone) != 0)
  671. part.EveryoneMask = item.EveryonePermissions;
  672. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteNextOwner) != 0)
  673. part.NextOwnerMask = item.NextPermissions;
  674. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteGroup) != 0)
  675. part.GroupMask = item.GroupPermissions;
  676. }
  677. group.ApplyNextOwnerPermissions();
  678. }
  679. }
  680. foreach (SceneObjectPart part in partList)
  681. {
  682. // TODO: Remove magic number badness
  683. if ((part.OwnerID != item.OwnerID) || (item.CurrentPermissions & 16) != 0 || (item.Flags & (uint)InventoryItemFlags.ObjectSlamPerm) != 0) // Magic number
  684. {
  685. part.LastOwnerID = part.OwnerID;
  686. part.OwnerID = item.OwnerID;
  687. part.Inventory.ChangeInventoryOwner(item.OwnerID);
  688. }
  689. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteEveryone) != 0)
  690. part.EveryoneMask = item.EveryonePermissions;
  691. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteNextOwner) != 0)
  692. part.NextOwnerMask = item.NextPermissions;
  693. if ((item.Flags & (uint)InventoryItemFlags.ObjectOverwriteGroup) != 0)
  694. part.GroupMask = item.GroupPermissions;
  695. }
  696. rootPart.TrimPermissions();
  697. }
  698. return true;
  699. }
  700. /// <summary>
  701. /// Update an existing inventory item.
  702. /// </summary>
  703. /// <param name="item">The updated item. An item with the same id must already exist
  704. /// in this prim's inventory.</param>
  705. /// <returns>false if the item did not exist, true if the update occurred successfully</returns>
  706. public bool UpdateInventoryItem(TaskInventoryItem item)
  707. {
  708. return UpdateInventoryItem(item, true, true);
  709. }
  710. public bool UpdateInventoryItem(TaskInventoryItem item, bool fireScriptEvents)
  711. {
  712. return UpdateInventoryItem(item, fireScriptEvents, true);
  713. }
  714. public bool UpdateInventoryItem(TaskInventoryItem item, bool fireScriptEvents, bool considerChanged)
  715. {
  716. TaskInventoryItem it = GetInventoryItem(item.ItemID);
  717. if (it != null)
  718. {
  719. // m_log.DebugFormat("[PRIM INVENTORY]: Updating item {0} in {1}", item.Name, m_part.Name);
  720. item.ParentID = m_part.UUID;
  721. item.ParentPartID = m_part.UUID;
  722. // If group permissions have been set on, check that the groupID is up to date in case it has
  723. // changed since permissions were last set.
  724. if (item.GroupPermissions != (uint)PermissionMask.None)
  725. item.GroupID = m_part.GroupID;
  726. if (item.AssetID == UUID.Zero)
  727. item.AssetID = it.AssetID;
  728. lock (m_items)
  729. {
  730. m_items[item.ItemID] = item;
  731. m_inventorySerial++;
  732. }
  733. if (fireScriptEvents)
  734. m_part.TriggerScriptChangedEvent(Changed.INVENTORY);
  735. if (considerChanged)
  736. {
  737. HasInventoryChanged = true;
  738. m_part.ParentGroup.HasGroupChanged = true;
  739. }
  740. return true;
  741. }
  742. else
  743. {
  744. m_log.ErrorFormat(
  745. "[PRIM INVENTORY]: " +
  746. "Tried to retrieve item ID {0} from prim {1}, {2} at {3} in {4} but the item does not exist in this inventory",
  747. item.ItemID, m_part.Name, m_part.UUID,
  748. m_part.AbsolutePosition, m_part.ParentGroup.Scene.RegionInfo.RegionName);
  749. }
  750. return false;
  751. }
  752. /// <summary>
  753. /// Remove an item from this prim's inventory
  754. /// </summary>
  755. /// <param name="itemID"></param>
  756. /// <returns>Numeric asset type of the item removed. Returns -1 if the item did not exist
  757. /// in this prim's inventory.</returns>
  758. public int RemoveInventoryItem(UUID itemID)
  759. {
  760. TaskInventoryItem item = GetInventoryItem(itemID);
  761. if (item != null)
  762. {
  763. int type = m_items[itemID].InvType;
  764. if (type == 10) // Script
  765. {
  766. m_part.RemoveScriptEvents(itemID);
  767. m_part.ParentGroup.Scene.EventManager.TriggerRemoveScript(m_part.LocalId, itemID);
  768. }
  769. m_items.Remove(itemID);
  770. m_inventorySerial++;
  771. m_part.TriggerScriptChangedEvent(Changed.INVENTORY);
  772. HasInventoryChanged = true;
  773. m_part.ParentGroup.HasGroupChanged = true;
  774. if (!ContainsScripts())
  775. m_part.RemFlag(PrimFlags.Scripted);
  776. m_part.ScheduleFullUpdate();
  777. return type;
  778. }
  779. else
  780. {
  781. m_log.ErrorFormat(
  782. "[PRIM INVENTORY]: " +
  783. "Tried to remove item ID {0} from prim {1}, {2} at {3} in {4} but the item does not exist in this inventory",
  784. itemID, m_part.Name, m_part.UUID,
  785. m_part.AbsolutePosition, m_part.ParentGroup.Scene.RegionInfo.RegionName);
  786. }
  787. return -1;
  788. }
  789. private bool CreateInventoryFile()
  790. {
  791. // m_log.DebugFormat(
  792. // "[PRIM INVENTORY]: Creating inventory file for {0} {1} {2}, serial {3}",
  793. // m_part.Name, m_part.UUID, m_part.LocalId, m_inventorySerial);
  794. if (m_inventoryFileName == String.Empty ||
  795. m_inventoryFileNameSerial < m_inventorySerial)
  796. {
  797. // Something changed, we need to create a new file
  798. m_inventoryFileName = "inventory_" + UUID.Random().ToString() + ".tmp";
  799. m_inventoryFileNameSerial = m_inventorySerial;
  800. InventoryStringBuilder invString = new InventoryStringBuilder(m_part.UUID, UUID.Zero);
  801. lock (m_items)
  802. {
  803. foreach (TaskInventoryItem item in m_items.Values)
  804. {
  805. // m_log.DebugFormat(
  806. // "[PRIM INVENTORY]: Adding item {0} {1} for serial {2} on prim {3} {4} {5}",
  807. // item.Name, item.ItemID, m_inventorySerial, m_part.Name, m_part.UUID, m_part.LocalId);
  808. UUID ownerID = item.OwnerID;
  809. uint everyoneMask = 0;
  810. uint baseMask = item.BasePermissions;
  811. uint ownerMask = item.CurrentPermissions;
  812. uint groupMask = item.GroupPermissions;
  813. invString.AddItemStart();
  814. invString.AddNameValueLine("item_id", item.ItemID.ToString());
  815. invString.AddNameValueLine("parent_id", m_part.UUID.ToString());
  816. invString.AddPermissionsStart();
  817. invString.AddNameValueLine("base_mask", Utils.UIntToHexString(baseMask));
  818. invString.AddNameValueLine("owner_mask", Utils.UIntToHexString(ownerMask));
  819. invString.AddNameValueLine("group_mask", Utils.UIntToHexString(groupMask));
  820. invString.AddNameValueLine("everyone_mask", Utils.UIntToHexString(everyoneMask));
  821. invString.AddNameValueLine("next_owner_mask", Utils.UIntToHexString(item.NextPermissions));
  822. invString.AddNameValueLine("creator_id", item.CreatorID.ToString());
  823. invString.AddNameValueLine("owner_id", ownerID.ToString());
  824. invString.AddNameValueLine("last_owner_id", item.LastOwnerID.ToString());
  825. invString.AddNameValueLine("group_id", item.GroupID.ToString());
  826. invString.AddSectionEnd();
  827. invString.AddNameValueLine("asset_id", item.AssetID.ToString());
  828. invString.AddNameValueLine("type", Utils.AssetTypeToString((AssetType)item.Type));
  829. invString.AddNameValueLine("inv_type", Utils.InventoryTypeToString((InventoryType)item.InvType));
  830. invString.AddNameValueLine("flags", Utils.UIntToHexString(item.Flags));
  831. invString.AddSaleStart();
  832. invString.AddNameValueLine("sale_type", "not");
  833. invString.AddNameValueLine("sale_price", "0");
  834. invString.AddSectionEnd();
  835. invString.AddNameValueLine("name", item.Name + "|");
  836. invString.AddNameValueLine("desc", item.Description + "|");
  837. invString.AddNameValueLine("creation_date", item.CreationDate.ToString());
  838. invString.AddSectionEnd();
  839. }
  840. }
  841. m_inventoryFileData = Utils.StringToBytes(invString.BuildString);
  842. return true;
  843. }
  844. // No need to recreate, the existing file is fine
  845. return false;
  846. }
  847. /// <summary>
  848. /// Serialize all the metadata for the items in this prim's inventory ready for sending to the client
  849. /// </summary>
  850. /// <param name="xferManager"></param>
  851. public void RequestInventoryFile(IClientAPI client, IXfer xferManager)
  852. {
  853. lock (m_items)
  854. {
  855. // Don't send a inventory xfer name if there are no items. Doing so causes viewer 3 to crash when rezzing
  856. // a new script if any previous deletion has left the prim inventory empty.
  857. if (m_items.Count == 0) // No inventory
  858. {
  859. // m_log.DebugFormat(
  860. // "[PRIM INVENTORY]: Not sending inventory data for part {0} {1} {2} for {3} since no items",
  861. // m_part.Name, m_part.LocalId, m_part.UUID, client.Name);
  862. client.SendTaskInventory(m_part.UUID, 0, new byte[0]);
  863. return;
  864. }
  865. CreateInventoryFile();
  866. // In principle, we should only do the rest if the inventory changed;
  867. // by sending m_inventorySerial to the client, it ought to know
  868. // that nothing changed and that it doesn't need to request the file.
  869. // Unfortunately, it doesn't look like the client optimizes this;
  870. // the client seems to always come back and request the Xfer,
  871. // no matter what value m_inventorySerial has.
  872. // FIXME: Could probably be > 0 here rather than > 2
  873. if (m_inventoryFileData.Length > 2)
  874. {
  875. // Add the file for Xfer
  876. // m_log.DebugFormat(
  877. // "[PRIM INVENTORY]: Adding inventory file {0} (length {1}) for transfer on {2} {3} {4}",
  878. // m_inventoryFileName, m_inventoryFileData.Length, m_part.Name, m_part.UUID, m_part.LocalId);
  879. xferManager.AddNewFile(m_inventoryFileName, m_inventoryFileData);
  880. }
  881. // Tell the client we're ready to Xfer the file
  882. client.SendTaskInventory(m_part.UUID, (short)m_inventorySerial,
  883. Util.StringToBytes256(m_inventoryFileName));
  884. }
  885. }
  886. /// <summary>
  887. /// Process inventory backup
  888. /// </summary>
  889. /// <param name="datastore"></param>
  890. public void ProcessInventoryBackup(ISimulationDataService datastore)
  891. {
  892. if (HasInventoryChanged)
  893. {
  894. HasInventoryChanged = false;
  895. List<TaskInventoryItem> items = GetInventoryItems();
  896. datastore.StorePrimInventory(m_part.UUID, items);
  897. }
  898. }
  899. public class InventoryStringBuilder
  900. {
  901. public string BuildString = String.Empty;
  902. public InventoryStringBuilder(UUID folderID, UUID parentID)
  903. {
  904. BuildString += "\tinv_object\t0\n\t{\n";
  905. AddNameValueLine("obj_id", folderID.ToString());
  906. AddNameValueLine("parent_id", parentID.ToString());
  907. AddNameValueLine("type", "category");
  908. AddNameValueLine("name", "Contents|");
  909. AddSectionEnd();
  910. }
  911. public void AddItemStart()
  912. {
  913. BuildString += "\tinv_item\t0\n";
  914. AddSectionStart();
  915. }
  916. public void AddPermissionsStart()
  917. {
  918. BuildString += "\tpermissions 0\n";
  919. AddSectionStart();
  920. }
  921. public void AddSaleStart()
  922. {
  923. BuildString += "\tsale_info\t0\n";
  924. AddSectionStart();
  925. }
  926. protected void AddSectionStart()
  927. {
  928. BuildString += "\t{\n";
  929. }
  930. public void AddSectionEnd()
  931. {
  932. BuildString += "\t}\n";
  933. }
  934. public void AddLine(string addLine)
  935. {
  936. BuildString += addLine;
  937. }
  938. public void AddNameValueLine(string name, string value)
  939. {
  940. BuildString += "\t\t";
  941. BuildString += name + "\t";
  942. BuildString += value + "\n";
  943. }
  944. public void Close()
  945. {
  946. }
  947. }
  948. public uint MaskEffectivePermissions()
  949. {
  950. uint mask=0x7fffffff;
  951. lock (m_items)
  952. {
  953. foreach (TaskInventoryItem item in m_items.Values)
  954. {
  955. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Copy) == 0)
  956. mask &= ~((uint)PermissionMask.Copy >> 13);
  957. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Transfer) == 0)
  958. mask &= ~((uint)PermissionMask.Transfer >> 13);
  959. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Modify) == 0)
  960. mask &= ~((uint)PermissionMask.Modify >> 13);
  961. if (item.InvType != (int)InventoryType.Object)
  962. {
  963. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Copy) == 0)
  964. mask &= ~((uint)PermissionMask.Copy >> 13);
  965. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Transfer) == 0)
  966. mask &= ~((uint)PermissionMask.Transfer >> 13);
  967. if ((item.CurrentPermissions & item.NextPermissions & (uint)PermissionMask.Modify) == 0)
  968. mask &= ~((uint)PermissionMask.Modify >> 13);
  969. }
  970. else
  971. {
  972. if ((item.CurrentPermissions & ((uint)PermissionMask.Copy >> 13)) == 0)
  973. mask &= ~((uint)PermissionMask.Copy >> 13);
  974. if ((item.CurrentPermissions & ((uint)PermissionMask.Transfer >> 13)) == 0)
  975. mask &= ~((uint)PermissionMask.Transfer >> 13);
  976. if ((item.CurrentPermissions & ((uint)PermissionMask.Modify >> 13)) == 0)
  977. mask &= ~((uint)PermissionMask.Modify >> 13);
  978. }
  979. if ((item.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  980. mask &= ~(uint)PermissionMask.Copy;
  981. if ((item.CurrentPermissions & (uint)PermissionMask.Transfer) == 0)
  982. mask &= ~(uint)PermissionMask.Transfer;
  983. if ((item.CurrentPermissions & (uint)PermissionMask.Modify) == 0)
  984. mask &= ~(uint)PermissionMask.Modify;
  985. }
  986. }
  987. return mask;
  988. }
  989. public void ApplyNextOwnerPermissions()
  990. {
  991. lock (m_items)
  992. {
  993. foreach (TaskInventoryItem item in m_items.Values)
  994. {
  995. // m_log.DebugFormat (
  996. // "[SCENE OBJECT PART INVENTORY]: Applying next permissions {0} to {1} in {2} with current {3}, base {4}, everyone {5}",
  997. // item.NextPermissions, item.Name, m_part.Name, item.CurrentPermissions, item.BasePermissions, item.EveryonePermissions);
  998. if (item.InvType == (int)InventoryType.Object && (item.CurrentPermissions & 7) != 0)
  999. {
  1000. if ((item.CurrentPermissions & ((uint)PermissionMask.Copy >> 13)) == 0)
  1001. item.CurrentPermissions &= ~(uint)PermissionMask.Copy;
  1002. if ((item.CurrentPermissions & ((uint)PermissionMask.Transfer >> 13)) == 0)
  1003. item.CurrentPermissions &= ~(uint)PermissionMask.Transfer;
  1004. if ((item.CurrentPermissions & ((uint)PermissionMask.Modify >> 13)) == 0)
  1005. item.CurrentPermissions &= ~(uint)PermissionMask.Modify;
  1006. }
  1007. item.CurrentPermissions &= item.NextPermissions;
  1008. item.BasePermissions &= item.NextPermissions;
  1009. item.EveryonePermissions &= item.NextPermissions;
  1010. item.OwnerChanged = true;
  1011. item.PermsMask = 0;
  1012. item.PermsGranter = UUID.Zero;
  1013. }
  1014. }
  1015. }
  1016. public void ApplyGodPermissions(uint perms)
  1017. {
  1018. lock (m_items)
  1019. {
  1020. foreach (TaskInventoryItem item in m_items.Values)
  1021. {
  1022. item.CurrentPermissions = perms;
  1023. item.BasePermissions = perms;
  1024. }
  1025. }
  1026. m_inventorySerial++;
  1027. HasInventoryChanged = true;
  1028. }
  1029. /// <summary>
  1030. /// Returns true if this part inventory contains any scripts. False otherwise.
  1031. /// </summary>
  1032. /// <returns></returns>
  1033. public bool ContainsScripts()
  1034. {
  1035. lock (m_items)
  1036. {
  1037. foreach (TaskInventoryItem item in m_items.Values)
  1038. {
  1039. if (item.InvType == (int)InventoryType.LSL)
  1040. {
  1041. return true;
  1042. }
  1043. }
  1044. }
  1045. return false;
  1046. }
  1047. /// <summary>
  1048. /// Returns the count of scripts in this parts inventory.
  1049. /// </summary>
  1050. /// <returns></returns>
  1051. public int ScriptCount()
  1052. {
  1053. int count = 0;
  1054. lock (m_items)
  1055. {
  1056. foreach (TaskInventoryItem item in m_items.Values)
  1057. {
  1058. if (item.InvType == (int)InventoryType.LSL)
  1059. {
  1060. count++;
  1061. }
  1062. }
  1063. }
  1064. return count;
  1065. }
  1066. /// <summary>
  1067. /// Returns the count of running scripts in this parts inventory.
  1068. /// </summary>
  1069. /// <returns></returns>
  1070. public int RunningScriptCount()
  1071. {
  1072. IScriptModule[] engines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  1073. if (engines.Length == 0)
  1074. return 0;
  1075. int count = 0;
  1076. List<TaskInventoryItem> scripts = GetInventoryItems(InventoryType.LSL);
  1077. foreach (TaskInventoryItem item in scripts)
  1078. {
  1079. foreach (IScriptModule engine in engines)
  1080. {
  1081. if (engine != null)
  1082. {
  1083. if (engine.GetScriptState(item.ItemID))
  1084. {
  1085. count++;
  1086. }
  1087. }
  1088. }
  1089. }
  1090. return count;
  1091. }
  1092. public List<UUID> GetInventoryList()
  1093. {
  1094. List<UUID> ret = new List<UUID>();
  1095. lock (m_items)
  1096. {
  1097. foreach (TaskInventoryItem item in m_items.Values)
  1098. ret.Add(item.ItemID);
  1099. }
  1100. return ret;
  1101. }
  1102. public List<TaskInventoryItem> GetInventoryItems()
  1103. {
  1104. List<TaskInventoryItem> ret = new List<TaskInventoryItem>();
  1105. lock (m_items)
  1106. ret = new List<TaskInventoryItem>(m_items.Values);
  1107. return ret;
  1108. }
  1109. public List<TaskInventoryItem> GetInventoryItems(InventoryType type)
  1110. {
  1111. List<TaskInventoryItem> ret = new List<TaskInventoryItem>();
  1112. lock (m_items)
  1113. {
  1114. foreach (TaskInventoryItem item in m_items.Values)
  1115. if (item.InvType == (int)type)
  1116. ret.Add(item);
  1117. }
  1118. return ret;
  1119. }
  1120. public Dictionary<UUID, string> GetScriptStates()
  1121. {
  1122. Dictionary<UUID, string> ret = new Dictionary<UUID, string>();
  1123. if (m_part.ParentGroup.Scene == null) // Group not in a scene
  1124. return ret;
  1125. IScriptModule[] engines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  1126. if (engines.Length == 0) // No engine at all
  1127. return ret;
  1128. List<TaskInventoryItem> scripts = GetInventoryItems(InventoryType.LSL);
  1129. foreach (TaskInventoryItem item in scripts)
  1130. {
  1131. foreach (IScriptModule e in engines)
  1132. {
  1133. if (e != null)
  1134. {
  1135. // m_log.DebugFormat(
  1136. // "[PRIM INVENTORY]: Getting script state from engine {0} for {1} in part {2} in group {3} in {4}",
  1137. // e.Name, item.Name, m_part.Name, m_part.ParentGroup.Name, m_part.ParentGroup.Scene.Name);
  1138. string n = e.GetXMLState(item.ItemID);
  1139. if (n != String.Empty)
  1140. {
  1141. if (!ret.ContainsKey(item.ItemID))
  1142. ret[item.ItemID] = n;
  1143. break;
  1144. }
  1145. }
  1146. }
  1147. }
  1148. return ret;
  1149. }
  1150. public void ResumeScripts()
  1151. {
  1152. IScriptModule[] engines = m_part.ParentGroup.Scene.RequestModuleInterfaces<IScriptModule>();
  1153. if (engines.Length == 0)
  1154. return;
  1155. List<TaskInventoryItem> scripts = GetInventoryItems(InventoryType.LSL);
  1156. foreach (TaskInventoryItem item in scripts)
  1157. {
  1158. foreach (IScriptModule engine in engines)
  1159. {
  1160. if (engine != null)
  1161. {
  1162. // m_log.DebugFormat(
  1163. // "[PRIM INVENTORY]: Resuming script {0} {1} for {2}, OwnerChanged {3}",
  1164. // item.Name, item.ItemID, item.OwnerID, item.OwnerChanged);
  1165. engine.ResumeScript(item.ItemID);
  1166. if (item.OwnerChanged)
  1167. engine.PostScriptEvent(item.ItemID, "changed", new Object[] { (int)Changed.OWNER });
  1168. item.OwnerChanged = false;
  1169. }
  1170. }
  1171. }
  1172. }
  1173. }
  1174. }