SetWearable() public method

public SetWearable ( int wearableId, AvatarWearable wearable ) : void
wearableId int
wearable AvatarWearable
return void
        AvatarAppearance CopyWearablesAndAttachments (UUID destination, UUID source,
                                                             AvatarAppearance avatarAppearance,
                                                             InventoryFolderBase destinationFolder, UUID agentid,
                                                             OSDMap itemsMap,
                                                             out List<InventoryItemBase> items)
        {
            items = new List<InventoryItemBase> ();

            if (destinationFolder == null) {
                MainConsole.Instance.Error ("[Avatar Archiver]: Cannot locate folder(s) for copying wearables!");
                return avatarAppearance;
            }

            List<InventoryItemBase> litems = new List<InventoryItemBase> ();
            foreach (KeyValuePair<string, OSD> kvp in itemsMap) {
                InventoryItemBase item = new InventoryItemBase ();
                item.FromOSD ((OSDMap)kvp.Value);
                MainConsole.Instance.Info ("[Avatar Archiver]: Loading item " + item.ID);
                litems.Add (item);
            }

            // Wearables
            AvatarWearable [] wearables = avatarAppearance.Wearables;
            MainConsole.Instance.InfoFormat ("[Avatar Archiver] Adding {0} wearables", wearables.Length);

            for (int i = 0; i < wearables.Length; i++) {
                AvatarWearable wearable = wearables [i];
                for (int ii = 0; ii < wearable.Count; ii++) {
                    if (wearable [ii].ItemID != UUID.Zero) {
                        // Get inventory item and copy it
                        InventoryItemBase item = inventoryService.GetItem (UUID.Zero, wearable [ii].ItemID);

                        if (item == null) {
                            //Attempt to get from the map if it doesn't already exist on the grid
                            item = litems.First ((itm) => itm.ID == wearable [ii].ItemID);
                        }
                        if (item != null) {
                            InventoryItemBase destinationItem = inventoryService.InnerGiveInventoryItem (destination,
                                                                                                        destination,
                                                                                                        item,
                                                                                                        destinationFolder
                                                                                                            .ID,
                                                                                                        false, false);
                            items.Add (destinationItem);
                            MainConsole.Instance.DebugFormat ("[Avatar Archiver]: Added item {0} to folder {1}",
                                                             destinationItem.ID, destinationFolder.ID);

                            // Wear item
                            AvatarWearable newWearable = new AvatarWearable ();
                            newWearable.Wear (destinationItem.ID, destinationItem.AssetID);
                            avatarAppearance.SetWearable (i, newWearable);
                        } else {
                            MainConsole.Instance.WarnFormat ("[Avatar Archiver]: Unable to transfer {0} to folder {1}",
                                                            wearable [ii].ItemID, destinationFolder.ID);
                        }
                    }
                }
            }

            // Attachments
            List<AvatarAttachment> attachments = avatarAppearance.GetAttachments ();
            MainConsole.Instance.InfoFormat ("[Avatar Archiver] Adding {0} attachments", attachments.Count);

            foreach (AvatarAttachment attachment in attachments) {
                int attachpoint = attachment.AttachPoint;
                UUID itemID = attachment.ItemID;

                if (itemID != UUID.Zero) {

                    // Get inventory item and copy it
                    InventoryItemBase item = inventoryService.GetItem (UUID.Zero, itemID);

                    if (item == null) {
                        //Attempt to get from the map if it doesn't already exist on the grid
                        item = litems.First ((itm) => itm.ID == itemID);
                    }

                    if (item != null) {
                        InventoryItemBase destinationItem = inventoryService.InnerGiveInventoryItem (destination,
                                                                                                    destination, item,
                                                                                                    destinationFolder.ID,
                                                                                                    false, false);
                        items.Add (destinationItem);
                        MainConsole.Instance.DebugFormat ("[Avatar Archiver]: Added item {0} to folder {1}", destinationItem.ID,
                                                         destinationFolder.ID);

                        // Attach item
                        avatarAppearance.SetAttachment (attachpoint, destinationItem.ID, destinationItem.AssetID);
                        MainConsole.Instance.DebugFormat ("[Avatar Archiver]: Attached {0}", destinationItem.ID);
                    } else {
                        MainConsole.Instance.WarnFormat ("[Avatar Archiver]: Error transferring {0} to folder {1}", itemID,
                                                        destinationFolder.ID);
                    }
                }
            }
            return avatarAppearance;
        }
        public LoginResponse Login(UUID AgentID, string Name, string authType, string passwd, string startLocation,
                                   string clientVersion, string channel, string mac, string id0, IPEndPoint clientIP,
                                   Hashtable requestData)
        {
            LoginResponse response;
            UUID session = UUID.Random();
            UUID secureSession = UUID.Zero;

            // TODO: Make this check better
            //
            // Some TPV's now send their name in Channel instead of clientVersion
            // while others send a Channel and a clientVersion.

            string realViewer;

            if (channel != "")
            {
                realViewer = channel + " " + clientVersion;
            }
            else
            {
                realViewer = clientVersion;
            }

            MainConsole.Instance.InfoFormat(
                "[LLOGIN SERVICE]: Login request for {0} from {1} with user agent {2} starting in {3}",
                Name, clientIP.Address, realViewer, startLocation);

            UserAccount account = AgentID != UUID.Zero
                                      ? m_UserAccountService.GetUserAccount(null, AgentID)
                                      : m_UserAccountService.GetUserAccount(null, Name);
            if (account == null && m_AllowAnonymousLogin)
            {
                m_UserAccountService.CreateUser(Name, passwd.StartsWith("$1$") ? passwd.Remove(0, 3) : passwd, "");
                account = m_UserAccountService.GetUserAccount(null, Name);
            }
            if (account == null)
            {
                MainConsole.Instance.InfoFormat("[LLOGIN SERVICE]: Login failed for user {0}: no account found", Name);
                return LLFailedLoginResponse.AccountProblem;
            }

            if (account.UserLevel < 0) //No allowing anyone less than 0
            {
                MainConsole.Instance.InfoFormat(
                    "[LLOGIN SERVICE]: Login failed for user {0}, reason: user is banned",
                    account.Name);
                return LLFailedLoginResponse.PermanentBannedProblem;
            }

            if (account.UserLevel < m_MinLoginLevel)
            {
                MainConsole.Instance.InfoFormat(
                    "[LLOGIN SERVICE]: Login failed for user {1}, reason: login is blocked for user level {0}",
                    account.UserLevel, account.Name);
                return LLFailedLoginResponse.LoginBlockedProblem;
            }

            IAgentInfo agent = null;
            IAgentConnector agentData = Framework.Utilities.DataManager.RequestPlugin<IAgentConnector>();
            if (agentData != null)
                agent = agentData.GetAgent(account.PrincipalID);
            if (agent == null)
            {
                agentData.CreateNewAgent(account.PrincipalID);
                agent = agentData.GetAgent(account.PrincipalID);
            }

            requestData["ip"] = clientIP.ToString();
            foreach (ILoginModule module in LoginModules)
            {
                object data;
                if ((response = module.Login(requestData, account, agent, authType, passwd, out data)) != null)
                {
                    MainConsole.Instance.InfoFormat(
                        "[LLOGIN SERVICE]: Login failed for user {1}, reason: {0}",
                        (data != null ? data.ToString() : (response is LLFailedLoginResponse) ? (response as LLFailedLoginResponse).Value : "Unknown"), account.Name);
                    return response;
                }
                if (data != null)
                    secureSession = (UUID) data; //TODO: NEED TO FIND BETTER WAY TO GET THIS DATA
            }

            try
            {
                string DisplayName = account.Name;
                AvatarAppearance avappearance = null;
                IProfileConnector profileData = Framework.Utilities.DataManager.RequestPlugin<IProfileConnector>();

                //
                // Get the user's inventory
                //
                if (m_RequireInventory && m_InventoryService == null)
                {
                    MainConsole.Instance.WarnFormat(
                        "[LLOGIN SERVICE]: Login failed for user {0}, reason: inventory service not set up",
                        account.Name);
                    return LLFailedLoginResponse.InventoryProblem;
                }
                List<InventoryFolderBase> inventorySkel = m_InventoryService.GetInventorySkeleton(account.PrincipalID);
                if (m_RequireInventory && ((inventorySkel == null) || (inventorySkel.Count == 0)))
                {
                    List<InventoryItemBase> defaultItems;
                    m_InventoryService.CreateUserInventory(account.PrincipalID, m_DefaultUserAvatarArchive == "",
                                                           out defaultItems);
                    inventorySkel = m_InventoryService.GetInventorySkeleton(account.PrincipalID);
                    if (m_RequireInventory && ((inventorySkel == null) || (inventorySkel.Count == 0)))
                    {
                        MainConsole.Instance.InfoFormat(
                            "[LLOGIN SERVICE]: Login failed for user {0}, reason: unable to retrieve user inventory",
                            account.Name);
                        return LLFailedLoginResponse.InventoryProblem;
                    }
                    if (defaultItems.Count > 0)
                    {
                        avappearance = new AvatarAppearance(account.PrincipalID);
                        avappearance.SetWearable((int)WearableType.Shape,
                                                 new AvatarWearable(defaultItems[0].ID, defaultItems[0].AssetID));
                        avappearance.SetWearable((int)WearableType.Skin,
                                                 new AvatarWearable(defaultItems[1].ID, defaultItems[1].AssetID));
                        avappearance.SetWearable((int)WearableType.Hair,
                                                 new AvatarWearable(defaultItems[2].ID, defaultItems[2].AssetID));
                        avappearance.SetWearable((int)WearableType.Eyes,
                                                 new AvatarWearable(defaultItems[3].ID, defaultItems[3].AssetID));
                        avappearance.SetWearable((int)WearableType.Shirt,
                                                 new AvatarWearable(defaultItems[4].ID, defaultItems[4].AssetID));
                        avappearance.SetWearable((int)WearableType.Pants,
                                                 new AvatarWearable(defaultItems[5].ID, defaultItems[5].AssetID));
                        m_AvatarService.SetAppearance(account.PrincipalID, avappearance);
                    }
                }

                if (profileData != null)
                {
                    IUserProfileInfo UPI = profileData.GetUserProfile(account.PrincipalID);
                    if (UPI == null)
                    {
                        profileData.CreateNewProfile(account.PrincipalID);
                        UPI = profileData.GetUserProfile(account.PrincipalID);
                        UPI.AArchiveName = m_DefaultUserAvatarArchive;
                        UPI.IsNewUser = true;
                        //profileData.UpdateUserProfile(UPI); //It gets hit later by the next thing
                    }
                    //Find which is set, if any
                    string archiveName = (UPI.AArchiveName != "" && UPI.AArchiveName != " ")
                                             ? UPI.AArchiveName
                                             : m_DefaultUserAvatarArchive;
                    if (UPI.IsNewUser && archiveName != "")
                    {
                        AvatarArchive arch = m_ArchiveService.LoadAvatarArchive(archiveName, account.PrincipalID);
                        UPI.AArchiveName = "";
                        if (arch != null)
                        {
                            avappearance = arch.Appearance;
                            m_AvatarService.SetAppearance(account.PrincipalID, avappearance);
                            //Must reload this, as we created a new folder
                            inventorySkel = m_InventoryService.GetInventorySkeleton(account.PrincipalID);
                        }
                    }
                    if (UPI.IsNewUser)
                    {
                        UPI.IsNewUser = false;
                        profileData.UpdateUserProfile(UPI);
                    }
                    if (UPI.DisplayName != "")
                        DisplayName = UPI.DisplayName;
                }

                // Get active gestures
                List<InventoryItemBase> gestures = m_InventoryService.GetActiveGestures(account.PrincipalID);
                //MainConsole.Instance.DebugFormat("[LLOGIN SERVICE]: {0} active gestures", gestures.Count);

                //Now get the logged in status, then below make sure to kill the previous agent if we crashed before
                UserInfo guinfo = m_agentInfoService.GetUserInfo(account.PrincipalID.ToString());
                //
                // Clear out any existing CAPS the user may have
                //
                if (m_CapsService != null)
                {
                    IAgentProcessing agentProcessor = m_registry.RequestModuleInterface<IAgentProcessing>();
                    if (agentProcessor != null)
                    {
                        IClientCapsService clientCaps = m_CapsService.GetClientCapsService(account.PrincipalID);
                        if (clientCaps != null)
                        {
                            IRegionClientCapsService rootRegionCaps = clientCaps.GetRootCapsService();
                            if (rootRegionCaps != null)
                                agentProcessor.LogoutAgent(rootRegionCaps, !m_AllowDuplicateLogin);
                        }
                    }
                    else
                        m_CapsService.RemoveCAPS(account.PrincipalID);
                }

                //
                // Change Online status and get the home region
                //
                GridRegion home = null;
                if (guinfo != null && (guinfo.HomeRegionID != UUID.Zero) && m_GridService != null)
                    home = m_GridService.GetRegionByUUID(account.AllScopeIDs, guinfo.HomeRegionID);

                if (guinfo == null || guinfo.HomeRegionID == UUID.Zero) //Give them a default home and last
                {
                    bool positionSet = false;
                    if (guinfo == null)
                        guinfo = new UserInfo {UserID = account.PrincipalID.ToString()};
                    GridRegion DefaultRegion = null, FallbackRegion = null, SafeRegion = null;
                    if (m_GridService != null)
                    {
                        if (m_DefaultHomeRegion != "")
                        {
                            DefaultRegion = m_GridService.GetRegionByName(account.AllScopeIDs, m_DefaultHomeRegion);
                            if (DefaultRegion != null)
                                guinfo.HomeRegionID = guinfo.CurrentRegionID = DefaultRegion.RegionID;
                            guinfo.HomePosition = guinfo.CurrentPosition = m_DefaultHomeRegionPos;
                            positionSet = true;
                        }
                        if (guinfo.HomeRegionID == UUID.Zero)
                        {
                            List<GridRegion> DefaultRegions = m_GridService.GetDefaultRegions(account.AllScopeIDs);
                            DefaultRegion = DefaultRegions.Count == 0 ? null : DefaultRegions[0];

                            if (DefaultRegion != null)
                                guinfo.HomeRegionID = guinfo.CurrentRegionID = DefaultRegion.RegionID;

                            if (guinfo.HomeRegionID == UUID.Zero)
                            {
                                List<GridRegion> Fallback = m_GridService.GetFallbackRegions(account.AllScopeIDs, 0, 0);
                                FallbackRegion = Fallback.Count == 0 ? null : Fallback[0];

                                if (FallbackRegion != null)
                                    guinfo.HomeRegionID = guinfo.CurrentRegionID = FallbackRegion.RegionID;

                                if (guinfo.HomeRegionID == UUID.Zero)
                                {
                                    List<GridRegion> Safe = m_GridService.GetSafeRegions(account.AllScopeIDs, 0, 0);
                                    SafeRegion = Safe.Count == 0 ? null : Safe[0];

                                    if (SafeRegion != null)
                                        guinfo.HomeRegionID = guinfo.CurrentRegionID = SafeRegion.RegionID;
                                }
                            }
                        }
                    }

                    if(!positionSet)
                        guinfo.CurrentPosition = guinfo.HomePosition = new Vector3(128, 128, 25);
                    guinfo.HomeLookAt = guinfo.CurrentLookAt = new Vector3(0, 0, 0);

                    m_agentInfoService.SetHomePosition(guinfo.UserID, guinfo.HomeRegionID, guinfo.HomePosition,
                                                       guinfo.HomeLookAt);

                    MainConsole.Instance.Info("[LLLoginService]: User did not have a home, set to " +
                                              (guinfo.HomeRegionID == UUID.Zero ? "(no region found)" : guinfo.HomeRegionID.ToString()));
                }

                //
                // Find the destination region/grid
                //
                string where = string.Empty;
                Vector3 position = Vector3.Zero;
                Vector3 lookAt = Vector3.Zero;
                TeleportFlags tpFlags = TeleportFlags.ViaLogin;
                GridRegion destination = FindDestination(account, guinfo, session, startLocation, home, out tpFlags,
                                                         out where, out position, out lookAt);
                if (destination == null)
                {
                    MainConsole.Instance.InfoFormat(
                        "[LLOGIN SERVICE]: Login failed for user {0}, reason: destination not found", account.Name);
                    return LLFailedLoginResponse.DeadRegionProblem;
                }

                #region Appearance

                //
                // Get the avatar
                //
                if (m_AvatarService != null)
                {
                    bool loadedArchive;
                    avappearance = m_AvatarService.GetAndEnsureAppearance(account.PrincipalID, m_DefaultUserAvatarArchive, out loadedArchive);
                    if (loadedArchive)
                        //Must reload this, as we created a new folder
                        inventorySkel = m_InventoryService.GetInventorySkeleton(account.PrincipalID);
                }
                else
                    avappearance = new AvatarAppearance(account.PrincipalID);

                if ((m_forceUserToWearFolderName != "") && (m_forceUserToWearFolderOwnerUUID.Length == 36))
                {
                    UUID userThatOwnersFolder;
                    if (UUID.TryParse(m_forceUserToWearFolderOwnerUUID, out userThatOwnersFolder))
                    {
                        avappearance = WearFolder(avappearance, account.PrincipalID, userThatOwnersFolder);
                    }
                }

                //Makes sure that all links are properly placed in the current outfit folder for v2 viewers
                FixCurrentOutFitFolder(account.PrincipalID, ref avappearance);

                #endregion

                List<UUID> friendsToInform = new List<UUID>();
                if (m_FriendsService != null)
                    friendsToInform = m_FriendsService.GetFriendOnlineStatuses(account.PrincipalID, true);

                //
                // Instantiate/get the simulation interface and launch an agent at the destination
                //
                string reason = "", seedCap = "";
                AgentCircuitData aCircuit = LaunchAgentAtGrid(destination, tpFlags, account, session,
                                                              secureSession, position, where,
                                                              clientIP, friendsToInform, out where, out reason, out seedCap,
                                                              out destination);

                if (aCircuit == null)
                {
                    MainConsole.Instance.InfoFormat("[LLOGIN SERVICE]: Login failed for user {1}, reason: {0}", reason,
                                                    account.Name);
                    return new LLFailedLoginResponse(LoginResponseEnum.InternalError, reason, false);
                }

                // Get Friends list
                List<FriendInfo> friendsList = new List<FriendInfo>();
                if (m_FriendsService != null)
                    friendsList = m_FriendsService.GetFriends(account.PrincipalID);

                //Set them as logged in now, they are ready, and fire the logged in event now, as we're all done
                m_agentInfoService.SetLastPosition(account.PrincipalID.ToString(), destination.RegionID, position,
                                                   lookAt, destination.ServerURI);
                m_agentInfoService.SetLoggedIn(account.PrincipalID.ToString(), true, destination.RegionID,
                                               destination.ServerURI);
                m_agentInfoService.FireUserStatusChangeEvent(account.PrincipalID.ToString(), true, destination.RegionID);

                //
                // Finally, fill out the response and return it
                //
                string MaturityRating = "A";
                string MaxMaturity = "A";
                if (agent != null)
                {
                    MaturityRating = agent.MaturityRating == 0
                                         ? "P"
                                         : agent.MaturityRating == 1 ? "M" : "A";
                    MaxMaturity = agent.MaxMaturity == 0
                                      ? "P"
                                      : agent.MaxMaturity == 1 ? "M" : "A";
                }

                ArrayList eventNotifications = new ArrayList();
                BuildEventNotifications(account.PrincipalID, ref eventNotifications);

                if (m_FriendsService != null)
                    m_FriendsService.SendFriendOnlineStatuses(account.PrincipalID, true);

                response = new LLLoginResponse(account, aCircuit, guinfo, destination, inventorySkel,
                                               friendsList.ToArray(), m_InventoryService, m_LibraryService,
                                               where, startLocation, position, lookAt, gestures, home, clientIP,
                                               MaxMaturity, MaturityRating,
                                               eventCategories, eventNotifications, classifiedCategories, seedCap,
                                               m_config, DisplayName, avappearance.Serial.ToString(), m_registry.RequestModuleInterface<IGridInfo>());

                MainConsole.Instance.InfoFormat(
                    "[LLOGIN SERVICE]: All clear. Sending login response to client to login to region " +
                    destination.RegionName + ", tried to login to " + startLocation + " at " + position + ".");

                return response;
            }
            catch (Exception e)
            {
                MainConsole.Instance.WarnFormat("[LLOGIN SERVICE]: Exception processing login for {0} : {1}", Name, e);
                if (account != null)
                {
                    //Revert their logged in status if we got that far
                    m_agentInfoService.SetLoggedIn(account.PrincipalID.ToString(), false, UUID.Zero, "");
                }
                return LLFailedLoginResponse.InternalError;
            }
        }