refactor: Use m_sittingAvatars to maintain the list of sitting avatars instead of two independent structures that do exactly the same thing

m_sittingAvatars code also already properly handles locking to avoid races.
0.8.0.3
Justin Clark-Casey (justincc) 2014-04-03 01:14:39 +01:00
parent 91b7679db9
commit 6ac9c9c972
6 changed files with 36 additions and 87 deletions

View File

@ -121,7 +121,6 @@ namespace OpenSim.Region.Framework.Scenes
private bool m_hasGroupChanged = false;
private long timeFirstChanged;
private long timeLastChanged;
private List<ScenePresence> m_linkedAvatars = new List<ScenePresence>();
/// <summary>
/// This indicates whether the object has changed such that it needs to be repersisted to permenant storage
@ -484,7 +483,7 @@ namespace OpenSim.Region.Framework.Scenes
m_rootPart.KeyframeMotion.StartCrossingCheck();
bool canCross = true;
foreach (ScenePresence av in m_linkedAvatars)
foreach (ScenePresence av in GetSittingAvatars())
{
// We need to cross these agents. First, let's find
// out if any of them can't cross for some reason.
@ -511,7 +510,7 @@ namespace OpenSim.Region.Framework.Scenes
List<avtocrossInfo> avsToCross = new List<avtocrossInfo>();
foreach (ScenePresence av in m_linkedAvatars)
foreach (ScenePresence av in GetSittingAvatars())
{
avtocrossInfo avinfo = new avtocrossInfo();
SceneObjectPart parentPart = m_scene.GetSceneObjectPart(av.ParentID);
@ -838,7 +837,7 @@ namespace OpenSim.Region.Framework.Scenes
/// No avatar should appear more than once in this list.
/// Do not manipulate this list directly - use the Add/Remove sitting avatar methods on SceneObjectPart.
/// </remarks>
protected internal List<UUID> m_sittingAvatars = new List<UUID>();
protected internal List<ScenePresence> m_sittingAvatars = new List<ScenePresence>();
#endregion
@ -1281,46 +1280,6 @@ namespace OpenSim.Region.Framework.Scenes
part.ParentID = m_rootPart.LocalId;
part.ClearUndoState();
}
/// <summary>
/// Add the avatar to this linkset (avatar is sat).
/// </summary>
/// <param name="agentID"></param>
public void AddAvatar(UUID agentID)
{
ScenePresence presence;
if (m_scene.TryGetScenePresence(agentID, out presence))
{
if (!m_linkedAvatars.Contains(presence))
{
m_linkedAvatars.Add(presence);
}
}
}
/// <summary>
/// Delete the avatar from this linkset (avatar is unsat).
/// </summary>
/// <param name="agentID"></param>
public void DeleteAvatar(UUID agentID)
{
ScenePresence presence;
if (m_scene.TryGetScenePresence(agentID, out presence))
{
if (m_linkedAvatars.Contains(presence))
{
m_linkedAvatars.Remove(presence);
}
}
}
/// <summary>
/// Returns the list of linked presences (avatars sat on this group)
/// </summary>
/// <param name="agentID"></param>
public List<ScenePresence> GetLinkedAvatars()
{
return m_linkedAvatars;
}
public ushort GetTimeDilation()
{
@ -1714,8 +1673,7 @@ namespace OpenSim.Region.Framework.Scenes
dupe.Backup = false;
dupe.m_parts = new MapAndArray<OpenMetaverse.UUID, SceneObjectPart>();
dupe.m_sittingAvatars = new List<UUID>();
dupe.m_linkedAvatars = new List<ScenePresence>();
dupe.m_sittingAvatars = new List<ScenePresence>();
dupe.CopyRootPart(m_rootPart, OwnerID, GroupID, userExposed);
dupe.m_rootPart.LinkNum = m_rootPart.LinkNum;
@ -3833,10 +3791,10 @@ namespace OpenSim.Region.Framework.Scenes
/// down after it move one place down the list.
/// </remarks>
/// <returns>A list of the sitting avatars. Returns an empty list if there are no sitting avatars.</returns>
public List<UUID> GetSittingAvatars()
public List<ScenePresence> GetSittingAvatars()
{
lock (m_sittingAvatars)
return new List<UUID>(m_sittingAvatars);
return new List<ScenePresence>(m_sittingAvatars);
}
/// <summary>

View File

@ -1299,7 +1299,7 @@ namespace OpenSim.Region.Framework.Scenes
/// <value>
/// null if there are no sitting avatars. This is to save us create a hashset for every prim in a scene.
/// </value>
private HashSet<UUID> m_sittingAvatars;
private HashSet<ScenePresence> m_sittingAvatars;
public virtual UUID RegionID
{
@ -1803,7 +1803,7 @@ namespace OpenSim.Region.Framework.Scenes
Array.Copy(Shape.ExtraParams, extraP, extraP.Length);
dupe.Shape.ExtraParams = extraP;
dupe.m_sittingAvatars = new HashSet<OpenMetaverse.UUID>();
dupe.m_sittingAvatars = new HashSet<ScenePresence>();
// safeguard actual copy is done in sog.copy
dupe.KeyframeMotion = null;
@ -4917,19 +4917,19 @@ namespace OpenSim.Region.Framework.Scenes
/// true if the avatar was not already recorded, false otherwise.
/// </returns>
/// <param name='avatarId'></param>
protected internal bool AddSittingAvatar(UUID avatarId)
protected internal bool AddSittingAvatar(ScenePresence sp)
{
lock (ParentGroup.m_sittingAvatars)
{
if (IsSitTargetSet && SitTargetAvatar == UUID.Zero)
SitTargetAvatar = avatarId;
SitTargetAvatar = sp.UUID;
if (m_sittingAvatars == null)
m_sittingAvatars = new HashSet<UUID>();
m_sittingAvatars = new HashSet<ScenePresence>();
if (m_sittingAvatars.Add(avatarId))
if (m_sittingAvatars.Add(sp))
{
ParentGroup.m_sittingAvatars.Add(avatarId);
ParentGroup.m_sittingAvatars.Add(sp);
return true;
}
@ -4946,22 +4946,22 @@ namespace OpenSim.Region.Framework.Scenes
/// true if the avatar was present and removed, false if it was not present.
/// </returns>
/// <param name='avatarId'></param>
protected internal bool RemoveSittingAvatar(UUID avatarId)
protected internal bool RemoveSittingAvatar(ScenePresence sp)
{
lock (ParentGroup.m_sittingAvatars)
{
if (SitTargetAvatar == avatarId)
if (SitTargetAvatar == sp.UUID)
SitTargetAvatar = UUID.Zero;
if (m_sittingAvatars == null)
return false;
if (m_sittingAvatars.Remove(avatarId))
if (m_sittingAvatars.Remove(sp))
{
if (m_sittingAvatars.Count == 0)
m_sittingAvatars = null;
ParentGroup.m_sittingAvatars.Remove(avatarId);
ParentGroup.m_sittingAvatars.Remove(sp);
return true;
}
@ -4975,14 +4975,14 @@ namespace OpenSim.Region.Framework.Scenes
/// </summary>
/// <remarks>This applies to all sitting avatars whether there is a sit target set or not.</remarks>
/// <returns>A hashset of the sitting avatars. Returns null if there are no sitting avatars.</returns>
public HashSet<UUID> GetSittingAvatars()
public HashSet<ScenePresence> GetSittingAvatars()
{
lock (ParentGroup.m_sittingAvatars)
{
if (m_sittingAvatars == null)
return null;
else
return new HashSet<UUID>(m_sittingAvatars);
return new HashSet<ScenePresence>(m_sittingAvatars);
}
}
@ -5002,4 +5002,4 @@ namespace OpenSim.Region.Framework.Scenes
}
}
}
}
}

View File

@ -1022,8 +1022,7 @@ namespace OpenSim.Region.Framework.Scenes
}
else
{
part.ParentGroup.AddAvatar(UUID);
part.AddSittingAvatar(UUID);
part.AddSittingAvatar(this);
if (part.SitTargetPosition != Vector3.Zero)
part.SitTargetAvatar = UUID;
// ParentPosition = part.GetWorldPosition();
@ -2522,7 +2521,6 @@ namespace OpenSim.Region.Framework.Scenes
}
}
part.ParentGroup.DeleteAvatar(UUID);
Vector3 sitPartWorldPosition = part.GetWorldPosition();
ControllingClient.SendClearFollowCamProperties(part.ParentUUID);
@ -2578,7 +2576,7 @@ namespace OpenSim.Region.Framework.Scenes
SendAvatarDataToAllAgents();
m_requestedSitTargetID = 0;
part.RemoveSittingAvatar(UUID);
part.RemoveSittingAvatar(this);
part.ParentGroup.TriggerScriptChangedEvent(Changed.LINK);
}
@ -2688,7 +2686,7 @@ namespace OpenSim.Region.Framework.Scenes
Velocity = Vector3.Zero;
part.AddSittingAvatar(UUID);
part.AddSittingAvatar(this);
cameraAtOffset = part.GetCameraAtOffset();
cameraEyeOffset = part.GetCameraEyeOffset();
@ -2821,7 +2819,7 @@ namespace OpenSim.Region.Framework.Scenes
Velocity = Vector3.Zero;
part.AddSittingAvatar(UUID);
part.AddSittingAvatar(this);
Vector3 cameraAtOffset = part.GetCameraAtOffset();
Vector3 cameraEyeOffset = part.GetCameraEyeOffset();
@ -2836,7 +2834,6 @@ namespace OpenSim.Region.Framework.Scenes
m_pos = offset;
m_requestedSitTargetID = 0;
part.ParentGroup.AddAvatar(UUID);
ParentPart = part;
ParentID = part.LocalId;
@ -2936,7 +2933,6 @@ namespace OpenSim.Region.Framework.Scenes
// Name, part.AbsolutePosition, m_pos, ParentPosition, part.Name, part.LocalId);
}
part.ParentGroup.AddAvatar(UUID);
ParentPart = m_scene.GetSceneObjectPart(m_requestedSitTargetID);
ParentID = m_requestedSitTargetID;
m_AngularVelocity = Vector3.Zero;

