SOPObjectInventory.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.Collections;
  29. using System.Collections.Generic;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Framework.Scenes;
  32. using OpenMetaverse;
  33. namespace OpenSim.Region.OptionalModules.Scripting.Minimodule.Object
  34. {
  35. public class SOPObjectInventory : IObjectInventory
  36. {
  37. TaskInventoryDictionary m_privateInventory; /// OpenSim's task inventory
  38. Dictionary<UUID, IInventoryItem> m_publicInventory; /// MRM's inventory
  39. Scene m_rootScene;
  40. public SOPObjectInventory(Scene rootScene, TaskInventoryDictionary taskInventory)
  41. {
  42. m_rootScene = rootScene;
  43. m_privateInventory = taskInventory;
  44. m_publicInventory = new Dictionary<UUID, IInventoryItem>();
  45. }
  46. /// <summary>
  47. /// Fully populate the public dictionary with the contents of the private dictionary
  48. /// </summary>
  49. /// <description>
  50. /// This will only convert those items which hasn't already been converted. ensuring that
  51. /// no items are converted twice, and that any references already in use are maintained.
  52. /// </description>
  53. private void SynchronizeDictionaries()
  54. {
  55. foreach (TaskInventoryItem privateItem in m_privateInventory.Values)
  56. if (!m_publicInventory.ContainsKey(privateItem.ItemID))
  57. m_publicInventory.Add(privateItem.ItemID, new InventoryItem(m_rootScene, privateItem));
  58. }
  59. #region IDictionary<UUID, IInventoryItem> implementation
  60. public void Add (UUID key, IInventoryItem value)
  61. {
  62. m_publicInventory.Add(key, value);
  63. m_privateInventory.Add(key, InventoryItem.FromInterface(value).ToTaskInventoryItem());
  64. }
  65. public bool ContainsKey (UUID key)
  66. {
  67. return m_privateInventory.ContainsKey(key);
  68. }
  69. public bool Remove (UUID key)
  70. {
  71. m_publicInventory.Remove(key);
  72. return m_privateInventory.Remove(key);
  73. }
  74. public bool TryGetValue (UUID key, out IInventoryItem value)
  75. {
  76. value = null;
  77. bool result = false;
  78. if (!m_publicInventory.TryGetValue(key, out value))
  79. {
  80. // wasn't found in the public inventory
  81. TaskInventoryItem privateItem;
  82. result = m_privateInventory.TryGetValue(key, out privateItem);
  83. if (result)
  84. {
  85. value = new InventoryItem(m_rootScene, privateItem);
  86. m_publicInventory.Add(key, value); // add item, so we don't convert again
  87. }
  88. } else
  89. return true;
  90. return result;
  91. }
  92. public ICollection<UUID> Keys {
  93. get {
  94. return m_privateInventory.Keys;
  95. }
  96. }
  97. public ICollection<IInventoryItem> Values {
  98. get {
  99. SynchronizeDictionaries();
  100. return m_publicInventory.Values;
  101. }
  102. }
  103. #endregion
  104. #region IEnumerable<KeyValuePair<UUID, IInventoryItem>> implementation
  105. public IEnumerator<KeyValuePair<UUID, IInventoryItem>> GetEnumerator ()
  106. {
  107. SynchronizeDictionaries();
  108. return m_publicInventory.GetEnumerator();
  109. }
  110. #endregion
  111. #region IEnumerable implementation
  112. IEnumerator IEnumerable.GetEnumerator ()
  113. {
  114. SynchronizeDictionaries();
  115. return m_publicInventory.GetEnumerator();
  116. }
  117. #endregion
  118. #region ICollection<KeyValuePair<UUID, IInventoryItem>> implementation
  119. public void Add (KeyValuePair<UUID, IInventoryItem> item)
  120. {
  121. Add(item.Key, item.Value);
  122. }
  123. public void Clear ()
  124. {
  125. m_publicInventory.Clear();
  126. m_privateInventory.Clear();
  127. }
  128. public bool Contains (KeyValuePair<UUID, IInventoryItem> item)
  129. {
  130. return m_privateInventory.ContainsKey(item.Key);
  131. }
  132. public void CopyTo (KeyValuePair<UUID, IInventoryItem>[] array, int arrayIndex)
  133. {
  134. throw new NotImplementedException();
  135. }
  136. public bool Remove (KeyValuePair<UUID, IInventoryItem> item)
  137. {
  138. return Remove(item.Key);
  139. }
  140. public int Count {
  141. get {
  142. return m_privateInventory.Count;
  143. }
  144. }
  145. public bool IsReadOnly {
  146. get {
  147. return false;
  148. }
  149. }
  150. #endregion
  151. #region Explicit implementations
  152. IInventoryItem System.Collections.Generic.IDictionary<UUID, IInventoryItem>.this[UUID key]
  153. {
  154. get {
  155. IInventoryItem result;
  156. if (TryGetValue(key, out result))
  157. return result;
  158. else
  159. throw new KeyNotFoundException("[MRM] The requrested item ID could not be found");
  160. }
  161. set {
  162. m_publicInventory[key] = value;
  163. m_privateInventory[key] = InventoryItem.FromInterface(value).ToTaskInventoryItem();
  164. }
  165. }
  166. void System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<UUID, IInventoryItem>>.CopyTo(System.Collections.Generic.KeyValuePair<UUID,IInventoryItem>[] array, int offset)
  167. {
  168. throw new NotImplementedException();
  169. }
  170. #endregion
  171. public IInventoryItem this[string name]
  172. {
  173. get {
  174. foreach (TaskInventoryItem i in m_privateInventory.Values)
  175. if (i.Name == name)
  176. {
  177. if (!m_publicInventory.ContainsKey(i.ItemID))
  178. m_publicInventory.Add(i.ItemID, new InventoryItem(m_rootScene, i));
  179. return m_publicInventory[i.ItemID];
  180. }
  181. throw new KeyNotFoundException();
  182. }
  183. }
  184. }
  185. }