Esempio n. 1
0
        public static Func <GameObject, IReplicatedObjectDatabase, ReplicatedObject> GameObjectReplicationDecorator(
            Func <GameObject, ObjectMessageRouter> objectMessageDispatcherFactory,
            Func <ReplicatedObject, ObjectMessageSender> messageSenderFactory,
            DependencyContainer globalDependencies,
            IReadOnlyDictionary <Type, MessageType> objectMessageTypes)
        {
            var objectMessageCache = new ImmutableArrayDictionary <MessageType, IObjectMessage>(objectMessageTypes.Count)
                                     .AddRange(objectMessageTypes.Select(kvPair => {
                var type             = kvPair.Key;
                var serializableType = kvPair.Value;
                return(new KeyValuePair <MessageType, IObjectMessage>(serializableType,
                                                                      (IObjectMessage)Activator.CreateInstance(type)));
            }));

            return((gameObject, replicationDatabase) => {
                var messageHandler = objectMessageDispatcherFactory(gameObject);

                var constructors = InitialStateLogic.FindInitialStateConstructors(gameObject, objectMessageTypes);

                var spawnables = gameObject.GetComponentsOfInterfaceInChildren <ISpawnable>()
                                 .ToImmutableList();
                var replicatedObject = new ReplicatedObject(gameObject, messageHandler,
                                                            new ReplicationConstructor(constructors, objectMessageCache),
                                                            spawnables);

                // Inject message sender
                var messageSender = messageSenderFactory(replicatedObject);
                DependencyInjector.Default.Inject(gameObject, globalDependencies, overrideExisting: true);
                DependencyInjector.Default.InjectSingle(gameObject, messageSender, overrideExisting: true);
                DependencyInjector.Default.InjectSingle(gameObject, replicationDatabase, overrideExisting: true);
                DependencyInjector.Default.InjectSingle(gameObject, replicatedObject.GameObjectNetworkInfo, overrideExisting: true);

                return replicatedObject;
            });
        }
Esempio n. 2
0
 public void Activate(ReplicatedObject replicatedObject)
 {
     _buffer.Reset();
     replicatedObject.ReplicationConstructor.SerializeInitialState(_buffer);
     _store.DispatchMessages(ConnectionId.NoConnection, replicatedObject.Id, _buffer, _buffer.LengthBytes);
     replicatedObject.Activate();
 }
Esempio n. 3
0
        public void Activate(ReplicatedObject replicatedObject)
        {
            replicatedObject.GameObject.SetActive(true);
            replicatedObject.OnSpawn();

            if (!replicatedObject.IsPreExisting)
            {
                if (replicatedObject.Role.IsAuthority())
                {
                    replicatedObject.GameObject.SetParent(_spawnedAuthorityObjects);
                }
                else if (replicatedObject.Role.IsOwner())
                {
                    replicatedObject.GameObject.SetParent(_spawnedOwnerObjects);
                }
                else
                {
                    replicatedObject.GameObject.SetParent(_spawnedOtherObjects);
                }
            }

            replicatedObject.MessageHandler.ClearLastReceivedMessages();

            if (ObjectAdded != null)
            {
                ObjectAdded(replicatedObject);
            }
        }
Esempio n. 4
0
 public void Replicate(IList <ConnectionId> receivers, ReplicatedObject instance)
 {
     for (int i = 0; i < receivers.Count; i++)
     {
         var receiver = receivers[i];
         Replicate(receiver, instance);
     }
 }
Esempio n. 5
0
 public ObjectMessageSender(IMessageSender sender,
                            TransportGroupRouter groupRouter,
                            TransportGroupId group,
                            IBasicObjectPool <MulticastNetworkMessage> networkMessages,
                            ReplicatedObject o)
 {
     _sender          = sender;
     _object          = o;
     _groupRouter     = groupRouter;
     _group           = group;
     _networkMessages = networkMessages;
 }
Esempio n. 6
0
 public static void Replicate(
     this IMessageSender messageSender,
     MessagePool messagePool,
     IList <ConnectionId> receivers,
     ReplicatedObject instance)
 {
     for (int i = 0; i < receivers.Count; i++)
     {
         var receiver = receivers[i];
         messageSender.Replicate(messagePool, receiver, instance);
     }
 }
