PrimCountModule.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 OpenSim.Region.Framework.Interfaces;
  37. using OpenSim.Region.Framework.Scenes;
  38. using OpenSim.Services.Interfaces;
  39. namespace OpenSim.Region.CoreModules.World.Land
  40. {
  41. public class ParcelCounts
  42. {
  43. public int Owner = 0;
  44. public int Group = 0;
  45. public int Others = 0;
  46. public int Selected = 0;
  47. public Dictionary <UUID, int> Users = new Dictionary <UUID, int>();
  48. }
  49. public class PrimCountModule : IPrimCountModule, INonSharedRegionModule
  50. {
  51. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  52. private Scene m_Scene;
  53. private Dictionary<UUID, PrimCounts> m_PrimCounts =
  54. new Dictionary<UUID, PrimCounts>();
  55. private Dictionary<UUID, UUID> m_OwnerMap =
  56. new Dictionary<UUID, UUID>();
  57. private Dictionary<UUID, int> m_SimwideCounts =
  58. new Dictionary<UUID, int>();
  59. private Dictionary<UUID, ParcelCounts> m_ParcelCounts =
  60. new Dictionary<UUID, ParcelCounts>();
  61. /// <value>
  62. /// For now, a simple simwide taint to get this up. Later parcel based
  63. /// taint to allow recounting a parcel if only ownership has changed
  64. /// without recounting the whole sim.
  65. ///
  66. /// We start out tainted so that the first get call resets the various prim counts.
  67. /// <value>
  68. private bool m_Tainted = true;
  69. private Object m_TaintLock = new Object();
  70. public Type ReplaceableInterface
  71. {
  72. get { return null; }
  73. }
  74. public void Initialise(IConfigSource source)
  75. {
  76. }
  77. public void AddRegion(Scene scene)
  78. {
  79. m_Scene = scene;
  80. m_Scene.RegisterModuleInterface<IPrimCountModule>(this);
  81. m_Scene.EventManager.OnObjectAddedToScene += OnParcelPrimCountAdd;
  82. m_Scene.EventManager.OnObjectBeingRemovedFromScene +=
  83. OnObjectBeingRemovedFromScene;
  84. m_Scene.EventManager.OnParcelPrimCountTainted +=
  85. 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.Parts.Length;
  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)
  185. {
  186. parcelCounts.Selected += partCount;
  187. }
  188. else
  189. {
  190. if (landData.IsGroupOwned)
  191. {
  192. if (obj.OwnerID == landData.GroupID)
  193. parcelCounts.Owner += partCount;
  194. else if (landData.GroupID != UUID.Zero && obj.GroupID == landData.GroupID)
  195. parcelCounts.Group += partCount;
  196. else
  197. parcelCounts.Others += partCount;
  198. }
  199. else
  200. {
  201. if (obj.OwnerID == landData.OwnerID)
  202. parcelCounts.Owner += partCount;
  203. else
  204. parcelCounts.Others += partCount;
  205. }
  206. }
  207. }
  208. }
  209. // NOTE: Call under Taint Lock
  210. private void RemoveObject(SceneObjectGroup obj)
  211. {
  212. // m_log.DebugFormat("[PRIM COUNT MODULE]: Removing object {0} {1} from prim count", obj.Name, obj.UUID);
  213. // Currently this is being done by tainting the count instead.
  214. }
  215. public IPrimCounts GetPrimCounts(UUID parcelID)
  216. {
  217. // m_log.DebugFormat(
  218. // "[PRIM COUNT MODULE]: GetPrimCounts for parcel {0} in {1}", parcelID, m_Scene.RegionInfo.RegionName);
  219. PrimCounts primCounts;
  220. lock (m_PrimCounts)
  221. {
  222. if (m_PrimCounts.TryGetValue(parcelID, out primCounts))
  223. return primCounts;
  224. primCounts = new PrimCounts(parcelID, this);
  225. m_PrimCounts[parcelID] = primCounts;
  226. }
  227. return primCounts;
  228. }
  229. /// <summary>
  230. /// Get the number of prims on the parcel that are owned by the parcel owner.
  231. /// </summary>
  232. /// <param name="parcelID"></param>
  233. /// <returns></returns>
  234. public int GetOwnerCount(UUID parcelID)
  235. {
  236. int count = 0;
  237. lock (m_TaintLock)
  238. {
  239. if (m_Tainted)
  240. Recount();
  241. ParcelCounts counts;
  242. if (m_ParcelCounts.TryGetValue(parcelID, out counts))
  243. count = counts.Owner;
  244. }
  245. // m_log.DebugFormat(
  246. // "[PRIM COUNT MODULE]: GetOwnerCount for parcel {0} in {1} returning {2}",
  247. // parcelID, m_Scene.RegionInfo.RegionName, count);
  248. return count;
  249. }
  250. /// <summary>
  251. /// Get the number of prims on the parcel that have been set to the group that owns the parcel.
  252. /// </summary>
  253. /// <param name="parcelID"></param>
  254. /// <returns></returns>
  255. public int GetGroupCount(UUID parcelID)
  256. {
  257. int count = 0;
  258. lock (m_TaintLock)
  259. {
  260. if (m_Tainted)
  261. Recount();
  262. ParcelCounts counts;
  263. if (m_ParcelCounts.TryGetValue(parcelID, out counts))
  264. count = counts.Group;
  265. }
  266. // m_log.DebugFormat(
  267. // "[PRIM COUNT MODULE]: GetGroupCount for parcel {0} in {1} returning {2}",
  268. // parcelID, m_Scene.RegionInfo.RegionName, count);
  269. return count;
  270. }
  271. /// <summary>
  272. /// Get the number of prims on the parcel that are not owned by the parcel owner or set to the parcel group.
  273. /// </summary>
  274. /// <param name="parcelID"></param>
  275. /// <returns></returns>
  276. public int GetOthersCount(UUID parcelID)
  277. {
  278. int count = 0;
  279. lock (m_TaintLock)
  280. {
  281. if (m_Tainted)
  282. Recount();
  283. ParcelCounts counts;
  284. if (m_ParcelCounts.TryGetValue(parcelID, out counts))
  285. count = counts.Others;
  286. }
  287. // m_log.DebugFormat(
  288. // "[PRIM COUNT MODULE]: GetOthersCount for parcel {0} in {1} returning {2}",
  289. // parcelID, m_Scene.RegionInfo.RegionName, count);
  290. return count;
  291. }
  292. /// <summary>
  293. /// Get the number of selected prims.
  294. /// </summary>
  295. /// <param name="parcelID"></param>
  296. /// <returns></returns>
  297. public int GetSelectedCount(UUID parcelID)
  298. {
  299. int count = 0;
  300. lock (m_TaintLock)
  301. {
  302. if (m_Tainted)
  303. Recount();
  304. ParcelCounts counts;
  305. if (m_ParcelCounts.TryGetValue(parcelID, out counts))
  306. count = counts.Selected;
  307. }
  308. // m_log.DebugFormat(
  309. // "[PRIM COUNT MODULE]: GetSelectedCount for parcel {0} in {1} returning {2}",
  310. // parcelID, m_Scene.RegionInfo.RegionName, count);
  311. return count;
  312. }
  313. /// <summary>
  314. /// Get the total count of owner, group and others prims on the parcel.
  315. /// FIXME: Need to do selected prims once this is reimplemented.
  316. /// </summary>
  317. /// <param name="parcelID"></param>
  318. /// <returns></returns>
  319. public int GetTotalCount(UUID parcelID)
  320. {
  321. int count = 0;
  322. lock (m_TaintLock)
  323. {
  324. if (m_Tainted)
  325. Recount();
  326. ParcelCounts counts;
  327. if (m_ParcelCounts.TryGetValue(parcelID, out counts))
  328. {
  329. count = counts.Owner;
  330. count += counts.Group;
  331. count += counts.Others;
  332. count += counts.Selected;
  333. }
  334. }
  335. // m_log.DebugFormat(
  336. // "[PRIM COUNT MODULE]: GetTotalCount for parcel {0} in {1} returning {2}",
  337. // parcelID, m_Scene.RegionInfo.RegionName, count);
  338. return count;
  339. }
  340. /// <summary>
  341. /// Get the number of prims that are in the entire simulator for the owner of this parcel.
  342. /// </summary>
  343. /// <param name="parcelID"></param>
  344. /// <returns></returns>
  345. public int GetSimulatorCount(UUID parcelID)
  346. {
  347. int count = 0;
  348. lock (m_TaintLock)
  349. {
  350. if (m_Tainted)
  351. Recount();
  352. UUID owner;
  353. if (m_OwnerMap.TryGetValue(parcelID, out owner))
  354. {
  355. int val;
  356. if (m_SimwideCounts.TryGetValue(owner, out val))
  357. count = val;
  358. }
  359. }
  360. // m_log.DebugFormat(
  361. // "[PRIM COUNT MODULE]: GetOthersCount for parcel {0} in {1} returning {2}",
  362. // parcelID, m_Scene.RegionInfo.RegionName, count);
  363. return count;
  364. }
  365. /// <summary>
  366. /// Get the number of prims that a particular user owns on this parcel.
  367. /// </summary>
  368. /// <param name="parcelID"></param>
  369. /// <param name="userID"></param>
  370. /// <returns></returns>
  371. public int GetUserCount(UUID parcelID, UUID userID)
  372. {
  373. int count = 0;
  374. lock (m_TaintLock)
  375. {
  376. if (m_Tainted)
  377. Recount();
  378. ParcelCounts counts;
  379. if (m_ParcelCounts.TryGetValue(parcelID, out counts))
  380. {
  381. int val;
  382. if (counts.Users.TryGetValue(userID, out val))
  383. count = val;
  384. }
  385. }
  386. // m_log.DebugFormat(
  387. // "[PRIM COUNT MODULE]: GetUserCount for user {0} in parcel {1} in region {2} returning {3}",
  388. // userID, parcelID, m_Scene.RegionInfo.RegionName, count);
  389. return count;
  390. }
  391. // NOTE: This method MUST be called while holding the taint lock!
  392. private void Recount()
  393. {
  394. // m_log.DebugFormat("[PRIM COUNT MODULE]: Recounting prims on {0}", m_Scene.RegionInfo.RegionName);
  395. m_OwnerMap.Clear();
  396. m_SimwideCounts.Clear();
  397. m_ParcelCounts.Clear();
  398. List<ILandObject> land = m_Scene.LandChannel.AllParcels();
  399. foreach (ILandObject l in land)
  400. {
  401. LandData landData = l.LandData;
  402. m_OwnerMap[landData.GlobalID] = landData.OwnerID;
  403. m_SimwideCounts[landData.OwnerID] = 0;
  404. // m_log.DebugFormat(
  405. // "[PRIM COUNT MODULE]: Initializing parcel count for {0} on {1}",
  406. // landData.Name, m_Scene.RegionInfo.RegionName);
  407. m_ParcelCounts[landData.GlobalID] = new ParcelCounts();
  408. }
  409. m_Scene.ForEachSOG(AddObject);
  410. List<UUID> primcountKeys = new List<UUID>(m_PrimCounts.Keys);
  411. foreach (UUID k in primcountKeys)
  412. {
  413. if (!m_OwnerMap.ContainsKey(k))
  414. m_PrimCounts.Remove(k);
  415. }
  416. m_Tainted = false;
  417. }
  418. }
  419. public class PrimCounts : IPrimCounts
  420. {
  421. private PrimCountModule m_Parent;
  422. private UUID m_ParcelID;
  423. private UserPrimCounts m_UserPrimCounts;
  424. public PrimCounts (UUID parcelID, PrimCountModule parent)
  425. {
  426. m_ParcelID = parcelID;
  427. m_Parent = parent;
  428. m_UserPrimCounts = new UserPrimCounts(this);
  429. }
  430. public int Owner
  431. {
  432. get
  433. {
  434. return m_Parent.GetOwnerCount(m_ParcelID);
  435. }
  436. }
  437. public int Group
  438. {
  439. get
  440. {
  441. return m_Parent.GetGroupCount(m_ParcelID);
  442. }
  443. }
  444. public int Others
  445. {
  446. get
  447. {
  448. return m_Parent.GetOthersCount(m_ParcelID);
  449. }
  450. }
  451. public int Selected
  452. {
  453. get
  454. {
  455. return m_Parent.GetSelectedCount(m_ParcelID);
  456. }
  457. }
  458. public int Total
  459. {
  460. get
  461. {
  462. return m_Parent.GetTotalCount(m_ParcelID);
  463. }
  464. }
  465. public int Simulator
  466. {
  467. get
  468. {
  469. return m_Parent.GetSimulatorCount(m_ParcelID);
  470. }
  471. }
  472. public IUserPrimCounts Users
  473. {
  474. get
  475. {
  476. return m_UserPrimCounts;
  477. }
  478. }
  479. public int GetUserCount(UUID userID)
  480. {
  481. return m_Parent.GetUserCount(m_ParcelID, userID);
  482. }
  483. }
  484. public class UserPrimCounts : IUserPrimCounts
  485. {
  486. private PrimCounts m_Parent;
  487. public UserPrimCounts(PrimCounts parent)
  488. {
  489. m_Parent = parent;
  490. }
  491. public int this[UUID userID]
  492. {
  493. get
  494. {
  495. return m_Parent.GetUserCount(userID);
  496. }
  497. }
  498. }
  499. }