public ViewerAgentAccessor LoginAgent(
            ScriptInstance instance,
            int circuitCode,
            LSLKey regionId,
            LSLKey agentId,
            LSLKey sessionId,
            LSLKey secureSessionId,
            string viewerChannel,
            string viewerVersion,
            string id0,
            string mac,
            int teleportFlags,
            Vector3 position,
            Vector3 lookAt)
        {
            lock (instance)
            {
                string      externalHostName = m_CapsRedirector.ExternalHostName;
                IPAddress[] addresses        = DnsNameCache.GetHostAddresses(externalHostName);

                string clientIP = string.Empty;

                if (addresses.Length == 0)
                {
                    m_Log.InfoFormat("ExternalHostName \"{0}\" does not resolve", externalHostName);
                    return(new ViewerAgentAccessor());
                }

                foreach (IPAddress addr in addresses)
                {
                    if (addr.AddressFamily == AddressFamily.InterNetwork)
                    {
                        clientIP = addr.ToString();
                    }
                }

                if (string.IsNullOrEmpty(clientIP))
                {
                    m_Log.InfoFormat("ExternalHostName \"{0}\" does not resolve", externalHostName);
                    return(new ViewerAgentAccessor());
                }

                UUID           capsId   = UUID.Random;
                string         capsPath = m_CapsRedirector.ServerURI + "CAPS/" + capsId.ToString() + "0000/";
                SceneInterface scene;
                if (!m_Scenes.TryGetValue(regionId, out scene))
                {
                    return(new ViewerAgentAccessor());
                }
                var clientInfo = new ClientInfo
                {
                    ClientIP      = clientIP,
                    Channel       = viewerChannel,
                    ClientVersion = viewerVersion,
                    ID0           = id0,
                    Mac           = mac
                };
                UserAccount userAccount;
                if (!m_UserAccountService.TryGetValue(agentId, out userAccount))
                {
                    m_Log.InfoFormat("User account {0} does not exist", agentId.ToString());
                    return(new ViewerAgentAccessor());
                }

                var presenceInfo = m_UserSessionService.CreateSession(userAccount.Principal, clientIP, sessionId.AsUUID, secureSessionId.AsUUID);
                var serviceList  = new AgentServiceList
                {
                    m_AgentAssetService,
                    m_AgentInventoryService,
                    m_AgentFriendsService,
                    m_AgentUserAgentService,
                    new StandalonePresenceService(m_UserAccountService, userAccount.Principal, m_UserSessionService, presenceInfo.SessionID, new List <IUserSessionStatusHandler>()),
                    m_GridService
                };
                if (m_AgentProfileService != null)
                {
                    serviceList.Add(m_AgentProfileService);
                }
                if (m_OfflineIMService != null)
                {
                    serviceList.Add(m_OfflineIMService);
                }
                if (m_AgentExperienceService != null)
                {
                    serviceList.Add(m_AgentExperienceService);
                }
                if (m_AgentGroupsService != null)
                {
                    serviceList.Add(m_AgentGroupsService);
                }

                var agent = new ViewerAgent(
                    m_Scenes,
                    agentId,
                    userAccount.Principal.FirstName,
                    userAccount.Principal.LastName,
                    userAccount.Principal.HomeURI,
                    presenceInfo.SessionID,
                    presenceInfo.SecureSessionID,
                    clientInfo,
                    userAccount,
                    serviceList);

                try
                {
                    scene.DetermineInitialAgentLocation(agent, (TeleportFlags)teleportFlags, position, lookAt);
                }
                catch (Exception e)
                {
                    m_Log.InfoFormat("Failed to determine initial location for agent {0}: {1}: {2}", userAccount.Principal.FullName, e.GetType().FullName, e.Message);
                    return(new ViewerAgentAccessor());
                }

                var udpServer = (UDPCircuitsManager)scene.UDPServer;

                IPAddress ipAddr;
                if (!IPAddress.TryParse(clientInfo.ClientIP, out ipAddr))
                {
                    m_Log.InfoFormat("Invalid IP address for agent {0}", userAccount.Principal.FullName);
                    return(new ViewerAgentAccessor());
                }

                ViewerConnection vc = AddAgent(instance, userAccount.Principal.ID);

                var ep             = new IPEndPoint(ipAddr, vc.ClientUDP.LocalPort);
                var regionEndPoint = new IPEndPoint(ipAddr, (int)scene.RegionPort);
                var circuit        = new AgentCircuit(
                    m_Commands,
                    agent,
                    udpServer,
                    (uint)circuitCode,
                    m_CapsRedirector,
                    capsId,
                    agent.ServiceURLs,
                    string.Empty,
                    m_PacketHandlerPlugins,
                    ep)
                {
                    LastTeleportFlags   = (TeleportFlags)teleportFlags,
                    Agent               = agent,
                    AgentID             = userAccount.Principal.ID,
                    SessionID           = sessionId.AsUUID,
                    ForceUseCircuitCode = true
                };
                agent.Circuits.Add(circuit.Scene.ID, circuit);

                try
                {
                    scene.Add(agent);
                    try
                    {
                        udpServer.AddCircuit(circuit);
                    }
                    catch
                    {
                        scene.Remove(agent);
                        throw;
                    }
                }
                catch (Exception e)
                {
                    m_Log.Debug("Failed agent post", e);
                    agent.Circuits.Clear();
                    return(new ViewerAgentAccessor());
                }
                /* make agent a root agent */
                agent.SceneID = scene.ID;
                try
                {
                    agent.UserAgentService.SetLastRegion(agent.Owner, new UserRegionData
                    {
                        RegionID      = scene.ID,
                        Position      = agent.GlobalPosition,
                        LookAt        = agent.LookAt,
                        GatekeeperURI = new URI(scene.GatekeeperURI)
                    });
                }
                catch (Exception e)
                {
                    m_Log.Warn("Could not contact UserAgentService", e);
                }

                try
                {
                    m_UserAccountService.SetPosition(agent.Owner.ID, new UserRegionData
                    {
                        RegionID      = scene.ID,
                        Position      = agent.GlobalPosition,
                        LookAt        = agent.LookAt,
                        GatekeeperURI = new URI(scene.GatekeeperURI)
                    });
                }
                catch (Exception e)
                {
                    m_Log.Warn("Could not contact PresenceService", e);
                }
                circuit.LogIncomingAgent(m_Log, false);
                var useCircuit = new UseCircuitCode
                {
                    AgentID     = agentId,
                    SessionID   = sessionId.AsUUID,
                    CircuitCode = (uint)circuitCode
                };

                var viewerCircuit = new ViewerCircuit(vc.ClientUDP, (uint)circuitCode, sessionId.AsUUID, agentId, regionEndPoint);
                vc.ClientUDP.AddCircuit(viewerCircuit);
                viewerCircuit.Start();
                var accessor = new ViewerAgentAccessor(agent.ID, scene.ID, circuitCode, capsPath, sessionId.AsUUID, secureSessionId.AsUUID);
                viewerCircuit.MessageRouting.Add(MessageType.AgentDataUpdate, (m) => AgentDataUpdateReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.AgentDropGroup, (m) => AgentDropGroupReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.AgentMovementComplete, (m) => AgentMovementCompleteReceivedEvent.HandleAgentMovementComplete(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.AlertMessage, (m) => AlertMessageReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.AttachedSound, (m) => AttachedSoundReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.AttachedSoundGainChange, (m) => AttachedSoundGainChangeReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.AvatarAnimation, (m) => AvatarAnimationReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.AvatarSitResponse, (m) => AvatarSitResponseReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.CameraConstraint, (m) => CameraConstraintReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ChangeUserRights, (m) => ChangeUserRightsReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ChatFromSimulator, (m) => ChatFromSimulatorReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ClearFollowCamProperties, (m) => ClearFollowCamPropertiesReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.CoarseLocationUpdate, (m) => CoarseLocationUpdateReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.DeRezAck, (m) => DeRezAckReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.DisableSimulator, (m) => DisableSimulatorReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.EconomyData, (m) => EconomyDataReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.EstateCovenantReply, (m) => EstateCovenantReplyReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.EstateOwnerMessage, (m) => HandleEstateOwnerMessage((EstateOwnerMessage)m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.FeatureDisabled, (m) => FeatureDisabledReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ForceObjectSelect, (m) => ForceObjectSelectReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.GrantGodlikePowers, (m) => GrantGodlikePowersReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.HealthMessage, (m) => HealthMessageReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ImageData, (m) => HandleImageData(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ImageNotInDatabase, (m) => HandleImageNotInDatabase(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ImagePacket, (m) => HandleImagePacket(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ImprovedInstantMessage, (m) => ImprovedInstantMessageReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.KillObject, (m) => KillObjectReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.LoadURL, (m) => LoadURLReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.LogoutReply, (m) => HandleLogoutReply(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.MoneyBalanceReply, (m) => MoneyBalanceReplyReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ObjectAnimation, (m) => ObjectAnimationReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ObjectProperties, (m) => ObjectPropertiesReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ObjectPropertiesFamily, (m) => ObjectPropertiesFamilyReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ObjectUpdate, (m) => ObjectUpdateReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ObjectUpdateCompressed, (m) => ObjectUpdateReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.OfflineNotification, (m) => OfflineNotificationReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.OnlineNotification, (m) => OnlineNotificationReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ParcelDwellReply, (m) => ParcelDwellReplyReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ParcelInfoReply, (m) => ParcelInfoReplyReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ParcelMediaCommandMessage, (m) => ParcelMediaCommandMessageReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ParcelMediaUpdate, (m) => ParcelMediaUpdateReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ParcelObjectOwnersReply, (m) => ParcelObjectOwnersReplyReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ParcelProperties, (m) => ParcelPropertiesReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.PayPriceReply, (m) => PayPriceReplyReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.PreloadSound, (m) => PreloadSoundReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.RegionHandshake, (m) => RegionHandshakeReceivedEvent.HandleRegionHandshake(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.RegionInfo, (m) => RegionInfoReceivedEvent.HandleRegionInfo(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ScriptControlChange, (m) => ScriptControlChangeReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ScriptDialog, (m) => ScriptDialogReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ScriptQuestion, (m) => ScriptQuestionReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ScriptRunningReply, (m) => ScriptRunningReplyReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.ScriptTeleportRequest, (m) => ScriptTeleportRequestReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.SetFollowCamProperties, (m) => SetFollowCamPropertiesReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.SimStats, (m) => SimStatsReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.SoundTrigger, (m) => SoundTriggerReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.TelehubInfo, (m) => TelehubInfoReceivedEvent.ToScriptEvent((TelehubInfo)m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.TeleportFailed, (m) => TeleportFailedReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.TeleportLocal, (m) => TeleportLocalReceivedEvent.ToScriptEvent((TeleportLocal)m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.TeleportProgress, (m) => TeleportProgressReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.TeleportStart, (m) => TeleportStartReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.UUIDGroupNameReply, (m) => UUIDGroupNameReplyReceivedEvent.ToScriptEvent(m, vc, accessor));
                viewerCircuit.MessageRouting.Add(MessageType.UUIDNameReply, (m) => UUIDNameReplyReceivedEvent.ToScriptEvent(m, vc, accessor));

                viewerCircuit.OnBulkUpdateInventory       += (m) => BulkUpdateInventoryReceivedEvent.ToScriptEvent(m, vc, accessor);
                viewerCircuit.OnUpdateCreateInventoryItem += (m) => UpdateCreateInventoryItemReceivedEvent.ToScriptEvent(m, vc, accessor);
                vc.ViewerCircuits.Add((uint)circuitCode, viewerCircuit);
                viewerCircuit.SendMessage(useCircuit);
                return(accessor);
            }
        }
Beispiel #2
0
        public int InjectScript(ScriptInstance instance, string name, string filename, int startparameter, LSLKey experienceID)
        {
            lock (instance)
            {
                UUID assetid = UUID.Random;
                ObjectPartInventoryItem item = new ObjectPartInventoryItem(UUID.Random, instance.Item)
                {
                    Name         = name,
                    AssetID      = assetid,
                    ExperienceID = new UEI(experienceID.ToString())
                };

                IScriptAssembly scriptAssembly = null;
                try
                {
                    using (var reader = new StreamReader(filename, new UTF8Encoding(false)))
                    {
                        m_AssetService.Store(new AssetData
                        {
                            ID   = assetid,
                            Type = AssetType.LSLText,
                            Data = reader.ReadToEnd().ToUTF8Bytes()
                        });
                    }
                    using (var reader = new StreamReader(filename, new UTF8Encoding(false)))
                    {
                        scriptAssembly = CompilerRegistry.ScriptCompilers.Compile(AppDomain.CurrentDomain, UGUI.Unknown, assetid, reader, includeOpen: instance.Part.OpenScriptInclude);
                    }
                    m_Log.InfoFormat("Compilation of injected {1} ({0}) successful", assetid, name);
                }
                catch (CompilerException e)
                {
                    m_Log.ErrorFormat("Compilation of injected {1} ({0}) failed: {2}", assetid, name, e.Message);
                    m_Log.WarnFormat("Stack Trace:\n{0}", e.StackTrace);
                    return(0);
                }
                catch (Exception e)
                {
                    m_Log.ErrorFormat("Compilation of injected {1} ({0}) failed: {2}", assetid, name, e.Message);
                    m_Log.WarnFormat("Stack Trace:\n{0}", e.StackTrace);
                    return(0);
                }

                ScriptInstance scriptInstance;
                try
                {
                    scriptInstance = scriptAssembly.Instantiate(instance.Part, item);
                }
                catch (Exception e)
                {
                    m_Log.ErrorFormat("Instancing of injected {1} ({0}) failed: {2}", assetid, name, e.Message);
                    m_Log.WarnFormat("Stack Trace:\n{0}", e.StackTrace);
                    return(0);
                }
                instance.Part.Inventory.Add(item);
                item.ScriptInstance = scriptInstance;
                try
                {
                    item.ScriptInstance.Start(startparameter);
                }
                catch (Exception e)
                {
                    m_Log.ErrorFormat("Starting of injected {1} ({0}) failed: {2}", assetid, name, e.Message);
                    m_Log.WarnFormat("Stack Trace:\n{0}", e.StackTrace);
                    return(0);
                }
                return(1);
            }
        }