fix(core): Adds Gump component functionality (#377)

- [X] Adds gump close packet
- [X] Fixes gump tooltip
- [X] Adds new compile/append functions for the new gump packets
This commit is contained in:
Kamron Batman 2021-01-02 19:08:35 -08:00 • committed by GitHub
parent cc91fe2b10
commit 3b413371dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
40 changed files with 1696 additions and 137 deletions

View file

@ -0,0 +1,123 @@
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using Server.Collections;
namespace Benchmarks
{
[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.NetCoreApp50)]
public class BenchmarkOrderedHashSet
{
private List<string> _list;
private OrderedHashSet<string> _ordered;
private HashSet<string> _hashSet;
private const int iterations = 16;
[IterationSetup]
public void IterationSetup()
{
_list = new List<string>();
_ordered = new OrderedHashSet<string>();
_hashSet = new HashSet<string>();
}
[IterationCleanup]
public void IterationCleanup()
{
_list = null;
_ordered = null;
_hashSet = null;
}
[Benchmark]
public int UsingList()
{
for (int i = 0; i < iterations / 2; i++)
{
AddIfNotPresent(_list, i.ToString());
}
for (int i = 0; i < iterations; i++)
{
AddIfNotPresent(_list, i.ToString());
}
for (int i = 0; i < iterations * 2; i++)
{
AddIfNotPresent(_list, i.ToString());
}
for (int i = 0; i < _list.Count; i++)
{
_list[i].ToString();
}
return _list.Count;
}
private static int AddIfNotPresent<T>(List<T> list, T item)
{
var index = list.IndexOf(item);
if (index > -1)
{
return index;
}
list.Add(item);
return list.Count - 1;
}
[Benchmark]
public int UsingOrderedHashSet()
{
for (int i = 0; i < iterations / 2; i++)
{
_ordered.GetOrAdd(i.ToString());
}
for (int i = 0; i < iterations; i++)
{
_ordered.GetOrAdd(i.ToString());
}
for (int i = 0; i < iterations * 2; i++)
{
_ordered.GetOrAdd(i.ToString());
}
foreach (var str in _ordered)
{
str.ToString();
}
return _ordered.Count;
}
[Benchmark]
public int UsingHashSet()
{
for (int i = 0; i < iterations / 2; i++)
{
_hashSet.Add(i.ToString());
}
for (int i = 0; i < iterations; i++)
{
_hashSet.Add(i.ToString());
}
for (int i = 0; i < iterations * 2; i++)
{
_hashSet.Add(i.ToString());
}
foreach (var str in _hashSet)
{
str.ToString();
}
return _hashSet.Count;
}
}
}

View file

@ -0,0 +1,90 @@
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Security.Cryptography;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using Server;
using Server.Items;
namespace Benchmarks
{
[SimpleJob(RuntimeMoniker.NetCoreApp50)]
public class BenchmarkFeatureFlags
{
public Dictionary<Type, FeatureFlag<Item>> m_Dictionary;
public ILookup<Type, FeatureFlag<Item>> m_Lookup;
public Type[] m_TypesToLookUp;
[GlobalSetup]
public void Setup()
{
RNGCryptoServiceProvider csp = new RNGCryptoServiceProvider();
string file = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "UOContent.dll");
Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(file);
m_Dictionary = new Dictionary<Type, FeatureFlag<Item>>();
List<FeatureFlag<Item>> m_Types = new List<FeatureFlag<Item>>();
m_TypesToLookUp = new Type[100];
foreach (var type in assembly.GetTypes())
{
if (typeof(Item).IsAssignableFrom(type))
{
m_Dictionary.Add(type, new FeatureFlag<Item>());
m_Types.Add(new FeatureFlag<Item>{Type = type});
}
}
Console.WriteLine("Dictionary Size: {0}", m_Dictionary.Count);
Console.WriteLine("Lookup Size: {0}", m_Types.Count);
m_Dictionary.TrimExcess();
m_Lookup = m_Types.ToLookup(f => f.Type);
Span<byte> bytes = stackalloc byte[4];
for (int i = 0; i < 100; i++)
{
csp.GetBytes(bytes);
m_TypesToLookUp[i] = m_Types[(int)(BinaryPrimitives.ReadUInt32BigEndian(bytes) % m_Types.Count)].Type;
}
}
[Benchmark]
public FeatureFlag<Item> TestDictionary()
{
for (int i = 0; i < 100; i++)
{
m_Dictionary.TryGetValue(typeof(ExplosionPotion), out var ff);
if (i == 99)
{
return ff;
}
}
return null;
}
[Benchmark]
public FeatureFlag<Item> TestLookup()
{
FeatureFlag<Item> ff;
for (int i = 0; i < 100; i++)
{
ff = m_Lookup[typeof(ExplosionPotion)].GetEnumerator().Current;
if (i == 99)
{
return ff;
}
}
return null;
}
}
}

