Пример #1
0
        public void Serialize <T>(Entity entity, T data)
            where T : ITransportableData
        {
            entity.Set(new TransportableDataType(typeof(T)));

            var buffer = new DataBufferWriter(data.GetCapacity());

            buffer.WriteStaticString(typeof(T).AssemblyQualifiedName);
            data.Serialize(ref buffer);
            entity.Set(buffer);
        }
Пример #2
0
        protected override void OnUpdate()
        {
            base.OnUpdate();

            if (withoutIdSet.Count > 0)
            {
                Span <Entity> entities = stackalloc Entity[withoutIdSet.Count];
                withoutIdSet.GetEntities().CopyTo(entities);
                foreach (ref readonly var entity in entities)
                {
                    entity.Set(new AudioPlayerId(selfLastMaxId++));
                }
            }

            var maxId = 0;

            foreach (var entity in playerSet.GetEntities())
            {
                maxId = Math.Max(maxId, entity.Get <AudioPlayerId>().Id);
            }

            // for memory usage, don't put this call into the foreach since stackalloc is only freed when this method itself is finished
            Span <Entity> clientUpdated = stackalloc Entity[playerSet.Count];

            foreach (var(featureEntity, feature) in Features)
            {
                var update     = false;
                var previousId = 0;
                if (!clientLastMaxId.TryGetValue(feature, out var clientMaxId) || clientMaxId < maxId)
                {
                    previousId = clientMaxId;
                    clientLastMaxId[feature] = maxId;
                    update = true;
                }

                if (update)
                {
                    var updatedCount = 0;
                    foreach (var entity in playerSet.GetEntities())
                    {
                        if (entity.Get <AudioPlayerId>().Id > previousId)
                        {
                            clientUpdated[updatedCount++] = entity;
                        }
                    }

                    using var writer = new DataBufferWriter(updatedCount);
                    writer.WriteInt((int)EAudioSendType.RegisterPlayer);
                    writer.WriteInt(updatedCount);
                    foreach (var entity in clientUpdated.Slice(0, updatedCount))
                    {
                        writer.WriteInt(entity.Get <AudioPlayerId>().Id);
                        writer.WriteStaticString(TypeExt.GetFriendlyName(entity.Get <AudioPlayerType>().Type));
                    }

                    if (feature.Driver.Broadcast(feature.PreferredChannel, writer.Span) < 0)
                    {
                        throw new InvalidOperationException("Couldn't send data!");
                    }
                }
            }
        }
        protected override void OnUpdate()
        {
            foreach (ref readonly var entity in playerSet.GetEntities())
            {
                if (entity.TryGet(out AudioStartTime startTime))
                {
                    entity.Set(new AudioCurrentPlayTime(worldTime.Total - startTime.Value));
                }
            }

            foreach (ref readonly var entity in playAudioSet.GetEntities())
            {
                var resource = entity.Get <ResourceHandle <AudioResource> >();
                if (!resource.IsLoaded)
                {
                    continue;
                }

                var volume = 1f;
                if (entity.TryGet(out AudioVolumeComponent volumeComponent))
                {
                    volume = volumeComponent.Volume;
                }

                var delay = TimeSpan.Zero;
                if (entity.TryGet(out AudioDelayComponent delayComponent))
                {
                    delay = delayComponent.Delay;
                }

                using var writer = new DataBufferWriter(16 + Unsafe.SizeOf <SControllerEvent>());
                writer.WriteValue((int)EAudioSendType.SendAudioPlayerData);
                writer.WriteStaticString(typeName);
                writer.WriteValue(new SControllerEvent
                {
                    State      = SControllerEvent.EState.Play,
                    ResourceId = entity.Get <ResourceHandle <AudioResource> >().Result.Id,
                    Player     = entity.Get <AudioPlayerId>().Id,
                    Volume     = volume,
                    Delay      = delay
                });

                entity.Set(new AudioStartTime {
                    Value = worldTime.Total + delay
                });

                foreach (var(_, feature) in Features)
                {
                    unsafe
                    {
                        feature.Driver.Broadcast(feature.PreferredChannel, new Span <byte>((void *)writer.GetSafePtr(), writer.Length));
                    }
                }
            }

            foreach (ref readonly var entity in stopAudioSet.GetEntities())
            {
                var delay = TimeSpan.Zero;
                if (entity.TryGet(out AudioDelayComponent delayComponent))
                {
                    delay = delayComponent.Delay;
                }

                using var writer = new DataBufferWriter(16 + Unsafe.SizeOf <SControllerEvent>());
                writer.WriteValue((int)EAudioSendType.SendAudioPlayerData);
                writer.WriteStaticString(typeName);
                writer.WriteValue(new SControllerEvent
                {
                    State  = SControllerEvent.EState.Stop,
                    Player = entity.Get <AudioPlayerId>().Id,
                    Delay  = delay
                });

                foreach (var(_, feature) in Features)
                {
                    unsafe
                    {
                        feature.Driver.Broadcast(feature.PreferredChannel, new Span <byte>((void *)writer.GetSafePtr(), writer.Length));
                    }
                }
            }

            playAudioSet.Remove <PlayAudioRequest>();
            playAudioSet.Remove <StopAudioRequest>();
            toDisposeSet.DisposeAllEntities();
        }