Esempio n. 7
0
        public void Activate(ReplicatedObject replicatedObject)
        {
            // Generate initial state and send it to self
            _initialStateBuffer.Reset();
            replicatedObject.ReplicationConstructor.SerializeInitialState(_initialStateBuffer);
            var bytesRead = 0;

            while (bytesRead < _initialStateBuffer.WriterPosition())
            {
                var objectMessage = _messageParser.Parse(_initialStateBuffer);
                bytesRead = _initialStateBuffer.ReaderPosition();
                _store.DispatchMessage(ConnectionId.Self, replicatedObject.Id, objectMessage,
                                       new SequenceNumber(1), latency: 0f);
            }

            _store.Activate(replicatedObject);

            _networkReplicator.Replicate(_connectionGroup.ActiveConnections, replicatedObject);
        }
        public static Func <GameObject, IReplicatedObjectDatabase, ReplicatedObject> GameObjectReplicationDecorator(
            Func <ObjectMessageRouter> objectMessageDispatcherFactory,
            Func <ReplicatedObject, ObjectMessageSender> messageSenderFactory,
            IList <object> dependencies,
            IDictionary <Type, MessageType> objectMessageTypes)
        {
            var globalDependencies = new DependencyContainer(dependencies.ToArray());

            var objectMessageCache = new ArrayDictionary <MessageType, IObjectMessage>(
                messageType => (int)messageType.Value,
                i => new MessageType((uint)i),
                objectMessageTypes.Count);

            foreach (var kvPair in objectMessageTypes)
            {
                var type             = kvPair.Key;
                var serializableType = kvPair.Value;
                objectMessageCache[serializableType] = (IObjectMessage)Activator.CreateInstance(type);
            }

            return((gameObject, replicationDatabase) => {
                var messageHandler = objectMessageDispatcherFactory();
                messageHandler.RegisterGameObject(gameObject);

                var constructors = InitialStateLogic.FindInitialStateConstructors(gameObject, objectMessageTypes);

                var replicatedObject = new ReplicatedObject(gameObject, messageHandler,
                                                            new ReplicationConstructor(constructors, objectMessageCache));

                // Inject message sender
                var messageSender = messageSenderFactory(replicatedObject);
                DependencyInjection.DependencyInjection.Inject(gameObject, globalDependencies, overrideExisting: true);
                DependencyInjection.DependencyInjection.InjectSingle(gameObject, messageSender, overrideExisting: true);
                DependencyInjection.DependencyInjection.InjectSingle(gameObject, replicationDatabase, overrideExisting: true);
                DependencyInjection.DependencyInjection.InjectSingle(gameObject, replicatedObject.GameObjectNetworkInfo, overrideExisting: true);

                return replicatedObject;
            });
        }
Esempio n. 9
0
        public static void Replicate(this IMessageSender messageSender,
                                     MessagePool messagePool,
                                     ConnectionId receiver,
                                     ReplicatedObject instance)
        {
            var objectRole  = ObjectRole.Nobody;
            var isOwner     = receiver == instance.OwnerConnectionId;
            var isAuthority = receiver == instance.AuthorityConnectionId;

            objectRole = objectRole | (isOwner ? ObjectRole.Owner : ObjectRole.Nobody);
            objectRole = objectRole | (isAuthority ? ObjectRole.Authority : ObjectRole.Nobody);
            objectRole = objectRole | (!isOwner && !isAuthority ? ObjectRole.Others : ObjectRole.Nobody);

            Debug.Log("replicating to " + receiver + " role: " + objectRole);

            if (instance.IsPreExisting)
            {
                var replicatePreExistingMsg = messagePool.GetMessage <BasicMessage.ReplicatePreExistingObject>();
                replicatePreExistingMsg.Content.GlobalObjectId.CopyFrom(instance.GlobalObjectId);
                replicatePreExistingMsg.Content.NetworkObjectId = instance.Id;
                replicatePreExistingMsg.Content.ObjectRole      = objectRole;
                messageSender.Send(receiver, replicatePreExistingMsg);
            }
            else
            {
                var createObjectMsg = messagePool.GetMessage <BasicMessage.CreateObject>();
                createObjectMsg.Content.ObjectId   = instance.Id;
                createObjectMsg.Content.ObjectRole = objectRole;
                createObjectMsg.Content.ObjectType = instance.Type.Value;

                // Add any additional messages that are required to atomically construct the object
                createObjectMsg.Content.AdditionalData.Reset();
                instance.ReplicationConstructor.SerializeInitialState(createObjectMsg.Content.AdditionalData);

                messageSender.Send(receiver, createObjectMsg);
            }
        }