Пример #1
0
        public void Dispose()
        {
            UnRegisterDispatcherCallbacks();
            UnregisterComponentFactories();

            var block = firstBlock;

            while (block != null)
            {
                var disposable = block as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }

                block = block.NextEntityBlock;
            }

            firstBlock = null;
            lastBlock  = null;
            started    = false;

            spatialCommunicator = null;
            connection          = null;
            dispatcher          = null;
        }
        public ComponentCommander(ISpatialOsComponentInternal component, ISpatialCommunicator communicator)
        {
            commandResponseThunksRegistered = new HashSet <Type>();
            requestIdToCallback             = new Dictionary <uint, ICommandCallbackWrapper>();

            this.component    = component;
            this.communicator = communicator;

            dispatcherCallbackKeys = new Collections.List <ulong>();
            dispatcherCallbackKeys.Add(
                communicator.RegisterReserveEntityIdResponse(CreateOnResponseThunk <ReserveEntityIdResponseOp, ReserveEntityIdResult>(
                                                                 op => op.RequestId.Id, op => op.StatusCode, op => op.Message, op => op.EntityId.HasValue ? new ReserveEntityIdResult(op.EntityId.Value) : new Option <ReserveEntityIdResult>())));
            dispatcherCallbackKeys.Add(
                communicator.RegisterCreateEntityResponse(CreateOnResponseThunk <CreateEntityResponseOp, CreateEntityResult>(
                                                              op => op.RequestId.Id, op => op.StatusCode, op => op.Message, op => op.EntityId.HasValue ? new CreateEntityResult(op.EntityId.Value) : new Option <CreateEntityResult>())));
            dispatcherCallbackKeys.Add(
                communicator.RegisterReserveEntityIdsResponse(CreateOnResponseThunk <ReserveEntityIdsResponseOp, ReserveEntityIdsResult>(
                                                                  op => op.RequestId.Id, op => op.StatusCode, op => op.Message, op => new ReserveEntityIdsResult(op.FirstEntityId.Value, op.NumberOfEntityIds))));
            dispatcherCallbackKeys.Add(
                communicator.RegisterDeleteEntityResponse(CreateOnResponseThunk <DeleteEntityResponseOp, DeleteEntityResult>(
                                                              op => op.RequestId.Id, op => op.StatusCode, op => op.Message, op => new DeleteEntityResult(op.EntityId))));
            dispatcherCallbackKeys.Add(
                communicator.RegisterEntityQueryResponse(CreateOnResponseThunk <EntityQueryResponseOp, EntityQueryResult>(
                                                             op => op.RequestId.Id, op => op.StatusCode, op => op.Message, op => new EntityQueryResult(op.ResultCount, op.Result))));

            if (component != null)
            {
                component.OnAuthorityChange += OnComponentAuthorityChange;
            }
        }
Пример #3
0
        public Commander(ComponentCommander componentCommander, ISpatialCommunicator communicator)
        {
            this.componentCommander = componentCommander;
            this.communicator       = communicator;

            componentToRequestIds = new Dictionary <EntityComponentId, HashSet <uint> >();

            communicator.ComponentAuthorityChanged += OnComponentAuthorityChanged;
        }
Пример #4
0
        public void Initialize(IEntityObject entityObject, ISpatialCommunicator spatialCommunicator)
        {
            if (spatialCommunicator == null)
            {
                throw new ArgumentNullException("spatialCommunicator");
            }

            Entity = entityObject;
            InitializeComponents(entityObject, spatialCommunicator);
        }
        /// <summary>
        ///     This is an implementation detail; it should not be called by user code.
        /// </summary>
        public virtual bool Init(ISpatialCommunicator communicator, IEntityObject entityObject)
        {
            if (this.communicator != null)
            {
                return(false);
            }

            this.communicator = communicator;
            this.entityObject = entityObject;
            this.entityId     = entityObject.EntityId;

            entityObject.Components.RegisterInterestedComponent(ComponentId, this);
            return(true);
        }
Пример #6
0
        private void InitializeComponents(IEntityObject entityObject, ISpatialCommunicator spatialCommunicator)
        {
            // N.B. this one works with interfaces, GetComponents<> doesn't.
            var spatialOsComponents = GetComponents(typeof(ISpatialOsComponentInternal));

            for (var i = 0; i < spatialOsComponents.Length; i++)
            {
                var spatialOsComponent = spatialOsComponents[i] as ISpatialOsComponentInternal;
                if (spatialOsComponent == null)
                {
                    continue;
                }

                spatialOsComponent.Init(spatialCommunicator, entityObject);
            }
        }
