fix(core): Converts BB packets (#406)

- [X] Organizes and splits up bulletin boards
- [X] Converts packets
This commit is contained in:
Kamron Batman 2021-01-13 22:28:31 -08:00 • committed by GitHub
parent bae568d856
commit c58aad733d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 952 additions and 737 deletions

View file

@ -284,7 +284,7 @@ namespace Server.Network
}
else
{
index = _second.Slice(0, remaining).IndexOfTerminator(sizeT);
index = _second.SliceToLength(remaining).IndexOfTerminator(sizeT);
int secondLength = index < 0 ? remaining : index;
int length = firstLength + secondLength;
@ -292,7 +292,7 @@ namespace Server.Network
// Assume no strings should be too long for the stack
Span<byte> bytes = stackalloc byte[length];
_first.Slice(Position).CopyTo(bytes);
_second.Slice(0, secondLength).CopyTo(bytes.Slice(firstLength));
_second.SliceToLength(secondLength).CopyTo(bytes.Slice(firstLength));
Position += length + (index >= 0 ? sizeT : 0);
return TextEncoding.GetString(bytes, encoding, safeString);
@ -309,7 +309,7 @@ namespace Server.Network
if (index >= 0)
{
span = span.Slice(0, index);
span = span.SliceToLength(index);
}
else
{

View file

@ -18,6 +18,7 @@ using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using Server;
using Server.Network;
using Server.Text;
namespace System.Buffers
@ -363,7 +364,7 @@ namespace System.Buffers
if (Position < _first.Length)
{
var sz = Math.Min(buffer.Length, _first.Length - Position);
buffer.Slice(0, sz).CopyTo(_first.Slice(Position));
buffer.SliceToLength(sz).CopyTo(_first.Slice(Position));
if (sz < buffer.Length)
{
buffer.Slice(sz).CopyTo(_second);
@ -398,7 +399,7 @@ namespace System.Buffers
if (Position < _first.Length)
{
count = Math.Min(_first.Length - Position, byteCount);
bytes.Slice(0, count).CopyTo(_first.Slice(Position));
bytes.SliceToLength(count).CopyTo(_first.Slice(Position));
byteCount -= count;
Position += count;
}

View file

@ -21,6 +21,7 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using Server;
using Server.Network;
using Server.Text;
namespace System.Buffers
@ -50,7 +51,7 @@ namespace System.Buffers
public int Capacity => _buffer.Length;
public ReadOnlySpan<byte> Span => _buffer.Slice(0, Position);
public ReadOnlySpan<byte> Span => _buffer.SliceToLength(Position);
public Span<byte> RawBuffer => _buffer;
@ -78,7 +79,7 @@ namespace System.Buffers
var newSize = Math.Max(BytesWritten + additionalCapacity, _buffer.Length * 2);
byte[] poolArray = ArrayPool<byte>.Shared.Rent(newSize);
_buffer.Slice(0, BytesWritten).CopyTo(poolArray);
_buffer.SliceToLength(BytesWritten).CopyTo(poolArray);
byte[]? toReturn = _arrayToReturnToPool;
_buffer = _arrayToReturnToPool = poolArray;

View file

@ -5,6 +5,7 @@ using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Server.Network;
namespace Server.Buffers
{
@ -63,7 +64,7 @@ namespace Server.Buffers
public ref char this[int index] => ref _chars[index];
public override string ToString() => _chars.Slice(0, Length).ToString();
public override string ToString() => _chars.SliceToLength(Length).ToString();
/// <summary>Returns the underlying storage of the builder.</summary>
public Span<char> RawChars => _chars;
@ -79,16 +80,16 @@ namespace Server.Buffers
EnsureCapacity(Length + 1);
_chars[Length] = '\0';
}
return _chars.Slice(0, Length);
return _chars.SliceToLength(Length);
}
public ReadOnlySpan<char> AsSpan() => _chars.Slice(0, Length);
public ReadOnlySpan<char> AsSpan() => _chars.SliceToLength(Length);
public ReadOnlySpan<char> AsSpan(int start) => _chars.Slice(start, Length - start);
public ReadOnlySpan<char> AsSpan(int start, int length) => _chars.Slice(start, length);
public bool TryCopyTo(Span<char> destination, out int charsWritten)
{
if (_chars.Slice(0, Length).TryCopyTo(destination))
if (_chars.SliceToLength(Length).TryCopyTo(destination))
{
charsWritten = Length;
return true;
@ -275,7 +276,7 @@ namespace Server.Buffers
{
char[] poolArray = ArrayPool<char>.Shared.Rent(Math.Max(Length + additionalCapacityBeyondPos, _chars.Length * 2));
_chars.Slice(0, Length).CopyTo(poolArray);
_chars.SliceToLength(Length).CopyTo(poolArray);
char[]? toReturn = _arrayToReturnToPool;
_chars = _arrayToReturnToPool = poolArray;
@ -349,7 +350,7 @@ namespace Server.Buffers
}
else if (startIndex + length == Length)
{
_chars = _chars.Slice(0, startIndex);
_chars = _chars.SliceToLength(startIndex);
}
else
{

View file

@ -1154,7 +1154,7 @@ namespace Server
var length = OutgoingItemPackets.CreateWorldItemNew(hsWorldItem, this, true);
if (length != hsWorldItem.Length)
{
hsWorldItem = hsWorldItem.Slice(0, length);
hsWorldItem = hsWorldItem.SliceToLength(length);
}
SendInfoTo(state, hsWorldItem, opl);
@ -1164,7 +1164,7 @@ namespace Server
var length = OutgoingItemPackets.CreateWorldItemNew(saWorldItem, this, false);
if (length != saWorldItem.Length)
{
saWorldItem = saWorldItem.Slice(0, length);
saWorldItem = saWorldItem.SliceToLength(length);
}
SendInfoTo(state, saWorldItem, opl);
@ -1174,7 +1174,7 @@ namespace Server
var length = OutgoingItemPackets.CreateWorldItem(oldWorldItem, this);
if (length != oldWorldItem.Length)
{
oldWorldItem = oldWorldItem.Slice(0, length);
oldWorldItem = oldWorldItem.SliceToLength(length);
}
SendInfoTo(state, oldWorldItem, opl);
@ -3345,7 +3345,7 @@ namespace Server
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size
buffer = buffer.SliceToLength(length); // Adjust to the actual size
}
state.Send(buffer);
@ -3379,7 +3379,7 @@ namespace Server
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size
buffer = buffer.SliceToLength(length); // Adjust to the actual size
}
state.Send(buffer);

View file

@ -5796,7 +5796,7 @@ namespace Server
if (length != regBuffer.Length)
{
regBuffer = regBuffer.Slice(0, length); // Adjust to the actual size
regBuffer = regBuffer.SliceToLength(length); // Adjust to the actual size
}
heard.OnSpeech(regArgs);
@ -5810,7 +5810,7 @@ namespace Server
if (length != mutBuffer.Length)
{
mutBuffer = mutBuffer.Slice(0, length); // Adjust to the actual size
mutBuffer = mutBuffer.SliceToLength(length); // Adjust to the actual size
}
heard.OnSpeech(mutatedArgs);
@ -9162,7 +9162,7 @@ namespace Server
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size
buffer = buffer.SliceToLength(length); // Adjust to the actual size
}
state.Send(buffer);
@ -9193,7 +9193,7 @@ namespace Server
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size
buffer = buffer.SliceToLength(length); // Adjust to the actual size
}
state.Send(buffer);
@ -9227,7 +9227,7 @@ namespace Server
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size
buffer = buffer.SliceToLength(length); // Adjust to the actual size
}
state.Send(buffer);
@ -9275,7 +9275,7 @@ namespace Server
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size
buffer = buffer.SliceToLength(length); // Adjust to the actual size
}
state.Send(buffer);
@ -9306,7 +9306,7 @@ namespace Server
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size
buffer = buffer.SliceToLength(length); // Adjust to the actual size
}
state.Send(buffer);

View file

@ -40,6 +40,12 @@ namespace Server.Network
writer.Seek(0, SeekOrigin.End);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<T> SliceToLength<T>(this Span<T> span, int length) => span.Slice(0, length);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan<T> SliceToLength<T>(this ReadOnlySpan<T> span, int length) => span.Slice(0, length);
// If LOCAL INIT is off, then stack/heap allocations have garbage data
// Initializes the first byte (Packet ID) so it can be used as a flag.
[MethodImpl(MethodImplOptions.AggressiveInlining)]

View file

@ -29,7 +29,7 @@ namespace Server.Network
{
return buffer.Length;
}
var itemID = item is BaseMulti ? item.ItemID | 0x4000 : item.ItemID & 0x3FFF;
var hasAmount = item.Amount != 0;
var amount = item.Amount;
@ -95,7 +95,7 @@ namespace Server.Network
CreateWorldItemNew(buffer, item, ns.HighSeas) :
CreateWorldItem(buffer, item);
ns.Send(buffer.Slice(0, length));
ns.Send(buffer.SliceToLength(length));
}
public static int CreateWorldItemNew(Span<byte> buffer, Item item, bool isHS)

View file

@ -46,7 +46,7 @@ namespace Server.Network
buffer, serial, graphic, type, hue, font, number, name, args
);
ns.Send(buffer.Slice(0, length));
ns.Send(buffer.SliceToLength(length));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -102,7 +102,7 @@ namespace Server.Network
buffer, serial, graphic, type, hue, font, number, name, affixType, affix, args
);
ns.Send(buffer.Slice(0, length));
ns.Send(buffer.SliceToLength(length));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -171,7 +171,7 @@ namespace Server.Network
text
);
ns.Send(buffer.Slice(0, length));
ns.Send(buffer.SliceToLength(length));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]

