PrimCountModule.cs 20 KB

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