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