EntityManager.cs 7.9 KB

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