From f9eb6bf158f1ff49a68a0c0872bc5c75942856fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Bergstr=C3=B6m?= <davbe125@student.liu.se> Date: Wed, 25 Jul 2018 09:48:03 +0200 Subject: [PATCH] Handle situtation where techtree.json is missing --- src/TechTree.cpp | 8 +++++++- src/TechTreeImproved.cpp | 14 +++++++++++--- src/TechTreeImproved.h | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/TechTree.cpp b/src/TechTree.cpp index d33039f78..8f7cccfbf 100644 --- a/src/TechTree.cpp +++ b/src/TechTree.cpp @@ -19,7 +19,13 @@ void TechTree::onStart() solution at the time. Hopefully the JSON-files are still up to date. */ TechTreeImproved tree; - tree.LoadData(); + bool success = tree.LoadData(); + + // If the file was not successfully open/parsed, don't replace the unitTypeData. + if (!success) + { + return; + } for (std::pair<const UnitType, TypeData> & pair : m_unitTypeData) { diff --git a/src/TechTreeImproved.cpp b/src/TechTreeImproved.cpp index 7cdd25589..b7a8dd1e0 100644 --- a/src/TechTreeImproved.cpp +++ b/src/TechTreeImproved.cpp @@ -112,13 +112,20 @@ void TechTreeImproved::parse_unit(json::iterator it) producer_to_data[producer_id] = build_descriptions; } -void TechTreeImproved::LoadData() { - // TODO: Do not hardcode this. Use the latest json available. - // TODO: Check if file exists +bool TechTreeImproved::LoadData() { std::ifstream i("techtree.json"); + + if (!i.good()) + { + std::wcerr << "File techtree.json cannot be found, information regarding addons and required buildings will not be up to date. Please put techtree.json in working directory." << std::endl; + return false; + } + + // Parse the file's content json j; i >> j; + // Time to parse content of the JSON file for (auto & race : j) { for (json::iterator it = race.begin(); it != race.end(); ++it) @@ -126,6 +133,7 @@ void TechTreeImproved::LoadData() { parse_unit(it); } } + return true; } const std::vector<BuildDescription> & TechTreeImproved::HowToBuild(sc2::UnitTypeID unit) const diff --git a/src/TechTreeImproved.h b/src/TechTreeImproved.h index c17d847f1..9f24a2161 100644 --- a/src/TechTreeImproved.h +++ b/src/TechTreeImproved.h @@ -31,7 +31,7 @@ class TechTreeImproved void parse_unit(nlohmann::json::iterator it); public: TechTreeImproved(); - void LoadData(); + bool LoadData(); // Given a unit, how can we build it? const std::vector<BuildDescription> & HowToBuild(sc2::UnitTypeID unit) const; }; \ No newline at end of file -- GitLab