View file

@ -0,0 +1,9 @@
using System;
namespace Server
{
public class FeatureFlag<T> where T : Item
{
public Type Type { get; set; }
}
}

View file

@ -0,0 +1,174 @@
using System;
using System.Buffers;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using Server;
using Server.Network;
namespace Benchmarks
{
[SimpleJob(RuntimeMoniker.NetCoreApp50)]
public class BenchmarkPacketBroadcast
{
public static int SendUnicodeMessage(
ArraySegment<byte>[] buffer,
Serial serial, int graphic, MessageType type, int hue, int font, string lang, string name, string text
)
{
name = name?.Trim() ?? "";
text = text?.Trim() ?? "";
lang = lang?.Trim() ?? "ENU";
if (hue == 0)
{
hue = 0x3B2;
}
var writer = new CircularBufferWriter(buffer);
writer.Write((byte)0xAE);
writer.Write((ushort)(50 + text.Length * 2));
writer.Write(serial);
writer.Write((short)graphic);
writer.Write((byte)type);
writer.Write((short)hue);
writer.Write((short)font);
writer.WriteAscii(lang, 4);
writer.WriteAscii(name, 30);
writer.WriteBigUniNull(text);
return writer.Position;
}
public static int CreateUnicodeMessage(
Span<byte> buffer,
Serial serial, int graphic, MessageType type, int hue, int font, string lang, string name, string text
)
{
name = name?.Trim() ?? "";
text = text?.Trim() ?? "";
lang = lang?.Trim() ?? "ENU";
if (hue == 0)
{
hue = 0x3B2;
}
var writer = new SpanWriter(buffer);
writer.Write((byte)0xAE);
writer.Write((ushort)(50 + text.Length * 2));
writer.Write(serial);
writer.Write((short)graphic);
writer.Write((byte)type);
writer.Write((short)hue);
writer.Write((short)font);
writer.WriteAscii(lang, 4);
writer.WriteAscii(name, 30);
writer.WriteBigUniNull(text);
return writer.Position;
}
private Pipe<byte>[] _pipes = new Pipe<byte>[25000];
[IterationSetup]
public void SetUp()
{
for (var i = 0; i < _pipes.Length; i++)
{
_pipes[i] = new Pipe<byte>(new byte[4096]);
}
}
[IterationCleanup]
public void CleanUp()
{
for (var i = 0; i < _pipes.Length; i++)
{
_pipes[i] = null;
}
}
[Benchmark]
public int TestCircularBuffer()
{
var text = "This is some really long text that we want to handle. It should take a little bit to encode this.";
foreach (var pipe in _pipes)
{
var result = pipe.Writer.TryGetMemory();
var length = SendUnicodeMessage(
result.Buffer,
Serial.MinusOne, -1, MessageType.Regular, 0x3B2, 3, "ENU", "System", text
);
pipe.Writer.Advance((uint)length);
}
return _pipes.Length;
}
[Benchmark]
public int TestSpanWriterFromBuffer()
{
var text = "This is some really long text that we want to handle. It should take a little bit to encode this.";
foreach (var pipe in _pipes)
{
var result = pipe.Writer.TryGetMemory();
Span<byte> buffer = result.Buffer[0];
var length = CreateUnicodeMessage(
buffer, Serial.MinusOne, -1, MessageType.Regular, 0x3B2, 3, "ENU", "System", text
);
pipe.Writer.Advance((uint)length);
}
return _pipes.Length;
}
[Benchmark]
public int TestSpanWriter()
{
var text = "This is some really long text that we want to handle. It should take a little bit to encode this.";
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)];
var length = CreateUnicodeMessage(
buffer, Serial.MinusOne, -1, MessageType.Regular, 0x3B2, 3, "ENU", "System", text
);
buffer = buffer.Slice(0, length);
foreach (var pipe in _pipes)
{
var result = pipe.Writer.TryGetMemory();
result.CopyFrom(buffer);
pipe.Writer.Advance((uint)buffer.Length);
}
return _pipes.Length;
}
private static void SendUnicodeMessageWithSpan(Pipe<byte> pipe, string text)
{
Span<byte> buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)];
var length = CreateUnicodeMessage(
buffer, Serial.MinusOne, -1, MessageType.Regular, 0x3B2, 3, "ENU", "System", text
);
buffer = buffer.Slice(0, length);
var result = pipe.Writer.TryGetMemory();
result.CopyFrom(buffer);
pipe.Writer.Advance((uint)buffer.Length);
}
[Benchmark]
public int TestSpanWriterLooped()
{
var text = "This is some really long text that we want to handle. It should take a little bit to encode this.";
foreach (var pipe in _pipes)
{
SendUnicodeMessageWithSpan(pipe, text);
}
return _pipes.Length;
}
}
}

