EntityManager.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.Environment.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. lock (m_lock)
  90. {
  91. return m_eb_uuid.Count;
  92. }
  93. }
  94. }
  95. public bool ContainsKey(UUID id)
  96. {
  97. lock (m_lock)
  98. {
  99. try
  100. {
  101. return m_eb_uuid.ContainsKey(id);
  102. }
  103. catch
  104. {
  105. return false;
  106. }
  107. }
  108. }
  109. public bool ContainsKey(uint localID)
  110. {
  111. lock (m_lock)
  112. {
  113. try
  114. {
  115. return m_eb_localID.ContainsKey(localID);
  116. }
  117. catch
  118. {
  119. return false;
  120. }
  121. }
  122. }
  123. public bool Remove(uint localID)
  124. {
  125. lock (m_lock)
  126. {
  127. try
  128. {
  129. bool a = m_eb_uuid.Remove(m_eb_localID[localID].UUID);
  130. bool b = m_eb_localID.Remove(localID);
  131. return a && b;
  132. }
  133. catch (Exception e)
  134. {
  135. m_log.ErrorFormat("Remove Entity failed for {0}", localID, e);
  136. return false;
  137. }
  138. }
  139. }
  140. public bool Remove(UUID id)
  141. {
  142. lock (m_lock)
  143. {
  144. try
  145. {
  146. bool a = m_eb_localID.Remove(m_eb_uuid[id].LocalId);
  147. bool b = m_eb_uuid.Remove(id);
  148. return a && b;
  149. }
  150. catch (Exception e)
  151. {
  152. m_log.ErrorFormat("Remove Entity failed for {0}", id, e);
  153. return false;
  154. }
  155. }
  156. }
  157. public List<EntityBase> GetAllByType<T>()
  158. {
  159. List<EntityBase> tmp = new List<EntityBase>();
  160. lock (m_lock)
  161. {
  162. try
  163. {
  164. foreach (KeyValuePair<UUID, EntityBase> pair in m_eb_uuid)
  165. {
  166. if (pair.Value is T)
  167. {
  168. tmp.Add(pair.Value);
  169. }
  170. }
  171. }
  172. catch (Exception e)
  173. {
  174. m_log.ErrorFormat("GetAllByType failed for {0}", e);
  175. tmp = null;
  176. }
  177. }
  178. return tmp;
  179. }
  180. public List<EntityBase> GetEntities()
  181. {
  182. lock (m_lock)
  183. {
  184. return new List<EntityBase>(m_eb_uuid.Values);
  185. }
  186. }
  187. public EntityBase this[UUID id]
  188. {
  189. get
  190. {
  191. lock (m_lock)
  192. {
  193. try
  194. {
  195. return m_eb_uuid[id];
  196. }
  197. catch
  198. {
  199. return null;
  200. }
  201. }
  202. }
  203. set
  204. {
  205. InsertOrReplace(value);
  206. }
  207. }
  208. public EntityBase this[uint localID]
  209. {
  210. get
  211. {
  212. lock (m_lock)
  213. {
  214. try
  215. {
  216. return m_eb_localID[localID];
  217. }
  218. catch
  219. {
  220. return null;
  221. }
  222. }
  223. }
  224. set
  225. {
  226. InsertOrReplace(value);
  227. }
  228. }
  229. public bool TryGetValue(UUID key, out EntityBase obj)
  230. {
  231. lock (m_lock)
  232. {
  233. return m_eb_uuid.TryGetValue(key, out obj);
  234. }
  235. }
  236. public bool TryGetValue(uint key, out EntityBase obj)
  237. {
  238. lock (m_lock)
  239. {
  240. return m_eb_localID.TryGetValue(key, out obj);
  241. }
  242. }
  243. /// <summary>
  244. /// This could be optimised to work on the list 'live' rather than making a safe copy and iterating that.
  245. /// </summary>
  246. /// <returns></returns>
  247. public IEnumerator<EntityBase> GetEnumerator()
  248. {
  249. return GetEntities().GetEnumerator();
  250. }
  251. IEnumerator IEnumerable.GetEnumerator()
  252. {
  253. return GetEnumerator();
  254. }
  255. }
  256. }