fix: Adds ISpanParsable and fixes command conditionals (#1241)

* Adds `ISpanParsable<T>`
* Removes `[Parsable]`
* Fixes querying by serial, body, and a few others.
* Adds `Parse` to `Rectangle3D`
* Fixes AutoArchive NPE


Closes #1209
This commit is contained in:
Kamron Batman 2022-11-12 00:26:02 -08:00 committed by GitHub
parent d494a9c78c
commit 03fb36c869
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 1119 additions and 271 deletions

View file

@ -14,13 +14,13 @@
*************************************************************************/
using System;
using System.Runtime.CompilerServices;
namespace Server;
[Parsable]
public struct Point2D
: IPoint2D, IComparable<Point2D>, IComparable<IPoint2D>, IEquatable<object>, IEquatable<Point2D>,
IEquatable<IPoint2D>, ISpanFormattable
IEquatable<IPoint2D>, ISpanFormattable, ISpanParsable<Point2D>
{
internal int m_X;
internal int m_Y;
@ -51,21 +51,6 @@ public struct Point2D
{
}
public static Point2D Parse(string value)
{
var start = value.IndexOfOrdinal('(');
var end = value.IndexOf(',', start + 1);
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var x);
start = end;
end = value.IndexOf(')', start + 1);
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var y);
return new Point2D(x, y);
}
public bool Equals(Point2D other) => m_X == other.m_X && m_Y == other.m_Y;
public bool Equals(IPoint2D other) => m_X == other?.X && m_Y == other.Y;
@ -130,4 +115,79 @@ public struct Point2D
// default ToString implementation.
return ToString();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Point2D Parse(string s) => Parse(s, null);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Point2D Parse(string s, IFormatProvider provider) => Parse(s.AsSpan(), provider);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryParse(string s, IFormatProvider provider, out Point2D result) =>
TryParse(s.AsSpan(), provider, out result);
public static Point2D Parse(ReadOnlySpan<char> s, IFormatProvider provider)
{
s = s.Trim();
if (!s.StartsWithOrdinal('(') || !s.EndsWithOrdinal(')'))
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
var comma = s.IndexOfOrdinal(',');
if (comma == -1)
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
var first = s.Slice(1, comma - 1).Trim();
if (!Utility.ToInt32(first, out var x))
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
var second = s.Slice(comma + 1, s.Length - comma - 2).Trim();
if (!Utility.ToInt32(second, out var y))
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
return new Point2D(x, y);
}
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out Point2D result)
{
s = s.Trim();
if (!s.StartsWithOrdinal('(') || !s.EndsWithOrdinal(')'))
{
result = default;
return false;
}
var comma = s.IndexOfOrdinal(',');
if (comma == -1)
{
result = default;
return false;
}
var first = s.Slice(1, comma - 1).Trim();
if (!Utility.ToInt32(first, out var x))
{
result = default;
return false;
}
var second = s.Slice(comma + 1, s.Length - comma - 2).Trim();
if (!Utility.ToInt32(second, out var y))
{
result = default;
return false;
}
result = new Point2D(x, y);
return true;
}
}

View file

@ -18,10 +18,9 @@ using System.Runtime.CompilerServices;
namespace Server;
[Parsable]
public struct Point3D
: IPoint3D, IComparable<Point3D>, IComparable<IPoint3D>, IEquatable<object>, IEquatable<Point3D>,
IEquatable<IPoint3D>, ISpanFormattable
IEquatable<IPoint3D>, ISpanFormattable, ISpanParsable<Point3D>
{
internal int m_X;
internal int m_Y;
@ -79,26 +78,6 @@ public struct Point3D
public override int GetHashCode() => HashCode.Combine(m_X, m_Y, m_Z);
public static Point3D Parse(string value)
{
var start = value.IndexOfOrdinal('(');
var end = value.IndexOf(',', start + 1);
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var x);
start = end;
end = value.IndexOf(',', start + 1);
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var y);
start = end;
end = value.IndexOf(')', start + 1);
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var z);
return new Point3D(x, y, z);
}
public static bool operator ==(Point3D l, Point3D r) => l.m_X == r.m_X && l.m_Y == r.m_Y && l.m_Z == r.m_Z;
public static bool operator ==(Point3D l, IPoint3D r) =>
@ -183,4 +162,115 @@ public struct Point3D
// default ToString implementation.
return ToString();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Point3D Parse(string s) => Parse(s, null);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Point3D Parse(string s, IFormatProvider provider) => Parse(s.AsSpan(), provider);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryParse(string s, IFormatProvider provider, out Point3D result) =>
TryParse(s.AsSpan(), provider, out result);
public static Point3D Parse(ReadOnlySpan<char> s, IFormatProvider provider)
{
s = s.Trim();
if (!s.StartsWithOrdinal('(') || !s.EndsWithOrdinal(')'))
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
var firstComma = s.IndexOfOrdinal(',');
if (firstComma == -1)
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
var first = s.Slice(1, firstComma - 1).Trim();
if (!Utility.ToInt32(first, out var x))
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
var offset = firstComma + 1;
var secondComma = s[offset..].IndexOfOrdinal(',');
if (secondComma == -1 || offset == secondComma)
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
var second = s.Slice(firstComma + 1, secondComma).Trim();
if (!Utility.ToInt32(second, out var y))
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
offset += secondComma + 1;
var third = s.Slice(offset, s.Length - offset - 1).Trim();
if (!Utility.ToInt32(third, out var z))
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
return new Point3D(x, y, z);
}
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out Point3D result)
{
s = s.Trim();
if (!s.StartsWithOrdinal('(') || !s.EndsWithOrdinal(')'))
{
result = default;
return false;
}
var firstComma = s.IndexOfOrdinal(',');
if (firstComma == -1)
{
result = default;
return false;
}
var first = s.Slice(1, firstComma - 1).Trim();
if (!Utility.ToInt32(first, out var x))
{
result = default;
return false;
}
var offset = firstComma + 1;
var secondComma = s[offset..].IndexOfOrdinal(',');
if (secondComma == -1 || offset == secondComma)
{
result = default;
return false;
}
var second = s.Slice(firstComma + 1, secondComma).Trim();
if (!Utility.ToInt32(second, out var y))
{
result = default;
return false;
}
offset += secondComma + 1;
var third = s.Slice(offset, s.Length - offset - 1).Trim();
if (!Utility.ToInt32(third, out var z))
{
result = default;
return false;
}
result = new Point3D(x, y, z);
return true;
}
}

