~ruther/NosSmooth

ref: fcd0751c2f76ffee45134260ce9d7c0bddf837d2 NosSmooth/Data/NosSmooth.Data.NOSFiles/Parsers/MonsterParser.cs -rw-r--r-- 1.6 KiB
fcd0751c — František Boháček Merge pull request #22 from Rutherther/data 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
//
//  MonsterParser.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.

using NosSmooth.Data.Abstractions.Enums;
using NosSmooth.Data.Abstractions.Infos;
using NosSmooth.Data.Abstractions.Language;
using NosSmooth.Data.NOSFiles.Parsers.Dat;
using Remora.Results;

namespace NosSmooth.Data.NOSFiles.Parsers;

/// <inheritdoc />
public class MonsterParser : IInfoParser<IMonsterInfo>
{
    /// <inheritdoc />
    public Result<Dictionary<int, IMonsterInfo>> Parse(NostaleFiles files)
    {
        var monsterDatResult = files.DatFiles.FindFile("monster.dat");
        if (!monsterDatResult.IsSuccess)
        {
            return Result<Dictionary<int, IMonsterInfo>>.FromError(monsterDatResult);
        }
        var reader = new DatReader(monsterDatResult.Entity);
        var result = new Dictionary<int, IMonsterInfo>();

        while (reader.ReadItem(out var itemNullable))
        {
            var item = itemNullable.Value;

            var vnum = item.GetEntry("VNUM").Read<int>(1);
            var nameKey = item.GetEntry("NAME").Read<string>(1);
            result.Add
            (
                vnum,
                new MonsterInfo
                (
                    vnum,
                    new TranslatableString(TranslationRoot.Monster, nameKey),
                    item.GetEntry("LEVEL").Read<int>(1)
                )
            );
        }

        return result;
    }

    private record MonsterInfo(int VNum, TranslatableString Name, int Level) : IMonsterInfo;
}
Do not follow this link