NdsForge.NET 1.0.1
Read, validate, edit, compare, and build Nintendo DS and DSi images from .NET
Loading...
Searching...
No Matches
NdsOverlayParser.cs
1namespace NdsForge;
2
4internal static class NdsOverlayParser
5{
7 private const int EntryLength = 32;
8
16 public static IReadOnlyList<NdsOverlay> Parse(
17 IImageDataSource source,
18 NdsRegion table,
19 NdsProcessor processor,
20 NdsFileSystem fileSystem,
21 NdsReadOptions options)
22 {
23 int length = ValidateLength(table, options);
24 byte[] data = new byte[length];
25 source.ReadExactly(table.Offset, data);
26 return ParseEntries(data, processor, fileSystem);
27 }
28
37 public static async ValueTask<IReadOnlyList<NdsOverlay>> ParseAsync(
38 IImageDataSource source,
39 NdsRegion table,
40 NdsProcessor processor,
41 NdsFileSystem fileSystem,
42 NdsReadOptions options,
43 CancellationToken cancellationToken)
44 {
45 int length = ValidateLength(table, options);
46 byte[] data = new byte[length];
47 await source.ReadExactlyAsync(table.Offset, data, cancellationToken).ConfigureAwait(false);
48 return ParseEntries(data, processor, fileSystem);
49 }
50
56 private static NdsOverlay[] ParseEntries(
57 ReadOnlySpan<byte> data,
58 NdsProcessor processor,
59 NdsFileSystem fileSystem)
60 {
61 var overlays = new NdsOverlay[data.Length / EntryLength];
62 for (int index = 0; index < overlays.Length; index++)
63 {
64 overlays[index] = new(processor, data.Slice(index * EntryLength, EntryLength), fileSystem);
65 }
66
67 return overlays;
68 }
69
74 private static int ValidateLength(NdsRegion table, NdsReadOptions options)
75 {
76 if (table.Length % EntryLength != 0 ||
77 table.Length / EntryLength > options.MaximumOverlayCount ||
78 table.Length > Array.MaxLength)
79 {
80 throw new InvalidDataException("An overlay table has an invalid or excessive length.");
81 }
82
83 return checked((int)table.Length);
84 }
85}
NdsProcessor
Identifies a processor and execution mode.