~ruther/NosSmooth

ref: fcd0751c2f76ffee45134260ce9d7c0bddf837d2 NosSmooth/Data/NosSmooth.Data.NOSFiles/Parsers/MapParser.cs -rw-r--r-- 2.2 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
//  MapParser.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 System.Text;
using NosSmooth.Data.Abstractions.Infos;
using NosSmooth.Data.Abstractions.Language;
using NosSmooth.Data.NOSFiles.Infos;
using Remora.Results;

namespace NosSmooth.Data.NOSFiles.Parsers;

/// <inheritdoc />
public class MapParser : IInfoParser<IMapInfo>
{
    /// <inheritdoc />
    public Result<Dictionary<int, IMapInfo>> Parse(NostaleFiles files)
    {
        var mapDatResult = files.DatFiles.FindFile("MapIDData.dat");
        if (!mapDatResult.IsSuccess)
        {
            return Result<Dictionary<int, IMapInfo>>.FromError(mapDatResult);
        }
        var mapGridsArchive = files.MapGridsFiles;
        var mapDatContent = Encoding.ASCII.GetString(mapDatResult.Entity.Content).Split('\r', '\n');

        var mapNames = new Dictionary<int, TranslatableString>();
        foreach (var line in mapDatContent)
        {
            var splitted = line.Split(' ', '\t');
            if (splitted.Length != 5)
            {
                continue;
            }

            var first = int.Parse(splitted[0]);
            var second = int.Parse(splitted[1]);
            if (first == second)
            {
                mapNames[first] = new TranslatableString(TranslationRoot.Map, splitted.Last());
            }

            for (int i = first; i < second; i++)
            {
                mapNames[i] = new TranslatableString(TranslationRoot.Map, splitted.Last());
            }
        }

        var result = new Dictionary<int, IMapInfo>();
        foreach (var file in mapGridsArchive.Files)
        {
            var id = int.Parse(file.Path);
            var grid = file.Content;
            result[id] = new MapInfo
            (
                id,
                mapNames.GetValueOrDefault(id, new TranslatableString(TranslationRoot.Map, "Map")),
                BitConverter.ToInt16(grid.Take(2).ToArray()),
                BitConverter.ToInt16(grid.Skip(2).Take(2).ToArray()),
                grid.Skip(4).ToArray()
            );
        }

        return result;
    }
}
Do not follow this link