summaryrefslogtreecommitdiff
path: root/src/map/grfio.cpp
blob: 3475108ae23a5b27940ba30b324e7ae0bb5a703d (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "grfio.hpp"
//    grfio.cpp - Don't read GRF files, just name-map .wlk files.
//
//    Copyright © ????-2004 Athena Dev Teams
//    Copyright © 2004-2011 The Mana World Development Team
//    Copyright © 2011-2014 Ben Longbons <b.r.longbons@gmail.com>
//
//    This file is part of The Mana World (Athena server)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

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

#include <cassert>

#include <map>

#include "../strings/mstring.hpp"
#include "../strings/rstring.hpp"
#include "../strings/astring.hpp"
#include "../strings/zstring.hpp"

#include "../io/cxxstdio.hpp"
#include "../io/extract.hpp"
#include "../io/read.hpp"

#include "../high/extract_mmo.hpp"
#include "../high/mmo.hpp"

#include "globals.hpp"

#include "../poison.hpp"


namespace tmwa
{
namespace map
{
bool load_resnametable(ZString filename)
{
    io::ReadFile in(filename);
    if (!in.is_open())
    {
        FPRINTF(stderr, "Missing %s\n"_fmt, filename);
        return false;
    }

    bool rv = true;
    AString line;
    while (in.getline(line))
    {
        MapName key;
        AString value;
        if (!extract(line,
                    record<'#'>(&key, &value)))
        {
            PRINTF("Bad resnametable line: %s\n"_fmt, line);
            rv = false;
            continue;
        }
        // TODO add "data/" here ...
        resnametable[key] = value;
    }
    return rv;
}

/// Change *.gat to *.wlk
static
RString grfio_resnametable(MapName rname)
{
    // TODO return an error instead of throwing an exception
    return resnametable.at(rname);
}

std::vector<uint8_t> grfio_reads(MapName rname)
{
    MString lfname_;
    // TODO ... instead of here
    lfname_ += "data/"_s;
    lfname_ += grfio_resnametable(rname);
    AString lfname = AString(lfname_);

    // TODO wrap this immediately
    int fd = open(lfname.c_str(), O_RDONLY);
    if (fd == -1)
    {
        FPRINTF(stderr, "Resource %s (file %s) not found\n"_fmt,
                rname, lfname);
        return {};
    }
    off_t len = lseek(fd, 0, SEEK_END);
    assert (len != -1);
    std::vector<uint8_t> buffer(len);
    ssize_t err = pread(fd, buffer.data(), len, 0);
    assert (err == len);
    close(fd);
    return buffer;
}
} // namespace map
} // namespace tmwa