InventoryItem.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 OpenSim.Framework;
  30. using OpenSim.Region.Framework.Scenes;
  31. //using OpenSim.Services.AssetService;
  32. using OpenMetaverse;
  33. using OpenMetaverse.Assets;
  34. namespace OpenSim.Region.OptionalModules.Scripting.Minimodule
  35. {
  36. public class InventoryItem : IInventoryItem
  37. {
  38. TaskInventoryItem m_privateItem;
  39. Scene m_rootSceene;
  40. public InventoryItem(Scene rootScene, TaskInventoryItem internalItem)
  41. {
  42. m_rootSceene = rootScene;
  43. m_privateItem = internalItem;
  44. }
  45. // Marked internal, to prevent scripts from accessing the internal type
  46. internal TaskInventoryItem ToTaskInventoryItem()
  47. {
  48. return m_privateItem;
  49. }
  50. /// <summary>
  51. /// This will attempt to convert from an IInventoryItem to an InventoryItem object
  52. /// </summary>
  53. /// <description>
  54. /// In order for this to work the object which implements IInventoryItem must inherit from InventoryItem, otherwise
  55. /// an exception is thrown.
  56. /// </description>
  57. /// <param name="i">
  58. /// The interface to upcast <see cref="IInventoryItem"/>
  59. /// </param>
  60. /// <returns>
  61. /// The object backing the interface implementation <see cref="InventoryItem"/>
  62. /// </returns>
  63. internal static InventoryItem FromInterface(IInventoryItem i)
  64. {
  65. if (typeof(InventoryItem).IsAssignableFrom(i.GetType()))
  66. {
  67. return (InventoryItem)i;
  68. }
  69. else
  70. {
  71. throw new ApplicationException("[MRM] There is no legal conversion from IInventoryItem to InventoryItem");
  72. }
  73. }
  74. public int Type { get { return m_privateItem.Type; } }
  75. public UUID AssetID { get { return m_privateItem.AssetID; } }
  76. // This method exposes OpenSim/OpenMetaverse internals and needs to be replaced with a IAsset specific to MRM.
  77. public T RetreiveAsset<T>() where T : OpenMetaverse.Assets.Asset, new()
  78. {
  79. AssetBase a = m_rootSceene.AssetService.Get(AssetID.ToString());
  80. T result = new T();
  81. if ((sbyte)result.AssetType != a.Type)
  82. throw new ApplicationException("[MRM] The supplied asset class does not match the found asset");
  83. result.AssetData = a.Data;
  84. result.Decode();
  85. return result;
  86. }
  87. }
  88. }