diff --git a/src/TechTree.cpp b/src/TechTree.cpp index d33039f7870d6a5f81cfca68b64238dad091a08d..8f7cccfbf85b5f196eb8ae2438bfbfd866e7450a 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 7cdd25589f2c4343a28acec3969d4c94d6bd968e..b7a8dd1e030e5b1ab922e36b75fc7511a18f8a44 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 c17d847f1ebb20d46582c9927029559955ebca68..9f24a2161293ba847e29fd04a3e9eac4848f99fb 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