opensim – Blame information for rev 1

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