aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Framework/Scenes/EntityManager.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Framework/Scenes/EntityManager.cs')
-rw-r--r--OpenSim/Region/Framework/Scenes/EntityManager.cs279
1 files changed, 279 insertions, 0 deletions
diff --git a/OpenSim/Region/Framework/Scenes/EntityManager.cs b/OpenSim/Region/Framework/Scenes/EntityManager.cs
new file mode 100644
index 0000000..a322766
--- /dev/null
+++ b/OpenSim/Region/Framework/Scenes/EntityManager.cs
@@ -0,0 +1,279 @@
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
28using System;
29using System.Collections;
30using System.Collections.Generic;
31using System.Reflection;
32using log4net;
33using OpenMetaverse;
34
35
36namespace OpenSim.Region.Framework.Scenes
37{
38 public class EntityManager : IEnumerable<EntityBase>
39 {
40 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
41 private readonly Dictionary<UUID,EntityBase> m_eb_uuid = new Dictionary<UUID, EntityBase>();
42 private readonly Dictionary<uint, EntityBase> m_eb_localID = new Dictionary<uint, EntityBase>();
43 //private readonly Dictionary<UUID, ScenePresence> m_pres_uuid = new Dictionary<UUID, ScenePresence>();
44 private readonly Object m_lock = new Object();
45
46 [Obsolete("Use Add() instead.")]
47 public void Add(UUID id, EntityBase eb)
48 {
49 Add(eb);
50 }
51
52 public void Add(EntityBase entity)
53 {
54 lock (m_lock)
55 {
56 try
57 {
58 m_eb_uuid.Add(entity.UUID, entity);
59 m_eb_localID.Add(entity.LocalId, entity);
60 }
61 catch(Exception e)
62 {
63 m_log.ErrorFormat("Add Entity failed: {0}", e.Message);
64 }
65 }
66 }
67
68 public void InsertOrReplace(EntityBase entity)
69 {
70 lock (m_lock)
71 {
72 try
73 {
74 m_eb_uuid[entity.UUID] = entity;
75 m_eb_localID[entity.LocalId] = entity;
76 }
77 catch(Exception e)
78 {
79 m_log.ErrorFormat("Insert or Replace Entity failed: {0}", e.Message);
80 }
81 }
82 }
83
84 public void Clear()
85 {
86 lock (m_lock)
87 {
88 m_eb_uuid.Clear();
89 m_eb_localID.Clear();
90 }
91 }
92
93 public int Count
94 {
95 get
96 {
97 lock (m_lock)
98 {
99 return m_eb_uuid.Count;
100 }
101 }
102 }
103
104 public bool ContainsKey(UUID id)
105 {
106 lock (m_lock)
107 {
108 try
109 {
110 return m_eb_uuid.ContainsKey(id);
111 }
112 catch
113 {
114 return false;
115 }
116 }
117 }
118
119 public bool ContainsKey(uint localID)
120 {
121 lock (m_lock)
122 {
123 try
124 {
125 return m_eb_localID.ContainsKey(localID);
126 }
127 catch
128 {
129 return false;
130 }
131 }
132 }
133
134 public bool Remove(uint localID)
135 {
136 lock (m_lock)
137 {
138 try
139 {
140 bool a = m_eb_uuid.Remove(m_eb_localID[localID].UUID);
141 bool b = m_eb_localID.Remove(localID);
142 return a && b;
143 }
144 catch (Exception e)
145 {
146 m_log.ErrorFormat("Remove Entity failed for {0}", localID, e);
147 return false;
148 }
149 }
150 }
151
152 public bool Remove(UUID id)
153 {
154 lock (m_lock)
155 {
156 try
157 {
158 bool a = m_eb_localID.Remove(m_eb_uuid[id].LocalId);
159 bool b = m_eb_uuid.Remove(id);
160 return a && b;
161 }
162 catch (Exception e)
163 {
164 m_log.ErrorFormat("Remove Entity failed for {0}", id, e);
165 return false;
166 }
167 }
168 }
169
170 public List<EntityBase> GetAllByType<T>()
171 {
172 List<EntityBase> tmp = new List<EntityBase>();
173
174 lock (m_lock)
175 {
176 try
177 {
178 foreach (KeyValuePair<UUID, EntityBase> pair in m_eb_uuid)
179 {
180 if (pair.Value is T)
181 {
182 tmp.Add(pair.Value);
183 }
184 }
185 }
186 catch (Exception e)
187 {
188 m_log.ErrorFormat("GetAllByType failed for {0}", e);
189 tmp = null;
190 }
191 }
192
193 return tmp;
194 }
195
196 public List<EntityBase> GetEntities()
197 {
198 lock (m_lock)
199 {
200 return new List<EntityBase>(m_eb_uuid.Values);
201 }
202 }
203
204 public EntityBase this[UUID id]
205 {
206 get
207 {
208 lock (m_lock)
209 {
210 try
211 {
212 return m_eb_uuid[id];
213 }
214 catch
215 {
216 return null;
217 }
218 }
219 }
220 set
221 {
222 InsertOrReplace(value);
223 }
224 }
225
226 public EntityBase this[uint localID]
227 {
228 get
229 {
230 lock (m_lock)
231 {
232 try
233 {
234 return m_eb_localID[localID];
235 }
236 catch
237 {
238 return null;
239 }
240 }
241 }
242 set
243 {
244 InsertOrReplace(value);
245 }
246 }
247
248 public bool TryGetValue(UUID key, out EntityBase obj)
249 {
250 lock (m_lock)
251 {
252 return m_eb_uuid.TryGetValue(key, out obj);
253 }
254 }
255
256 public bool TryGetValue(uint key, out EntityBase obj)
257 {
258 lock (m_lock)
259 {
260 return m_eb_localID.TryGetValue(key, out obj);
261 }
262 }
263
264 /// <summary>
265 /// This could be optimised to work on the list 'live' rather than making a safe copy and iterating that.
266 /// </summary>
267 /// <returns></returns>
268 public IEnumerator<EntityBase> GetEnumerator()
269 {
270 return GetEntities().GetEnumerator();
271 }
272
273 IEnumerator IEnumerable.GetEnumerator()
274 {
275 return GetEnumerator();
276 }
277
278 }
279}