~ruther/NosSmooth

ref: 05c2624a4a566cda726bfd76e8a8dc4affc463b1 NosSmooth/Data/NosSmooth.Data.NOSFiles/Parsers/Dat/DatItem.cs -rw-r--r-- 1.7 KiB
05c2624a — František Boháček feat(data): add database nostale data implementation 3 years ago
                                                                                
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
//
//  DatItem.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace NosSmooth.Data.NOSFiles.Parsers.Dat;

/// <summary>
/// An item from a dat file obtained using <see cref="DatReader"/>.
/// </summary>
public struct DatItem
{
    private readonly IReadOnlyDictionary<string, IReadOnlyList<DatEntry>> _entries;

    /// <summary>
    /// Initializes a new instance of the <see cref="DatItem"/> struct.
    /// </summary>
    /// <param name="entries">The entries of the item.</param>
    public DatItem(IReadOnlyDictionary<string, IReadOnlyList<DatEntry>> entries)
    {
        _entries = entries;
    }

    /// <summary>
    /// Gets the entry with the given name.
    /// </summary>
    /// <param name="name">The name of the entry.</param>
    /// <returns>An entry, or null, if not found.</returns>
    public DatEntry? GetNullableEntry(string name)
    {
        return GetEntries(name)?.FirstOrDefault() ?? null;
    }

    /// <summary>
    /// Gets the entry with the given name.
    /// </summary>
    /// <param name="name">The name of the entry.</param>
    /// <returns>An entry, or null, if not found.</returns>
    public DatEntry GetEntry(string name)
    {
        return GetEntries(name).First();
    }

    /// <summary>
    /// Gets the entry with the given name.
    /// </summary>
    /// <param name="name">The name of the entry.</param>
    /// <returns>An entry, or null, if not found.</returns>
    public IReadOnlyList<DatEntry> GetEntries(string name)
    {
        if (!_entries.ContainsKey(name))
        {
            return Array.Empty<DatEntry>();
        }

        return _entries[name];
    }
}
Do not follow this link