Exemplo n.º 1
0
        private long SerializeComponents(MyBinaryWriter binaryWriter, List <IUpdateComponent> oldComponents,
                                         List <IUpdateComponent> currentComponents, out int count)
        {
            int c            = 0;
            var startPostion = binaryWriter.Position;

            foreach (var currentComponent in currentComponents)
            {
                var  serializer = _componentSerializerManager.GetSerializer(currentComponent.GetComponentId());
                bool isModife   = false;
                foreach (var oldComponent in oldComponents)
                {
                    if (currentComponent.GetComponentId() == oldComponent.GetComponentId())
                    {
                        var bitMask = serializer.DiffNetworkObject(oldComponent,
                                                                   currentComponent);
                        if (bitMask.HasValue())
                        {
                            var modifyPatch = ModifyComponentPatch.Allocate(oldComponent, currentComponent, bitMask);

                            modifyPatch.Serialize(binaryWriter, _componentSerializerManager);
                            modifyPatch.ReleaseReference();
                            c++;
                        }
                        isModife = true;
                        break;
                    }
                }

                if (!isModife)
                {
                    var addPatch = AddComponentPatch.Allocate(currentComponent);
                    addPatch.Serialize(binaryWriter, _componentSerializerManager);
                    addPatch.ReleaseReference();
                    c++;
                }
            }

            var endPosition = binaryWriter.Position;

            count = c;
            return(endPosition - startPostion);
        }