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