View file

@ -0,0 +1,27 @@
using System;
using System.Buffers.Binary;
using System.Runtime.CompilerServices;
using Server;
namespace Benchmarks
{
public static class PacketTestUtilities
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Write(this Span<byte> data, ref int pos, Serial serial)
{
BinaryPrimitives.WriteUInt32BigEndian(data.Slice(pos, 4), serial);
pos += 4;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Write(this Span<byte> data, ref int pos, ushort value)
{
BinaryPrimitives.WriteUInt16BigEndian(data.Slice(pos, 2), value);
pos += 2;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Write(this Span<byte> data, ref int pos, byte value) => data[pos++] = value;
}
}

View file

@ -0,0 +1,87 @@
using System.Buffers;
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using Server.Buffers;
namespace Benchmarks.BenchmarkUtilities
{
[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.NetCoreApp50)]
public class BenchmarkStringHelpers
{
private readonly string[] names =
{
"Kamron", "Owyn", "Luthius", "Jaedan", "Vorspire", "other people",
"Kamron-2", "Owyn-2", "Luthius-2", "Jaedan-2", "Vorspire-2", "other people too"
};
private int length;
[GlobalSetup]
public void Setup()
{
var chrs = ArrayPool<char>.Shared.Rent(65535);
ArrayPool<char>.Shared.Return(chrs);
length = 0;
for (int i = 0; i < names.Length; i++)
{
length += names.Length;
}
length += 2 * (names.Length - 1) + 3;
}
[Benchmark]
public string BenchmarkStringBuilder()
{
var sb = new StringBuilder();
for (var i = 0; i < names.Length; i++)
{
if (i > 0)
{
sb.Append(i == names.Length - 1 ? ", and" : ", ");
}
sb.Append(names[i]);
}
return sb.ToString();
}
[Benchmark]
public string BenchmarkValueStringBuilderWithStack()
{
using var sb = new ValueStringBuilder(stackalloc char[length]);
for (var i = 0; i < names.Length; i++)
{
if (i > 0)
{
sb.Append(i == names.Length - 1 ? ", and" : ", ");
}
sb.Append(names[i]);
}
return sb.ToString();
}
[Benchmark]
public string BenchmarkValueStringBuilderWithRentedBuffer()
{
using var sb = new ValueStringBuilder(stackalloc char[32]);
for (var i = 0; i < names.Length; i++)
{
if (i > 0)
{
sb.Append(i == names.Length - 1 ? ", and" : ", ");
}
sb.Append(names[i]);
}
return sb.ToString();
}
}
}