Exemplo n.º 1
0
        public void ReflectionManager_TestGetType()
        {
            IReflectionManager reflectionManager = IoCManager.Resolve <IReflectionManager>();

            Assert.Multiple(() =>
            {
                Assert.That(reflectionManager.GetType("Shared.Reflection.TestGetType1"), Is.EqualTo(typeof(TestGetType1)));
                Assert.That(reflectionManager.GetType("Shared.Reflection.TestGetType2"), Is.EqualTo(typeof(TestGetType2)));
                Assert.That(reflectionManager.GetType("Shared.Reflection.ITestGetType3"), Is.EqualTo(typeof(ITestGetType3)));
            });
        }
Exemplo n.º 2
0
        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 = _reflectionManager.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.PlayerUser != message.MsgChannel.UserId)
                {
                    // 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 ViewVariablesesSession(message.MsgChannel.UserId, theObject, sessionId, this,
                                                       _robustSerializer);

            _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);
                }
            };
        }