EntityManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using log4net;
  32. using OpenMetaverse;
  33. namespace OpenSim.Region.Framework.Scenes
  34. {
  35. public class EntityManager
  36. {
  37. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  38. private readonly DoubleDictionary<UUID, uint, EntityBase> m_entities = new DoubleDictionary<UUID, uint, EntityBase>();
  39. public int Count
  40. {
  41. get { return m_entities.Count; }
  42. }
  43. public void Add(EntityBase entity)
  44. {
  45. m_entities.Add(entity.UUID, entity.LocalId, entity);
  46. }
  47. public void Clear()
  48. {
  49. m_entities.Clear();
  50. }
  51. public bool ContainsKey(UUID id)
  52. {
  53. return m_entities.ContainsKey(id);
  54. }
  55. public bool ContainsKey(uint localID)
  56. {
  57. return m_entities.ContainsKey(localID);
  58. }
  59. public bool Remove(uint localID)
  60. {
  61. return m_entities.Remove(localID);
  62. }
  63. public bool Remove(UUID id)
  64. {
  65. return m_entities.Remove(id);
  66. }
  67. public EntityBase[] GetAllByType<T>()
  68. {
  69. List<EntityBase> tmp = new List<EntityBase>();
  70. m_entities.ForEach(
  71. delegate(EntityBase entity)
  72. {
  73. if (entity is T)
  74. tmp.Add(entity);
  75. }
  76. );
  77. return tmp.ToArray();
  78. }
  79. public EntityBase[] GetEntities()
  80. {
  81. List<EntityBase> tmp = new List<EntityBase>(m_entities.Count);
  82. m_entities.ForEach(delegate(EntityBase entity) { tmp.Add(entity); });
  83. return tmp.ToArray();
  84. }
  85. public void ForEach(Action<EntityBase> action)
  86. {
  87. m_entities.ForEach(action);
  88. }
  89. public EntityBase Find(Predicate<EntityBase> predicate)
  90. {
  91. return m_entities.FindValue(predicate);
  92. }
  93. public EntityBase this[UUID id]
  94. {
  95. get
  96. {
  97. EntityBase entity;
  98. m_entities.TryGetValue(id, out entity);
  99. return entity;
  100. }
  101. set
  102. {
  103. Add(value);
  104. }
  105. }
  106. public EntityBase this[uint localID]
  107. {
  108. get
  109. {
  110. EntityBase entity;
  111. m_entities.TryGetValue(localID, out entity);
  112. return entity;
  113. }
  114. set
  115. {
  116. Add(value);
  117. }
  118. }
  119. public bool TryGetValue(UUID key, out EntityBase obj)
  120. {
  121. return m_entities.TryGetValue(key, out obj);
  122. }
  123. public bool TryGetValue(uint key, out EntityBase obj)
  124. {
  125. return m_entities.TryGetValue(key, out obj);
  126. }
  127. }
  128. }