PrimCountModule.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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.Diagnostics;
  31. using System.Reflection;
  32. using log4net;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using Mono.Addins;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Services.Interfaces;
  40. namespace OpenSim.Region.CoreModules.World.Land
  41. {
  42. public class ParcelCounts
  43. {
  44. public int Owner = 0;
  45. public int Group = 0;
  46. public int Others = 0;
  47. public int Selected = 0;
  48. public Dictionary <UUID, int> Users = new Dictionary <UUID, int>();
  49. }
  50. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "PrimCountModule")]
  51. public class PrimCountModule : IPrimCountModule, INonSharedRegionModule
  52. {
  53. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  54. private Scene m_Scene;
  55. private Dictionary<UUID, PrimCounts> m_PrimCounts =
  56. new Dictionary<UUID, PrimCounts>();
  57. private Dictionary<UUID, UUID> m_OwnerMap =
  58. new Dictionary<UUID, UUID>();
  59. private Dictionary<UUID, int> m_SimwideCounts =
  60. new Dictionary<UUID, int>();
  61. private Dictionary<UUID, ParcelCounts> m_ParcelCounts =
  62. new Dictionary<UUID, ParcelCounts>();
  63. /// <value>
  64. /// For now, a simple simwide taint to get this up. Later parcel based
  65. /// taint to allow recounting a parcel if only ownership has changed
  66. /// without recounting the whole sim.
  67. ///
  68. /// We start out tainted so that the first get call resets the various prim counts.
  69. /// </value>
  70. private bool m_Tainted = true;
  71. private Object m_TaintLock = new Object();
  72. public Type ReplaceableInterface
  73. {
  74. get { return null; }
  75. }
  76. public void Initialise(IConfigSource source)
  77. {
  78. }
  79. public void AddRegion(Scene scene)
  80. {
  81. m_Scene = scene;
  82. m_Scene.RegisterModuleInterface<IPrimCountModule>(this);
  83. m_Scene.EventManager.OnObjectAddedToScene += OnParcelPrimCountAdd;
  84. m_Scene.EventManager.OnObjectBeingRemovedFromScene += OnObjectBeingRemovedFromScene;
  85. m_Scene.EventManager.OnParcelPrimCountTainted += OnParcelPrimCountTainted;
  86. m_Scene.EventManager.OnLandObjectAdded += delegate(ILandObject lo) { OnParcelPrimCountTainted(); };
  87. }
  88. public void RegionLoaded(Scene scene)
  89. {
  90. }
  91. public void RemoveRegion(Scene scene)
  92. {
  93. }
  94. public void Close()
  95. {
  96. }
  97. public string Name
  98. {
  99. get { return "PrimCountModule"; }
  100. }
  101. private void OnParcelPrimCountAdd(SceneObjectGroup obj)
  102. {
  103. // If we're tainted already, don't bother to add. The next
  104. // access will cause a recount anyway
  105. lock (m_TaintLock)
  106. {
  107. if (!m_Tainted)
  108. AddObject(obj);
  109. // else
  110. // m_log.DebugFormat(
  111. // "[PRIM COUNT MODULE]: Ignoring OnParcelPrimCountAdd() for {0} on {1} since count is tainted",
  112. // obj.Name, m_Scene.RegionInfo.RegionName);
  113. }
  114. }
  115. private void OnObjectBeingRemovedFromScene(SceneObjectGroup obj)
  116. {
  117. // Don't bother to update tainted counts
  118. lock (m_TaintLock)
  119. {
  120. if (!m_Tainted)
  121. RemoveObject(obj);
  122. // else
  123. // m_log.DebugFormat(
  124. // "[PRIM COUNT MODULE]: Ignoring OnObjectBeingRemovedFromScene() for {0} on {1} since count is tainted",
  125. // obj.Name, m_Scene.RegionInfo.RegionName);
  126. }
  127. }
  128. private void OnParcelPrimCountTainted()
  129. {
  130. // m_log.DebugFormat(
  131. // "[PRIM COUNT MODULE]: OnParcelPrimCountTainted() called on {0}", m_Scene.RegionInfo.RegionName);
  132. lock (m_TaintLock)
  133. m_Tainted = true;
  134. }
  135. public void TaintPrimCount(ILandObject land)
  136. {
  137. lock (m_TaintLock)
  138. m_Tainted = true;
  139. }
  140. public void TaintPrimCount(int x, int y)
  141. {
  142. lock (m_TaintLock)
  143. m_Tainted = true;
  144. }
  145. public void TaintPrimCount()
  146. {
  147. lock (m_TaintLock)
  148. m_Tainted = true;
  149. }
  150. // NOTE: Call under Taint Lock
  151. private void AddObject(SceneObjectGroup obj)
  152. {
  153. if (obj.IsAttachment)
  154. return;
  155. if (((obj.RootPart.Flags & PrimFlags.TemporaryOnRez) != 0))
  156. return;
  157. Vector3 pos = obj.AbsolutePosition;
  158. ILandObject landObject = m_Scene.LandChannel.GetLandObject(pos.X, pos.Y);
  159. // If for some reason there is no land object (perhaps the object is out of bounds) then we can't count it
  160. if (landObject == null)
  161. {
  162. // m_log.WarnFormat(
  163. // "[PRIM COUNT MODULE]: Found no land object for {0} at position ({1}, {2}) on {3}",
  164. // obj.Name, pos.X, pos.Y, m_Scene.RegionInfo.RegionName);
  165. return;
  166. }
  167. LandData landData = landObject.LandData;
  168. // m_log.DebugFormat(
  169. // "[PRIM COUNT MODULE]: Adding object {0} with {1} parts to prim count for parcel {2} on {3}",
  170. // obj.Name, obj.Parts.Length, landData.Name, m_Scene.RegionInfo.RegionName);
  171. // m_log.DebugFormat(
  172. // "[PRIM COUNT MODULE]: Object {0} is owned by {1} over land owned by {2}",
  173. // obj.Name, obj.OwnerID, landData.OwnerID);
  174. ParcelCounts parcelCounts;
  175. if (m_ParcelCounts.TryGetValue(landData.GlobalID, out parcelCounts))
  176. {
  177. UUID landOwner = landData.OwnerID;
  178. int partCount = obj.GetPartCount();
  179. m_SimwideCounts[landOwner] += partCount;
  180. if (parcelCounts.Users.ContainsKey(obj.OwnerID))
  181. parcelCounts.Users[obj.OwnerID] += partCount;
  182. else
  183. parcelCounts.Users[obj.OwnerID] = partCount;
  184. if (obj.IsSelected || obj.GetSittingAvatarsCount() > 0)
  185. parcelCounts.Selected += partCount;
  186. if (obj.OwnerID == landData.OwnerID)
  187. parcelCounts.Owner += partCount;
  188. else if (landData.GroupID != UUID.Zero && obj.GroupID == landData.GroupID)
  189. parcelCounts.Group += partCount;
  190. else
  191. parcelCounts.Others += partCount;
  192. }
  193. }
  194. // NOTE: Call under Taint Lock
  195. private void RemoveObject(SceneObjectGroup obj)
  196. {
  197. // m_log.DebugFormat("[PRIM COUNT MODULE]: Removing object {0} {1} from prim count", obj.Name, obj.UUID);
  198. // Currently this is being done by tainting the count instead.
  199. }
  200. public IPrimCounts GetPrimCounts(UUID parcelID)
  201. {
  202. // m_log.DebugFormat(
  203. // "[PRIM COUNT MODULE]: GetPrimCounts for parcel {0} in {1}", parcelID, m_Scene.RegionInfo.RegionName);
  204. PrimCounts primCounts;
  205. lock (m_PrimCounts)
  206. {
  207. if (m_PrimCounts.TryGetValue(parcelID, out primCounts))
  208. return primCounts;
  209. primCounts = new PrimCounts(parcelID, this);
  210. m_PrimCounts[parcelID] = primCounts;
  211. }
  212. return primCounts;
  213. }
  214. /// <summary>
  215. /// Get the number of prims on the parcel that are owned by the parcel owner.
  216. /// </summary>
  217. /// <param name="parcelID"></param>
  218. /// <returns></returns>
  219. public int GetOwnerCount(UUID parcelID)
  220. {
  221. int count = 0;
  222. lock (m_TaintLock)
  223. {
  224. if (m_Tainted)
  225. Recount();
  226. ParcelCounts counts;
  227. if (m_ParcelCounts.TryGetValue(parcelID, out counts))
  228. count = counts.Owner;
  229. }
  230. // m_log.DebugFormat(
  231. // "[PRIM COUNT MODULE]: GetOwnerCount for parcel {0} in {1} returning {2}",
  232. // parcelID, m_Scene.RegionInfo.RegionName, count);
  233. return count;
  234. }
  235. /// <summary>
  236. /// Get the number of prims on the parcel that have been set to the group that owns the parcel.
  237. /// </summary>
  238. /// <param name="parcelID"></param>
  239. /// <returns></returns>
  240. public int GetGroupCount(UUID parcelID)
  241. {
  242. int count = 0;
  243. lock (m_TaintLock)
  244. {
  245. if (m_Tainted)
  246. Recount();
  247. ParcelCounts counts;
  248. if (m_ParcelCounts.TryGetValue(parcelID, out counts))
  249. count = counts.Group;
  250. }
  251. // m_log.DebugFormat(
  252. // "[PRIM COUNT MODULE]: GetGroupCount for parcel {0} in {1} returning {2}",
  253. // parcelID, m_Scene.RegionInfo.RegionName, count);
  254. return count;
  255. }
  256. /// <summary>
  257. /// Get the number of prims on the parcel that are not owned by the parcel owner or set to the parcel group.
  258. /// </summary>
  259. /// <param name="parcelID"></param>
  260. /// <returns></returns>
  261. public int GetOthersCount(UUID parcelID)
  262. {
  263. int count = 0;
  264. lock (m_TaintLock)
  265. {
  266. if (m_Tainted)
  267. Recount();
  268. ParcelCounts counts;
  269. if (m_ParcelCounts.TryGetValue(parcelID, out counts))
  270. count = counts.Others;
  271. }
  272. // m_log.DebugFormat(
  273. // "[PRIM COUNT MODULE]: GetOthersCount for parcel {0} in {1} returning {2}",
  274. // parcelID, m_Scene.RegionInfo.RegionName, count);
  275. return count;
  276. }
  277. /// <summary>
  278. /// Get the number of selected prims.
  279. /// </summary>
  280. /// <param name="parcelID"></param>
  281. /// <returns></returns>
  282. public int GetSelectedCount(UUID parcelID)
  283. {
  284. int count = 0;
  285. lock (m_TaintLock)
  286. {
  287. if (m_Tainted)
  288. Recount();
  289. ParcelCounts counts;
  290. if (m_ParcelCounts.TryGetValue(parcelID, out counts))
  291. count = counts.Selected;
  292. }
  293. // m_log.DebugFormat(
  294. // "[PRIM COUNT MODULE]: GetSelectedCount for parcel {0} in {1} returning {2}",
  295. // parcelID, m_Scene.RegionInfo.RegionName, count);
  296. return count;
  297. }
  298. /// <summary>
  299. /// Get the total count of owner, group and others prims on the parcel.
  300. /// FIXME: Need to do selected prims once this is reimplemented.
  301. /// </summary>
  302. /// <param name="parcelID"></param>
  303. /// <returns></returns>
  304. public int GetTotalCount(UUID parcelID)
  305. {
  306. int count = 0;
  307. lock (m_TaintLock)
  308. {
  309. if (m_Tainted)
  310. Recount();
  311. ParcelCounts counts;
  312. if (m_ParcelCounts.TryGetValue(parcelID, out counts))
  313. {
  314. count = counts.Owner;
  315. count += counts.Group;
  316. count += counts.Others;
  317. }
  318. }
  319. // m_log.DebugFormat(
  320. // "[PRIM COUNT MODULE]: GetTotalCount for parcel {0} in {1} returning {2}",
  321. // parcelID, m_Scene.RegionInfo.RegionName, count);
  322. return count;
  323. }
  324. /// <summary>
  325. /// Get the number of prims that are in the entire simulator for the owner of this parcel.
  326. /// </summary>
  327. /// <param name="parcelID"></param>
  328. /// <returns></returns>
  329. public int GetSimulatorCount(UUID parcelID)
  330. {
  331. int count = 0;
  332. lock (m_TaintLock)
  333. {
  334. if (m_Tainted)
  335. Recount();
  336. UUID owner;
  337. if (m_OwnerMap.TryGetValue(parcelID, out owner))
  338. {
  339. int val;
  340. if (m_SimwideCounts.TryGetValue(owner, out val))
  341. count = val;
  342. }
  343. }
  344. // m_log.DebugFormat(
  345. // "[PRIM COUNT MODULE]: GetOthersCount for parcel {0} in {1} returning {2}",
  346. // parcelID, m_Scene.RegionInfo.RegionName, count);
  347. return count;
  348. }
  349. /// <summary>
  350. /// Get the number of prims that a particular user owns on this parcel.
  351. /// </summary>
  352. /// <param name="parcelID"></param>
  353. /// <param name="userID"></param>
  354. /// <returns></returns>
  355. public int GetUserCount(UUID parcelID, UUID userID)
  356. {
  357. int count = 0;
  358. lock (m_TaintLock)
  359. {
  360. if (m_Tainted)
  361. Recount();
  362. ParcelCounts counts;
  363. if (m_ParcelCounts.TryGetValue(parcelID, out counts))
  364. {
  365. int val;
  366. if (counts.Users.TryGetValue(userID, out val))
  367. count = val;
  368. }
  369. }
  370. // m_log.DebugFormat(
  371. // "[PRIM COUNT MODULE]: GetUserCount for user {0} in parcel {1} in region {2} returning {3}",
  372. // userID, parcelID, m_Scene.RegionInfo.RegionName, count);
  373. return count;
  374. }
  375. // NOTE: This method MUST be called while holding the taint lock!
  376. private void Recount()
  377. {
  378. // m_log.DebugFormat("[PRIM COUNT MODULE]: Recounting prims on {0}", m_Scene.RegionInfo.RegionName);
  379. m_OwnerMap.Clear();
  380. m_SimwideCounts.Clear();
  381. m_ParcelCounts.Clear();
  382. List<ILandObject> land = m_Scene.LandChannel.AllParcels();
  383. foreach (ILandObject l in land)
  384. {
  385. LandData landData = l.LandData;
  386. m_OwnerMap[landData.GlobalID] = landData.OwnerID;
  387. m_SimwideCounts[landData.OwnerID] = 0;
  388. // m_log.DebugFormat(
  389. // "[PRIM COUNT MODULE]: Initializing parcel count for {0} on {1}",
  390. // landData.Name, m_Scene.RegionInfo.RegionName);
  391. m_ParcelCounts[landData.GlobalID] = new ParcelCounts();
  392. }
  393. m_Scene.ForEachSOG(AddObject);
  394. lock (m_PrimCounts)
  395. {
  396. List<UUID> primcountKeys = new List<UUID>(m_PrimCounts.Keys);
  397. foreach (UUID k in primcountKeys)
  398. {
  399. if (!m_OwnerMap.ContainsKey(k))
  400. m_PrimCounts.Remove(k);
  401. }
  402. }
  403. m_Tainted = false;
  404. }
  405. }
  406. public class PrimCounts : IPrimCounts
  407. {
  408. private PrimCountModule m_Parent;
  409. private UUID m_ParcelID;
  410. private UserPrimCounts m_UserPrimCounts;
  411. public PrimCounts (UUID parcelID, PrimCountModule parent)
  412. {
  413. m_ParcelID = parcelID;
  414. m_Parent = parent;
  415. m_UserPrimCounts = new UserPrimCounts(this);
  416. }
  417. public int Owner
  418. {
  419. get
  420. {
  421. return m_Parent.GetOwnerCount(m_ParcelID);
  422. }
  423. }
  424. public int Group
  425. {
  426. get
  427. {
  428. return m_Parent.GetGroupCount(m_ParcelID);
  429. }
  430. }
  431. public int Others
  432. {
  433. get
  434. {
  435. return m_Parent.GetOthersCount(m_ParcelID);
  436. }
  437. }
  438. public int Selected
  439. {
  440. get
  441. {
  442. return m_Parent.GetSelectedCount(m_ParcelID);
  443. }
  444. }
  445. public int Total
  446. {
  447. get
  448. {
  449. return m_Parent.GetTotalCount(m_ParcelID);
  450. }
  451. }
  452. public int Simulator
  453. {
  454. get
  455. {
  456. return m_Parent.GetSimulatorCount(m_ParcelID);
  457. }
  458. }
  459. public IUserPrimCounts Users
  460. {
  461. get
  462. {
  463. return m_UserPrimCounts;
  464. }
  465. }
  466. public int GetUserCount(UUID userID)
  467. {
  468. return m_Parent.GetUserCount(m_ParcelID, userID);
  469. }
  470. }
  471. public class UserPrimCounts : IUserPrimCounts
  472. {
  473. private PrimCounts m_Parent;
  474. public UserPrimCounts(PrimCounts parent)
  475. {
  476. m_Parent = parent;
  477. }
  478. public int this[UUID userID]
  479. {
  480. get
  481. {
  482. return m_Parent.GetUserCount(userID);
  483. }
  484. }
  485. }
  486. }