View file

@ -14,13 +14,13 @@
*************************************************************************/
using System;
using System.Runtime.CompilerServices;
namespace Server;
[NoSort]
[Parsable]
[PropertyObject]
public struct Rectangle2D : IEquatable<Rectangle2D>, ISpanFormattable
public struct Rectangle2D : IEquatable<Rectangle2D>, ISpanFormattable, ISpanParsable<Rectangle2D>
{
private Point2D _start;
private Point2D _end;
@ -118,31 +118,6 @@ public struct Rectangle2D : IEquatable<Rectangle2D>, ISpanFormattable
public static bool operator !=(Rectangle2D l, Rectangle2D r) => l._start != r._start || l._end != r._end;
public static Rectangle2D Parse(string value)
{
var start = value.IndexOfOrdinal('(');
var end = value.IndexOf(',', start + 1);
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var x);
start = end;
end = value.IndexOf(',', start + 1);
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var y);
start = end;
end = value.IndexOf(',', start + 1);
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var w);
start = end;
end = value.IndexOf(')', start + 1);
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var h);
return new Rectangle2D(x, y, w, h);
}
public bool Contains(Point3D p) =>
_start.m_X <= p.m_X && _start.m_Y <= p.m_Y && _end.m_X > p.m_X && _end.m_Y > p.m_Y;
@ -172,4 +147,64 @@ public struct Rectangle2D : IEquatable<Rectangle2D>, ISpanFormattable
// default ToString implementation.
return ToString();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle2D Parse(string s) => Parse(s, null);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle2D Parse(string s, IFormatProvider provider) => Parse(s.AsSpan(), provider);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryParse(string s, IFormatProvider provider, out Rectangle2D result) =>
TryParse(s.AsSpan(), provider, out result);
public static Rectangle2D Parse(ReadOnlySpan<char> s, IFormatProvider provider)
{
s = s.Trim();
var delimiter = s.IndexOfOrdinal('+');
if (delimiter == -1)
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
if (!Point2D.TryParse(s[..delimiter], provider, out var start))
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
if (!Point2D.TryParse(s[(delimiter + 1)..], provider, out var end))
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
return new Rectangle2D(start, end);
}
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out Rectangle2D result)
{
s = s.Trim();
var delimiter = s.IndexOfOrdinal('+');
if (delimiter == -1)
{
result = default;
return false;
}
if (!Point2D.TryParse(s[..delimiter], provider, out var start))
{
result = default;
return false;
}
if (!Point2D.TryParse(s[(delimiter + 1)..], provider, out var end))
{
result = default;
return false;
}
result = new Rectangle2D(start, end);
return true;
}
}

View file

@ -14,11 +14,11 @@
*************************************************************************/
using System;
using System.Runtime.CompilerServices;
namespace Server;
[NoSort]
[Parsable]
[PropertyObject]
public struct Rectangle3D : IEquatable<Rectangle3D>, ISpanFormattable
{
@ -172,4 +172,64 @@ public struct Rectangle3D : IEquatable<Rectangle3D>, ISpanFormattable
// default ToString implementation.
return ToString();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle3D Parse(string s) => Parse(s, null);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle3D Parse(string s, IFormatProvider provider) => Parse(s.AsSpan(), provider);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryParse(string s, IFormatProvider provider, out Rectangle3D result) =>
TryParse(s.AsSpan(), provider, out result);
public static Rectangle3D Parse(ReadOnlySpan<char> s, IFormatProvider provider)
{
s = s.Trim();
var delimiter = s.IndexOfOrdinal('+');
if (delimiter == -1)
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
if (!Point3D.TryParse(s[..delimiter], provider, out var start))
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
if (!Point3D.TryParse(s[(delimiter + 1)..], provider, out var end))
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
return new Rectangle3D(start, end);
}
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out Rectangle3D result)
{
s = s.Trim();
var delimiter = s.IndexOfOrdinal('+');
if (delimiter == -1)
{
result = default;
return false;
}
if (!Point3D.TryParse(s[..delimiter], provider, out var start))
{
result = default;
return false;
}
if (!Point3D.TryParse(s[(delimiter + 1)..], provider, out var end))
{
result = default;
return false;
}
result = new Rectangle3D(start, end);
return true;
}
}

