From ce34b402281f75cb1e9d214c925382fdc522455b Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 10 Feb 2018 05:09:16 +0300 Subject: Move reading and combining item stats into separate file. --- src/CMakeLists.txt | 3 +++ src/Makefile.am | 3 +++ src/defaults.cpp | 3 +++ src/resources/db/itemdb.cpp | 36 ++++--------------------- src/resources/db/itemfielddb.cpp | 12 ++++----- src/resources/db/itemfielddb.h | 8 +++--- src/resources/db/itemoptiondb.cpp | 10 +++---- src/resources/itemfieldinfos.h | 33 +++++++++++++++++++++++ src/utils/itemxmlutils.cpp | 56 +++++++++++++++++++++++++++++++++++++++ src/utils/itemxmlutils.h | 34 ++++++++++++++++++++++++ 10 files changed, 152 insertions(+), 46 deletions(-) create mode 100644 src/resources/itemfieldinfos.h create mode 100644 src/utils/itemxmlutils.cpp create mode 100644 src/utils/itemxmlutils.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 84f22491b..68eeb7eb4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -688,6 +688,7 @@ SET(SRCS resources/imageset.h resources/imageset.cpp resources/itemcolordata.h + resources/itemfieldinfos.h resources/db/itemdb.cpp resources/db/itemdb.h resources/basicstat.h @@ -906,6 +907,8 @@ SET(SRCS utils/gmfunctions.cpp utils/gmfunctions.h utils/intmap.h + utils/itemxmlutils.cpp + utils/itemxmlutils.h utils/langs.cpp utils/langs.h utils/likely.h diff --git a/src/Makefile.am b/src/Makefile.am index 95168fdb1..216655796 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -479,6 +479,7 @@ BASE_SRC += client.h \ resources/imageset.cpp \ resources/imageset.h \ resources/itemcolordata.h \ + resources/itemfieldinfos.h \ resources/mstack.h \ resources/notificationinfo.h \ resources/notifications.h \ @@ -619,6 +620,8 @@ BASE_SRC += client.h \ utils/glxhelper.cpp \ utils/glxhelper.h \ utils/intmap.h \ + utils/itemxmlutils.cpp \ + utils/itemxmlutils.h \ utils/langs.cpp \ utils/langs.h \ utils/likely.h \ diff --git a/src/defaults.cpp b/src/defaults.cpp index 9ee375e9b..888778dbb 100644 --- a/src/defaults.cpp +++ b/src/defaults.cpp @@ -681,6 +681,9 @@ void setPathsDefaults(Configuration &cfg) AddDEF("groupsFile", "groups.xml"); AddDEF("groupsPatchFile", "groups_patch.xml"); AddDEF("groupsPatchDir", "groups.d"); + AddDEF("clansFile", "clans.xml"); + AddDEF("clansPatchFile", "clans_patch.xml"); + AddDEF("clansPatchDir", "clans.d"); AddDEF("equipmentSlotsFile", "equipmentslots.xml"); AddDEF("weaponsFile", "weapons.xml"); AddDEF("poisonEffectName", "poison"); diff --git a/src/resources/db/itemdb.cpp b/src/resources/db/itemdb.cpp index d0889a808..fcb0d5651 100644 --- a/src/resources/db/itemdb.cpp +++ b/src/resources/db/itemdb.cpp @@ -48,6 +48,7 @@ #include "utils/delete2.h" #include "utils/dtor.h" #include "utils/foreach.h" +#include "utils/itemxmlutils.h" #include "utils/stdmove.h" #include "utils/stringmap.h" @@ -130,33 +131,6 @@ static std::string useButton2FromItemType(const ItemDbTypeT &type) return std::string(); } -static void readFields(std::string &effect, - XmlNodeConstPtr node, - const ItemFieldDb::FieldInfos &fields) -{ - if (translator == nullptr) - return; - - FOR_EACH (ItemFieldDb::FieldInfos::const_iterator, it, fields) - { - const std::string fieldName = (*it).first; - const ItemFieldType *const field = (*it).second; - - std::string value = XML::getProperty(node, - fieldName.c_str(), - ""); - if (value.empty()) - continue; - if (!effect.empty()) - effect.append(" / "); - if (field->sign && isDigit(value)) - value = std::string("+").append(value); - const std::string format = translator->getStr(field->description); - effect.append(strprintf(format.c_str(), - value.c_str())); - } -} - static void initStatic() { mConstructed = true; @@ -311,9 +285,9 @@ void ItemDB::loadXmlFile(const std::string &fileName, return; } - const ItemFieldDb::FieldInfos &requiredFields = + const ItemFieldInfos &requiredFields = ItemFieldDb::getRequiredFields(); - const ItemFieldDb::FieldInfos &addFields = + const ItemFieldInfos &addFields = ItemFieldDb::getAddFields(); for_each_xml_child_node(node, rootNode) @@ -526,8 +500,8 @@ void ItemDB::loadXmlFile(const std::string &fileName, } std::string effect; - readFields(effect, node, requiredFields); - readFields(effect, node, addFields); + readItemStatsString(effect, node, requiredFields); + readItemStatsString(effect, node, addFields); std::string temp = XML::langProperty(node, "effect", ""); if (!effect.empty() && !temp.empty()) effect.append(" / "); diff --git a/src/resources/db/itemfielddb.cpp b/src/resources/db/itemfielddb.cpp index 231582458..76ad26036 100644 --- a/src/resources/db/itemfielddb.cpp +++ b/src/resources/db/itemfielddb.cpp @@ -33,8 +33,8 @@ namespace { - ItemFieldDb::FieldInfos mRequiredInfos; - ItemFieldDb::FieldInfos mAddInfos; + ItemFieldInfos mRequiredInfos; + ItemFieldInfos mAddInfos; bool mLoaded = false; } // namespace @@ -52,8 +52,8 @@ void ItemFieldDb::load() } static void loadFields(XmlNodeConstPtr groupNode, - ItemFieldDb::FieldInfos &fields1, - ItemFieldDb::FieldInfos &fields2) + ItemFieldInfos &fields1, + ItemFieldInfos &fields2) { for_each_xml_child_node(node, groupNode) { @@ -142,12 +142,12 @@ void ItemFieldDb::unload() mLoaded = false; } -const ItemFieldDb::FieldInfos &ItemFieldDb::getRequiredFields() +const ItemFieldInfos &ItemFieldDb::getRequiredFields() { return mRequiredInfos; } -const ItemFieldDb::FieldInfos &ItemFieldDb::getAddFields() +const ItemFieldInfos &ItemFieldDb::getAddFields() { return mAddInfos; } diff --git a/src/resources/db/itemfielddb.h b/src/resources/db/itemfielddb.h index bd05aad7f..8d88fef84 100644 --- a/src/resources/db/itemfielddb.h +++ b/src/resources/db/itemfielddb.h @@ -23,6 +23,8 @@ #include "enums/simpletypes/skiperror.h" +#include "resources/itemfieldinfos.h" + #include #include @@ -39,11 +41,9 @@ namespace ItemFieldDb void loadXmlFile(const std::string &fileName, const SkipError skipError); - typedef std::map FieldInfos; - - const FieldInfos &getRequiredFields(); + const ItemFieldInfos &getRequiredFields(); - const FieldInfos &getAddFields(); + const ItemFieldInfos &getAddFields(); } // namespace ItemFieldDb #endif // RESOURCES_DB_ITEMFIELDDB_H diff --git a/src/resources/db/itemoptiondb.cpp b/src/resources/db/itemoptiondb.cpp index 204fafb34..e983cd801 100644 --- a/src/resources/db/itemoptiondb.cpp +++ b/src/resources/db/itemoptiondb.cpp @@ -51,14 +51,14 @@ void ItemOptionDb::load() static void addFieldByName(STD_VECTOR &options, XmlNodeConstPtr node, - const ItemFieldDb::FieldInfos &fields, + const ItemFieldInfos &fields, const char *const name) { std::string value = XML::getProperty(node, name, ""); if (value.empty()) return; - FOR_EACH (ItemFieldDb::FieldInfos::const_iterator, it, fields) + FOR_EACH (ItemFieldInfos::const_iterator, it, fields) { const std::string fieldName = (*it).first; if (fieldName == value) @@ -71,7 +71,7 @@ static void addFieldByName(STD_VECTOR &options, static void readOptionFields(STD_VECTOR &options, XmlNodeConstPtr node, - const ItemFieldDb::FieldInfos &fields) + const ItemFieldInfos &fields) { addFieldByName(options, node, fields, "field"); for (int f = 0; f < 15; f ++) @@ -104,9 +104,9 @@ void ItemOptionDb::loadXmlFile(const std::string &fileName, return; } - const ItemFieldDb::FieldInfos &requiredFields = + const ItemFieldInfos &requiredFields = ItemFieldDb::getRequiredFields(); - const ItemFieldDb::FieldInfos &addFields = + const ItemFieldInfos &addFields = ItemFieldDb::getAddFields(); for_each_xml_child_node(node, rootNode) diff --git a/src/resources/itemfieldinfos.h b/src/resources/itemfieldinfos.h new file mode 100644 index 000000000..c19a1837f --- /dev/null +++ b/src/resources/itemfieldinfos.h @@ -0,0 +1,33 @@ +/* + * The ManaPlus Client + * Copyright (C) 2016-2018 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_ITEMFIELDINFOS_H +#define RESOURCES_ITEMFIELDINFOS_H + +#include +#include + +#include "localconsts.h" + +struct ItemFieldType; + +typedef std::map ItemFieldInfos; + +#endif // RESOURCES_ITEMFIELDINFOS_H diff --git a/src/utils/itemxmlutils.cpp b/src/utils/itemxmlutils.cpp new file mode 100644 index 000000000..cbf642c4f --- /dev/null +++ b/src/utils/itemxmlutils.cpp @@ -0,0 +1,56 @@ +/* + * The ManaPlus Client + * Copyright (C) 2014-2018 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 "utils/itemxmlutils.h" + +#include "utils/foreach.h" +#include "utils/stringutils.h" +#include "utils/translation/podict.h" + +#include "resources/item/itemfieldtype.h" + +#include "debug.h" + +void readItemStatsString(std::string &effect, + XmlNodeConstPtr node, + const ItemFieldInfos &fields) +{ + if (translator == nullptr) + return; + + FOR_EACH (ItemFieldInfos::const_iterator, it, fields) + { + const std::string fieldName = (*it).first; + const ItemFieldType *const field = (*it).second; + + std::string value = XML::getProperty(node, + fieldName.c_str(), + ""); + if (value.empty()) + continue; + if (!effect.empty()) + effect.append(" / "); + if (field->sign && isDigit(value)) + value = std::string("+").append(value); + const std::string format = translator->getStr(field->description); + effect.append(strprintf(format.c_str(), + value.c_str())); + } +} diff --git a/src/utils/itemxmlutils.h b/src/utils/itemxmlutils.h new file mode 100644 index 000000000..66f5d8fcd --- /dev/null +++ b/src/utils/itemxmlutils.h @@ -0,0 +1,34 @@ +/* + * The ManaPlus Client + * Copyright (C) 2014-2018 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 UTILS_ITEMXMLUTILS_H +#define UTILS_ITEMXMLUTILS_H + +#include "utils/xml.h" + +#include "resources/itemfieldinfos.h" + +#include + +void readItemStatsString(std::string &effect, + XmlNodeConstPtr node, + const ItemFieldInfos &fields); + +#endif // UTILS_ITEMXMLUTILS_H -- cgit v1.2.3-70-g09d2