diff --git a/CMakeLists.txt b/CMakeLists.txt index e32804f3..5aa578bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -151,7 +151,8 @@ set (LOOT_API_SRC ${LOOT_SRC} "${CMAKE_SOURCE_DIR}/src/api/api.cpp") set (LOOT_API_HEADERS ${LOOT_HEADERS} - "${CMAKE_SOURCE_DIR}/include/loot/api.h") + "${CMAKE_SOURCE_DIR}/include/loot/api.h" + "${CMAKE_SOURCE_DIR}/src/api/_loot_db_int.h") set (LOOT_VALIDATOR_SRC ${LOOT_SRC} "${CMAKE_SOURCE_DIR}/src/validator/main.cpp") diff --git a/src/api/_loot_db_int.h b/src/api/_loot_db_int.h new file mode 100644 index 00000000..c855c5f9 --- /dev/null +++ b/src/api/_loot_db_int.h @@ -0,0 +1,187 @@ +/* LOOT + + A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and + Fallout: New Vegas. + + Copyright (C) 2013-2015 WrinklyNinja + + This file is part of LOOT. + + LOOT 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 3 of + the License, or (at your option) any later version. + + LOOT 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 LOOT. If not, see + . + */ + +#ifndef LOOT_API_LOOT_DB_INT_H +#define LOOT_API_LOOT_DB_INT_H + +#include "../backend/game/game.h" +#include "../backend/error.h" + +#include +#include + +struct _loot_db_int : public loot::Game { + _loot_db_int(const unsigned int clientGame, const std::string& gamePath, const boost::filesystem::path& gameLocalDataPath) + : Game(clientGame) { + this->SetGamePath(gamePath); + this->Init(false, gameLocalDataPath); + } + + loot::MetadataList rawUserMetadata; + loot::Masterlist rawMetadata; + + const char * getRevisionIdString() const { + return revisionId.c_str(); + } + + const char * getRevisionDateString() const { + return revisionDate.c_str(); + } + + const std::vector& getPluginNames() const { + return cPluginNames; + } + + const std::vector& getBashTagMap() const { + return cBashTagMap; + } + + unsigned int getBashTagUid(const std::string& name) const { + auto it = bashTagMap.find(name); + if (it != end(bashTagMap)) + return it->second; + + throw loot::error(loot::error::no_tag_map, "The Bash Tag \"" + name + "\" does not exist in the Bash Tag map."); + } + + const std::vector& getAddedTagIds() const { + return addedTagIds; + } + + const std::vector& getRemovedTagIds() const { + return removedTagIds; + } + + const std::vector& getPluginMessages() const { + return cPluginMessages; + } + + void setRevisionIdString(const std::string& str) { + revisionId = str; + } + + void setRevisionDateString(const std::string& str) { + revisionDate = str; + } + + template + void setPluginNames(const T& plugins) { + // First take copies of the C++ strings to store. + pluginNames.resize(plugins.size()); + std::transform(begin(plugins), + end(plugins), + begin(pluginNames), + [](const loot::Plugin& plugin) { + return plugin.Name(); + }); + + // Now store their C strings. + cPluginNames.resize(pluginNames.size()); + std::transform(begin(pluginNames), + end(pluginNames), + begin(cPluginNames), + [](const std::string& pluginName) { + return pluginName.c_str(); + }); + } + + template + void setAddedTags(const T& names) { + for (const auto& name : names) + addedTagIds.push_back(getBashTagUid(name)); + } + + template + void setRemovedTags(const T& names) { + for (const auto& name : names) + removedTagIds.push_back(getBashTagUid(name)); + } + + template + void setPluginMessages(const T& pluginMessages) { + cPluginMessages.resize(pluginMessages.size()); + pluginMessageStrings.resize(pluginMessages.size()); + + size_t i = 0; + for (const auto& message : pluginMessages) { + pluginMessageStrings[i] = message.ChooseContent(loot::Language::any).Str(); + + cPluginMessages[i].type = message.Type(); + cPluginMessages[i].message = pluginMessageStrings[i].c_str(); + + ++i; + } + } + + void addBashTagsToMap(std::set names) { + for (const auto& name : names) { + // Try adding the Bash Tag to the map assuming it's not already in + // there, then use the UID in the returned value, as that will be + // equal to the value in the map, even if the Bash Tag was already + // present. + unsigned int uid = bashTagMap.size(); + // If the tag already exists in the map, do + auto it = bashTagMap.emplace(name, uid).first; + if (it->second == cBashTagMap.size()) + cBashTagMap.push_back(it->first.c_str()); + else + cBashTagMap.at(it->second) = it->first.c_str(); + } + } + + void clearBashTagMap() { + bashTagMap.clear(); + cBashTagMap.clear(); + } + + void clearArrays() { + pluginNames.clear(); + cPluginNames.clear(); + + addedTagIds.clear(); + removedTagIds.clear(); + + cPluginMessages.clear(); + pluginMessageStrings.clear(); + } +private: + std::string revisionId; + std::string revisionDate; + + std::vector pluginNames; + std::vector cPluginNames; + + // For the Bash Tag map, a string is mapped to a UID that is also the + // index of the vector where the C string can be found. + std::unordered_map bashTagMap; + std::vector cBashTagMap; + + std::vector addedTagIds; + std::vector removedTagIds; + + std::vector cPluginMessages; + std::vector pluginMessageStrings; +}; + +#endif \ No newline at end of file diff --git a/src/api/api.cpp b/src/api/api.cpp index 5a41af2e..3b576fb1 100644 --- a/src/api/api.cpp +++ b/src/api/api.cpp @@ -23,9 +23,8 @@ */ #include "loot/api.h" -#include "../backend/game/game.h" +#include "_loot_db_int.h" #include "../backend/globals.h" -#include "../backend/error.h" #include "../backend/plugin_sorter.h" #include @@ -33,9 +32,7 @@ #include #include #include -#include #include -#include #include #include @@ -89,159 +86,6 @@ const unsigned int loot_needs_cleaning_no = 0; const unsigned int loot_needs_cleaning_yes = 1; const unsigned int loot_needs_cleaning_unknown = 2; -struct _loot_db_int : public loot::Game { - _loot_db_int(const unsigned int clientGame, const std::string& gamePath, const boost::filesystem::path& gameLocalDataPath) - : Game(clientGame) { - this->SetGamePath(gamePath); - this->Init(false, gameLocalDataPath); - } - - loot::MetadataList rawUserMetadata; - loot::Masterlist rawMetadata; - - const char * getRevisionIdString() const { - return revisionId.c_str(); - } - - const char * getRevisionDateString() const { - return revisionDate.c_str(); - } - - const std::vector& getPluginNames() const { - return cPluginNames; - } - - const std::vector& getBashTagMap() const { - return cBashTagMap; - } - - unsigned int getBashTagUid(const std::string& name) const { - auto it = bashTagMap.find(name); - if (it != end(bashTagMap)) - return it->second; - - throw loot::error(loot::error::no_tag_map, "The Bash Tag \"" + name + "\" does not exist in the Bash Tag map."); - } - - const std::vector& getAddedTagIds() const { - return addedTagIds; - } - - const std::vector& getRemovedTagIds() const { - return removedTagIds; - } - - const std::vector& getPluginMessages() const { - return cPluginMessages; - } - - void setRevisionIdString(const std::string& str) { - revisionId = str; - } - - void setRevisionDateString(const std::string& str) { - revisionDate = str; - } - - template - void setPluginNames(const T& plugins) { - // First take copies of the C++ strings to store. - pluginNames.resize(plugins.size()); - std::transform(begin(plugins), - end(plugins), - begin(pluginNames), - [](const loot::Plugin& plugin) { - return plugin.Name(); - }); - - // Now store their C strings. - cPluginNames.resize(pluginNames.size()); - std::transform(begin(pluginNames), - end(pluginNames), - begin(cPluginNames), - [](const std::string& pluginName) { - return pluginName.c_str(); - }); - } - - template - void setAddedTags(const T& names) { - for (const auto& name : names) - addedTagIds.push_back(getBashTagUid(name)); - } - - template - void setRemovedTags(const T& names) { - for (const auto& name : names) - removedTagIds.push_back(getBashTagUid(name)); - } - - template - void setPluginMessages(const T& pluginMessages) { - cPluginMessages.resize(pluginMessages.size()); - pluginMessageStrings.resize(pluginMessages.size()); - - size_t i = 0; - for (const auto& message : pluginMessages) { - pluginMessageStrings[i] = message.ChooseContent(loot::Language::any).Str(); - - cPluginMessages[i].type = message.Type(); - cPluginMessages[i].message = pluginMessageStrings[i].c_str(); - - ++i; - } - } - - void addBashTagsToMap(std::set names) { - for (const auto& name : names) { - // Try adding the Bash Tag to the map assuming it's not already in - // there, then use the UID in the returned value, as that will be - // equal to the value in the map, even if the Bash Tag was already - // present. - unsigned int uid = bashTagMap.size(); - // If the tag already exists in the map, do - auto it = bashTagMap.emplace(name, uid).first; - if (it->second == cBashTagMap.size()) - cBashTagMap.push_back(it->first.c_str()); - else - cBashTagMap.at(it->second) = it->first.c_str(); - } - } - - void clearBashTagMap() { - bashTagMap.clear(); - cBashTagMap.clear(); - } - - void clearArrays() { - pluginNames.clear(); - cPluginNames.clear(); - - addedTagIds.clear(); - removedTagIds.clear(); - - cPluginMessages.clear(); - pluginMessageStrings.clear(); - } -private: - std::string revisionId; - std::string revisionDate; - - std::vector pluginNames; - std::vector cPluginNames; - - // For the Bash Tag map, a string is mapped to a UID that is also the - // index of the vector where the C string can be found. - std::unordered_map bashTagMap; - std::vector cBashTagMap; - - std::vector addedTagIds; - std::vector removedTagIds; - - std::vector cPluginMessages; - std::vector pluginMessageStrings; -}; - std::string extMessageStr; unsigned int c_error(const loot::error& e) {