From 7ce88589571a773c657675da98c23c33ec126cd9 Mon Sep 17 00:00:00 2001 From: jordan4ibanez Date: Tue, 28 Nov 2023 05:38:24 -0500 Subject: [PATCH] Now make it even more elegant --- source/Irrlicht/CB3DJSONMeshFileLoader.cpp | 22 +++++++++++++--------- source/Irrlicht/CB3DJSONMeshFileLoader.h | 3 ++- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/source/Irrlicht/CB3DJSONMeshFileLoader.cpp b/source/Irrlicht/CB3DJSONMeshFileLoader.cpp index 8a2418b1..ea92ff66 100644 --- a/source/Irrlicht/CB3DJSONMeshFileLoader.cpp +++ b/source/Irrlicht/CB3DJSONMeshFileLoader.cpp @@ -207,9 +207,9 @@ CSkinnedMesh* CB3DJSONMeshFileLoader::cleanUp(std::string failure) { } /** - * Returns success. + * Returns (success, failure reason). */ -bool CB3DJSONMeshFileLoader::parseJSONFile(io::IReadFile* file) { +std::tuple CB3DJSONMeshFileLoader::parseJSONFile(io::IReadFile* file) { // So here we turn this mangled disaster into a C string. // Please consider this the equivalent of duct taping a chainsaw onto a car to cut your lawn. auto buffer = std::make_unique(file->getSize()); @@ -227,15 +227,15 @@ bool CB3DJSONMeshFileLoader::parseJSONFile(io::IReadFile* file) { try { JSONDataContainer = json::parse(output); } catch (const json::parse_error& e) { - std::cout << "message: " << e.what() << '\n' - << "exception id: " << e.id << '\n' - << "byte position of error: " << e.byte << std::endl; + std::string failureReason = "message: " + std::string(e.what()) + '\n' + + "exception id: " + std::to_string(e.id) + '\n' + + "byte position of error: " + std::to_string(e.byte) + '\n'; os::Printer::log("JSON: Failed to parse!", ELL_WARNING); - return false; + return {false, failureReason}; } // I'm not sure if buffer and output gets dropped here. - return true; + return {true, nullptr}; } IAnimatedMesh* CB3DJSONMeshFileLoader::createMesh(io::IReadFile* file) { @@ -246,8 +246,12 @@ IAnimatedMesh* CB3DJSONMeshFileLoader::createMesh(io::IReadFile* file) { } // Try to read this file. - if (!this->parseJSONFile(file)) { - return(this->cleanUp("B3D JSON severe error! File size is 0!")); + const std::tuple parseResult = this->parseJSONFile(file); + + // If it failed to parse, we give up. + if (!std::get<0>(parseResult)) { + // Print the reason, and return a nullptr. + return(this->cleanUp(std::get<1>(parseResult))); } //? Now JSONDataContainer exists on this object. diff --git a/source/Irrlicht/CB3DJSONMeshFileLoader.h b/source/Irrlicht/CB3DJSONMeshFileLoader.h index 8544c97f..d500be59 100644 --- a/source/Irrlicht/CB3DJSONMeshFileLoader.h +++ b/source/Irrlicht/CB3DJSONMeshFileLoader.h @@ -6,6 +6,7 @@ #include "IReadFile.h" #include "path.h" #include "json/json.hpp" +#include using json = nlohmann::json; @@ -31,7 +32,7 @@ private: // Methods. CSkinnedMesh* cleanUp(std::string); - bool parseJSONFile(io::IReadFile* file); + std::tuple parseJSONFile(io::IReadFile* file); std::tuple load(); public: