using System; using System.Collections; using System.Collections.Generic; using OpenMetaverse; namespace OpenSim.Region.Environment.Scenes { public class EntityManager : IEnumerable { 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; } 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); } } /// /// This could be optimised to work on the list 'live' rather than making a safe copy and iterating that. /// /// public IEnumerator GetEnumerator() { return GetEntities().GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } }