From 601607488ae529b7dfd9bd55afd0e89e42762281 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 28 Oct 2014 19:19:02 +0300 Subject: Dehardcode default spells/commands. Read commands from defaultcommands.xml. Also improve a bit saving commands in spellmanager. --- src/resources/db/commandsdb.cpp | 144 ++++++++++++++++++++++++++++++++++++++++ src/resources/db/commandsdb.h | 49 ++++++++++++++ 2 files changed, 193 insertions(+) create mode 100644 src/resources/db/commandsdb.cpp create mode 100644 src/resources/db/commandsdb.h (limited to 'src/resources/db') diff --git a/src/resources/db/commandsdb.cpp b/src/resources/db/commandsdb.cpp new file mode 100644 index 000000000..c5c1d9e3c --- /dev/null +++ b/src/resources/db/commandsdb.cpp @@ -0,0 +1,144 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2014 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "resources/db/commandsdb.h" + +#include "configuration.h" +#include "logger.h" +#include "textcommand.h" + +#include "resources/beingcommon.h" + +#include "utils/dtor.h" +#include "utils/gettext.h" + +#include "debug.h" + +namespace +{ + CommandsMap mCommands; + bool mLoaded = false; +} + +void CommandsDB::load() +{ + if (mLoaded) + unload(); + loadXmlFile(paths.getStringValue("defaultCommandsFile")); + loadXmlFile(paths.getStringValue("defaultCommandsPatchFile")); + loadXmlDir("defaultCommandsPatchDir", loadXmlFile); + mLoaded = true; +} + +static SpellTarget parseTarget(const std::string &text) +{ + if (text == "allow target") + return ALLOWTARGET; + else if (text == "need target") + return NEEDTARGET; + else + return NOTARGET; +} + +void CommandsDB::loadXmlFile(const std::string &fileName) +{ + XML::Document doc(fileName); + const XmlNodePtrConst rootNode = doc.rootNode(); + + if (!rootNode || !xmlNameEqual(rootNode, "commands")) + { + logger->log("Commands Database: Error while loading %s!", + fileName.c_str()); + return; + } + + for_each_xml_child_node(commandNode, rootNode) + { + if (xmlNameEqual(commandNode, "include")) + { + const std::string name = XML::getProperty(commandNode, "name", ""); + if (!name.empty()) + loadXmlFile(name); + continue; + } + + if (!xmlNameEqual(commandNode, "command")) + continue; + + const int id = XML::getProperty(commandNode, "id", -1); + if (id == -1) + continue; + + CommandsMapIter it = mCommands.find(id); + if (it != mCommands.end()) + { + logger->log("Commands database: duplicate id: %d", id); + TextCommand *tempCmd = (*it).second; + mCommands.erase(it); + delete tempCmd; + } + const std::string name = XML::langProperty( + commandNode, "name", "?"); + const std::string command = XML::getProperty( + commandNode, "command", ""); + const std::string comment = XML::getProperty( + commandNode, "comment", ""); + const SpellTarget targetType = parseTarget(XML::getProperty( + commandNode, "target", "")); + const std::string icon = XML::getProperty( + commandNode, "icon", ""); + const int skill1 = XML::getIntProperty( + commandNode, "skill1", 0, 0, 1000000); + const int level1 = XML::getIntProperty( + commandNode, "level1", 0, 0, 1000); + const int skill2 = XML::getIntProperty( + commandNode, "skill2", 0, 0, 1000000); + const int level2 = XML::getIntProperty( + commandNode, "level2", 0, 0, 1000); + const int mana = XML::getIntProperty( + commandNode, "mana", 0, 0, 100000); + + TextCommand *cmd = nullptr; + if (skill1) + { + cmd = new TextCommand(id, name, command, comment, + targetType, icon, level1, static_cast(skill2), level2, mana); + } + else + { + cmd = new TextCommand(id, name, command, comment, + targetType, icon); + } + mCommands[id] = cmd; + } +} + +void CommandsDB::unload() +{ + mCommands.clear(); + mLoaded = false; +} + +std::map &CommandsDB::getAll() +{ + return mCommands; +} diff --git a/src/resources/db/commandsdb.h b/src/resources/db/commandsdb.h new file mode 100644 index 000000000..1725171d1 --- /dev/null +++ b/src/resources/db/commandsdb.h @@ -0,0 +1,49 @@ +/* + * The ManaPlus Client + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011-2014 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef RESOURCES_DB_COMMANDSDB_H +#define RESOURCES_DB_COMMANDSDB_H + +#include "resources/modinfo.h" + +#include +#include + +#include "localconsts.h" + +class TextCommand; + +typedef std::map CommandsMap; +typedef CommandsMap::iterator CommandsMapIter; + +namespace CommandsDB +{ + void load(); + + void unload(); + + void loadXmlFile(const std::string &fileName); + + CommandsMap &getAll(); +} // namespace CommandsDB + +#endif // RESOURCES_DB_COMMANDSDB_H -- cgit v1.2.3-70-g09d2