/// <inheritdoc /> public override void Write(string value, IWireStreamWriterStrategy dest) { if (dest == null) { throw new ArgumentNullException(nameof(dest)); } //Review the source for Trinitycore's string reading for their ByteBuffer (payload/packet) Type. //(ctr+f << for std::string): http://www.trinitycore.net/d1/d17/ByteBuffer_8h_source.html //They use 0 byte to terminate the string in the stream //We should check this so we don't try to decode //or write null. Null should be considered empty. if (!String.IsNullOrEmpty(value)) { //TODO: Pointer hack for speed //Convert the string to bytes //Not sure about encoding yet byte[] stringBytes = EncodingStrategy.GetBytes(value); dest.Write(stringBytes); } //Write the null terminator; Client expects it. for (int i = 0; i < CharacterSize; i++) { dest.Write(0); } }
/// <inheritdoc /> public override void Write([NotNull] BitArray value, IWireStreamWriterStrategy dest) { if (value == null) { throw new ArgumentNullException(nameof(value)); } if (dest == null) { throw new ArgumentNullException(nameof(dest)); } //The size must be equal to the length divided by 8 bits (1 byte) but we do not include the //remainder from a modular division. The reason for this is it's always sent as 4 byte chunks from //Trinitycore and the size is always in terms of an int array byte[] bitmask = new byte[value.Length / 8]; ((ICollection)value).CopyTo(bitmask, 0); byte size = (byte)(bitmask.Length / sizeof(int)); //Write the size as if it were an int array first dest.Write(size); dest.Write(bitmask); }
/// <inheritdoc /> public override void Write([NotNull] string value, IWireStreamWriterStrategy dest) { if (value == null) { throw new ArgumentNullException(nameof(value)); } if (dest == null) { throw new ArgumentNullException(nameof(dest)); } int size = sizeProvider.Size(value, dest); //Now that we know the size, and the header will be written if it was needed, we can write it //Don't write the size. Leave it up to the strategy above decoratedSerializer.Write(value, dest); //the tricky part here is that the serializer just wrote the string plus the null terminator //So, if the length of the string was less than the expected size write some more 0s. //However, DO NOT write another null terminator either way because we already have one. if (value.Length < size) { dest.Write(new byte[(CharacterSize * (size - value.Length))]); } }
//From jackpoz's bot: https://github.com/jackpoz/BotFarm/blob/592dbc9dbb58c06175bffecfced56eba42107c9a/Client/World/Network/OutPacket.cs#L47 /// <inheritdoc /> public override void Write(PackedGuid value, IWireStreamWriterStrategy dest) { //TODO: Can we use span or stackalloc? Or maybe shared buffer? byte[] packGuid = GeneratePackedGuid(value, out int size); dest.Write(packGuid, 0, size); }
/// <inheritdoc /> public override void Write(LinearPathMoveInfo value, IWireStreamWriterStrategy dest) { if (value == null) { return; } dest.Write(value.LastIndex.Reinterpret()); dest.Write(value.FinalPosition.X.Reinterpret()); dest.Write(value.FinalPosition.Y.Reinterpret()); dest.Write(value.FinalPosition.Z.Reinterpret()); if (value.LastIndex > 1 && value.SplineMiddlePoints != null) { dest.Write(value.SplineMiddlePoints); } }
/// <inheritdoc /> public override void Write(bool value, IWireStreamWriterStrategy dest) { if (dest == null) { throw new ArgumentNullException(nameof(dest)); } dest.Write(ConvertFromBool(value)); }
/// <inheritdoc /> public override void Write(UpdateFieldValueCollection value, IWireStreamWriterStrategy dest) { //TODO: Major performance gains if we can avoid allocations here. MAJOR!! //The size must be equal to the length divided by 8 bits (1 byte) but we do not include the //remainder from a modular division. The reason for this is it's always sent as 4 byte chunks from //Trinitycore and the size is always in terms of an int array byte[] bitmask = new byte[value.UpdateMask.Length / 8]; ((ICollection)value.UpdateMask).CopyTo(bitmask, 0); byte size = (byte)(bitmask.Length / sizeof(int)); //Write the size as if it were an int array first dest.Write(size); dest.Write(bitmask); //We must also write the update values dest.Write(value.UpdateDiffValues); }
/// <inheritdoc /> public override void Write(byte[] value, IWireStreamWriterStrategy dest) { if (dest == null) { throw new ArgumentNullException(nameof(dest)); } //MUST copy or it will modify the external objects byte[] bytes = value.ToArray(); Array.Reverse(bytes); dest.Write(bytes); }
/// <inheritdoc /> public override void Write(string value, IWireStreamWriterStrategy dest) { if (dest == null) { throw new ArgumentNullException(nameof(dest)); } //TODO: Pointer hack for speed byte[] stringBytes = EncodingStrategy.GetBytes(value); dest.Write(stringBytes); //Just don't write terminator. Very simple. }
/// <inheritdoc /> public override void Write(TType[] value, IWireStreamWriterStrategy dest) { if (dest == null) { throw new ArgumentNullException(nameof(dest)); } int size = SizeStrategyService.Size <TType[], TType>(value, dest); //We no longer verify size thanks to PHANTASY STAR ONLINE. Thanks Sega. Sometimes we have to fake the size dest.Write(value.Reinterpret()); }
/// <inheritdoc /> public override void Write(TType value, [NotNull] IWireStreamWriterStrategy dest) { if (dest == null) { throw new ArgumentNullException(nameof(dest)); } bool isPopulated = PopulateSharedBufferWith(value); if (!isPopulated) { throw new InvalidOperationException($"Failed to populate {nameof(SharedByteBuffer)} in {nameof(SharedBufferTypeSerializer<TType>)} for Type: {typeof(TType)} with Value: {value}."); } //At this point the SharedBuffer contains the serialized representation of the value //DO NOT RELEASE LOCK UNTIL WRITTEN dest.Write(SharedByteBuffer.Value); }
/// <inheritdoc /> public override void Write(TType value, IWireStreamWriterStrategy dest) { byte[] bytes = SerializerStrategy.GetBytes(value); Array.Reverse(bytes); dest.Write(bytes); }
/// <inheritdoc /> public override void Write(TypeWithCustomSerializer value, IWireStreamWriterStrategy dest) { dest.Write(new byte[1000]); }
public override void Write(byte[] value, IWireStreamWriterStrategy dest) { dest.Write(value); }