ManaPlus
commandsdb.cpp
Go to the documentation of this file.
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 
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 {
37  bool mLoaded = false;
38 } // namespace
39 
41 {
42  if (mLoaded)
43  unload();
44  logger->log1("Initializing commands database...");
45  loadXmlFile(paths.getStringValue("defaultCommandsFile"),
47  loadXmlFile(paths.getStringValue("defaultCommandsPatchFile"),
49  loadXmlDir("defaultCommandsPatchDir", loadXmlFile)
50  mLoaded = true;
51 }
52 
53 static CommandTargetT parseTarget(const std::string &text)
54 {
55  if (text == "allow target")
57  else if (text == "need target")
59  else
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 
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 }
static void loadXmlFile(const std::string &file, const std::string &name, BadgesInfos &arr, const SkipError skipError)
Definition: badgesdb.cpp:43
#define loadXmlDir(name, function)
Definition: beingcommon.h:39
std::string getStringValue(const std::string &key) const
void log(const char *const log_text,...)
Definition: logger.cpp:269
void log1(const char *const log_text)
Definition: logger.cpp:238
xmlNodePtr rootNode()
Definition: libxml.cpp:169
static CommandTargetT parseTarget(const std::string &text)
Definition: commandsdb.cpp:53
CommandsMap::iterator CommandsMapIter
Definition: commandsdb.h:37
std::map< int, TextCommand * > CommandsMap
Definition: commandsdb.h:34
CommandTarget ::T CommandTargetT
Definition: commandtarget.h:33
Configuration paths
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
Logger * logger
Definition: logger.cpp:89
MagicSchool ::T MagicSchoolT
Definition: magicschool.h:38
void loadXmlFile(const std::string &fileName, const SkipError skipError)
Definition: commandsdb.cpp:63
void unload()
Definition: commandsdb.cpp:153
CommandsMap & getAll()
Definition: commandsdb.cpp:160
void load()
Definition: commandsdb.cpp:40
void unload()
Definition: net.cpp:180
std::string langProperty(const xmlNodePtr node, const char *const name, const std::string &def)
Definition: libxml.cpp:258
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:174
int getIntProperty(const xmlNodePtr node, const char *const name, int def, const int min, const int max)
Definition: libxml.cpp:190
const bool SkipError_false
Definition: skiperror.h:30
const bool SkipError_true
Definition: skiperror.h:30
bool SkipError
Definition: skiperror.h:30
std::string fileName
Definition: testmain.cpp:39
const bool UseVirtFs_true
Definition: usevirtfs.h:30