Change AttachmentsModule.DetachSingleAttachmentToInv() to accept a SOG directly instead of an item ID to then shuffle through attachments, saving CPU busywork.

Almost all callers already had the sog to hand.
Still checking that it's really an attachment, but now by inspecting SOG.AttachedAvatar
0.7.3-extended
Justin Clark-Casey (justincc) 2012-06-28 23:31:23 +01:00
parent 733a8c9f89
commit 663b0cc681
5 changed files with 58 additions and 52 deletions

View File

@ -302,10 +302,8 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
// At the moment we can only deal with a single attachment // At the moment we can only deal with a single attachment
if (attachments.Count != 0) if (attachments.Count != 0)
{ {
UUID oldAttachmentItemID = attachments[0].FromItemID; if (attachments[0].FromItemID != UUID.Zero)
DetachSingleAttachmentToInvInternal(sp, attachments[0]);
if (oldAttachmentItemID != UUID.Zero)
DetachSingleAttachmentToInvInternal(sp, oldAttachmentItemID);
else else
m_log.WarnFormat( m_log.WarnFormat(
"[ATTACHMENTS MODULE]: When detaching existing attachment {0} {1} at point {2} to make way for {3} {4} for {5}, couldn't find the associated item ID to adjust inventory attachment record!", "[ATTACHMENTS MODULE]: When detaching existing attachment {0} {1} at point {2} to make way for {3} {4} for {5}, couldn't find the associated item ID to adjust inventory attachment record!",
@ -442,18 +440,27 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
m_scene.EventManager.TriggerOnAttach(so.LocalId, so.UUID, UUID.Zero); m_scene.EventManager.TriggerOnAttach(so.LocalId, so.UUID, UUID.Zero);
} }
public void DetachSingleAttachmentToInv(IScenePresence sp, UUID itemID) public void DetachSingleAttachmentToInv(IScenePresence sp, SceneObjectGroup so)
{ {
lock (sp.AttachmentsSyncLock) lock (sp.AttachmentsSyncLock)
{ {
// Save avatar attachment information // Save avatar attachment information
// m_log.Debug("[ATTACHMENTS MODULE]: Detaching from UserID: " + sp.UUID + ", ItemID: " + itemID); // m_log.Debug("[ATTACHMENTS MODULE]: Detaching from UserID: " + sp.UUID + ", ItemID: " + itemID);
bool changed = sp.Appearance.DetachAttachment(itemID); if (so.AttachedAvatar != sp.UUID)
{
m_log.WarnFormat(
"[ATTACHMENTS MODULE]: Tried to detach object {0} from {1} {2} but attached avatar id was {3} in {4}",
so.Name, sp.Name, sp.UUID, so.AttachedAvatar, m_scene.RegionInfo.RegionName);
return;
}
bool changed = sp.Appearance.DetachAttachment(so.FromItemID);
if (changed && m_scene.AvatarFactory != null) if (changed && m_scene.AvatarFactory != null)
m_scene.AvatarFactory.QueueAppearanceSave(sp.UUID); m_scene.AvatarFactory.QueueAppearanceSave(sp.UUID);
DetachSingleAttachmentToInvInternal(sp, itemID); DetachSingleAttachmentToInvInternal(sp, so);
} }
} }
@ -657,39 +664,21 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
return newItem; return newItem;
} }
// What makes this method odd and unique is it tries to detach using an UUID.... Yay for standards. private void DetachSingleAttachmentToInvInternal(IScenePresence sp, SceneObjectGroup so)
// To LocalId or UUID, *THAT* is the question. How now Brown UUID??
private void DetachSingleAttachmentToInvInternal(IScenePresence sp, UUID itemID)
{ {
// m_log.DebugFormat("[ATTACHMENTS MODULE]: Detaching item {0} to inventory for {1}", itemID, sp.Name); // m_log.DebugFormat("[ATTACHMENTS MODULE]: Detaching item {0} to inventory for {1}", itemID, sp.Name);
if (itemID == UUID.Zero) // If this happened, someone made a mistake.... m_scene.EventManager.TriggerOnAttach(so.LocalId, so.FromItemID, UUID.Zero);
return; sp.RemoveAttachment(so);
m_scene.DeleteSceneObject(so, false);
lock (sp.AttachmentsSyncLock) // Prepare sog for storage
{ so.AttachedAvatar = UUID.Zero;
List<SceneObjectGroup> attachments = sp.GetAttachments(); so.RootPart.SetParentLocalId(0);
so.IsAttachment = false;
so.AbsolutePosition = so.RootPart.AttachedPos;
foreach (SceneObjectGroup group in attachments) UpdateKnownItem(sp, so, true);
{
if (group.FromItemID == itemID)
{
m_scene.EventManager.TriggerOnAttach(group.LocalId, itemID, UUID.Zero);
sp.RemoveAttachment(group);
m_scene.DeleteSceneObject(group, false);
// Prepare sog for storage
group.AttachedAvatar = UUID.Zero;
group.RootPart.SetParentLocalId(0);
group.IsAttachment = false;
group.AbsolutePosition = group.RootPart.AttachedPos;
UpdateKnownItem(sp, group, true);
return;
}
}
}
} }
private SceneObjectGroup RezSingleAttachmentFromInventoryInternal( private SceneObjectGroup RezSingleAttachmentFromInventoryInternal(
@ -897,8 +886,9 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId); ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
SceneObjectGroup group = m_scene.GetGroupByPrim(objectLocalID); SceneObjectGroup group = m_scene.GetGroupByPrim(objectLocalID);
if (sp != null && group != null) if (sp != null && group != null)
DetachSingleAttachmentToInv(sp, group.FromItemID); DetachSingleAttachmentToInv(sp, group);
} }
private void Client_OnDetachAttachmentIntoInv(UUID itemID, IClientAPI remoteClient) private void Client_OnDetachAttachmentIntoInv(UUID itemID, IClientAPI remoteClient)
@ -908,7 +898,21 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments
ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId); ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
if (sp != null) if (sp != null)
DetachSingleAttachmentToInv(sp, itemID); {
lock (sp.AttachmentsSyncLock)
{
List<SceneObjectGroup> attachments = sp.GetAttachments();
foreach (SceneObjectGroup group in attachments)
{
if (group.FromItemID == itemID)
{
DetachSingleAttachmentToInv(sp, group);
return;
}
}
}
}
} }
private void Client_OnObjectDrop(uint soLocalId, IClientAPI remoteClient) private void Client_OnObjectDrop(uint soLocalId, IClientAPI remoteClient)