View file

@ -470,7 +470,7 @@ namespace Server.Network
}
var length = CreateMobileStatus(buffer, beholder, beheld, version, beheld.CanBeRenamedBy(beholder));
ns.Send(buffer.Slice(0, length));
ns.Send(buffer.SliceToLength(length));
}
public static int CreateMobileStatus(

View file

@ -20,6 +20,7 @@ using System.IO;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text;
using Server.Network;
using Server.Text;
namespace Server
@ -222,7 +223,7 @@ namespace Server
Span<byte> stack = stackalloc byte[16];
value.TryWriteBytes(stack, out var bytesWritten);
Write((byte)bytesWritten);
Write(stack.Slice(0, bytesWritten));
Write(stack.SliceToLength(bytesWritten));
}
public void Write(TimeSpan value)
@ -438,7 +439,7 @@ namespace Server
charsLeft -= charCount;
current += charCount;
Write(span.Slice(0, bytesWritten));
Write(span.SliceToLength(bytesWritten));
}
}
}

View file

@ -18,6 +18,7 @@ using System.Buffers;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Server.Network;
namespace Server
{
@ -57,7 +58,7 @@ namespace Server
throw new OutOfMemoryException(nameof(buffer));
}
sliced.Slice(0, indexOf).CopyTo(buffer.Slice(size));
sliced.SliceToLength(indexOf).CopyTo(buffer.Slice(size));
size += indexOf;
if (indexOf == sliced.Length)
@ -96,7 +97,7 @@ namespace Server
a.Remove(b, comparison, span, out var size);
var str = span.Slice(0, size).ToString();
var str = span.SliceToLength(size).ToString();
if (chrs != null)
{
@ -192,7 +193,7 @@ namespace Server
if (newLineLength == perLine || newLineLength == span.Length)
{
list.Add(span.Slice(0, newLineLength).ToString());
list.Add(span.SliceToLength(newLineLength).ToString());
if (list.Count == maxLines || newLineLength == span.Length)
{
break;
@ -207,7 +208,7 @@ namespace Server
}
else if (lineLength > 0 && lineLength <= perLine)
{
list.Add(span.Slice(0, lineLength - 1).ToString());
list.Add(span.SliceToLength(lineLength - 1).ToString());
if (list.Count == maxLines)
{
break;

View file

@ -10,6 +10,7 @@ using System.Runtime.CompilerServices;
using System.Text;
using System.Xml;
using Microsoft.Toolkit.HighPerformance.Extensions;
using Server.Network;
using Server.Random;
namespace Server
@ -386,7 +387,7 @@ namespace Server
var sectionStart = 0;
var hasCompressor = false;
var num = BinaryPrimitives.ReadUInt16BigEndian(ip.Slice(0, 2));
var num = BinaryPrimitives.ReadUInt16BigEndian(ip.SliceToLength(2));
for (int i = 0; i < end; i++)
{

View file

@ -167,7 +167,7 @@ namespace Server
if (length != buffer.Length)
{
buffer = buffer.Slice(0, length); // Adjust to the actual size
buffer = buffer.SliceToLength(length); // Adjust to the actual size
}
ns.Send(buffer);