From 3f87764a94c68256fcb8f4dc5197cfb593e76389 Mon Sep 17 00:00:00 2001 From: jordan4ibanez Date: Tue, 28 Nov 2023 05:21:34 -0500 Subject: [PATCH] Make this a bit elegant --- source/Irrlicht/CB3DJSONMeshFileLoader.cpp | 36 ++++++++++++++-------- source/Irrlicht/CB3DJSONMeshFileLoader.h | 1 + 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/source/Irrlicht/CB3DJSONMeshFileLoader.cpp b/source/Irrlicht/CB3DJSONMeshFileLoader.cpp index 1e7a3935..5724222f 100644 --- a/source/Irrlicht/CB3DJSONMeshFileLoader.cpp +++ b/source/Irrlicht/CB3DJSONMeshFileLoader.cpp @@ -15,6 +15,7 @@ #include #include "json/json.hpp" #include +#include using json = nlohmann::json; namespace irr @@ -156,8 +157,21 @@ bool parseNode(json data, SMeshBuffer* meshBuffer) { return false; } -// Returns success. -bool load(json data) { +/** + * Returns (success, failure reason). +*/ +std::tuple CB3DJSONMeshFileLoader::load() { + + + + // Now check some real basic elements of the JSON file. + if (!JSONDataContainer.contains("format") || !JSONDataContainer["format"].is_string() || JSONDataContainer["format"] != "BB3D") { + return {false, "No format in B3D JSON! Expected: BB3D"}; + } + if (!JSONDataContainer.contains("version") || !JSONDataContainer["version"].is_number_integer() || JSONDataContainer["version"] != 1) { + os::Printer::log("Wrong version in B3D JSON! Expected: 1", ELL_WARNING); + return false; + } //! Remember: This is basically a linked tree. @@ -242,18 +256,14 @@ IAnimatedMesh* CB3DJSONMeshFileLoader::createMesh(io::IReadFile* file) { //? Now JSONDataContainer exists on this object. //? So we can commence loading it. - // Now check some real basic elements of the JSON file. - if (!data.contains("format") || !data["format"].is_string() || data["format"] != "BB3D") { - os::Printer::log("No format in B3D JSON! Expected: BB3DJSON", ELL_WARNING); - return nullptr; - } - if (!data.contains("version") || !data["version"].is_number_integer() || data["version"] != 1) { - os::Printer::log("Wrong version in B3D JSON! Expected: 1", ELL_WARNING); - return nullptr; - } + // Now we can start doing a full parse of the data in the model JSON and build it as an irrlicht model. + const std::tuple loadResult = load(); - // Now we can start doing a full parse of the data in the model JSON. - const bool placeholder = parseModel(data); + // If it failed to load, we give up. + if (!std::get<0>(loadResult)) { + // Print the reason, and return a nullptr. + return(this->cleanUp(std::get<1>(loadResult))); + } println("We got to the end."); return nullptr; diff --git a/source/Irrlicht/CB3DJSONMeshFileLoader.h b/source/Irrlicht/CB3DJSONMeshFileLoader.h index 9b60771c..233ad2c4 100644 --- a/source/Irrlicht/CB3DJSONMeshFileLoader.h +++ b/source/Irrlicht/CB3DJSONMeshFileLoader.h @@ -29,6 +29,7 @@ private: // Methods. CSkinnedMesh* cleanUp(std::string); bool parseJSONFile(io::IReadFile* file); + std::tuple load(); public: