From 9fd5711bb6d34849b156d1cd1f261f1c91f01d78 Mon Sep 17 00:00:00 2001 From: WrinklyNinja Date: Thu, 14 Aug 2014 22:25:28 +0100 Subject: [PATCH] Implemented C++ side of masterlist updating. --- src/gui/handler.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++- src/gui/handler.h | 1 + 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/gui/handler.cpp b/src/gui/handler.cpp index 969e4cac..e79bc42e 100644 --- a/src/gui/handler.cpp +++ b/src/gui/handler.cpp @@ -167,6 +167,10 @@ namespace loot { callback->Success(""); return true; } + else if (request == "updateMasterlist") { + callback->Success(UpdateMasterlist()); + return true; + } else { // May be a request with arguments. YAML::Node req; @@ -258,7 +262,6 @@ namespace loot { text = yout.c_str(); } - #if defined(OS_WIN) if (!OpenClipboard(NULL)) { BOOST_LOG_TRIVIAL(error) << "Failed to open the Windows clipboard."; @@ -490,6 +493,7 @@ namespace loot { language = Language(g_app_state.GetSettings()["language"].as()).Code(); else language = Language::any; + BOOST_LOG_TRIVIAL(info) << "Using message language: " << Language(language).Name(); // The data structure is to be set as 'loot.game'. YAML::Node gameNode; @@ -560,8 +564,79 @@ namespace loot { gameNode["plugins"].push_back(pluginNode); } + //Evaluate any conditions in the global messages. + BOOST_LOG_TRIVIAL(debug) << "Evaluating global message conditions."; + list messages = g_app_state.CurrentGame().masterlist.messages; + try { + list::iterator it = messages.begin(); + while (it != messages.end()) { + if (!it->EvalCondition(g_app_state.CurrentGame(), language)) + it = messages.erase(it); + else + ++it; + } + } + catch (std::exception& e) { + BOOST_LOG_TRIVIAL(error) << "A global message contains a condition that could not be evaluated. Details: " << e.what(); + messages.push_back(Message(Message::error, (format(loc::translate("A global message contains a condition that could not be evaluated. Details: %1%")) % e.what()).str())); + } + + // Now store global messages from masterlist. + gameNode["globalMessages"] = messages; + + return JSON::stringify(gameNode); + } + + std::string Handler::UpdateMasterlist() { + BOOST_LOG_TRIVIAL(debug) << "Updating and parsing masterlist."; + + //Set language. + unsigned int language; + if (g_app_state.GetSettings()["language"]) + language = Language(g_app_state.GetSettings()["language"].as()).Code(); + else + language = Language::any; BOOST_LOG_TRIVIAL(info) << "Using message language: " << Language(language).Name(); + // Update / parse masterlist. + g_app_state.CurrentGame().masterlist.Load(g_app_state.CurrentGame(), language); + + // Now regenerate the JS-side masterlist data. + + // The data structure is to be set as 'loot.game'. + YAML::Node gameNode; + + // Store the masterlist revision and date. + gameNode["masterlist"]["revision"] = g_app_state.CurrentGame().masterlist.GetRevision(g_app_state.CurrentGame().MasterlistPath()); + gameNode["masterlist"]["date"] = g_app_state.CurrentGame().masterlist.GetDate(g_app_state.CurrentGame().MasterlistPath()); + + for (const auto& pluginPair : g_app_state.CurrentGame().plugins) { + Plugin mlistPlugin(pluginPair.second); + mlistPlugin.MergeMetadata(g_app_state.CurrentGame().masterlist.FindPlugin(pluginPair.first)); + + YAML::Node pluginNode; + if (!mlistPlugin.HasNameOnly()) { + // Now add the masterlist metadata to the pluginNode. + pluginNode["masterlist"]["after"] = mlistPlugin.LoadAfter(); + pluginNode["masterlist"]["req"] = mlistPlugin.Reqs(); + pluginNode["masterlist"]["inc"] = mlistPlugin.Incs(); + pluginNode["masterlist"]["msg"] = mlistPlugin.Messages(); + pluginNode["masterlist"]["tag"] = mlistPlugin.Tags(); + pluginNode["masterlist"]["dirty"] = mlistPlugin.DirtyInfo(); + } + + // Now merge masterlist and userlist metadata and evaluate, + // putting any resulting metadata into the base of the pluginNode. + YAML::Node derivedNode = GenerateDerivedMetadata(pluginPair.first); + + for (auto it = derivedNode.begin(); it != derivedNode.end(); ++it) { + const string key = it->first.as(); + pluginNode[key] = it->second; + } + + gameNode["plugins"].push_back(pluginNode); + } + //Evaluate any conditions in the global messages. BOOST_LOG_TRIVIAL(debug) << "Evaluating global message conditions."; list messages = g_app_state.CurrentGame().masterlist.messages; @@ -592,6 +667,7 @@ namespace loot { language = Language(g_app_state.GetSettings()["language"].as()).Code(); else language = Language::any; + BOOST_LOG_TRIVIAL(info) << "Using message language: " << Language(language).Name(); // Now rederive the displayed metadata from the masterlist and userlist. Plugin tempPlugin(file); diff --git a/src/gui/handler.h b/src/gui/handler.h index 0ce8306a..362143a8 100644 --- a/src/gui/handler.h +++ b/src/gui/handler.h @@ -57,6 +57,7 @@ namespace loot { std::string GetGameTypes(); std::string GetInstalledGames(); std::string GetGameData(); + std::string UpdateMasterlist(); YAML::Node Handler::GenerateDerivedMetadata(const std::string& pluginName); YAML::Node Handler::GenerateDerivedMetadata(const Plugin& file, const Plugin& masterlist, const Plugin& userlist);