View file

@ -16,12 +16,12 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Server;
[Parsable]
public struct WorldLocation : IPoint3D, IComparable<WorldLocation>, IEquatable<WorldLocation>, IEquatable<IEntity>,
ISpanFormattable
ISpanFormattable, ISpanParsable<WorldLocation>
{
internal Point3D _loc;
internal Map _map;
@ -63,7 +63,7 @@ public struct WorldLocation : IPoint3D, IComparable<WorldLocation>, IEquatable<W
set => _map = value;
}
public WorldLocation(IEntity e) : this(e.Location.X, e.Location.Y, e.Location.Z, e.Map)
public WorldLocation(IEntity e) : this(e.Location, e.Map)
{
}
@ -75,8 +75,10 @@ public struct WorldLocation : IPoint3D, IComparable<WorldLocation>, IEquatable<W
{
}
public WorldLocation(Point3D p, Map map) : this(p.X, p.Y, p.Z, map)
public WorldLocation(Point3D p, Map map)
{
_loc = p;
_map = map;
}
public WorldLocation(int x, int y, int z, Map map)
@ -88,11 +90,11 @@ public struct WorldLocation : IPoint3D, IComparable<WorldLocation>, IEquatable<W
}
public bool Equals(WorldLocation other) =>
_loc.Equals(other._loc) && _map.MapID == other._map.MapID;
_loc.Equals(other._loc) && _map?.MapID == other._map?.MapID;
public bool Equals(IEntity other) =>
!ReferenceEquals(other, null) && _loc == other.Location &&
_map.MapID == other.Map.MapID;
_map?.MapID == other.Map?.MapID;
public override bool Equals(object obj) =>
obj is WorldLocation other && Equals(other);
@ -138,31 +140,6 @@ public struct WorldLocation : IPoint3D, IComparable<WorldLocation>, IEquatable<W
public static bool operator <=(WorldLocation l, IEntity r) =>
!ReferenceEquals(r, null) && l._loc <= r.Location && l._map == r.Map;
public static WorldLocation Parse(string value)
{
var start = value.IndexOfOrdinal('(');
var end = value.IndexOf(',', start + 1);
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var x);
start = end;
end = value.IndexOf(',', start + 1);
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var y);
start = end;
end = value.IndexOf(',', start + 1);
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var z);
start = end;
end = value.IndexOf(')', start + 1);
var map = Map.Parse(value.AsSpan(start + 1, end - (start + 1)).Trim());
return new WorldLocation(x, y, z, map);
}
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider)
{
if (_map == null)
@ -208,4 +185,77 @@ public struct WorldLocation : IPoint3D, IComparable<WorldLocation>, IEquatable<W
// default ToString implementation.
return ToString();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static WorldLocation Parse(string s) => Parse(s, null);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static WorldLocation Parse(string s, IFormatProvider provider) => Parse(s.AsSpan(), provider);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryParse(string s, IFormatProvider provider, out WorldLocation result) =>
TryParse(s.AsSpan(), provider, out result);
public static WorldLocation Parse(ReadOnlySpan<char> s, IFormatProvider provider)
{
s = s.Trim();
if (!s.EndsWithOrdinal(']'))
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
var mapStartBracket = s.IndexOf('[');
if (mapStartBracket == -1)
{
throw new FormatException($"The input string '{s}' was not in a correct format.");
}
var loc = Point3D.Parse(s[..(mapStartBracket - 1)], provider);
var mapSlice = s.Slice(mapStartBracket + 1, s.Length - mapStartBracket - 2);
return mapSlice.EqualsOrdinal("(-null-)")
? new WorldLocation(loc, null)
: new WorldLocation(loc, Map.Parse(mapSlice, provider));
}
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out WorldLocation result)
{
s = s.Trim();
if (!s.EndsWithOrdinal(']'))
{
result = default;
return false;
}
var mapStartBracket = s.IndexOf('[');
if (mapStartBracket == -1)
{
result = default;
return false;
}
if (!Point3D.TryParse(s[..(mapStartBracket - 1)], provider, out var loc))
{
result = default;
return false;
}
var mapSlice = s.Slice(mapStartBracket + 1, s.Length - mapStartBracket - 2);
if (mapSlice.EqualsOrdinal("(-null-)"))
{
result = new WorldLocation(loc, null);
return true;
}
if (!Map.TryParse(mapSlice, provider, out var map))
{
result = default;
return false;
}
result = new WorldLocation(loc, map);
return true;
}
}