diff --git a/OpenSim/Region/Environment/Scenes/EntityManager.cs b/OpenSim/Region/Environment/Scenes/EntityManager.cs new file mode 100644 index 0000000000..f515cba039 --- /dev/null +++ b/OpenSim/Region/Environment/Scenes/EntityManager.cs @@ -0,0 +1,148 @@ +using System; +using System.Collections.Generic; +using OpenMetaverse; + +namespace OpenSim.Region.Environment.Scenes +{ + public class EntityManager + { + private readonly Dictionary m_eb_uuid = new Dictionary(); + private readonly Dictionary m_eb_localID = new Dictionary(); + private readonly Object m_lock = new Object(); + + [Obsolete("Use Add() instead.")] + public void Add(UUID id, EntityBase eb) + { + Add(eb); + } + + public void Add(EntityBase entity) + { + lock(m_lock) + { + m_eb_uuid.Add(entity.UUID, entity); + m_eb_localID.Add(entity.LocalId, entity); + } + } + + public void InsertOrReplace(EntityBase entity) + { + lock(m_lock) + { + m_eb_uuid[entity.UUID] = entity; + m_eb_localID[entity.LocalId] = entity; + } + } + + public void Clear() + { + lock (m_lock) + { + m_eb_uuid.Clear(); + m_eb_localID.Clear(); + } + } + + public int Count + { + get + { + lock (m_lock) + { + return m_eb_uuid.Count; + } + } + } + + public bool ContainsKey(UUID id) + { + lock(m_lock) + { + return m_eb_uuid.ContainsKey(id); + } + } + + public bool ContainsKey(uint localID) + { + lock (m_lock) + { + return m_eb_localID.ContainsKey(localID); + } + } + + public void Remove(uint localID) + { + lock(m_lock) + { + m_eb_uuid.Remove(m_eb_localID[localID].UUID); + m_eb_localID.Remove(localID); + } + } + + public void Remove(UUID id) + { + lock(m_lock) + { + m_eb_localID.Remove(m_eb_uuid[id].LocalId); + m_eb_uuid.Remove(id); + } + } + + public List GetAllByType() + { + List tmp = new List(); + + lock(m_lock) + { + foreach (KeyValuePair pair in m_eb_uuid) + { + if(pair.Value is T) + { + tmp.Add(pair.Value); + } + } + } + + return tmp; + } + + [Obsolete("Please used indexed access to this instead. Indexes can be accessed via EntitiesManager[index]. LocalID and UUID are supported.")] + public List GetEntities() + { + lock (m_lock) + { + return new List(m_eb_uuid.Values); + } + } + + public EntityBase this[UUID id] + { + get + { + lock (m_lock) + { + return m_eb_uuid[id]; + } + } + set + { + InsertOrReplace(value); + } + } + + public EntityBase this[uint localID] + { + get + { + lock (m_lock) + { + return m_eb_localID[localID]; + } + } + set + { + InsertOrReplace(value); + } + } + } +}