GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/mapdb.cpp Lines: 4 98 4.1 %
Date: 2017-11-29 Branches: 2 186 1.1 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008  Aethyra Development Team
4
 *  Copyright (C) 2011-2017  The ManaPlus Developers
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "resources/db/mapdb.h"
23
24
#include "configuration.h"
25
#include "logger.h"
26
27
#include "resources/beingcommon.h"
28
29
#include "debug.h"
30
31
namespace
32
{
33
    bool mLoaded = false;
34
2
    MapDB::Maps mMaps;
35
2
    MapDB::MapInfos mInfos;
36
2
    MapDB::Atlases mAtlases;
37
}  // namespace
38
39
namespace MapDB
40
{
41
    void readMap(XmlNodePtrConst node);
42
    void readAtlas(XmlNodePtrConst node);
43
}  // namespace MapDB
44
45
void MapDB::load()
46
{
47
    if (mLoaded)
48
        unload();
49
50
    logger->log1("Initializing maps database...");
51
    loadRemapXmlFile(paths.getStringValue("mapsRemapFile"),
52
        SkipError_true);
53
    loadRemapXmlFile(paths.getStringValue("mapsRemapPatchFile"),
54
        SkipError_true);
55
    loadXmlDir("mapsRemapPatchDir", loadRemapXmlFile);
56
57
    loadInfo(paths.getStringValue("mapsFile"), SkipError_false);
58
    loadInfo(paths.getStringValue("mapsPatchFile"), SkipError_true);
59
    loadXmlDir("mapsPatchDir", loadInfo);
60
    mLoaded = true;
61
}
62
63
void MapDB::loadRemapXmlFile(const std::string &fileName,
64
                             const SkipError skipError)
65
{
66
    XML::Document *const doc = new XML::Document(fileName,
67
        UseVirtFs_true,
68
        skipError);
69
70
    XmlNodeConstPtrConst root = doc->rootNode();
71
    if (root == nullptr)
72
    {
73
        delete doc;
74
        return;
75
    }
76
77
    for_each_xml_child_node(node, root)
78
    {
79
        if (xmlNameEqual(node, "map"))
80
        {
81
            const std::string name = XML::getProperty(node, "name", "");
82
            if (name.empty())
83
                continue;
84
85
            const std::string value = XML::getProperty(node, "value", "");
86
            if (value.empty())
87
                continue;
88
89
            mMaps[name] = value;
90
        }
91
        else if (xmlNameEqual(node, "include"))
92
        {
93
            const std::string name = XML::getProperty(node, "name", "");
94
            if (!name.empty())
95
                loadRemapXmlFile(name, skipError);
96
            continue;
97
        }
98
    }
99
100
    delete doc;
101
}
102
103
void MapDB::readMap(XmlNodePtrConst node)
104
{
105
    if (node == nullptr)
106
        return;
107
    const std::string map = XML::getProperty(node, "name", "");
108
    if (map.empty())
109
        return;
110
111
    for_each_xml_child_node(childNode, node)
112
    {
113
        if (xmlNameEqual(childNode, "atlas"))
114
        {
115
            const std::string atlas = XML::getProperty(childNode, "name", "");
116
            if (atlas.empty())
117
                continue;
118
            mInfos[map].atlas = atlas;
119
        }
120
    }
121
}
122
123
void MapDB::readAtlas(XmlNodePtrConst node)
124
{
125
    if (node == nullptr)
126
        return;
127
    const std::string atlas = XML::getProperty(node, "name", "");
128
    if (atlas.empty())
129
        return;
130
    for_each_xml_child_node(childNode, node)
131
    {
132
        if (xmlNameEqual(childNode, "file"))
133
        {
134
            const std::string file = XML::getProperty(childNode, "name", "");
135
            if (file.empty())
136
                continue;
137
            mAtlases[atlas].push_back(file);
138
        }
139
    }
140
    if (atlas != "all" && atlas != paths.getStringValue("emptyAtlasName"))
141
    {
142
        const AtlasCIter &allAtlas = mAtlases.find("all");
143
        if (allAtlas != mAtlases.end())
144
        {
145
            FOR_EACH (StringVectCIter, it, (*allAtlas).second)
146
                mAtlases[atlas].push_back(*it);
147
        }
148
    }
149
}
150
151
void MapDB::loadInfo(const std::string &fileName,
152
                     const SkipError skipError)
153
{
154
    XML::Document *doc = new XML::Document(fileName,
155
        UseVirtFs_true,
156
        skipError);
157
    XmlNodeConstPtrConst root = doc->rootNode();
158
    if (root == nullptr)
159
    {
160
        delete doc;
161
        return;
162
    }
163
164
    for_each_xml_child_node(node, root)
165
    {
166
        if (xmlNameEqual(node, "map"))
167
        {
168
            readMap(node);
169
        }
170
        else if (xmlNameEqual(node, "atlas"))
171
        {
172
            readAtlas(node);
173
        }
174
        else if (xmlNameEqual(node, "include"))
175
        {
176
            const std::string name = XML::getProperty(node, "name", "");
177
            if (!name.empty())
178
                loadInfo(name, skipError);
179
            continue;
180
        }
181
    }
182
    delete doc;
183
}
184
185
void MapDB::unload()
186
{
187
    logger->log1("Unloading map database...");
188
189
    mMaps.clear();
190
    mLoaded = false;
191
}
192
193
const std::string MapDB::getMapName(const std::string &name)
194
{
195
    const MapIterator it = mMaps.find(name);
196
197
    if (it != mMaps.end())
198
        return it->second;
199
    return name;
200
}
201
202
const MapInfo *MapDB::getMapAtlas(const std::string &name)
203
{
204
    const MapInfoIter it = mInfos.find(name);
205
    if (it == mInfos.end())
206
        return nullptr;
207
    MapInfo *const info = &(*it).second;
208
    const AtlasCIter it2 = mAtlases.find(info->atlas);
209
    if (it2 == mAtlases.end())
210
        return nullptr;
211
    info->files = &((*it2).second);
212
    return info;
213
}
214
215
const MapInfo *MapDB::getAtlas(const std::string &name)
216
{
217
    const AtlasCIter it = mAtlases.find(name);
218
    if (it == mAtlases.end())
219
        return nullptr;
220
221
    MapInfo *const info = new MapInfo;
222
    info->atlas = name;
223
    info->files = &(*it).second;
224
    return info;
225

6
}