From 4608bc6250459316a3259debe6a16f9fa092ea6a Mon Sep 17 00:00:00 2001 From: WrinklyNinja Date: Fri, 9 May 2014 11:17:45 +0100 Subject: [PATCH] Re-implemented "no changes" report message. This completes issue #136. --- src/backend/generators.cpp | 98 +++++++++++++++++++++++++++++++++----- 1 file changed, 85 insertions(+), 13 deletions(-) diff --git a/src/backend/generators.cpp b/src/backend/generators.cpp index daed8171..502ab49d 100644 --- a/src/backend/generators.cpp +++ b/src/backend/generators.cpp @@ -33,6 +33,57 @@ along with LOOT. If not, see namespace loot { //LOOT Report generation stuff. + void GetOldReportDetails(const boost::filesystem::path& filepath, YAML::Node& node) { + if (boost::filesystem::exists(filepath)) { + BOOST_LOG_TRIVIAL(debug) << "Reading the previous report's details section."; + //Read the whole file in. + std::string contents; + loot::ifstream in(filepath, std::ios::binary); + in.seekg(0, std::ios::end); + contents.resize(in.tellg()); + in.seekg(0, std::ios::beg); + in.read(&contents[0], contents.size()); + in.close(); + + //Cut off non-JSON part of file. + contents = contents.substr(11); //"var data = ". + //Parse as YAML. + node = YAML::Load(contents); + if (node["plugins"]) { + node = node["plugins"]; + } + } + } + + bool AreDetailsEqual(const YAML::Node& lhs, const YAML::Node& rhs) { + //We want to check for plugin order and messages. + if (lhs.IsSequence() && rhs.IsSequence()) { + std::list lhs_names, rhs_names; + std::list lhs_messages, rhs_messages; + + for (YAML::const_iterator it = lhs.begin(); it != lhs.end(); ++it) { + if ((*it)["name"]) + lhs_names.push_back((*it)["name"].as()); + if ((*it)["messages"]) { + std::list messages = (*it)["messages"].as< std::list >(); + lhs_messages.insert(lhs_messages.end(), messages.begin(), messages.end()); + } + } + for (YAML::const_iterator it = rhs.begin(); it != rhs.end(); ++it) { + if ((*it)["name"]) + rhs_names.push_back((*it)["name"].as()); + if ((*it)["messages"]) { + std::list messages = (*it)["messages"].as< std::list >(); + rhs_messages.insert(rhs_messages.end(), messages.begin(), messages.end()); + } + } + + return lhs_names == rhs_names && lhs_messages == rhs_messages; + } + else + return false; + } + void WriteMessage(YAML::Emitter& out, const Message& message) { //Look for Markdown URL syntax and convert any found. @@ -170,6 +221,9 @@ namespace loot { const std::list& plugins, const std::string& masterlistVersion, const bool masterlistUpdateEnabled) { + + YAML::Node oldDetails; + GetOldReportDetails(file, oldDetails); //Need to output YAML as a JSON Javascript variable. YAML::Emitter yout; @@ -202,6 +256,37 @@ namespace loot { << YAML::Value << "" << YAML::EndMap; + if (!plugins.empty()) { + BOOST_LOG_TRIVIAL(debug) << "Generating JSON plugin data."; + YAML::Emitter tempout; + tempout.SetOutputCharset(YAML::EscapeNonAscii); + tempout.SetStringFormat(YAML::DoubleQuoted); + tempout.SetBoolFormat(YAML::TrueFalseBool); + tempout.SetSeqFormat(YAML::Flow); + tempout.SetMapFormat(YAML::Flow); + + tempout << YAML::BeginSeq; + for (std::list::const_iterator it = plugins.begin(), endit = plugins.end(); it != endit; ++it) { + WritePlugin(tempout, *it); + } + tempout << YAML::EndSeq; + + YAML::Node node = YAML::Load(tempout.c_str()); + + if (AreDetailsEqual(node, oldDetails)) + messages.push_front(loot::Message(loot::Message::say, boost::locale::translate("There have been no changes in the Details tab since LOOT was last run for this game.").str())); + + //Need to generate output twice because passing the node causes ! to be written before every key and value for some reason. + yout << YAML::Key << "plugins" + << YAML::Value << YAML::BeginSeq; + for (std::list::const_iterator it = plugins.begin(), endit = plugins.end(); it != endit; ++it) { + WritePlugin(yout, *it); + } + yout << YAML::EndSeq; + } + else if (oldDetails.size() == 0) + messages.push_front(loot::Message(loot::Message::say, boost::locale::translate("There have been no changes in the Details tab since LOOT was last run for this game.").str())); + if (!messages.empty()) { BOOST_LOG_TRIVIAL(debug) << "Generating JSON general message data."; yout << YAML::Key << "globalMessages" @@ -212,20 +297,7 @@ namespace loot { yout << YAML::EndSeq; } - if (!plugins.empty()) { - BOOST_LOG_TRIVIAL(debug) << "Generating JSON plugin data."; - yout << YAML::Key << "plugins" - << YAML::Value << YAML::BeginSeq; - for (std::list::const_iterator it = plugins.begin(), endit = plugins.end(); it != endit; ++it) { - WritePlugin(yout, *it); - } - yout << YAML::EndSeq; - } - BOOST_LOG_TRIVIAL(debug) << "Generating text translations."; - - - yout << YAML::Key << "l10n" << YAML::BeginMap