12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505 |
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.IO;
- using System.Text;
- using System.Threading;
- using System.Xml;
- using log4net;
- using Mono.Addins;
- using Nini.Config;
- using OpenMetaverse;
- using OpenMetaverse.Packets;
- using OpenSim.Framework;
- using OpenSim.Framework.Console;
- using OpenSim.Region.Framework;
- using OpenSim.Region.Framework.Interfaces;
- using OpenSim.Region.Framework.Scenes;
- using OpenSim.Region.Framework.Scenes.Serialization;
- using OpenSim.Services.Interfaces;
- using PermissionMask = OpenSim.Framework.PermissionMask;
- namespace OpenSim.Region.CoreModules.Avatar.Attachments
- {
- [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "AttachmentsModule")]
- public class AttachmentsModule : IAttachmentsModule, INonSharedRegionModule
- {
- #region INonSharedRegionModule
- private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- public int DebugLevel { get; set; }
- private Scene m_scene;
- private IRegionConsole m_regionConsole;
- private IInventoryAccessModule m_invAccessModule;
- private bool m_wearReplacesAllOption = true;
-
-
-
- public bool Enabled { get; private set; }
- public string Name { get { return "Attachments Module"; } }
- public Type ReplaceableInterface { get { return null; } }
- public void Initialise(IConfigSource source)
- {
- IConfig config = source.Configs["Attachments"];
- if (config != null)
- {
- Enabled = config.GetBoolean("Enabled", true);
- m_wearReplacesAllOption = config.GetBoolean("WearReplacesAll", true);
- }
- else
- {
- Enabled = true;
- }
- }
- public void AddRegion(Scene scene)
- {
- m_scene = scene;
- if (Enabled)
- {
-
-
-
- m_scene.RegisterModuleInterface<IAttachmentsModule>(this);
- m_scene.EventManager.OnNewClient += SubscribeToClientEvents;
- m_scene.EventManager.OnStartScript += (localID, itemID) => OnScriptStateChange(localID, true);
- m_scene.EventManager.OnStopScript += (localID, itemID) => OnScriptStateChange(localID, false);
- }
-
- }
- public void RemoveRegion(Scene scene)
- {
- if (!Enabled)
- return;
- m_scene.UnregisterModuleInterface<IAttachmentsModule>(this);
- m_scene.EventManager.OnNewClient -= SubscribeToClientEvents;
- }
- public void RegionLoaded(Scene scene)
- {
- if (!Enabled)
- return;
- m_invAccessModule = m_scene.RequestModuleInterface<IInventoryAccessModule>();
- m_regionConsole = scene.RequestModuleInterface<IRegionConsole>();
- if (m_regionConsole != null)
- {
- m_regionConsole.AddCommand("AttachModule", false, "set auto_grant_attach_perms", "set auto_grant_attach_perms true|false", "Allow objects owned by the region owner or estate managers to obtain attach permissions without asking the user", HandleSetAutoGrantAttachPerms);
- }
- scene.AddCommand(
- "Debug",
- this,
- "debug attachments log",
- "debug attachments log [0|1]",
- "Turn on attachments debug logging",
- " <= 0 - turns off debug logging\n"
- + " >= 1 - turns on attachment message debug logging",
- HandleDebugAttachmentsLog);
- scene.AddCommand(
- "Debug",
- this,
- "debug attachments status",
- "debug attachments status",
- "Show current attachments debug status",
- HandleDebugAttachmentsStatus);
-
- MainConsole.Instance.Commands.AddCommand(
- "Users", true, "attachments show",
- "attachments show [<first-name> <last-name>]",
- "Show attachment information for avatars in this simulator.",
- "If no name is supplied then information for all avatars is shown.",
- HandleShowAttachmentsCommand);
- }
- public void Close()
- {
- if (!Enabled)
- return;
- RemoveRegion(m_scene);
- }
- private void HandleDebugAttachmentsLog(string module, string[] args)
- {
- int debugLevel;
- if (!(args.Length == 4 && int.TryParse(args[3], out debugLevel)))
- {
- MainConsole.Instance.Output("Usage: debug attachments log [0|1]");
- }
- else
- {
- DebugLevel = debugLevel;
- MainConsole.Instance.Output(
- "Set attachments debug level to {0} in {1}", DebugLevel, m_scene.Name);
- }
- }
- private void HandleDebugAttachmentsStatus(string module, string[] args)
- {
- MainConsole.Instance.Output("Settings for {0}", m_scene.Name);
- MainConsole.Instance.Output("Debug logging level: {0}", DebugLevel);
- }
- protected void HandleShowAttachmentsCommand(string module, string[] cmd)
- {
- if (cmd.Length != 2 && cmd.Length < 4)
- {
- MainConsole.Instance.Output("Usage: attachments show [<first-name> <last-name>]");
- return;
- }
- SceneManager sm = SceneManager.Instance;
- if(sm == null || sm.Scenes.Count == 0)
- return;
- bool targetNameSupplied = false;
- string optionalTargetFirstName = null;
- string optionalTargetLastName = null;
- if (cmd.Length >= 4)
- {
- targetNameSupplied = true;
- optionalTargetFirstName = cmd[2];
- optionalTargetLastName = cmd[3];
- }
- StringBuilder sb = new StringBuilder();
- sm.ForEachSelectedScene(
- scene =>
- {
- if (targetNameSupplied)
- {
- ScenePresence sp = scene.GetScenePresence(optionalTargetFirstName, optionalTargetLastName);
- if (sp != null && !sp.IsChildAgent)
- GetAttachmentsReport(sp, sb);
- }
- else
- {
- sb.AppendFormat("--- All attachments for region {0}:\n", scene.Name);
- scene.ForEachRootScenePresence(sp => GetAttachmentsReport(sp, sb));
- }
- });
- MainConsole.Instance.Output(sb.ToString());
- }
- private void GetAttachmentsReport(ScenePresence sp, StringBuilder sb)
- {
- sb.AppendFormat("Attachments for {0}\n", sp.Name);
- ConsoleDisplayList ct = new ConsoleDisplayList();
- int totalprims = 0;
- List<SceneObjectGroup> attachmentObjects = sp.GetAttachments();
- for (int i = 0; i < attachmentObjects.Count; ++i)
- {
- SceneObjectGroup attachmentObject = attachmentObjects[i];
- ct.Indent = 2;
- ct.AddRow("Attachment Name", attachmentObject.Name);
- ct.AddRow("Local ID", attachmentObject.LocalId);
- ct.AddRow("Item ID", attachmentObject.UUID);
- ct.AddRow("From Item ID", attachmentObject.FromItemID);
- ct.AddRow("Attach Point", ((AttachmentPoint)attachmentObject.AttachmentPoint));
- ct.AddRow("Prims", attachmentObject.PrimCount);
- ct.AddRow("Position", attachmentObject.RootPart.AttachedPos + "\n");
- totalprims += attachmentObject.PrimCount;
- }
- sb.AppendFormat("--Total Attachment prims for {0} : {1}\n\n", sp.Name, totalprims);
- ct.AddToStringBuilder(sb);
- }
- private void SendConsoleOutput(UUID agentID, string text)
- {
- if (m_regionConsole == null)
- return;
- m_regionConsole.SendConsoleOutput(agentID, text);
- }
- private void HandleSetAutoGrantAttachPerms(string module, string[] parms)
- {
- UUID agentID = new UUID(parms[parms.Length - 1]);
- Array.Resize(ref parms, parms.Length - 1);
- if (parms.Length != 3)
- {
- SendConsoleOutput(agentID, "Command parameter error");
- return;
- }
- string val = parms[2];
- if (val != "true" && val != "false")
- {
- SendConsoleOutput(agentID, "Command parameter error");
- return;
- }
- m_scene.StoreExtraSetting("auto_grant_attach_perms", val);
- SendConsoleOutput(agentID, String.Format("auto_grant_attach_perms set to {0}", val));
- }
-
-
-
-
-
- private void OnScriptStateChange(uint localID, bool started)
- {
- SceneObjectGroup sog = m_scene.GetGroupByPrim(localID);
- if (sog != null && sog.IsAttachment)
- {
- if (!started)
- {
-
-
-
- ScenePresence sp = m_scene.GetScenePresence(sog.AttachedAvatar);
- if (sp.ControllingClient.IsActive)
- sog.HasGroupChanged = true;
- }
- else
- {
- sog.HasGroupChanged = true;
- }
- }
- }
- #endregion
- #region IAttachmentsModule
- public void CopyAttachments(IScenePresence sp, AgentData ad)
- {
- lock (sp.AttachmentsSyncLock)
- {
-
- List<SceneObjectGroup> attachments = sp.GetAttachments();
- if (attachments.Count > 0)
- {
- ad.AttachmentObjects = new List<ISceneObject>(attachments.Count);
- ad.AttachmentObjectStates = new List<string>(attachments.Count);
- sp.InTransitScriptStates.Clear();
- for (int indx = 0; indx < attachments.Count; ++indx)
- {
- SceneObjectGroup sog = attachments[indx];
-
-
- ISceneObject clone = sog.CloneForNewScene();
-
- ((SceneObjectGroup)clone).RootPart.GroupPosition = sog.RootPart.AttachedPos;
- ((SceneObjectGroup)clone).IsAttachment = false;
- ad.AttachmentObjects.Add(clone);
- string state = sog.GetStateSnapshot();
- ad.AttachmentObjectStates.Add(state);
- sp.InTransitScriptStates.Add(state);
-
-
- }
- }
- }
- }
- public void CopyAttachments(AgentData ad, IScenePresence sp)
- {
- if (ad.AttachmentObjects != null && ad.AttachmentObjects.Count > 0)
- {
- lock (sp.AttachmentsSyncLock)
- DeleteAttachmentsFromScene(sp, true);
- int i = 0;
- for (int indx = 0; indx < ad.AttachmentObjects.Count; ++indx)
- {
- SceneObjectGroup sog = (SceneObjectGroup)ad.AttachmentObjects[indx];
- sog.LocalId = 0;
- sog.RootPart.ClearUpdateSchedule();
- sog.SetState(ad.AttachmentObjectStates[i++], m_scene);
- m_scene.IncomingCreateObject(Vector3.Zero, sog);
- }
- }
- }
- public void RezAttachments(IScenePresence sp)
- {
- if (!Enabled)
- return;
- if (sp.Appearance == null)
- {
- m_log.WarnFormat("[ATTACHMENTS MODULE]: Appearance has not been initialized for agent {0}", sp.UUID);
- return;
- }
- if (sp.GetAttachmentsCount() > 0)
- {
- if (DebugLevel > 0)
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: Not doing simulator-side attachment rez for {0} in {1} as their viewer has already rezzed attachments",
- m_scene.Name, sp.Name);
- return;
- }
- if (DebugLevel > 0)
- m_log.DebugFormat("[ATTACHMENTS MODULE]: Rezzing any attachments for {0} from simulator-side", sp.Name);
- XmlDocument doc = new XmlDocument();
- string stateData = String.Empty;
- IAttachmentsService attServ = m_scene.RequestModuleInterface<IAttachmentsService>();
- if (attServ != null)
- {
- m_log.DebugFormat("[ATTACHMENT]: Loading attachment data from attachment service");
- stateData = attServ.Get(sp.UUID.ToString());
- if (stateData != String.Empty)
- {
- try
- {
- doc.LoadXml(stateData);
- }
- catch { }
- }
- }
- Dictionary<UUID, string> itemData = new Dictionary<UUID, string>();
- XmlNodeList nodes = doc.GetElementsByTagName("Attachment");
- if (nodes.Count > 0)
- {
- foreach (XmlNode n in nodes)
- {
- XmlElement elem = (XmlElement)n;
- string itemID = elem.GetAttribute("ItemID");
- string xml = elem.InnerXml;
- itemData[new UUID(itemID)] = xml;
- }
- }
- List<AvatarAttachment> attachments = sp.Appearance.GetAttachments();
-
- UUID[] items = new UUID[attachments.Count];
- for (int i = 0; i < attachments.Count; ++i)
- items[i] = attachments[i].ItemID;
- m_scene.InventoryService.GetMultipleItems(sp.UUID, items);
- for (int indx = 0; indx < attachments.Count; ++indx)
- {
- AvatarAttachment attach = attachments[indx];
- uint attachmentPt = (uint)attach.AttachPoint;
- try
- {
- string xmlData;
- XmlDocument d = null;
- if (itemData.TryGetValue(attach.ItemID, out xmlData))
- {
- d = new XmlDocument();
- d.LoadXml(xmlData);
- m_log.InfoFormat("[ATTACHMENT]: Found saved state for item {0}, loading it", attach.ItemID);
- }
-
-
- RezSingleAttachmentFromInventoryInternal(
- sp, sp.PresenceType == PresenceType.Npc ? UUID.Zero : attach.ItemID, attach.AssetID, attachmentPt, true, d);
- }
- catch (Exception e)
- {
- UUID agentId = (sp.ControllingClient == null) ? default(UUID) : sp.ControllingClient.AgentId;
- m_log.ErrorFormat("[ATTACHMENTS MODULE]: Unable to rez attachment with itemID {0}, assetID {1}, point {2} for {3}: {4}\n{5}",
- attach.ItemID, attach.AssetID, attachmentPt, agentId, e.Message, e.StackTrace);
- }
- }
- }
- public void DeRezAttachments(IScenePresence sp)
- {
- if (!Enabled)
- return;
- List<SceneObjectGroup> attachments = sp.GetAttachments();
- if (DebugLevel > 0)
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: Saving for {0} attachments for {1} in {2}",
- attachments.Count, sp.Name, m_scene.Name);
- if (attachments.Count <= 0)
- return;
- if (sp.PresenceType != PresenceType.Npc)
- {
- lock (sp.AttachmentsSyncLock)
- {
- for (int i = 0; i < attachments.Count; ++i)
- {
- SceneObjectGroup sog = attachments[i];
- UpdateDetachedObject(sp, sog, PrepareScriptInstanceForSave(sog, false));
- }
- sp.ClearAttachments();
- }
- }
- else
- {
- lock (sp.AttachmentsSyncLock)
- {
- for (int i = 0; i < attachments.Count; ++i)
- UpdateDetachedObject(sp, attachments[i], String.Empty);
- sp.ClearAttachments();
- }
- }
- }
- public void DeleteAttachmentsFromScene(IScenePresence sp, bool silent)
- {
- if (!Enabled)
- return;
- if (DebugLevel > 0)
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: Deleting attachments from scene {0} for {1}, silent = {2}",
- m_scene.RegionInfo.RegionName, sp.Name, silent);
- List<SceneObjectGroup> attachments = sp.GetAttachments();
- for(int i = 0; i < attachments.Count; ++i)
- {
- SceneObjectGroup sog = attachments[i];
- sog.Scene.DeleteSceneObject(sog, silent);
- }
- sp.ClearAttachments();
- }
- public bool AttachObject(IScenePresence sp, SceneObjectGroup group, uint attachmentPt, bool silent,
- bool addToInventory, bool append)
- {
- if (!Enabled)
- return false;
- return AttachObjectInternal(sp, group, attachmentPt, silent, addToInventory, false, append);
- }
-
-
-
-
-
-
-
-
-
-
- private bool AttachObjectInternal(IScenePresence sp, SceneObjectGroup group, uint attachmentPt,
- bool silent, bool addToInventory, bool resumeScripts, bool append)
- {
- if (group.GetSittingAvatarsCount() != 0)
- {
- if (DebugLevel > 0)
- m_log.WarnFormat(
- "[ATTACHMENTS MODULE]: Ignoring request to attach {0} {1} to {2} on {3} since {4} avatars are still sitting on it",
- group.Name, group.LocalId, sp.Name, attachmentPt, group.GetSittingAvatarsCount());
- return false;
- }
- Vector3 attachPos = group.AbsolutePosition;
-
-
- attachmentPt &= 0x7f;
-
-
-
- if (attachmentPt != (uint)AttachmentPoint.Default && attachmentPt != group.AttachmentPoint)
- {
- attachPos = Vector3.Zero;
- }
-
-
- if (attachmentPt != 0 && attachmentPt == group.RootPart.Shape.LastAttachPoint)
- {
- attachPos = group.RootPart.AttachedPos;
- }
-
- if (attachmentPt == (uint)AttachmentPoint.Default)
- {
-
- attachmentPt = group.AttachmentPoint;
- }
-
- if (attachmentPt == 0)
- {
- attachmentPt = (uint)group.RootPart.Shape.LastAttachPoint;
- attachPos = group.RootPart.AttachedPos;
- }
-
- if (attachmentPt == 0)
- {
-
- attachmentPt = (uint)AttachmentPoint.LeftHand;
- attachPos = Vector3.Zero;
- }
- if(attachmentPt > (uint)AttachmentPoint.LastValid)
- {
- m_log.WarnFormat("[ATTACHMENTS MODULE]: Invalid attachment point {0} SP {1}", attachmentPt, sp.Name);
- return false;
- }
- List<SceneObjectGroup> attachments = sp.GetAttachments();
- List<SceneObjectGroup> toRemove = new List<SceneObjectGroup>(attachments.Count);
- bool doRemCheck = !append;
- for(int i = 0; i < attachments.Count; ++i)
- {
- SceneObjectGroup sog = attachments[i];
-
- if (group.UUID == sog.UUID)
- {
-
-
-
-
- return false;
- }
- if(doRemCheck)
- {
- if(sog.AttachmentPoint == attachmentPt)
- {
- toRemove.Add(sog);
- if(!m_wearReplacesAllOption)
- doRemCheck = false;
- }
- }
- }
- if(attachments.Count - toRemove.Count >= Constants.MaxAgentAttachments)
- {
- m_log.WarnFormat("[ATTACHMENTS MODULE]: Max attachments exceded {0}",sp.Name);
- return false;
- }
- group.DetachFromBackup();
- group.AttachmentPoint = attachmentPt;
- group.RootPart.AttachedPos = attachPos;
-
- lock (sp.AttachmentsSyncLock)
- {
- if (toRemove.Count > 0)
- {
- for (int i = 0; i< toRemove.Count; ++i)
- {
- SceneObjectGroup g = toRemove[i];
- if (g.FromItemID != UUID.Zero)
- DetachSingleAttachmentToInv(sp, g);
- }
- }
- if (addToInventory && sp.PresenceType != PresenceType.Npc)
- UpdateUserInventoryWithAttachment(sp, group, attachmentPt, append);
- AttachToAgent(sp, group, attachmentPt, attachPos, silent);
- if (resumeScripts)
- {
-
-
- group.CreateScriptInstances(0, true, m_scene.DefaultScriptEngine, 4);
- group.ResumeScripts();
- }
- else
-
-
-
-
- m_scene.EventManager.TriggerOnAttach(group.LocalId, group.FromItemID, sp.UUID);
- }
- return true;
- }
- private void UpdateUserInventoryWithAttachment(IScenePresence sp, SceneObjectGroup group, uint attachmentPt, bool append)
- {
-
- UUID newAttachmentItemID = group.FromItemID;
- if (newAttachmentItemID == UUID.Zero)
- newAttachmentItemID = AddSceneObjectAsNewAttachmentInInv(sp, group).ID;
- ShowAttachInUserInventory(sp, attachmentPt, newAttachmentItemID, group, append);
- }
- public ISceneEntity RezSingleAttachmentFromInventory(IScenePresence sp, UUID itemID, uint AttachmentPt)
- {
- return RezSingleAttachmentFromInventory(sp, itemID, AttachmentPt, null);
- }
- public ISceneEntity RezSingleAttachmentFromInventory(IScenePresence sp, UUID itemID, uint AttachmentPt, XmlDocument doc)
- {
- if (!Enabled)
- return null;
- if (DebugLevel > 0)
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: RezSingleAttachmentFromInventory to point {0} from item {1} for {2} in {3}",
- (AttachmentPoint)AttachmentPt, itemID, sp.Name, m_scene.Name);
-
-
-
-
- bool alreadyOn = false;
- List<AvatarAttachment> existingAttachments = sp.Appearance.GetAttachments();
- foreach (AvatarAttachment existingAttachment in existingAttachments)
- {
- if (existingAttachment.ItemID == itemID)
- {
- alreadyOn = true;
- break;
- }
- }
- if (alreadyOn)
- {
- if (DebugLevel > 0)
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: Ignoring request by {0} to wear item {1} at {2} since it is already worn",
- sp.Name, itemID, AttachmentPt);
- return null;
- }
- bool append = (AttachmentPt & 0x80) != 0;
- AttachmentPt &= 0x7f;
- return RezSingleAttachmentFromInventoryInternal(sp, itemID, UUID.Zero, AttachmentPt, append, doc);
- }
- public void RezMultipleAttachmentsFromInventory(IScenePresence sp, List<KeyValuePair<UUID, uint>> rezlist)
- {
- if (!Enabled)
- return;
- if (DebugLevel > 0)
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: Rezzing {0} attachments from inventory for {1} in {2}",
- rezlist.Count, sp.Name, m_scene.Name);
- foreach (KeyValuePair<UUID, uint> rez in rezlist)
- {
- RezSingleAttachmentFromInventory(sp, rez.Key, rez.Value);
- }
- }
- public void DetachSingleAttachmentToGround(IScenePresence sp, uint soLocalId)
- {
- Vector3 pos = new Vector3(2.5f, 0f, 0f);
- pos *= ((ScenePresence)sp).Rotation;
- pos += sp.AbsolutePosition;
- DetachSingleAttachmentToGround(sp, soLocalId, pos, Quaternion.Identity);
- }
- public void DetachSingleAttachmentToGround(IScenePresence sp, uint soLocalId, Vector3 absolutePos, Quaternion absoluteRot)
- {
- if (!Enabled)
- return;
- if (DebugLevel > 0)
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: DetachSingleAttachmentToGround() for {0}, object {1}",
- sp.UUID, soLocalId);
- SceneObjectGroup so = m_scene.GetGroupByPrim(soLocalId);
- if (so == null)
- return;
- if (so.AttachedAvatar != sp.UUID)
- return;
- UUID inventoryID = so.FromItemID;
-
- if (inventoryID == UUID.Zero)
- return;
- if (DebugLevel > 0)
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: In DetachSingleAttachmentToGround(), object is {0} {1}, associated item is {2}",
- so.Name, so.LocalId, inventoryID);
- lock (sp.AttachmentsSyncLock)
- {
- if (!m_scene.Permissions.CanRezObject(
- so.PrimCount, sp.UUID, sp.AbsolutePosition))
- return;
- bool changed = false;
- if (inventoryID != UUID.Zero)
- changed = sp.Appearance.DetachAttachment(inventoryID);
- if (changed && m_scene.AvatarFactory != null)
- m_scene.AvatarFactory.QueueAppearanceSave(sp.UUID);
- so.RootPart.Shape.LastAttachPoint = (byte)so.AttachmentPoint;
- sp.RemoveAttachment(so);
- so.FromItemID = UUID.Zero;
- so.AttachedAvatar = UUID.Zero;
- so.ClearPartAttachmentData();
- SceneObjectPart rootPart = so.RootPart;
- rootPart.SetParentLocalId(0);
- so.AbsolutePosition = absolutePos;
- if (absoluteRot != Quaternion.Identity)
- {
- so.UpdateGroupRotationR(absoluteRot);
- }
- rootPart.RemFlag(PrimFlags.TemporaryOnRez);
- so.ApplyPhysics();
- rootPart.Rezzed = DateTime.Now;
- so.AttachToBackup();
- m_scene.EventManager.TriggerParcelPrimCountTainted();
- rootPart.ClearUndoState();
- List<UUID> uuids = new List<UUID>();
- uuids.Add(inventoryID);
- m_scene.InventoryService.DeleteItems(sp.UUID, uuids);
- sp.ControllingClient.SendRemoveInventoryItem(inventoryID);
- }
- m_scene.EventManager.TriggerOnAttach(so.LocalId, so.UUID, UUID.Zero);
-
- so.RemoveScriptsPermissions(4 | 2048);
- so.ResumeScripts();
- so.HasGroupChanged = true;
- so.RootPart.ScheduleFullUpdate();
- so.ScheduleGroupForTerseUpdate();
- }
- public void DetachSingleAttachmentToInv(IScenePresence sp, SceneObjectGroup so)
- {
- 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;
- }
- so.RemoveScriptsPermissions(4 | 2048);
-
-
- if (so.FromItemID == UUID.Zero)
- {
- PrepareScriptInstanceForSave(so, true);
- lock (sp.AttachmentsSyncLock)
- {
- bool changed = sp.Appearance.DetachAttachment(so.FromItemID);
- if (changed && m_scene.AvatarFactory != null)
- m_scene.AvatarFactory.QueueAppearanceSave(sp.UUID);
- sp.RemoveAttachment(so);
- }
- m_scene.DeleteSceneObject(so, false, false);
- so.RemoveScriptInstances(true);
- so.Dispose();
- return;
- }
- if (DebugLevel > 0)
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: Detaching object {0} {1} (FromItemID {2}) for {3} in {4}",
- so.Name, so.LocalId, so.FromItemID, sp.Name, m_scene.Name);
-
-
-
-
-
- string scriptedState = PrepareScriptInstanceForSave(so, true);
- lock (sp.AttachmentsSyncLock)
- {
-
- bool changed = sp.Appearance.DetachAttachment(so.FromItemID);
- if (changed && m_scene.AvatarFactory != null)
- m_scene.AvatarFactory.QueueAppearanceSave(sp.UUID);
- sp.RemoveAttachment(so);
- UpdateDetachedObject(sp, so, scriptedState);
- }
- }
- public void UpdateAttachmentPosition(SceneObjectGroup sog, Vector3 pos)
- {
- if (!Enabled)
- return;
- sog.UpdateGroupPosition(pos);
- sog.HasGroupChanged = true;
- }
- #endregion
- #region AttachmentModule private methods
-
-
-
- public void SubscribeToClientEvents(IClientAPI client)
- {
- client.OnRezSingleAttachmentFromInv += Client_OnRezSingleAttachmentFromInv;
- client.OnRezMultipleAttachmentsFromInv += Client_OnRezMultipleAttachmentsFromInv;
- client.OnObjectAttach += Client_OnObjectAttach;
- client.OnObjectDetach += Client_OnObjectDetach;
- client.OnDetachAttachmentIntoInv += Client_OnDetachAttachmentIntoInv;
- client.OnObjectDrop += Client_OnObjectDrop;
- }
-
-
-
- public void UnsubscribeFromClientEvents(IClientAPI client)
- {
- client.OnRezSingleAttachmentFromInv -= Client_OnRezSingleAttachmentFromInv;
- client.OnRezMultipleAttachmentsFromInv -= Client_OnRezMultipleAttachmentsFromInv;
- client.OnObjectAttach -= Client_OnObjectAttach;
- client.OnObjectDetach -= Client_OnObjectDetach;
- client.OnDetachAttachmentIntoInv -= Client_OnDetachAttachmentIntoInv;
- client.OnObjectDrop -= Client_OnObjectDrop;
- }
-
-
-
-
-
-
-
-
-
-
- private void UpdateKnownItem(IScenePresence sp, SceneObjectGroup grp, string scriptedState)
- {
- if (grp.FromItemID == UUID.Zero)
- {
-
- grp.HasGroupChanged = false;
- return;
- }
- if(sp.IsNPC)
- return;
- if (grp.HasGroupChanged)
- {
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: Updating asset for attachment {0}, attachpoint {1}",
- grp.UUID, grp.AttachmentPoint);
- string sceneObjectXml = SceneObjectSerializer.ToOriginalXmlFormat(grp, scriptedState);
- InventoryItemBase item = m_scene.InventoryService.GetItem(sp.UUID, grp.FromItemID);
- if (item != null)
- {
-
- item.Flags &= ~(uint)(InventoryItemFlags.ObjectSlamPerm | InventoryItemFlags.ObjectOverwriteBase |
- InventoryItemFlags.ObjectOverwriteOwner | InventoryItemFlags.ObjectOverwriteGroup |
- InventoryItemFlags.ObjectOverwriteEveryone | InventoryItemFlags.ObjectOverwriteNextOwner);
- uint permsBase = (uint)(PermissionMask.Copy | PermissionMask.Transfer |
- PermissionMask.Modify | PermissionMask.Move |
- PermissionMask.Export | PermissionMask.FoldedMask);
-
- permsBase &= grp.CurrentAndFoldedNextPermissions();
- permsBase |= (uint)PermissionMask.Move;
- item.BasePermissions = permsBase;
- item.CurrentPermissions = permsBase;
- item.NextPermissions = permsBase & grp.RootPart.NextOwnerMask | (uint)PermissionMask.Move;
- item.EveryOnePermissions = permsBase & grp.RootPart.EveryoneMask;
- item.GroupPermissions = permsBase & grp.RootPart.GroupMask;
- item.CurrentPermissions &=
- ((uint)PermissionMask.Copy |
- (uint)PermissionMask.Transfer |
- (uint)PermissionMask.Modify |
- (uint)PermissionMask.Move |
- (uint)PermissionMask.Export |
- (uint)PermissionMask.FoldedMask);
- string name = grp.RootPart.Name;
- string desc = grp.RootPart.Description;
- AssetBase asset = m_scene.CreateAsset(
- name, desc,
- (sbyte)AssetType.Object,
- Utils.StringToBytes(sceneObjectXml),
- sp.UUID);
- item.Name = name;
- item.Description = desc;
- item.AssetID = asset.FullID;
- item.AssetType = (int)AssetType.Object;
- item.InvType = (int)InventoryType.Object;
- if (m_invAccessModule != null)
- m_invAccessModule.UpdateInventoryItemAsset(sp.UUID, item, asset);
- if (sp.ControllingClient != null)
- sp.ControllingClient.SendInventoryItemCreateUpdate(item, 0);
- }
- grp.HasGroupChanged = false;
- }
- else if (DebugLevel > 0)
- {
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: Don't need to update asset for unchanged attachment {0}, attachpoint {1}",
- grp.UUID, grp.AttachmentPoint);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
- private void AttachToAgent(
- IScenePresence sp, SceneObjectGroup so, uint attachmentpoint, Vector3 attachOffset, bool silent)
- {
- if (DebugLevel > 0)
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: Adding attachment {0} to avatar {1} at pt {2} pos {3} {4} in {5}",
- so.Name, sp.Name, attachmentpoint, attachOffset, so.RootPart.AttachedPos, m_scene.Name);
-
- m_scene.DeleteFromStorage(so.UUID);
- m_scene.EventManager.TriggerParcelPrimCountTainted();
- foreach (SceneObjectPart part in so.Parts)
- {
- if (part.PhysActor != null)
- {
- part.RemoveFromPhysics();
- }
- }
- so.RootPart.SetParentLocalId(sp.LocalId);
- so.AttachedAvatar = sp.UUID;
- so.AttachmentPoint = attachmentpoint;
- so.RootPart.AttachedPos = attachOffset;
- so.AbsolutePosition = attachOffset;
- so.IsAttachment = true;
- sp.AddAttachment(so);
- if (!silent)
- {
- if (so.HasPrivateAttachmentPoint)
- {
- if (DebugLevel > 0)
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: Killing private HUD {0} for avatars other than {1} at attachment point {2}",
- so.Name, sp.Name, so.AttachmentPoint);
-
-
- m_scene.ForEachClient(
- client =>
- { if (client.AgentId != so.AttachedAvatar)
- client.SendKillObject(new List<uint>() { so.LocalId });
- });
- }
-
-
- so.IsSelected = false;
- so.ScheduleGroupForFullAnimUpdate();
- }
-
-
- so.RootPart.RemFlag(PrimFlags.TemporaryOnRez);
- }
-
-
-
-
-
-
- private InventoryItemBase AddSceneObjectAsNewAttachmentInInv(IScenePresence sp, SceneObjectGroup grp)
- {
- if (m_invAccessModule == null)
- return null;
- if (DebugLevel > 0)
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: Called AddSceneObjectAsAttachment for object {0} {1} for {2}",
- grp.Name, grp.LocalId, sp.Name);
- InventoryItemBase newItem
- = m_invAccessModule.CopyToInventory(
- DeRezAction.TakeCopy,
- m_scene.InventoryService.GetFolderForType(sp.UUID, FolderType.Object).ID,
- new List<SceneObjectGroup> { grp },
- sp.ControllingClient, true)[0];
-
- grp.FromItemID = newItem.ID;
- return newItem;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private string PrepareScriptInstanceForSave(SceneObjectGroup grp, bool fireDetachEvent)
- {
- if (fireDetachEvent)
- {
- m_scene.EventManager.TriggerOnAttach(grp.LocalId, grp.FromItemID, UUID.Zero);
-
- Thread.Sleep(30);
- }
- using (StringWriter sw = new StringWriter())
- {
- using (XmlTextWriter writer = new XmlTextWriter(sw))
- {
- grp.SaveScriptedState(writer);
- }
- return sw.ToString();
- }
- }
- private void UpdateDetachedObject(IScenePresence sp, SceneObjectGroup so, string scriptedState)
- {
-
-
-
-
-
-
- bool saveChanged
- = sp.PresenceType != PresenceType.Npc
- && (m_scene.UserManagementModule == null
- || m_scene.UserManagementModule.IsLocalGridUser(sp.UUID));
-
-
-
- m_scene.DeleteSceneObject(so, false, false);
-
- so.AttachedAvatar = UUID.Zero;
- so.RootPart.SetParentLocalId(0);
- so.IsAttachment = false;
- if (saveChanged)
- {
-
-
- so.ForEachPart(x => { x.GroupPosition = so.RootPart.AttachedPos; });
- UpdateKnownItem(sp, so, scriptedState);
- }
-
- so.RemoveScriptInstances(true);
- so.Dispose();
- }
- protected SceneObjectGroup RezSingleAttachmentFromInventoryInternal(
- IScenePresence sp, UUID itemID, UUID assetID, uint attachmentPt, bool append, XmlDocument doc)
- {
- if (m_invAccessModule == null)
- return null;
- SceneObjectGroup objatt;
- UUID rezGroupID;
-
-
- try
- {
- rezGroupID = sp.ControllingClient.ActiveGroupId;
- }
- catch
- {
- return null;
- }
- if (itemID != UUID.Zero)
- objatt = m_invAccessModule.RezObject(sp.ControllingClient,
- itemID, rezGroupID, Vector3.Zero, Vector3.Zero, UUID.Zero, (byte)1, true,
- false, false, sp.UUID, true);
- else
- objatt = m_invAccessModule.RezObject(sp.ControllingClient,
- null, rezGroupID, assetID, Vector3.Zero, Vector3.Zero, UUID.Zero, (byte)1, true,
- false, false, sp.UUID, true);
- if (objatt == null)
- {
- m_log.WarnFormat(
- "[ATTACHMENTS MODULE]: did not attached item {0} to avatar {1} at point {2}",
- itemID, sp.Name, attachmentPt);
- return null;
- }
- else if (itemID == UUID.Zero)
- {
-
-
-
- objatt.FromItemID = UUID.Random();
- }
- if (DebugLevel > 0)
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: Rezzed single object {0} with {1} prims for attachment to {2} on point {3} in {4}",
- objatt.Name, objatt.PrimCount, sp.Name, attachmentPt, m_scene.Name);
-
- objatt.HasGroupChanged = false;
- Vector3 lastPos = objatt.RootPart.OffsetPosition;
- Vector3 lastAttPos = objatt.RootPart.AttachedPos;
- uint lastattPoint = objatt.AttachmentPoint;
- bool doneAttach = false;
-
-
-
-
-
-
- try
- {
- if (doc != null)
- {
- objatt.LoadScriptState(doc);
- objatt.ResetOwnerChangeFlag();
- }
- doneAttach = AttachObjectInternal(sp, objatt, attachmentPt, false, true, true, append);
- }
- catch (Exception e)
- {
- m_log.ErrorFormat(
- "[ATTACHMENTS MODULE]: Failed to attach {0} {1} for {2}, exception {3}{4}",
- objatt.Name, objatt.UUID, sp.Name, e.Message, e.StackTrace);
- doneAttach = false;
- }
- if(!doneAttach)
- {
-
- sp.RemoveAttachment(objatt);
- m_scene.DeleteSceneObject(objatt, false);
- return null;
- }
- if (lastattPoint != objatt.AttachmentPoint ||
- lastPos != objatt.RootPart.OffsetPosition ||
- lastAttPos != objatt.RootPart.AttachedPos)
- objatt.HasGroupChanged = true;
- return objatt;
- }
-
-
-
-
-
-
-
- private void ShowAttachInUserInventory(IScenePresence sp, uint AttachmentPt, UUID itemID, SceneObjectGroup att, bool append)
- {
- if (UUID.Zero == itemID)
- {
- m_log.Error("[ATTACHMENTS MODULE]: Unable to save attachment. Error inventory item ID.");
- return;
- }
- if (0 == AttachmentPt)
- {
- m_log.Error("[ATTACHMENTS MODULE]: Unable to save attachment. Error attachment point.");
- return;
- }
- InventoryItemBase item = m_scene.InventoryService.GetItem(sp.UUID, itemID);
- if (item == null)
- return;
- int attFlag = append ? 0x80 : 0;
- bool changed = sp.Appearance.SetAttachment((int)AttachmentPt | attFlag, itemID, item.AssetID);
- if (changed && m_scene.AvatarFactory != null)
- {
- if (DebugLevel > 0)
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: Queueing appearance save for {0}, attachment {1} point {2} in ShowAttachInUserInventory()",
- sp.Name, att.Name, AttachmentPt);
- m_scene.AvatarFactory.QueueAppearanceSave(sp.UUID);
- }
- }
- #endregion
- #region Client Event Handlers
- private ISceneEntity Client_OnRezSingleAttachmentFromInv(IClientAPI remoteClient, UUID itemID, uint AttachmentPt)
- {
- if (!Enabled)
- return null;
- if (DebugLevel > 0)
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: Rezzing attachment to point {0} from item {1} for {2}",
- (AttachmentPoint)AttachmentPt, itemID, remoteClient.Name);
- ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
- if (sp == null)
- {
- m_log.ErrorFormat(
- "[ATTACHMENTS MODULE]: Could not find presence for client {0} {1} in RezSingleAttachmentFromInventory()",
- remoteClient.Name, remoteClient.AgentId);
- return null;
- }
- return RezSingleAttachmentFromInventory(sp, itemID, AttachmentPt);
- }
- private void Client_OnRezMultipleAttachmentsFromInv(IClientAPI remoteClient, List<KeyValuePair<UUID, uint>> rezlist)
- {
- if (!Enabled)
- return;
- ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
- if (sp != null)
- RezMultipleAttachmentsFromInventory(sp, rezlist);
- else
- m_log.ErrorFormat(
- "[ATTACHMENTS MODULE]: Could not find presence for client {0} {1} in RezMultipleAttachmentsFromInventory()",
- remoteClient.Name, remoteClient.AgentId);
- }
- private void Client_OnObjectAttach(IClientAPI remoteClient, uint objectLocalID, uint AttachmentPt, bool silent)
- {
- if (DebugLevel > 0)
- m_log.DebugFormat(
- "[ATTACHMENTS MODULE]: Attaching object local id {0} to {1} point {2} from ground (silent = {3})",
- objectLocalID, remoteClient.Name, AttachmentPt, silent);
- if (!Enabled)
- return;
- try
- {
- ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
- if (sp == null)
- {
- m_log.ErrorFormat(
- "[ATTACHMENTS MODULE]: Could not find presence for client {0} {1}", remoteClient.Name, remoteClient.AgentId);
- return;
- }
-
- SceneObjectPart part = m_scene.GetSceneObjectPart(objectLocalID);
- if (part == null)
- return;
- SceneObjectGroup group = part.ParentGroup;
- if (!m_scene.Permissions.CanTakeObject(group, sp))
- {
- remoteClient.SendAgentAlertMessage(
- "You don't have sufficient permissions to attach this object", false);
- return;
- }
- bool append = (AttachmentPt & 0x80) != 0;
- AttachmentPt &= 0x7f;
-
- if (AttachObject(sp, group , AttachmentPt, false, true, append))
- {
- if (DebugLevel > 0)
- m_log.Debug(
- "[ATTACHMENTS MODULE]: Saving avatar attachment. AgentID: " + remoteClient.AgentId
- + ", AttachmentPoint: " + AttachmentPt);
-
- m_scene.AvatarFactory.QueueAppearanceSave(sp.UUID);
- }
- }
- catch (Exception e)
- {
- m_log.ErrorFormat("[ATTACHMENTS MODULE]: exception upon Attach Object {0}{1}", e.Message, e.StackTrace);
- }
- }
- private void Client_OnObjectDetach(uint objectLocalID, IClientAPI remoteClient)
- {
- if (!Enabled)
- return;
- ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
- SceneObjectGroup group = m_scene.GetGroupByPrim(objectLocalID);
- if (sp != null && group != null)
- DetachSingleAttachmentToInv(sp, group);
- }
- private void Client_OnDetachAttachmentIntoInv(UUID itemID, IClientAPI remoteClient)
- {
- if (!Enabled)
- return;
- ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
- if (sp != null)
- {
- List<SceneObjectGroup> attachments = sp.GetAttachments();
- foreach (SceneObjectGroup group in attachments)
- {
- if (group.FromItemID == itemID && group.FromItemID != UUID.Zero)
- {
- DetachSingleAttachmentToInv(sp, group);
- return;
- }
- }
- }
- }
- private void Client_OnObjectDrop(uint soLocalId, IClientAPI remoteClient)
- {
- if (!Enabled)
- return;
- ScenePresence sp = m_scene.GetScenePresence(remoteClient.AgentId);
- if (sp != null)
- DetachSingleAttachmentToGround(sp, soLocalId);
- }
- #endregion
- }
- }
|