SceneObjectGroup.Inventory.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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.IO;
  29. using System.Reflection;
  30. using OpenMetaverse;
  31. using log4net;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Framework.Interfaces;
  34. using System.Collections.Generic;
  35. using System.Xml;
  36. namespace OpenSim.Region.Framework.Scenes
  37. {
  38. public partial class SceneObjectGroup : EntityBase
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. /// <summary>
  42. /// Force all task inventories of prims in the scene object to persist
  43. /// </summary>
  44. public void ForceInventoryPersistence()
  45. {
  46. lock (m_parts)
  47. {
  48. foreach (SceneObjectPart part in m_parts.Values)
  49. {
  50. part.Inventory.ForceInventoryPersistence();
  51. }
  52. }
  53. }
  54. /// <summary>
  55. /// Start the scripts contained in all the prims in this group.
  56. /// </summary>
  57. public void CreateScriptInstances(int startParam, bool postOnRez,
  58. string engine, int stateSource)
  59. {
  60. // Don't start scripts if they're turned off in the region!
  61. if (!m_scene.RegionInfo.RegionSettings.DisableScripts)
  62. {
  63. foreach (SceneObjectPart part in m_parts.Values)
  64. {
  65. part.Inventory.CreateScriptInstances(startParam, postOnRez, engine, stateSource);
  66. }
  67. }
  68. }
  69. /// <summary>
  70. /// Stop the scripts contained in all the prims in this group
  71. /// </summary>
  72. public void RemoveScriptInstances()
  73. {
  74. lock (m_parts)
  75. {
  76. foreach (SceneObjectPart part in m_parts.Values)
  77. {
  78. part.Inventory.RemoveScriptInstances();
  79. }
  80. }
  81. }
  82. /// <summary>
  83. ///
  84. /// </summary>
  85. /// <param name="remoteClient"></param>
  86. /// <param name="localID"></param>
  87. public bool GetPartInventoryFileName(IClientAPI remoteClient, uint localID)
  88. {
  89. SceneObjectPart part = GetChildPart(localID);
  90. if (part != null)
  91. {
  92. return part.Inventory.GetInventoryFileName(remoteClient, localID);
  93. }
  94. else
  95. {
  96. m_log.ErrorFormat(
  97. "[PRIM INVENTORY]: " +
  98. "Couldn't find part {0} in object group {1}, {2} to retreive prim inventory",
  99. localID, Name, UUID);
  100. }
  101. return false;
  102. }
  103. /// <summary>
  104. /// Return serialized inventory metadata for the given constituent prim
  105. /// </summary>
  106. /// <param name="localID"></param>
  107. /// <param name="xferManager"></param>
  108. public void RequestInventoryFile(IClientAPI client, uint localID, IXfer xferManager)
  109. {
  110. SceneObjectPart part = GetChildPart(localID);
  111. if (part != null)
  112. {
  113. part.Inventory.RequestInventoryFile(client, xferManager);
  114. }
  115. else
  116. {
  117. m_log.ErrorFormat(
  118. "[PRIM INVENTORY]: " +
  119. "Couldn't find part {0} in object group {1}, {2} to request inventory data",
  120. localID, Name, UUID);
  121. }
  122. }
  123. /// <summary>
  124. /// Add an inventory item to a prim in this group.
  125. /// </summary>
  126. /// <param name="remoteClient"></param>
  127. /// <param name="localID"></param>
  128. /// <param name="item"></param>
  129. /// <param name="copyItemID">The item UUID that should be used by the new item.</param>
  130. /// <returns></returns>
  131. public bool AddInventoryItem(IClientAPI remoteClient, uint localID,
  132. InventoryItemBase item, UUID copyItemID)
  133. {
  134. UUID newItemId = (copyItemID != UUID.Zero) ? copyItemID : item.ID;
  135. SceneObjectPart part = GetChildPart(localID);
  136. if (part != null)
  137. {
  138. TaskInventoryItem taskItem = new TaskInventoryItem();
  139. taskItem.ItemID = newItemId;
  140. taskItem.AssetID = item.AssetID;
  141. taskItem.Name = item.Name;
  142. taskItem.Description = item.Description;
  143. taskItem.OwnerID = part.OwnerID; // Transfer ownership
  144. taskItem.CreatorID = item.CreatorIdAsUuid;
  145. taskItem.Type = item.AssetType;
  146. taskItem.InvType = item.InvType;
  147. if (remoteClient != null &&
  148. remoteClient.AgentId != part.OwnerID &&
  149. m_scene.Permissions.PropagatePermissions())
  150. {
  151. taskItem.BasePermissions = item.BasePermissions &
  152. item.NextPermissions;
  153. taskItem.CurrentPermissions = item.CurrentPermissions &
  154. item.NextPermissions;
  155. taskItem.EveryonePermissions = item.EveryOnePermissions &
  156. item.NextPermissions;
  157. taskItem.GroupPermissions = item.GroupPermissions &
  158. item.NextPermissions;
  159. taskItem.NextPermissions = item.NextPermissions;
  160. taskItem.CurrentPermissions |= 8;
  161. } else {
  162. taskItem.BasePermissions = item.BasePermissions;
  163. taskItem.CurrentPermissions = item.CurrentPermissions;
  164. taskItem.CurrentPermissions |= 8;
  165. taskItem.EveryonePermissions = item.EveryOnePermissions;
  166. taskItem.GroupPermissions = item.GroupPermissions;
  167. taskItem.NextPermissions = item.NextPermissions;
  168. }
  169. taskItem.Flags = item.Flags;
  170. // TODO: These are pending addition of those fields to TaskInventoryItem
  171. // taskItem.SalePrice = item.SalePrice;
  172. // taskItem.SaleType = item.SaleType;
  173. taskItem.CreationDate = (uint)item.CreationDate;
  174. bool addFromAllowedDrop = false;
  175. if (remoteClient!=null)
  176. {
  177. addFromAllowedDrop = remoteClient.AgentId != part.OwnerID;
  178. }
  179. part.Inventory.AddInventoryItem(taskItem, addFromAllowedDrop);
  180. return true;
  181. }
  182. else
  183. {
  184. m_log.ErrorFormat(
  185. "[PRIM INVENTORY]: " +
  186. "Couldn't find prim local ID {0} in group {1}, {2} to add inventory item ID {3}",
  187. localID, Name, UUID, newItemId);
  188. }
  189. return false;
  190. }
  191. /// <summary>
  192. /// Returns an existing inventory item. Returns the original, so any changes will be live.
  193. /// </summary>
  194. /// <param name="primID"></param>
  195. /// <param name="itemID"></param>
  196. /// <returns>null if the item does not exist</returns>
  197. public TaskInventoryItem GetInventoryItem(uint primID, UUID itemID)
  198. {
  199. SceneObjectPart part = GetChildPart(primID);
  200. if (part != null)
  201. {
  202. return part.Inventory.GetInventoryItem(itemID);
  203. }
  204. else
  205. {
  206. m_log.ErrorFormat(
  207. "[PRIM INVENTORY]: " +
  208. "Couldn't find prim local ID {0} in prim {1}, {2} to get inventory item ID {3}",
  209. primID, part.Name, part.UUID, itemID);
  210. }
  211. return null;
  212. }
  213. /// <summary>
  214. /// Update an existing inventory item.
  215. /// </summary>
  216. /// <param name="item">The updated item. An item with the same id must already exist
  217. /// in this prim's inventory</param>
  218. /// <returns>false if the item did not exist, true if the update occurred succesfully</returns>
  219. public bool UpdateInventoryItem(TaskInventoryItem item)
  220. {
  221. SceneObjectPart part = GetChildPart(item.ParentPartID);
  222. if (part != null)
  223. {
  224. part.Inventory.UpdateInventoryItem(item);
  225. return true;
  226. }
  227. else
  228. {
  229. m_log.ErrorFormat(
  230. "[PRIM INVENTORY]: " +
  231. "Couldn't find prim ID {0} to update item {1}, {2}",
  232. item.ParentPartID, item.Name, item.ItemID);
  233. }
  234. return false;
  235. }
  236. public int RemoveInventoryItem(uint localID, UUID itemID)
  237. {
  238. SceneObjectPart part = GetChildPart(localID);
  239. if (part != null)
  240. {
  241. int type = part.Inventory.RemoveInventoryItem(itemID);
  242. return type;
  243. }
  244. return -1;
  245. }
  246. public uint GetEffectivePermissions()
  247. {
  248. uint perms=(uint)(PermissionMask.Modify |
  249. PermissionMask.Copy |
  250. PermissionMask.Move |
  251. PermissionMask.Transfer) | 7;
  252. uint ownerMask = 0x7ffffff;
  253. foreach (SceneObjectPart part in m_parts.Values)
  254. {
  255. ownerMask &= part.OwnerMask;
  256. perms &= part.Inventory.MaskEffectivePermissions();
  257. }
  258. if ((ownerMask & (uint)PermissionMask.Modify) == 0)
  259. perms &= ~(uint)PermissionMask.Modify;
  260. if ((ownerMask & (uint)PermissionMask.Copy) == 0)
  261. perms &= ~(uint)PermissionMask.Copy;
  262. if ((ownerMask & (uint)PermissionMask.Transfer) == 0)
  263. perms &= ~(uint)PermissionMask.Transfer;
  264. if ((ownerMask & RootPart.NextOwnerMask & (uint)PermissionMask.Modify) == 0)
  265. perms &= ~((uint)PermissionMask.Modify >> 13);
  266. if ((ownerMask & RootPart.NextOwnerMask & (uint)PermissionMask.Copy) == 0)
  267. perms &= ~((uint)PermissionMask.Copy >> 13);
  268. if ((ownerMask & RootPart.NextOwnerMask & (uint)PermissionMask.Transfer) == 0)
  269. perms &= ~((uint)PermissionMask.Transfer >> 13);
  270. return perms;
  271. }
  272. public void ApplyNextOwnerPermissions()
  273. {
  274. foreach (SceneObjectPart part in m_parts.Values)
  275. {
  276. part.ApplyNextOwnerPermissions();
  277. }
  278. }
  279. public string GetStateSnapshot()
  280. {
  281. //m_log.Debug(" >>> GetStateSnapshot <<<");
  282. List<string> assemblies = new List<string>();
  283. Dictionary<UUID, string> states = new Dictionary<UUID, string>();
  284. foreach (SceneObjectPart part in m_parts.Values)
  285. {
  286. foreach (string a in part.Inventory.GetScriptAssemblies())
  287. {
  288. if (a != "" && !assemblies.Contains(a))
  289. assemblies.Add(a);
  290. }
  291. foreach (KeyValuePair<UUID, string> s in part.Inventory.GetScriptStates())
  292. {
  293. states[s.Key] = s.Value;
  294. }
  295. }
  296. if (states.Count < 1 || assemblies.Count < 1)
  297. return "";
  298. XmlDocument xmldoc = new XmlDocument();
  299. XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration,
  300. "", "");
  301. xmldoc.AppendChild(xmlnode);
  302. XmlElement rootElement = xmldoc.CreateElement("", "ScriptData",
  303. "");
  304. xmldoc.AppendChild(rootElement);
  305. XmlElement wrapper = xmldoc.CreateElement("", "Assemblies",
  306. "");
  307. rootElement.AppendChild(wrapper);
  308. foreach (string assembly in assemblies)
  309. {
  310. string fn = Path.GetFileName(assembly);
  311. if (fn == String.Empty)
  312. continue;
  313. String filedata = String.Empty;
  314. if (File.Exists(assembly+".text"))
  315. {
  316. FileInfo tfi = new FileInfo(assembly+".text");
  317. if (tfi == null)
  318. continue;
  319. Byte[] tdata = new Byte[tfi.Length];
  320. try
  321. {
  322. FileStream tfs = File.Open(assembly+".text", FileMode.Open, FileAccess.Read);
  323. tfs.Read(tdata, 0, tdata.Length);
  324. tfs.Close();
  325. }
  326. catch (Exception e)
  327. {
  328. m_log.DebugFormat("[SOG]: Unable to open script textfile {0}, reason: {1}", assembly+".text", e.Message);
  329. }
  330. filedata = new System.Text.ASCIIEncoding().GetString(tdata);
  331. }
  332. else
  333. {
  334. FileInfo fi = new FileInfo(assembly);
  335. if (fi == null)
  336. continue;
  337. Byte[] data = new Byte[fi.Length];
  338. try
  339. {
  340. FileStream fs = File.Open(assembly, FileMode.Open, FileAccess.Read);
  341. fs.Read(data, 0, data.Length);
  342. fs.Close();
  343. }
  344. catch (Exception e)
  345. {
  346. m_log.DebugFormat("[SOG]: Unable to open script assembly {0}, reason: {1}", assembly, e.Message);
  347. }
  348. filedata = System.Convert.ToBase64String(data);
  349. }
  350. XmlElement assemblyData = xmldoc.CreateElement("", "Assembly", "");
  351. XmlAttribute assemblyName = xmldoc.CreateAttribute("", "Filename", "");
  352. assemblyName.Value = fn;
  353. assemblyData.Attributes.Append(assemblyName);
  354. assemblyData.InnerText = filedata;
  355. wrapper.AppendChild(assemblyData);
  356. }
  357. wrapper = xmldoc.CreateElement("", "ScriptStates",
  358. "");
  359. rootElement.AppendChild(wrapper);
  360. foreach (KeyValuePair<UUID, string> state in states)
  361. {
  362. XmlElement stateData = xmldoc.CreateElement("", "State", "");
  363. XmlAttribute stateID = xmldoc.CreateAttribute("", "UUID", "");
  364. stateID.Value = state.Key.ToString();
  365. stateData.Attributes.Append(stateID);
  366. XmlDocument sdoc = new XmlDocument();
  367. sdoc.LoadXml(state.Value);
  368. XmlNodeList rootL = sdoc.GetElementsByTagName("ScriptState");
  369. XmlNode rootNode = rootL[0];
  370. XmlNode newNode = xmldoc.ImportNode(rootNode, true);
  371. stateData.AppendChild(newNode);
  372. wrapper.AppendChild(stateData);
  373. }
  374. return xmldoc.InnerXml;
  375. }
  376. public void SetState(string objXMLData, UUID RegionID)
  377. {
  378. if (objXMLData == String.Empty)
  379. return;
  380. XmlDocument doc = new XmlDocument();
  381. try
  382. {
  383. doc.LoadXml(objXMLData);
  384. }
  385. catch (Exception) // (System.Xml.XmlException)
  386. {
  387. // We will get here if the XML is invalid or in unit
  388. // tests. Really should determine which it is and either
  389. // fail silently or log it
  390. // Fail silently, for now.
  391. // TODO: Fix this
  392. //
  393. return;
  394. }
  395. XmlNodeList rootL = doc.GetElementsByTagName("ScriptData");
  396. if (rootL.Count == 1)
  397. {
  398. XmlNode rootNode = rootL[0];
  399. if (rootNode != null)
  400. {
  401. XmlNodeList partL = rootNode.ChildNodes;
  402. foreach (XmlNode part in partL)
  403. {
  404. XmlNodeList nodeL = part.ChildNodes;
  405. switch (part.Name)
  406. {
  407. case "Assemblies":
  408. foreach (XmlNode asm in nodeL)
  409. {
  410. string fn = asm.Attributes.GetNamedItem("Filename").Value;
  411. Byte[] filedata = Convert.FromBase64String(asm.InnerText);
  412. string path = Path.Combine("ScriptEngines", RegionID.ToString());
  413. path = Path.Combine(path, fn);
  414. if (!File.Exists(path))
  415. {
  416. FileStream fs = File.Create(path);
  417. fs.Write(filedata, 0, filedata.Length);
  418. fs.Close();
  419. Byte[] textbytes = new System.Text.ASCIIEncoding().GetBytes(asm.InnerText);
  420. fs = File.Create(path+".text");
  421. fs.Write(textbytes, 0, textbytes.Length);
  422. fs.Close();
  423. }
  424. }
  425. break;
  426. case "ScriptStates":
  427. foreach (XmlNode st in nodeL)
  428. {
  429. string id = st.Attributes.GetNamedItem("UUID").Value;
  430. UUID uuid = new UUID(id);
  431. XmlNode state = st.ChildNodes[0];
  432. XmlDocument sdoc = new XmlDocument();
  433. XmlNode sxmlnode = sdoc.CreateNode(
  434. XmlNodeType.XmlDeclaration,
  435. "", "");
  436. sdoc.AppendChild(sxmlnode);
  437. XmlNode newnode = sdoc.ImportNode(state, true);
  438. sdoc.AppendChild(newnode);
  439. string spath = Path.Combine("ScriptEngines", RegionID.ToString());
  440. spath = Path.Combine(spath, uuid.ToString());
  441. FileStream sfs = File.Create(spath + ".state");
  442. System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  443. Byte[] buf = enc.GetBytes(sdoc.InnerXml);
  444. sfs.Write(buf, 0, buf.Length);
  445. sfs.Close();
  446. }
  447. break;
  448. }
  449. }
  450. }
  451. }
  452. }
  453. }
  454. }