Make this more readable

This commit is contained in:
jordan4ibanez 2023-11-26 09:46:25 -05:00
parent fadbb249c6
commit 2a16c10eba
1 changed files with 15 additions and 22 deletions

View File

@ -50,45 +50,37 @@ bool CB3DJSONMeshFileLoader::isALoadableFileExtension(
// Returns if errored. // Returns if errored.
bool parseNode(json data, SMeshBuffer* meshBuffer) { bool parseNode(json data, SMeshBuffer* meshBuffer) {
auto position = irr::core::vector3df{0,0,0}; auto position = irr::core::vector3df{0,0,0};
auto scale = irr::core::vector3df{1,1,1}; auto scale = irr::core::vector3df{1,1,1};
auto rotation = irr::core::quaternion{0,0,0,1}; auto rotation = irr::core::quaternion{0,0,0,1};
if (data.contains("position") && data["position"].is_array()) { if (data.contains("position") && data["position"].is_array() && data["position"].size() == 3) {
auto positionJSON = data["position"];
auto positionJSON = data["position"].array();
if (!positionJSON.size() == 3) {
os::Printer::log("Error, position in NODE must be an array of 3 integers!", ELL_WARNING);
return true;
}
println("found a position data!");
int i = 0; int i = 0;
for (auto p = positionJSON.begin(); p != positionJSON.end(); ++p) { for (auto reference = positionJSON.begin(); reference != positionJSON.end(); ++reference) {
auto value = *reference;
auto dereffed = *p; if (!value.is_number_integer()) {
if (!dereffed.is_number_integer()) {
os::Printer::log("Error, position in NODE must be an array of 3 integers!", ELL_WARNING); os::Printer::log("Error, position in NODE must be an array of 3 integers!", ELL_WARNING);
return true; return true;
} }
switch (i){ switch (i){
case 0: case 0:
position.X = dereffed; position.X = value;
println(std::string("x is: ").append(std::to_string(position.X)).c_str());
break; break;
case 1: case 1:
position.Y = dereffed; position.Y = value;
break; break;
case 2: case 2:
position.Z = dereffed; position.Z = value;
break; break;
} }
i++; i++;
} }
} else {
os::Printer::log("Error, position in NODE must be an array of 3 integers!", ELL_WARNING);
return true;
} }
return false; return false;
} }
@ -108,6 +100,7 @@ IAnimatedMesh* parseModel(json data) {
if (data.contains("NODE") && data["NODE"].is_object()) { if (data.contains("NODE") && data["NODE"].is_object()) {
println("Yep, that's a node!"); println("Yep, that's a node!");
// If it fails, give up basically.
if (parseNode(data["NODE"], meshBuffer)) { if (parseNode(data["NODE"], meshBuffer)) {
return nullptr; return nullptr;
} }
@ -139,7 +132,7 @@ IAnimatedMesh* CB3DJSONMeshFileLoader::createMesh(io::IReadFile* file) {
char* output = buffer.get(); char* output = buffer.get();
// Irrlicht doesn't null terminate the raw output. So we must doe it ourself. // Irrlicht doesn't null terminate the raw output. So we must doe it ourself.
buffer[file->getSize()] = '\0'; output[file->getSize()] = '\0';
// We have to catch a JSON parse error or else the game will segfault. // We have to catch a JSON parse error or else the game will segfault.
json data; json data;