fix: Adds ISpanFormattable support to Serial (#1065)
This commit is contained in:
parent
4b2b7e2277
commit
b779d7737f
18 changed files with 269 additions and 204 deletions
|
|
@ -16,120 +16,148 @@
|
|||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Server
|
||||
namespace Server;
|
||||
|
||||
public readonly struct Serial : IComparable<Serial>, IComparable<uint>, IEquatable<Serial>, ISpanFormattable
|
||||
{
|
||||
public readonly struct Serial : IComparable<Serial>, IComparable<uint>, IEquatable<Serial>
|
||||
public static readonly Serial MinusOne = new(0xFFFFFFFF);
|
||||
public static readonly Serial Zero = new(0);
|
||||
|
||||
private Serial(uint serial) => Value = serial;
|
||||
|
||||
public uint Value { get; }
|
||||
|
||||
public bool IsMobile
|
||||
{
|
||||
public static readonly Serial MinusOne = new(0xFFFFFFFF);
|
||||
public static readonly Serial Zero = new(0);
|
||||
|
||||
private Serial(uint serial) => Value = serial;
|
||||
|
||||
public uint Value { get; }
|
||||
|
||||
public bool IsMobile
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => Value > 0 && Value < World.ItemOffset;
|
||||
}
|
||||
|
||||
public bool IsItem
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => Value >= World.ItemOffset && Value < World.MaxItemSerial;
|
||||
}
|
||||
|
||||
public bool IsValid
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => Value > 0;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override int GetHashCode() => Value.GetHashCode();
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int CompareTo(Serial other) => Value.CompareTo(other.Value);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int CompareTo(uint other) => Value.CompareTo(other);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override bool Equals(object obj) =>
|
||||
obj switch
|
||||
{
|
||||
Serial serial => this == serial,
|
||||
uint u => Value == u,
|
||||
_ => false
|
||||
};
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator ==(Serial l, Serial r) => l.Value == r.Value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator ==(Serial l, uint r) => l.Value == r;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator !=(Serial l, Serial r) => l.Value != r.Value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator !=(Serial l, uint r) => l.Value != r;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator >(Serial l, Serial r) => l.Value > r.Value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator >(Serial l, uint r) => l.Value > r;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator <(Serial l, Serial r) => l.Value < r.Value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator <(Serial l, uint r) => l.Value < r;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator >=(Serial l, Serial r) => l.Value >= r.Value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator >=(Serial l, uint r) => l.Value >= r;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator <=(Serial l, Serial r) => l.Value <= r.Value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator <=(Serial l, uint r) => l.Value <= r;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Serial operator +(Serial l, Serial r) => (Serial)(l.Value + r.Value);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Serial operator +(Serial l, uint r) => (Serial)(l.Value + r);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Serial operator ++(Serial l) => (Serial)(l.Value + 1);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Serial operator -(Serial l, Serial r) => (Serial)(l.Value - r.Value);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Serial operator -(Serial l, uint r) => (Serial)(l.Value - r);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Serial operator --(Serial l) => (Serial)(l.Value - 1);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override string ToString() => $"0x{Value:X8}";
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static explicit operator uint(Serial a) => a.Value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static explicit operator Serial(uint a) => new(a);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool Equals(Serial other) => Value == other.Value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int ToInt32() => (int)Value;
|
||||
get => Value > 0 && Value < World.ItemOffset;
|
||||
}
|
||||
|
||||
public bool IsItem
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => Value >= World.ItemOffset && Value < World.MaxItemSerial;
|
||||
}
|
||||
|
||||
public bool IsValid
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => Value > 0;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override int GetHashCode() => Value.GetHashCode();
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int CompareTo(Serial other) => Value.CompareTo(other.Value);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int CompareTo(uint other) => Value.CompareTo(other);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override bool Equals(object obj) =>
|
||||
obj switch
|
||||
{
|
||||
Serial serial => this == serial,
|
||||
uint u => Value == u,
|
||||
_ => false
|
||||
};
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator ==(Serial l, Serial r) => l.Value == r.Value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator ==(Serial l, uint r) => l.Value == r;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator !=(Serial l, Serial r) => l.Value != r.Value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator !=(Serial l, uint r) => l.Value != r;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator >(Serial l, Serial r) => l.Value > r.Value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator >(Serial l, uint r) => l.Value > r;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator <(Serial l, Serial r) => l.Value < r.Value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator <(Serial l, uint r) => l.Value < r;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator >=(Serial l, Serial r) => l.Value >= r.Value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator >=(Serial l, uint r) => l.Value >= r;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator <=(Serial l, Serial r) => l.Value <= r.Value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator <=(Serial l, uint r) => l.Value <= r;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Serial operator +(Serial l, Serial r) => (Serial)(l.Value + r.Value);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Serial operator +(Serial l, uint r) => (Serial)(l.Value + r);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Serial operator ++(Serial l) => (Serial)(l.Value + 1);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Serial operator -(Serial l, Serial r) => (Serial)(l.Value - r.Value);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Serial operator -(Serial l, uint r) => (Serial)(l.Value - r);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Serial operator --(Serial l) => (Serial)(l.Value - 1);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override string ToString() => $"0x{Value:X8}";
|
||||
|
||||
public string ToString(string format, IFormatProvider formatProvider) => ToString();
|
||||
|
||||
public bool TryFormat(
|
||||
Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider
|
||||
)
|
||||
{
|
||||
if (format != null)
|
||||
{
|
||||
return Value.TryFormat(destination, out charsWritten, format, provider);
|
||||
}
|
||||
|
||||
if (destination.Length < 10)
|
||||
{
|
||||
charsWritten = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
destination[0] = '0';
|
||||
destination[1] = 'x';
|
||||
|
||||
var result = Value.TryFormat(destination[2..], out charsWritten, "X8", provider);
|
||||
if (result)
|
||||
{
|
||||
charsWritten += 2;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static explicit operator uint(Serial a) => a.Value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static explicit operator Serial(uint a) => new(a);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool Equals(Serial other) => Value == other.Value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int ToInt32() => (int)Value;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue