fix(core): Converts boat packets (#416)
- [X] Converts boat packets - [X] Makes a packet container builder for packet 0xF7 - [X] Generalizes world item packet so it works for mobiles too Notes: - This PR doesn't address proper smooth movement for boats.
This commit is contained in:
parent
b4b1b0b122
commit
08f05afd01
15 changed files with 619 additions and 220 deletions
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using Server.Items;
|
||||
|
||||
namespace Server.Network
|
||||
{
|
||||
|
|
@ -22,6 +23,7 @@ namespace Server.Network
|
|||
{
|
||||
public const int OPLPacketLength = 9;
|
||||
public const int RemoveEntityLength = 5;
|
||||
public const int MaxWorldEntityPacketLength = 26;
|
||||
|
||||
public static void CreateOPLInfo(Span<byte> buffer, Item item) =>
|
||||
CreateOPLInfo(buffer, item.Serial, item.PropertyList.Hash);
|
||||
|
|
@ -79,5 +81,71 @@ namespace Server.Network
|
|||
|
||||
ns.Send(buffer);
|
||||
}
|
||||
|
||||
public static int CreateWorldEntity(Span<byte> buffer, IEntity entity, bool isSA, bool isHS)
|
||||
{
|
||||
if (buffer[0] != 0)
|
||||
{
|
||||
return buffer.Length;
|
||||
}
|
||||
|
||||
var writer = new SpanWriter(buffer);
|
||||
writer.Write((byte)0xF3); // Packet ID
|
||||
writer.Write((short)0x1); // command
|
||||
|
||||
int type = 0;
|
||||
int gfx = 0;
|
||||
int amount = 1;
|
||||
int hue = 0;
|
||||
byte light = 0;
|
||||
int flags = 0;
|
||||
|
||||
if (entity is BaseMulti multi)
|
||||
{
|
||||
type = 2;
|
||||
gfx = multi.ItemID & (isHS ? 0xFFFF : 0x7FFF);
|
||||
hue = multi.Hue;
|
||||
amount = multi.Amount;
|
||||
}
|
||||
else if (entity is Item item)
|
||||
{
|
||||
// type = 3 if is damageable
|
||||
gfx = item.ItemID & (isHS ? 0xFFFF : 0x7FFF);
|
||||
hue = item.Hue;
|
||||
amount = item.Amount;
|
||||
light = (byte)item.Light;
|
||||
flags = item.GetPacketFlags();
|
||||
}
|
||||
else if (entity is Mobile mobile)
|
||||
{
|
||||
type = 1;
|
||||
gfx = mobile.BodyValue;
|
||||
hue = mobile.Hue;
|
||||
flags = mobile.GetPacketFlags(isSA);
|
||||
}
|
||||
|
||||
writer.Write((byte)type);
|
||||
writer.Write(entity.Serial);
|
||||
writer.Write((ushort)gfx);
|
||||
writer.Write((byte)0);
|
||||
|
||||
writer.Write((short)amount); // Min
|
||||
writer.Write((short)amount); // Max
|
||||
|
||||
writer.Write((short)(entity.X & 0x7FFF));
|
||||
writer.Write((short)(entity.Y & 0x3FFF));
|
||||
writer.Write((sbyte)entity.Z);
|
||||
|
||||
writer.Write((byte)light);
|
||||
writer.Write((short)hue);
|
||||
writer.Write((byte)flags);
|
||||
|
||||
if (isHS)
|
||||
{
|
||||
writer.Write((short)0);
|
||||
}
|
||||
|
||||
return writer.Position;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,8 +21,6 @@ namespace Server.Network
|
|||
{
|
||||
public static class OutgoingItemPackets
|
||||
{
|
||||
public const int MaxWorldItemPacketLength = 26;
|
||||
|
||||
public static int CreateWorldItem(Span<byte> buffer, Item item)
|
||||
{
|
||||
if (buffer[0] != 0)
|
||||
|
|
@ -89,62 +87,13 @@ namespace Server.Network
|
|||
return;
|
||||
}
|
||||
|
||||
Span<byte> buffer = stackalloc byte[MaxWorldItemPacketLength];
|
||||
Span<byte> buffer = stackalloc byte[OutgoingEntityPackets.MaxWorldEntityPacketLength];
|
||||
|
||||
var length = ns.StygianAbyss ?
|
||||
CreateWorldItemNew(buffer, item, ns.HighSeas) :
|
||||
OutgoingEntityPackets.CreateWorldEntity(buffer, item, ns.StygianAbyss, ns.HighSeas) :
|
||||
CreateWorldItem(buffer, item);
|
||||
|
||||
ns.Send(buffer.SliceToLength(length));
|
||||
}
|
||||
|
||||
public static int CreateWorldItemNew(Span<byte> buffer, Item item, bool isHS)
|
||||
{
|
||||
if (buffer[0] != 0)
|
||||
{
|
||||
return buffer.Length;
|
||||
}
|
||||
|
||||
var writer = new SpanWriter(buffer);
|
||||
writer.Write((byte)0xF3); // Packet ID
|
||||
writer.Write((short)0x1); // command
|
||||
|
||||
var itemID = item.ItemID;
|
||||
|
||||
if (item is BaseMulti)
|
||||
{
|
||||
writer.Write((byte)2);
|
||||
writer.Write(item.Serial);
|
||||
writer.Write((short)(itemID & 0x3FFF));
|
||||
writer.Write((byte)0);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.Write((byte)0);
|
||||
writer.Write(item.Serial);
|
||||
writer.Write((short)(itemID & (isHS ? 0xFFFF : 0x7FFF)));
|
||||
writer.Write((byte)0);
|
||||
}
|
||||
|
||||
var amount = item.Amount;
|
||||
writer.Write((short)amount); // Min
|
||||
writer.Write((short)amount); // Max
|
||||
|
||||
var loc = item.Location;
|
||||
writer.Write((short)loc.X);
|
||||
writer.Write((short)loc.Y);
|
||||
writer.Write((sbyte)loc.Z);
|
||||
|
||||
writer.Write((byte)item.Light);
|
||||
writer.Write((short)item.Hue);
|
||||
writer.Write((byte)item.GetPacketFlags());
|
||||
|
||||
if (isHS)
|
||||
{
|
||||
writer.Write((short)0);
|
||||
}
|
||||
|
||||
return writer.Position;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
115
Projects/Server/Network/Packets/PacketContainerBuilder.cs
Normal file
115
Projects/Server/Network/Packets/PacketContainerBuilder.cs
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: PacketContainerBuilder.cs *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Buffers.Binary;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Server.Network
|
||||
{
|
||||
public ref struct PacketContainerBuilder
|
||||
{
|
||||
public const int MinPacketLength = 5;
|
||||
|
||||
private bool _finished;
|
||||
private int _count;
|
||||
|
||||
private byte[]? _arrayToReturnToPool;
|
||||
private Span<byte> _bytes;
|
||||
|
||||
public PacketContainerBuilder(Span<byte> initialBuffer)
|
||||
{
|
||||
_arrayToReturnToPool = null;
|
||||
_finished = false;
|
||||
_count = 0;
|
||||
|
||||
_bytes = initialBuffer;
|
||||
_bytes[0] = 0xF7; // Packet ID
|
||||
Length = MinPacketLength; // Length + Count
|
||||
}
|
||||
|
||||
public int Length { get; set; }
|
||||
|
||||
public int Capacity => _bytes.Length;
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public ReadOnlySpan<byte> Finalize()
|
||||
{
|
||||
if (!_finished)
|
||||
{
|
||||
BinaryPrimitives.WriteUInt16BigEndian(_bytes.Slice(1, 2), (ushort)Length);
|
||||
BinaryPrimitives.WriteUInt16BigEndian(_bytes.Slice(3, 2), (ushort)_count);
|
||||
}
|
||||
|
||||
return _bytes.SliceToLength(Length);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public Span<byte> GetSpan(int bytesNeeded)
|
||||
{
|
||||
if (_finished)
|
||||
{
|
||||
throw new InvalidOperationException("Attempted to use PacketContainerBuilder after finalize");
|
||||
}
|
||||
|
||||
if (Length > _bytes.Length - bytesNeeded)
|
||||
{
|
||||
Grow(bytesNeeded);
|
||||
}
|
||||
|
||||
return _bytes.Slice(Length);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public void Advance(int bytesWritten)
|
||||
{
|
||||
if (_finished)
|
||||
{
|
||||
throw new InvalidOperationException("Attempted to use PacketContainerBuilder after finalize");
|
||||
}
|
||||
|
||||
_count++;
|
||||
Length += bytesWritten;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private void Grow(int additionalCapacityBeyondPos)
|
||||
{
|
||||
var newLength = Math.Max(Length + additionalCapacityBeyondPos, _bytes.Length * 2);
|
||||
byte[] poolArray = ArrayPool<byte>.Shared.Rent(newLength);
|
||||
|
||||
_bytes.SliceToLength(Length).CopyTo(poolArray);
|
||||
|
||||
byte[]? toReturn = _arrayToReturnToPool;
|
||||
_bytes = _arrayToReturnToPool = poolArray;
|
||||
if (toReturn != null)
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(toReturn);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Dispose()
|
||||
{
|
||||
byte[]? toReturn = _arrayToReturnToPool;
|
||||
this = default; // for safety, to avoid using pooled array if this instance is erroneously appended to again
|
||||
if (toReturn != null)
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(toReturn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue