SceneObjectPartInventory.cs 59 KB

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