ManaPlus
Functions
CommandsDB Namespace Reference

Functions

void load ()
 
void unload ()
 
void loadXmlFile (const std::string &fileName, const SkipError skipError)
 
CommandsMapgetAll ()
 

Function Documentation

◆ getAll()

std::map< int, TextCommand * > & CommandsDB::getAll ( )

Definition at line 160 of file commandsdb.cpp.

161 {
162  return mCommands;
163 }

References anonymous_namespace{commandsdb.cpp}::mCommands.

Referenced by SpellManager::fillSpells().

◆ load()

void CommandsDB::load ( )

Definition at line 40 of file commandsdb.cpp.

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 }
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 log1(const char *const log_text)
Definition: logger.cpp:238
Configuration paths
Logger * logger
Definition: logger.cpp:89
void unload()
Definition: net.cpp:180
const bool SkipError_false
Definition: skiperror.h:30
const bool SkipError_true
Definition: skiperror.h:30

References Configuration::getStringValue(), loadXmlDir, loadXmlFile(), Logger::log1(), logger, anonymous_namespace{commandsdb.cpp}::mLoaded, paths, SkipError_false, SkipError_true, and Net::unload().

Referenced by SpellManager::fillSpells().

◆ loadXmlFile()

void CommandsDB::loadXmlFile ( const std::string &  fileName,
const SkipError  skipError 
)

Definition at line 63 of file commandsdb.cpp.

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 }
void log(const char *const log_text,...)
Definition: logger.cpp:269
static CommandTargetT parseTarget(const std::string &text)
Definition: commandsdb.cpp:53
CommandsMap::iterator CommandsMapIter
Definition: commandsdb.h:37
CommandTarget ::T CommandTargetT
Definition: commandtarget.h:33
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
MagicSchool ::T MagicSchoolT
Definition: magicschool.h:38
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
std::string fileName
Definition: testmain.cpp:39
const bool UseVirtFs_true
Definition: usevirtfs.h:30

References fileName, for_each_xml_child_node, XML::getIntProperty(), XML::getProperty(), XML::langProperty(), loadXmlFile(), Logger::log(), logger, anonymous_namespace{commandsdb.cpp}::mCommands, parseTarget(), XML::Document::rootNode(), and UseVirtFs_true.

◆ unload()

void CommandsDB::unload ( )

Definition at line 153 of file commandsdb.cpp.

154 {
155  logger->log1("Unloading commands database...");
156  mCommands.clear();
157  mLoaded = false;
158 }

References Logger::log1(), logger, anonymous_namespace{commandsdb.cpp}::mCommands, and anonymous_namespace{commandsdb.cpp}::mLoaded.

Referenced by SpellManager::fillSpells().