Esempio n. 1
0
        /// <summary>
        /// Serialises a list of objects to <paramref name="writer"/>
        /// </summary>
        /// <param name="writer">The writer to serialise to.</param>
        /// <param name="objects">The object to write.</param>
        /// <param name="processedCount">Number of objects processed.</param>
        /// <returns>An error code on failure.</returns>
        /// <remarks>
        /// Default serialisation uses the following logic on each object:
        /// <list type="bullet">
        /// <item>Call <see cref="CreateSerialisationShape(ShapeComponent)"/> to
        ///       create a temporary shape to match the unity object</item>
        /// <item>Call <see cref="Shapes.Shape.WriteCreate(PacketBuffer)"/> to generate the creation message
        ///       and serialise the packet.</item>
        /// <item>For complex shapes, call <see cref="Shapes.Shape.WriteData(PacketBuffer, ref uint)"/> as required
        ///       and serialise the packets.</item>
        /// </list>
        ///
        /// Using the <see cref="Shapes.Shape"/> classes ensures serialisation is consistent with the server code
        /// and reduces the code maintenance to one code path.
        /// </remarks>
        protected virtual Error SerialiseObjects(BinaryWriter writer, IEnumerator <GameObject> objects, ref uint processedCount)
        {
            // Serialise transient objects.
            PacketBuffer packet = new PacketBuffer();
            Error        err;

            Shapes.Shape tempShape  = null;
            uint         dataMarker = 0;
            int          dataResult = 0;

            Debug.Assert(tempShape != null && tempShape.RoutingID == RoutingID);

            processedCount = 0;
            while (objects.MoveNext())
            {
                ++processedCount;
                GameObject obj = objects.Current;
                tempShape = CreateSerialisationShape(obj.GetComponent <ShapeComponent>());
                if (tempShape != null)
                {
                    tempShape.WriteCreate(packet);
                    packet.FinalisePacket();
                    packet.ExportTo(writer);

                    if (tempShape.IsComplex)
                    {
                        dataResult = 1;
                        dataMarker = 0;
                        while (dataResult > 0)
                        {
                            dataResult = tempShape.WriteData(packet, ref dataMarker);
                            packet.FinalisePacket();
                            packet.ExportTo(writer);
                        }

                        if (dataResult < 0)
                        {
                            return(new Error(ErrorCode.SerialisationFailure));
                        }

                        // Post serialisation extensions.
                        err = PostSerialiseCreateObject(packet, writer, obj.GetComponent <ShapeComponent>());
                        if (err.Failed)
                        {
                            return(err);
                        }
                    }
                }
                else
                {
                    return(new Error(ErrorCode.SerialisationFailure));
                }
            }

            return(new Error());
        }