EntityManager.cs 4.5 KB

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