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