1
0
mirror of https://github.com/oliverbooth/VpSharp synced 2024-11-09 22:55:42 +00:00

Reduce cyclomatic complexity of VirtualParadisePathObject

Just... all of it
This commit is contained in:
Oliver Booth 2022-11-30 20:14:41 +00:00
parent 554d326819
commit 2fe3c8b98e
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634

View File

@ -45,30 +45,9 @@ public sealed class VirtualParadisePathObject : VirtualParadiseObject
Span<char> chars = stackalloc char[data.Length];
Encoding.UTF8.GetChars(data, chars);
using Utf8ValueStringBuilder buffer = ZString.CreateUtf8StringBuilder();
int index;
Utf8ValueStringBuilder buffer = ZString.CreateUtf8StringBuilder();
int index = CheckVersion(chars, ref buffer);
// version
var version = 0;
for (index = 0; index < chars.Length; index++)
{
if (chars[index] == '\n')
{
version = buffer.AsSpan().ToInt32();
break;
}
buffer.Append(chars[index]);
}
buffer.Clear();
if (version != 1)
{
throw new NotSupportedException($"Unsupported path version {version}");
}
// path name
var name = string.Empty;
for (index += 1; index < chars.Length; index++)
{
@ -100,69 +79,123 @@ public sealed class VirtualParadisePathObject : VirtualParadiseObject
for (index += 1; index < chars.Length; index++)
{
if (char.IsWhiteSpace(chars[index]))
char current = chars[index];
if (char.IsWhiteSpace(current))
{
if (offset is null)
{
offset = TimeSpan.FromSeconds(buffer.AsSpan().ToDouble());
buffer.Clear();
}
else if (x is null)
{
x = buffer.AsSpan().ToDouble();
buffer.Clear();
}
else if (y is null)
{
y = buffer.AsSpan().ToDouble();
buffer.Clear();
}
else if (z is null)
{
z = buffer.AsSpan().ToDouble();
buffer.Clear();
}
else if (rx is null)
{
rx = buffer.AsSpan().ToSingle();
buffer.Clear();
}
else if (ry is null)
{
ry = buffer.AsSpan().ToSingle();
buffer.Clear();
}
else if (rz is null)
{
rz = buffer.AsSpan().ToSingle();
buffer.Clear();
}
else if (ra is null)
{
ra = buffer.AsSpan().ToSingle();
buffer.Clear();
}
ProcessBuffer(ref offset, ref buffer, ref x, ref y, ref z, ref rx, ref ry, ref rz, ref ra);
}
if (chars[index] == '\n' || index == chars.Length - 1)
ProcessEndOfLine(chars, index, ref x, ref y, ref z, ref rx, ref ry, ref rz, ref ra, ref offset, points);
buffer.Append(current);
}
Path = new VirtualParadisePath((PathEasing)pathType, name, points, closed == 1);
buffer.Dispose();
}
private static int CheckVersion(Span<char> chars, ref Utf8ValueStringBuilder buffer)
{
int index;
var version = 0;
for (index = 0; index < chars.Length; index++)
{
if (chars[index] == '\n')
{
var position = new Vector3d(x ?? 0, y ?? 0, z ?? 0);
var axis = new Vector3(rx ?? 0, ry ?? 0, rz ?? 0);
Quaternion rotation = double.IsPositiveInfinity(ra ?? 0)
? Quaternion.CreateFromYawPitchRoll(axis.Y, axis.X, axis.Z)
: Quaternion.CreateFromAxisAngle(axis, ra ?? 0);
var point = new PathPoint(offset ?? TimeSpan.Zero, position, rotation);
points.Add(point);
x = y = z = rx = ry = rz = ra = null;
offset = null;
version = buffer.AsSpan().ToInt32();
break;
}
buffer.Append(chars[index]);
}
Path = new VirtualParadisePath((PathEasing)pathType, name, points, closed == 1);
buffer.Clear();
if (version != 1)
{
throw new NotSupportedException($"Unsupported path version {version}");
}
return index;
}
private static void ProcessEndOfLine(
Span<char> chars,
int index,
ref double? x, ref double? y, ref double? z,
ref float? rx, ref float? ry, ref float? rz,
ref float? ra,
ref TimeSpan? offset,
ICollection<PathPoint> points
)
{
if (chars[index] != '\n' && index != chars.Length - 1)
{
return;
}
var position = new Vector3d(x ?? 0, y ?? 0, z ?? 0);
var axis = new Vector3(rx ?? 0, ry ?? 0, rz ?? 0);
Quaternion rotation = double.IsPositiveInfinity(ra ?? 0)
? Quaternion.CreateFromYawPitchRoll(axis.Y, axis.X, axis.Z)
: Quaternion.CreateFromAxisAngle(axis, ra ?? 0);
var point = new PathPoint(offset ?? TimeSpan.Zero, position, rotation);
points.Add(point);
x = y = z = rx = ry = rz = ra = null;
offset = null;
}
private static void ProcessBuffer(
ref TimeSpan? offset,
ref Utf8ValueStringBuilder buffer,
ref double? x, ref double? y, ref double? z,
ref float? rx, ref float? ry, ref float? rz,
ref float? ra
)
{
if (offset is null)
{
offset = TimeSpan.FromSeconds(buffer.AsSpan().ToDouble());
buffer.Clear();
}
else if (x is null)
{
x = buffer.AsSpan().ToDouble();
buffer.Clear();
}
else if (y is null)
{
y = buffer.AsSpan().ToDouble();
buffer.Clear();
}
else if (z is null)
{
z = buffer.AsSpan().ToDouble();
buffer.Clear();
}
else if (rx is null)
{
rx = buffer.AsSpan().ToSingle();
buffer.Clear();
}
else if (ry is null)
{
ry = buffer.AsSpan().ToSingle();
buffer.Clear();
}
else if (rz is null)
{
rz = buffer.AsSpan().ToSingle();
buffer.Clear();
}
else if (ra is null)
{
ra = buffer.AsSpan().ToSingle();
buffer.Clear();
}
}
}