View File

@ -172,7 +172,6 @@ namespace OpenSim.Region.Framework.Scenes.Tests
so1PostCross = sceneB.GetSceneObjectGroup(so1Id);
Assert.NotNull(so1PostCross);
Assert.AreEqual(1, so1PostCross.GetSittingAvatarsCount());
Assert.AreEqual(1, so1PostCross.GetLinkedAvatars().Count);
}
Vector3 so1PostCrossPos = so1PostCross.AbsolutePosition;
@ -198,7 +197,6 @@ namespace OpenSim.Region.Framework.Scenes.Tests
SceneObjectGroup so1PostReCross = sceneA.GetSceneObjectGroup(so1Id);
Assert.NotNull(so1PostReCross);
Assert.AreEqual(1, so1PostReCross.GetSittingAvatarsCount());
Assert.AreEqual(1, so1PostReCross.GetLinkedAvatars().Count);
}
}

View File

@ -93,9 +93,9 @@ namespace OpenSim.Region.Framework.Scenes.Tests
Assert.That(part.SitTargetAvatar, Is.EqualTo(UUID.Zero));
Assert.That(part.GetSittingAvatarsCount(), Is.EqualTo(1));
HashSet<UUID> sittingAvatars = part.GetSittingAvatars();
HashSet<ScenePresence> sittingAvatars = part.GetSittingAvatars();
Assert.That(sittingAvatars.Count, Is.EqualTo(1));
Assert.That(sittingAvatars.Contains(m_sp.UUID));
Assert.That(sittingAvatars.Contains(m_sp));
Assert.That(m_sp.ParentID, Is.EqualTo(part.LocalId));
}
@ -151,9 +151,9 @@ namespace OpenSim.Region.Framework.Scenes.Tests
Assert.That(m_sp.PhysicsActor, Is.Null);
Assert.That(part.GetSittingAvatarsCount(), Is.EqualTo(1));
HashSet<UUID> sittingAvatars = part.GetSittingAvatars();
HashSet<ScenePresence> sittingAvatars = part.GetSittingAvatars();
Assert.That(sittingAvatars.Count, Is.EqualTo(1));
Assert.That(sittingAvatars.Contains(m_sp.UUID));
Assert.That(sittingAvatars.Contains(m_sp));
m_sp.StandUp();

