fix: Adds ISpanFormattable support to Serial (#1065)

This commit is contained in:
Kamron Batman 2022-06-14 18:39:46 -07:00 committed by GitHub
parent 4b2b7e2277
commit b779d7737f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 269 additions and 204 deletions

View file

@ -135,6 +135,6 @@ namespace Server.Guilds
return results;
}
public override string ToString() => $"0x{Serial.Value:X} \"{Name} [{Abbreviation}]\"";
public override string ToString() => $"{Serial} \"{Name} [{Abbreviation}]\"";
}
}

View file

@ -3165,10 +3165,10 @@ namespace Server
if (item == this)
{
Console.WriteLine(
"Warning: Adding item to itself: [0x{0:X} {1}].AddItem( [0x{2:X} {3}] )",
Serial.Value,
"Warning: Adding item to itself: [0x{0} {1}].AddItem( [0x{2} {3}] )",
Serial,
GetType().Name,
item.Serial.Value,
item.Serial,
item.GetType().Name
);
Console.WriteLine(new StackTrace());
@ -3178,10 +3178,10 @@ namespace Server
if (IsChildOf(item))
{
Console.WriteLine(
"Warning: Adding parent item to child: [0x{0:X} {1}].AddItem( [0x{2:X} {3}] )",
Serial.Value,
"Warning: Adding parent item to child: [0x{0} {1}].AddItem( [0x{2} {3}] )",
Serial,
GetType().Name,
item.Serial.Value,
item.Serial,
item.GetType().Name
);
Console.WriteLine(new StackTrace());
@ -4245,7 +4245,7 @@ namespace Server
public virtual bool IsStandardLoot() =>
(!Mobile.InsuranceEnabled || !Insured) && BlessedFor == null && m_LootType == LootType.Regular;
public override string ToString() => $"0x{Serial.Value:X} \"{GetType().Name}\"";
public override string ToString() => $"{Serial} \"{GetType().Name}\"";
public virtual void OnSectorActivate()
{

View file

@ -3790,7 +3790,7 @@ namespace Server
}
}
public override string ToString() => $"0x{Serial.Value:X} \"{Name}\"";
public override string ToString() => $"{Serial} \"{Name}\"";
public virtual void SendSkillMessage()
{
@ -5311,8 +5311,8 @@ namespace Server
catch
{
Console.WriteLine(
"Warning: 0x{0:X}: Item must have a zero parameter constructor to be separated from a stack. '{1}'.",
oldItem.Serial.Value,
"Warning: {0}: Item must have a zero parameter constructor to be separated from a stack. '{1}'.",
oldItem.Serial,
oldItem.GetType().Name
);
return null;

View file

@ -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;
}