示例#1
0
 protected ViewVariablesTrait(ViewVariablesSession session)
 {
     Session = session;
 }
        private void _msgReqSession(MsgViewVariablesReqSession message)
        {
            void Deny(DenyReason reason)
            {
                var denyMsg = _netManager.CreateNetMessage <MsgViewVariablesDenySession>();

                denyMsg.RequestId = message.RequestId;
                denyMsg.Reason    = reason;
                _netManager.ServerSendMessage(denyMsg, message.MsgChannel);
            }

            var player = _playerManager.GetSessionByChannel(message.MsgChannel);

            if (!_groupController.CanViewVar(player))
            {
                Deny(DenyReason.NoAccess);
                return;
            }

            object theObject;

            switch (message.Selector)
            {
            case ViewVariablesComponentSelector componentSelector:
                var compType = Type.GetType(componentSelector.ComponentType);
                if (compType == null ||
                    !_componentManager.TryGetComponent(componentSelector.Entity, compType, out var component))
                {
                    Deny(DenyReason.NoObject);
                    return;
                }

                theObject = component;
                break;

            case ViewVariablesEntitySelector entitySelector:
            {
                if (!_entityManager.TryGetEntity(entitySelector.Entity, out var entity))
                {
                    Deny(DenyReason.NoObject);
                    return;
                }

                theObject = entity;
                break;
            }

            case ViewVariablesSessionRelativeSelector sessionRelativeSelector:
                if (!_sessions.TryGetValue(sessionRelativeSelector.SessionId, out var relSession) ||
                    relSession.PlayerSession != message.MsgChannel.SessionId)
                {
                    // TODO: logging?
                    Deny(DenyReason.NoObject);
                    return;
                }

                object value;
                try
                {
                    if (!relSession.TryGetRelativeObject(sessionRelativeSelector.PropertyIndex, out value))
                    {
                        Deny(DenyReason.InvalidRequest);
                        return;
                    }
                }
                catch (ArgumentOutOfRangeException)
                {
                    Deny(DenyReason.NoObject);
                    return;
                }
                catch (Exception e)
                {
                    Logger.ErrorS("vv", "Exception while retrieving value for session. {0}", e);
                    Deny(DenyReason.NoObject);
                    return;
                }

                if (value == null || value.GetType().IsValueType)
                {
                    Deny(DenyReason.NoObject);
                    return;
                }

                theObject = value;
                break;

            case ViewVariablesIoCSelector ioCSelector:
                var reflectionManager = IoCManager.Resolve <IReflectionManager>();
                if (!reflectionManager.TryLooseGetType(ioCSelector.TypeName, out var type))
                {
                    Deny(DenyReason.InvalidRequest);
                    return;
                }
                theObject = IoCManager.ResolveType(type);
                break;

            default:
                Deny(DenyReason.InvalidRequest);
                return;
            }

            var sessionId = _nextSessionId++;
            var session   = new ViewVariablesSession(message.MsgChannel.SessionId, theObject, sessionId, this);

            _sessions.Add(sessionId, session);

            var allowMsg = _netManager.CreateNetMessage <MsgViewVariablesOpenSession>();

            allowMsg.RequestId = message.RequestId;
            allowMsg.SessionId = session.SessionId;
            _netManager.ServerSendMessage(allowMsg, message.MsgChannel);

            player.PlayerStatusChanged += (_, args) =>
            {
                if (args.NewStatus == SessionStatus.Disconnected)
                {
                    _closeSession(session.SessionId, false);
                }
            };
        }