summaryrefslogtreecommitdiff
path: root/src/map/grfio.cpp
blob: a190d1b74d0949af26b04a24a4565eaaa4a44e11 (plain) (blame)
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
// Reads .gat files by name-mapping .wlk files
#include "grfio.hpp"

#include <sys/stat.h>

#include <fcntl.h>
#include <unistd.h>

#include <cassert>
#include <cstdio>
#include <cstring>

#include <fstream>
#include <map>

#include "../common/cxxstdio.hpp"
#include "../common/extract.hpp"

#include "../poison.hpp"

static
std::map<std::string, std::string> load_resnametable()
{
    std::ifstream in("data/resnametable.txt");
    if (!in.is_open())
    {
        fprintf(stderr, "Missing data/resnametable.txt");
        abort();
    }
    std::map<std::string, std::string> out;

    std::string line;
    while (std::getline(in, line))
    {
        std::string key, value;
        if (!extract(line,
                    record<'#'>(&key, &value)))
            continue;
        out[key] = value;
    }
    return out;
}

/// Change *.gat to *.wlk
static
std::string grfio_resnametable(const_string rname)
{
    static
    std::map<std::string, std::string> resnametable = load_resnametable();

    return resnametable.at(std::string(rname.begin(), rname.end()));
}

std::vector<uint8_t> grfio_reads(const_string rname)
{
    std::string lfname = "data/" + grfio_resnametable(rname);

    int fd = open(lfname.c_str(), O_RDONLY);
    if (fd == -1)
    {
        FPRINTF(stderr, "Resource %s (file %s) not found\n",
                std::string(rname.begin(), rname.end()), lfname);
        return {};
    }
    off_t len = lseek(fd, 0, SEEK_END);
    assert (len != -1);
    std::vector<uint8_t> buffer(len);
    int err = pread(fd, buffer.data(), len, 0);
    assert (err == len);
    close(fd);
    return buffer;
}