View File

@ -227,9 +227,10 @@ namespace OpenSim.Region.CoreModules.Avatar.Attachments.Tests
InventoryItemBase attItem = CreateAttachmentItem(scene, ua1.PrincipalID, "att", 0x10, 0x20); InventoryItemBase attItem = CreateAttachmentItem(scene, ua1.PrincipalID, "att", 0x10, 0x20);
scene.AttachmentsModule.RezSingleAttachmentFromInventory( SceneObjectGroup so
sp, attItem.ID, (uint)AttachmentPoint.Chest); = (SceneObjectGroup)scene.AttachmentsModule.RezSingleAttachmentFromInventory(
scene.AttachmentsModule.DetachSingleAttachmentToInv(sp, attItem.ID); sp, attItem.ID, (uint)AttachmentPoint.Chest);
scene.AttachmentsModule.DetachSingleAttachmentToInv(sp, so);
// Check status on scene presence // Check status on scene presence
Assert.That(sp.HasAttachments(), Is.False); Assert.That(sp.HasAttachments(), Is.False);

View File

@ -109,11 +109,11 @@ namespace OpenSim.Region.Framework.Interfaces
void DetachSingleAttachmentToGround(IScenePresence sp, uint objectLocalID); void DetachSingleAttachmentToGround(IScenePresence sp, uint objectLocalID);
/// <summary> /// <summary>
/// Detach the given item so that it remains in the user's inventory. /// Detach the given attachment so that it remains in the user's inventory.
/// </summary> /// </summary>
/// <param name="sp">/param> /// <param name="sp">/param>
/// <param name="itemID"></param> /// <param name="grp">The attachment to detach.</param>
void DetachSingleAttachmentToInv(IScenePresence sp, UUID itemID); void DetachSingleAttachmentToInv(IScenePresence sp, SceneObjectGroup grp);
/// <summary> /// <summary>
/// Update the position of an attachment. /// Update the position of an attachment.

View File

@ -72,6 +72,10 @@ namespace OpenSim.Region.Framework.Interfaces
/// <returns></returns> /// <returns></returns>
List<SceneObjectGroup> GetAttachments(uint attachmentPoint); List<SceneObjectGroup> GetAttachments(uint attachmentPoint);
/// <summary>
/// Does this avatar have any attachments?
/// </summary>
/// <returns></returns>
bool HasAttachments(); bool HasAttachments();
// Don't use these methods directly. Instead, use the AttachmentsModule // Don't use these methods directly. Instead, use the AttachmentsModule

View File

@ -2950,15 +2950,12 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
private void DetachWrapper(object o) private void DetachWrapper(object o)
{ {
SceneObjectPart host = (SceneObjectPart)o; if (World.AttachmentsModule != null)
{
SceneObjectGroup grp = host.ParentGroup; SceneObjectPart host = (SceneObjectPart)o;
UUID itemID = grp.FromItemID; ScenePresence presence = World.GetScenePresence(host.OwnerID);
ScenePresence presence = World.GetScenePresence(host.OwnerID); World.AttachmentsModule.DetachSingleAttachmentToInv(presence, host.ParentGroup);
}
IAttachmentsModule attachmentsModule = m_ScriptEngine.World.AttachmentsModule;
if (attachmentsModule != null)
attachmentsModule.DetachSingleAttachmentToInv(presence, itemID);
} }
public void llAttachToAvatar(int attachmentPoint) public void llAttachToAvatar(int attachmentPoint)