1
0
mirror of https://github.com/oliverbooth/VpSharp synced 2024-11-23 01:08:47 +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]; Span<char> chars = stackalloc char[data.Length];
Encoding.UTF8.GetChars(data, chars); Encoding.UTF8.GetChars(data, chars);
using Utf8ValueStringBuilder buffer = ZString.CreateUtf8StringBuilder(); Utf8ValueStringBuilder buffer = ZString.CreateUtf8StringBuilder();
int index; 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; var name = string.Empty;
for (index += 1; index < chars.Length; index++) for (index += 1; index < chars.Length; index++)
{ {
@ -100,7 +79,83 @@ public sealed class VirtualParadisePathObject : VirtualParadiseObject
for (index += 1; index < chars.Length; index++) for (index += 1; index < chars.Length; index++)
{ {
if (char.IsWhiteSpace(chars[index])) char current = chars[index];
if (char.IsWhiteSpace(current))
{
ProcessBuffer(ref offset, ref buffer, ref x, ref y, ref z, ref rx, ref ry, ref rz, ref ra);
}
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')
{
version = buffer.AsSpan().ToInt32();
break;
}
buffer.Append(chars[index]);
}
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) if (offset is null)
{ {
@ -143,26 +198,4 @@ public sealed class VirtualParadisePathObject : VirtualParadiseObject
buffer.Clear(); buffer.Clear();
} }
} }
if (chars[index] == '\n' || index == chars.Length - 1)
{
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;
}
buffer.Append(chars[index]);
}
Path = new VirtualParadisePath((PathEasing)pathType, name, points, closed == 1);
}
} }