GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/commandsdb.cpp Lines: 2 58 3.4 %
Date: 2017-11-29 Branches: 0 110 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "resources/db/commandsdb.h"
24
25
#include "configuration.h"
26
#include "logger.h"
27
#include "textcommand.h"
28
29
#include "resources/beingcommon.h"
30
31
#include "debug.h"
32
33
namespace
34
{
35
2
    CommandsMap mCommands;
36
    bool mLoaded = false;
37
}  // namespace
38
39
void CommandsDB::load()
40
{
41
    if (mLoaded)
42
        unload();
43
    logger->log1("Initializing commands database...");
44
    loadXmlFile(paths.getStringValue("defaultCommandsFile"),
45
        SkipError_false);
46
    loadXmlFile(paths.getStringValue("defaultCommandsPatchFile"),
47
        SkipError_true);
48
    loadXmlDir("defaultCommandsPatchDir", loadXmlFile);
49
    mLoaded = true;
50
}
51
52
static CommandTargetT parseTarget(const std::string &text)
53
{
54
    if (text == "allow target")
55
        return CommandTarget::AllowTarget;
56
    else if (text == "need target")
57
        return CommandTarget::NeedTarget;
58
    else
59
        return CommandTarget::NoTarget;
60
}
61
62
void CommandsDB::loadXmlFile(const std::string &fileName,
63
                             const SkipError skipError)
64
{
65
    XML::Document doc(fileName, UseVirtFs_true, skipError);
66
    XmlNodeConstPtrConst rootNode = doc.rootNode();
67
68
    if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "commands"))
69
    {
70
        logger->log("Commands Database: Error while loading %s!",
71
            fileName.c_str());
72
        return;
73
    }
74
75
    for_each_xml_child_node(commandNode, rootNode)
76
    {
77
        if (xmlNameEqual(commandNode, "include"))
78
        {
79
            const std::string name = XML::getProperty(commandNode, "name", "");
80
            if (!name.empty())
81
                loadXmlFile(name, skipError);
82
            continue;
83
        }
84
85
        if (!xmlNameEqual(commandNode, "command"))
86
            continue;
87
88
        const int id = XML::getProperty(commandNode, "id", -1);
89
        if (id == -1)
90
            continue;
91
92
        const CommandsMapIter it = mCommands.find(id);
93
        if (it != mCommands.end())
94
        {
95
            logger->log("Commands database: duplicate id: %d", id);
96
            TextCommand *tempCmd = (*it).second;
97
            mCommands.erase(it);
98
            delete tempCmd;
99
        }
100
        const std::string name = XML::langProperty(
101
            commandNode, "name", "?");
102
        const std::string command = XML::getProperty(
103
            commandNode, "command", "");
104
        const std::string comment = XML::getProperty(
105
            commandNode, "comment", "");
106
        const CommandTargetT targetType = parseTarget(XML::getProperty(
107
            commandNode, "target", ""));
108
        const std::string icon = XML::getProperty(
109
            commandNode, "icon", "");
110
#ifdef TMWA_SUPPORT
111
        const int skill1 = XML::getIntProperty(
112
            commandNode, "skill1", 0, 0, 1000000);
113
        const int level1 = XML::getIntProperty(
114
            commandNode, "level1", 0, 0, 1000);
115
        const int skill2 = XML::getIntProperty(
116
            commandNode, "skill2", 0, 0, 1000000);
117
        const int level2 = XML::getIntProperty(
118
            commandNode, "level2", 0, 0, 1000);
119
        const int mana = XML::getIntProperty(
120
            commandNode, "mana", 0, 0, 100000);
121
#endif  // TMWA_SUPPORT
122
123
        TextCommand *cmd = nullptr;
124
#ifdef TMWA_SUPPORT
125
        if (skill1 != 0)
126
        {
127
            cmd = new TextCommand(id,
128
                name,
129
                command,
130
                comment,
131
                targetType,
132
                icon,
133
                level1,
134
                static_cast<MagicSchoolT>(skill2),
135
                level2,
136
                mana);
137
        }
138
        else
139
#endif  // TMWA_SUPPORT
140
        {
141
            cmd = new TextCommand(id,
142
                name,
143
                command,
144
                comment,
145
                targetType,
146
                icon);
147
        }
148
        mCommands[id] = cmd;
149
    }
150
}
151
152
void CommandsDB::unload()
153
{
154
    logger->log1("Unloading commands database...");
155
    mCommands.clear();
156
    mLoaded = false;
157
}
158
159
std::map<int, TextCommand*> &CommandsDB::getAll()
160
{
161
    return mCommands;
162
4
}