Пример #7
0
        /// <inheritdoc />
        public void Start(ISpatialCommunicator spatialCommunicator)
        {
            if (started)
            {
                throw new InvalidOperationException("The pipeline has already been started.");
            }

            this.spatialCommunicator = spatialCommunicator;
            RegisterDispatcherCallbacks();

            // Set the empty block as the last block to prevent NREs being thrown
            // when the last block uses 'NextEntityBlock' property.
            lastBlock.NextEntityBlock = FinalBlock;

            started = true;
        }
        public LegacyEntityCreator(IEntityTemplateProvider templateProvider,
                                   ISpatialCommunicator spatialCommunicator,
                                   IPrefabFactory <GameObject> prefabFactory,
                                   IMutableUniverse universe,
                                   IEntityComponentInterestOverridesUpdater entityComponentInterestOverridesUpdater,
                                   IInterestedComponentUpdaterProvider interestedComponentUpdaterProvider,
                                   WorkerMetrics metrics)
        {
            this.templateProvider    = templateProvider;
            this.prefabFactory       = new PrefabFactoryMetrics(prefabFactory, metrics); // Associate metrics with the factory
            this.spatialCommunicator = spatialCommunicator;
            this.universe            = universe;
            this.entityComponentInterestOverridesUpdater = entityComponentInterestOverridesUpdater;
            this.interestedComponentUpdaterProvider      = interestedComponentUpdaterProvider;

            entitiesToSpawn = new Dictionary <EntityId, EntitySpawnData>();
            knownEntities   = new HashSet <EntityId>();

            this.metrics = metrics;
        }
Пример #9
0
        public LegacyEntityPipelineSetup(MonoBehaviour hostBehaviour, IEntityPipeline entityPipeline, ISpatialCommunicator spatialCommunicator, IMutableUniverse universe, ILegacyEntityPipelineConfiguration config, IEntitySpawnLimiter spawnLimiter)
        {
            this.entityPipeline = entityPipeline;
            IPrefabFactory <GameObject> prefabFactory;

            if (!config.UsePrefabPooling && config.AssetsToPrePool != null && config.AssetsToPrePool.Any())
            {
                Debug.LogError("There are prefabs specified for pre-pooling, but prefab pooling is not enabled - pooling will occur");
            }

            bool preloaderHasFactory = false;

            if (config.UsePrefabPooling || config.AssetsToPrecache != null || config.AssetsToPrePool != null)
            {
                preloaderHasFactory = true;
#pragma warning disable 0612
                assetPreloader = new AssetPreloader(hostBehaviour,
                                                    config.TemplateProvider,
                                                    config.AssetsToPrecache,
                                                    config.AssetsToPrePool,
                                                    config.MaxConcurrentPrecacheConnections);
#pragma warning restore 0612
                assetPreloader.PrecachingCompleted += () =>
                {
                    if (config.OnPrecachingCompleted != null)
                    {
                        config.OnPrecachingCompleted();
                    }
                };

                assetPreloader.PrecachingProgress += progress =>
                {
                    if (config.OnPrecacheProgress != null)
                    {
                        config.OnPrecacheProgress(progress);
                    }
                };
            }

            if (preloaderHasFactory && config.UsePrefabPooling)
            {
                prefabFactory = assetPreloader.PrefabFactory;
            }
            else
            {
                prefabFactory = new UnityPrefabFactory();
            }

            criticalSectionPipelineBlock = new CriticalSectionPipelineBlock();

            throttledEntityDispatcher = new ThrottledEntityDispatcher(universe, spawnLimiter, config.Metrics);

            legacyEntityCreator = new LegacyEntityCreator(
                config.TemplateProvider,
                spatialCommunicator,
                prefabFactory,
                universe,
                config.EntityComponentInterestOverridesUpdater,
                config.InterestedComponentUpdaterProvider,
                config.Metrics);

            legacyComponentPipeline = new LegacyComponentPipeline(universe);

            entityComponentUpdater = new EntityComponentUpdater(universe);

            templateProvider = config.TemplateProvider;
        }