-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathColumnTypes.cs
More file actions
88 lines (81 loc) · 3.55 KB
/
Copy pathColumnTypes.cs
File metadata and controls
88 lines (81 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using EXDTooler.Schema;
using Lumina;
using Lumina.Data.Files.Excel;
using Lumina.Data.Structs.Excel;
namespace EXDTooler.Validators;
public sealed class ColumnTypes : IValidator<ColumnTypes>
{
private ColumnTypes() { }
public static void Validate(Sheet sheet, IReadOnlyList<ExcelColumnDefinition> cols, ColDefReader colDefs)
{
ReadOnlyMemory<ExcelColumnDefinition> offsetCols = Utils.OrderByOffset(cols);
foreach (var f in sheet.Fields)
ValidateColumns(f, ref offsetCols);
if (!offsetCols.IsEmpty)
throw new ValidationException("Column count mismatch");
}
private static void ValidateColumns(Field field, ref ReadOnlyMemory<ExcelColumnDefinition> cols)
{
if (field.Type == FieldType.Scalar)
{
cols = cols[1..];
return;
}
else if (field.Type == FieldType.Array)
{
var count = field.Count!.Value;
var elementCount = Utils.GetColumnCount(field.Fields ?? [new() { Type = FieldType.Scalar }]);
var arrayCols = cols[..(elementCount * count)];
cols = cols[(elementCount * count)..];
var firstElementTypes = arrayCols[..elementCount].ToArray().Select(x => x.Type).ToArray();
for (var i = 0; i < count; i++)
{
var element = arrayCols[(i * elementCount)..((i + 1) * elementCount)].ToArray().Select(x => x.Type);
if (!firstElementTypes.SequenceEqual(element))
throw new ValidationException($"Array element type mismatch: ({string.Join(", ", element)}) != ({string.Join(", ", firstElementTypes)})");
}
if (field.Fields != null)
{
var c = arrayCols[..elementCount];
foreach (var f in field.Fields)
ValidateColumns(f, ref c);
if (!c.IsEmpty)
throw new ValidationException("Array element count mismatch");
}
}
else if (field.Type == FieldType.Icon)
{
var type = cols.Span[0].Type;
cols = cols[1..];
if (type is not (ExcelColumnDataType.UInt32 or ExcelColumnDataType.Int32 or ExcelColumnDataType.UInt16))
throw new ValidationException($"Icon type mismatch for {field.Name} ({type})");
}
else if (field.Type == FieldType.ModelId)
{
var type = cols.Span[0].Type;
cols = cols[1..];
if (type is not (ExcelColumnDataType.UInt32 or ExcelColumnDataType.UInt64))
throw new ValidationException($"Model ID type mismatch for {field.Name} ({type})");
}
else if (field.Type == FieldType.Color)
{
var type = cols.Span[0].Type;
cols = cols[1..];
if (type != ExcelColumnDataType.UInt32)
throw new ValidationException($"Color type mismatch for {field.Name} ({type})");
}
else if (field.Type == FieldType.Link)
{
var type = cols.Span[0].Type;
cols = cols[1..];
if (type is not (
ExcelColumnDataType.UInt8 or ExcelColumnDataType.Int8 or
ExcelColumnDataType.UInt16 or ExcelColumnDataType.Int16 or
ExcelColumnDataType.UInt32 or ExcelColumnDataType.Int32 or
ExcelColumnDataType.UInt64 or ExcelColumnDataType.Int64))
throw new ValidationException($"Link type mismatch for {field.Name} ({type})");
}
else
throw new ValidationException("Unknown field type");
}
}