View File

@ -335,14 +335,14 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
}
int actualPrimCount = part.ParentGroup.PrimCount;
List<UUID> sittingAvatarIds = part.ParentGroup.GetSittingAvatars();
int adjustedPrimCount = actualPrimCount + sittingAvatarIds.Count;
List<ScenePresence> sittingAvatars = part.ParentGroup.GetSittingAvatars();
int adjustedPrimCount = actualPrimCount + sittingAvatars.Count;
// Special case for a single prim. In this case the linknum is zero. However, this will not match a single
// prim that has any avatars sat upon it (in which case the root prim is link 1).
if (linknum == 0)
{
if (actualPrimCount == 1 && sittingAvatarIds.Count == 0)
if (actualPrimCount == 1 && sittingAvatars.Count == 0)
return part;
return null;
@ -351,7 +351,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
// here we must match 1 (ScriptBaseClass.LINK_ROOT).
else if (linknum == ScriptBaseClass.LINK_ROOT && actualPrimCount == 1)
{
if (sittingAvatarIds.Count > 0)
if (sittingAvatars.Count > 0)
return part.ParentGroup.RootPart;
else
return null;
@ -364,11 +364,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
}
else
{
ScenePresence sp = World.GetScenePresence(sittingAvatarIds[linknum - actualPrimCount - 1]);
if (sp != null)
return sp;
else
return null;
return sittingAvatars[linknum - actualPrimCount - 1];
}
}
else
@ -3619,7 +3615,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
}
else
{
if (m_host.ParentGroup.GetSittingAvatars().Contains(agentID))
if (m_host.ParentGroup.GetSittingAvatars().SingleOrDefault(sp => sp.UUID == agentID) != null)
{
// When agent is sitting, certain permissions are implicit if requested from sitting agent
implicitPerms = ScriptBaseClass.PERMISSION_TRIGGER_ANIMATION |
@ -3651,10 +3647,11 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
}
ScenePresence presence = World.GetScenePresence(agentID);
if (presence != null)
{
// If permissions are being requested from an NPC and were not implicitly granted above then
// auto grant all reuqested permissions if the script is owned by the NPC or the NPCs owner
// auto grant all requested permissions if the script is owned by the NPC or the NPCs owner
INPCModule npcModule = World.RequestModuleInterface<INPCModule>();
if (npcModule != null && npcModule.IsNPC(agentID, World))
{