2023-11-26 08:53:55 +01:00
|
|
|
#include "CB3DJSONMeshFileLoader.h"
|
|
|
|
#include "coreutil.h"
|
2023-11-26 07:43:26 +01:00
|
|
|
#include "IAnimatedMesh.h"
|
2023-11-28 12:08:17 +01:00
|
|
|
#include "SB3DStructs.h"
|
2023-11-26 12:52:22 +01:00
|
|
|
#include "S3DVertex.h"
|
|
|
|
#include "SAnimatedMesh.h"
|
|
|
|
#include "SColor.h"
|
|
|
|
#include "CMeshBuffer.h"
|
|
|
|
#include "SMesh.h"
|
|
|
|
#include "vector3d.h"
|
|
|
|
#include "quaternion.h"
|
2023-11-26 07:43:26 +01:00
|
|
|
#include "IReadFile.h"
|
|
|
|
#include "path.h"
|
2023-11-26 11:10:04 +01:00
|
|
|
#include <iostream>
|
2023-11-26 09:37:35 +01:00
|
|
|
#include "os.h"
|
2023-11-26 15:30:26 +01:00
|
|
|
#include <string>
|
2023-11-26 11:10:04 +01:00
|
|
|
#include "json/json.hpp"
|
|
|
|
#include <array>
|
2023-11-28 11:21:34 +01:00
|
|
|
#include <tuple>
|
2023-11-26 09:50:44 +01:00
|
|
|
using json = nlohmann::json;
|
2023-11-26 07:48:13 +01:00
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace scene
|
|
|
|
{
|
|
|
|
|
2023-11-26 09:09:50 +01:00
|
|
|
//! Remember to remove this function. It's very dumb.
|
|
|
|
void println(const char* data) {
|
2023-11-26 09:37:35 +01:00
|
|
|
printf(data);
|
|
|
|
printf("\n");
|
2023-11-26 09:09:50 +01:00
|
|
|
}
|
2023-11-28 13:11:55 +01:00
|
|
|
void println(std::string data) {
|
|
|
|
printf((data + "\n").c_str());
|
|
|
|
}
|
2023-11-26 09:50:44 +01:00
|
|
|
const char * const boolToString(bool b)
|
|
|
|
{
|
|
|
|
return b ? "true" : "false";
|
|
|
|
}
|
2023-11-26 09:09:50 +01:00
|
|
|
|
|
|
|
|
2023-11-26 08:53:55 +01:00
|
|
|
// Class methods.
|
2023-11-26 08:03:32 +01:00
|
|
|
CB3DJSONMeshFileLoader::CB3DJSONMeshFileLoader()
|
|
|
|
{
|
2023-11-26 08:53:55 +01:00
|
|
|
|
2023-11-26 08:03:32 +01:00
|
|
|
}
|
2023-11-26 07:48:13 +01:00
|
|
|
|
2023-11-26 08:03:32 +01:00
|
|
|
bool CB3DJSONMeshFileLoader::isALoadableFileExtension(
|
|
|
|
const io::path& fileName) const
|
|
|
|
{
|
2023-11-26 08:53:55 +01:00
|
|
|
//* we could do multiple accepted extensions, like b3dj jb3d, j3d, etc. Talk with core devs about this.
|
2023-11-26 09:21:28 +01:00
|
|
|
//? For now, just JSON because it has vscode linting.
|
|
|
|
return core::hasFileExtension(fileName, "json");
|
2023-11-26 08:03:32 +01:00
|
|
|
}
|
2023-11-26 07:48:13 +01:00
|
|
|
|
2023-11-28 12:30:07 +01:00
|
|
|
/**
|
|
|
|
* Simple error builder for Vec2, Vec3, and Quaternion.
|
|
|
|
*/
|
|
|
|
std::string buildVectorError(std::string key, int index, int width) {
|
|
|
|
|
|
|
|
if (index == -1) {
|
|
|
|
return std::string("Error, " + key + " is missing!");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string component;
|
|
|
|
switch (index) {
|
|
|
|
case 0:
|
|
|
|
component = "X";
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
component = "Y";
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
component = "Z";
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
component = "W";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return std::string("Error, " + component + " in " + key + "is not a number! " + key + " must be an array of " + std::to_string(width) + " numbers!");
|
|
|
|
}
|
|
|
|
|
2023-11-28 12:35:06 +01:00
|
|
|
|
|
|
|
/**
|
2023-11-28 12:38:24 +01:00
|
|
|
* Returns (success, failure reason).
|
2023-11-28 12:35:06 +01:00
|
|
|
*/
|
|
|
|
std::tuple<bool, std::string> grabVec2f(json data, std::string key, irr::core::vector2df& refVec) {
|
2023-11-28 12:30:07 +01:00
|
|
|
|
|
|
|
// todo: make a CurrentElement thing in the class header so that we can print nice debug info.
|
|
|
|
|
|
|
|
if (data.contains(key) && data[key].is_array() && data[key].size() == 2) {
|
|
|
|
auto jsonVec2 = data[key];
|
|
|
|
int i = 0;
|
|
|
|
for (auto reference = jsonVec2.begin(); reference != jsonVec2.end(); ++reference) {
|
|
|
|
auto value = *reference;
|
|
|
|
// Can take integer OR float.
|
|
|
|
if (!value.is_number()) {
|
2023-11-28 12:35:06 +01:00
|
|
|
return {false, buildVectorError(key, i, 2)};
|
2023-11-28 12:30:07 +01:00
|
|
|
}
|
|
|
|
switch (i){
|
|
|
|
case 0:
|
|
|
|
refVec.X = value;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
refVec.Y = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} else {
|
2023-11-28 12:35:06 +01:00
|
|
|
return {false, buildVectorError(key, -1, 2)};
|
2023-11-28 12:30:07 +01:00
|
|
|
}
|
2023-11-28 12:53:23 +01:00
|
|
|
return {true, ""};
|
2023-11-28 12:30:07 +01:00
|
|
|
}
|
|
|
|
|
2023-11-26 16:02:11 +01:00
|
|
|
/**
|
2023-11-28 12:38:24 +01:00
|
|
|
* Returns (success, failure reason).
|
2023-11-26 16:02:11 +01:00
|
|
|
*/
|
2023-11-28 12:35:06 +01:00
|
|
|
std::tuple<bool, std::string> grabVec3f(json data, std::string key, irr::core::vector3df& refVec) {
|
2023-11-28 11:43:22 +01:00
|
|
|
|
|
|
|
// todo: make a CurrentElement thing in the class header so that we can print nice debug info.
|
|
|
|
|
2023-11-26 15:51:43 +01:00
|
|
|
if (data.contains(key) && data[key].is_array() && data[key].size() == 3) {
|
|
|
|
auto jsonVec3 = data[key];
|
2023-11-26 12:52:22 +01:00
|
|
|
int i = 0;
|
2023-11-26 15:51:43 +01:00
|
|
|
for (auto reference = jsonVec3.begin(); reference != jsonVec3.end(); ++reference) {
|
2023-11-26 15:46:25 +01:00
|
|
|
auto value = *reference;
|
2023-11-26 15:58:03 +01:00
|
|
|
// Can take integer OR float.
|
|
|
|
if (!value.is_number()) {
|
2023-11-28 12:35:06 +01:00
|
|
|
return {false, buildVectorError(key, i, 3)};
|
2023-11-26 12:52:22 +01:00
|
|
|
}
|
|
|
|
switch (i){
|
|
|
|
case 0:
|
2023-11-26 15:51:43 +01:00
|
|
|
refVec.X = value;
|
2023-11-26 12:52:22 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
2023-11-26 15:51:43 +01:00
|
|
|
refVec.Y = value;
|
2023-11-26 12:52:22 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
2023-11-26 15:51:43 +01:00
|
|
|
refVec.Z = value;
|
2023-11-26 12:52:22 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2023-11-26 15:46:25 +01:00
|
|
|
} else {
|
2023-11-28 12:35:06 +01:00
|
|
|
return {false, buildVectorError(key, -1, 3)};
|
2023-11-26 12:52:22 +01:00
|
|
|
}
|
2023-11-28 12:53:23 +01:00
|
|
|
return {true, ""};
|
2023-11-26 16:02:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-28 12:38:24 +01:00
|
|
|
* Returns (success, failure reason).
|
2023-11-26 16:02:11 +01:00
|
|
|
*/
|
2023-11-28 12:35:06 +01:00
|
|
|
std::tuple<bool, std::string> grabQuaternionf(json data, std::string key, irr::core::quaternion& refQuat) {
|
2023-11-26 16:02:11 +01:00
|
|
|
if (data.contains(key) && data[key].is_array() && data[key].size() == 4) {
|
|
|
|
auto jsonQuat = data[key];
|
|
|
|
int i = 0;
|
|
|
|
for (auto reference = jsonQuat.begin(); reference != jsonQuat.end(); ++reference) {
|
|
|
|
auto value = *reference;
|
|
|
|
// Can take integer OR float.
|
|
|
|
if (!value.is_number()) {
|
2023-11-28 12:35:06 +01:00
|
|
|
return {false, buildVectorError(key, i, 4)};
|
2023-11-26 16:02:11 +01:00
|
|
|
}
|
|
|
|
switch (i){
|
|
|
|
case 0:
|
|
|
|
refQuat.X = value;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
refQuat.Y = value;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
refQuat.Z = value;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
refQuat.W = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} else {
|
2023-11-28 12:35:06 +01:00
|
|
|
return {false, buildVectorError(key, -1, 4)};
|
2023-11-26 16:02:11 +01:00
|
|
|
}
|
2023-11-28 12:53:23 +01:00
|
|
|
return {true, ""};
|
2023-11-26 16:02:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-28 12:38:24 +01:00
|
|
|
// Returns (success, failure reason).
|
|
|
|
std::tuple<bool, std::string> parseNode(json data, SMeshBuffer* meshBuffer) {
|
2023-11-26 15:51:43 +01:00
|
|
|
|
|
|
|
auto position = irr::core::vector3df{0,0,0};
|
|
|
|
auto scale = irr::core::vector3df{1,1,1};
|
|
|
|
auto rotation = irr::core::quaternion{0,0,0,1};
|
|
|
|
|
2023-11-28 12:38:24 +01:00
|
|
|
auto positionResult = grabVec3f(data, "position", position);
|
|
|
|
if (!std::get<0>(positionResult)) {
|
|
|
|
return {false, std::get<1>(positionResult)};
|
2023-11-26 15:53:47 +01:00
|
|
|
}
|
|
|
|
|
2023-11-28 12:38:24 +01:00
|
|
|
auto scaleResult = grabVec3f(data, "scale", scale);
|
|
|
|
if (!std::get<0>(scaleResult)) {
|
|
|
|
return {false, std::get<1>(scaleResult)};
|
2023-11-26 15:53:47 +01:00
|
|
|
}
|
2023-11-26 15:51:43 +01:00
|
|
|
|
2023-11-28 12:38:24 +01:00
|
|
|
auto rotationResult = grabQuaternionf(data, "rotation", rotation);
|
|
|
|
if (!std::get<0>(rotationResult)) {
|
|
|
|
return {false, std::get<1>(rotationResult)};
|
2023-11-26 16:02:11 +01:00
|
|
|
}
|
2023-11-26 16:02:28 +01:00
|
|
|
|
2023-11-28 12:53:23 +01:00
|
|
|
return {true, ""};
|
2023-11-26 12:52:22 +01:00
|
|
|
}
|
|
|
|
|
2023-11-28 12:08:17 +01:00
|
|
|
/**
|
|
|
|
* Returns (success, failure reason).
|
2023-11-28 14:03:34 +01:00
|
|
|
*/
|
|
|
|
std::tuple<bool, std::string> CB3DJSONMeshFileLoader::readChunkBRUS() {
|
|
|
|
|
|
|
|
|
|
|
|
return {true, ""};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns (success, failure reason).
|
2023-11-28 14:15:15 +01:00
|
|
|
* [optional]
|
2023-11-28 12:08:17 +01:00
|
|
|
*/
|
|
|
|
std::tuple<bool, std::string> CB3DJSONMeshFileLoader::readChunkTEXS() {
|
|
|
|
|
2023-11-28 13:08:17 +01:00
|
|
|
//? note: TEXS is an array of objects.
|
|
|
|
|
2023-11-28 12:08:17 +01:00
|
|
|
// We're checking the root of the JSON structure here. So we can just peak directly into the root.
|
|
|
|
json texs;
|
2023-11-28 13:08:17 +01:00
|
|
|
if (JSONDataContainer.contains("TEXS") && JSONDataContainer["TEXS"].is_array()) {
|
2023-11-28 12:08:17 +01:00
|
|
|
// We're referencing static memory.
|
|
|
|
texs = JSONDataContainer["TEXS"];
|
2023-11-28 13:25:48 +01:00
|
|
|
} else {
|
2023-11-28 14:15:15 +01:00
|
|
|
|
|
|
|
// We also want to warn developers if they're using {} instead of [].
|
|
|
|
if (JSONDataContainer.contains("TEXS") && !JSONDataContainer["TEXS"].is_array()) {
|
|
|
|
return {false, "TEXS is not an array."};
|
|
|
|
}
|
|
|
|
|
2023-11-28 12:08:17 +01:00
|
|
|
// Since it's optional, it succeeds if it's not there.
|
2023-11-28 12:53:23 +01:00
|
|
|
return {true, ""};
|
2023-11-28 12:08:17 +01:00
|
|
|
}
|
|
|
|
|
2023-11-28 13:08:17 +01:00
|
|
|
int index = 0;
|
|
|
|
for (auto reference = texs.begin(); reference != texs.end(); ++reference) {
|
2023-11-28 12:10:46 +01:00
|
|
|
|
2023-11-28 13:08:17 +01:00
|
|
|
// t stands for texture.
|
|
|
|
json t = *reference;
|
|
|
|
|
|
|
|
if (!t.is_object()) {
|
|
|
|
return {false, "TEXS: Element " + std::to_string(index) + " is not an object."};
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a very strange way to do this but I won't complain.
|
|
|
|
// Create the texture object inside of the Mesh.
|
|
|
|
Textures.push_back(SB3dTexture());
|
|
|
|
SB3dTexture& B3DTexture = Textures.getLast();
|
2023-11-28 12:42:47 +01:00
|
|
|
|
2023-11-28 13:08:17 +01:00
|
|
|
|
|
|
|
// This part should probably be it's own function.
|
|
|
|
//todo: look into making this it's own function.
|
2023-11-28 12:53:23 +01:00
|
|
|
|
2023-11-28 13:50:29 +01:00
|
|
|
// This part is a bit complex, for ease of use for the plugin dev/modders advantage.
|
|
|
|
|
2023-11-28 13:43:54 +01:00
|
|
|
//* Name.
|
2023-11-28 13:08:17 +01:00
|
|
|
if (t.contains("name") && t["name"].is_string()) {
|
2023-11-28 12:42:47 +01:00
|
|
|
|
2023-11-28 13:08:17 +01:00
|
|
|
// Strings are implicitly converted.
|
2023-11-28 13:25:48 +01:00
|
|
|
std::string textureName = t["name"];
|
|
|
|
|
|
|
|
B3DTexture.TextureName = textureName.c_str();
|
|
|
|
|
|
|
|
// Convert Windows '\' to Posix '/'.
|
|
|
|
B3DTexture.TextureName.replace('\\','/');
|
2023-11-28 13:35:48 +01:00
|
|
|
|
2023-11-28 13:34:32 +01:00
|
|
|
} else {
|
2023-11-28 13:35:57 +01:00
|
|
|
|
2023-11-28 13:34:32 +01:00
|
|
|
if (t.contains("name") && !t["name"].is_string()) {
|
|
|
|
return {false, "\"name\" in TEXS block index (" + std::to_string(index) +") is not a string."};
|
|
|
|
}
|
2023-11-28 13:43:54 +01:00
|
|
|
|
2023-11-28 13:34:32 +01:00
|
|
|
return {false, "Missing \"name\" in TEXS block index (" + std::to_string(index) +")."};
|
2023-11-28 13:08:17 +01:00
|
|
|
}
|
2023-11-28 12:42:47 +01:00
|
|
|
|
2023-11-28 13:43:54 +01:00
|
|
|
//* Flags.
|
|
|
|
if (t.contains("flags") && t["flags"].is_number_integer()) {
|
|
|
|
|
|
|
|
B3DTexture.Flags = t["flags"];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (t.contains("flags") && !t["flags"].is_number_integer()) {
|
|
|
|
return {false, "\"flags\" in TEXS block index(" + std::to_string(index)+") is not an integer."};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {false, "Missing \"flags\" in TEXS block index(" + std::to_string(index)+")."};
|
|
|
|
}
|
|
|
|
|
|
|
|
//* Blend.
|
|
|
|
if (t.contains("blend") && t["blend"].is_number_integer()) {
|
|
|
|
|
|
|
|
B3DTexture.Blend = t["blend"];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (t.contains("blend") && !t["blend"].is_number_integer()) {
|
|
|
|
return {false, "\"blend\" in TEXS block index(" + std::to_string(index)+") is not an integer."};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {false, "Missing \"blend\" in TEXS block index(" + std::to_string(index)+")."};
|
|
|
|
}
|
|
|
|
|
2023-11-28 13:50:29 +01:00
|
|
|
//* Position.
|
|
|
|
if (t.contains("pos") && t["pos"].is_array()) {
|
|
|
|
|
|
|
|
irr::core::vector2df pos {0,0};
|
|
|
|
|
|
|
|
auto posSuccess = grabVec2f(t, "pos", pos);
|
|
|
|
|
|
|
|
// Something went horribly wrong.
|
|
|
|
if (!std::get<0>(posSuccess)) {
|
|
|
|
|
|
|
|
return {false, "TEXS: " + std::get<1>(posSuccess)};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success.
|
|
|
|
B3DTexture.Xpos = pos.X;
|
|
|
|
B3DTexture.Ypos = pos.Y;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (t.contains("pos") && !t["pos"].is_array()) {
|
|
|
|
return {false, "\"pos\" in TEXS block index(" + std::to_string(index)+") is not an array."};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!t.contains("pos") ) {
|
|
|
|
return {false, "\"pos\" in TEXS block index(" + std::to_string(index)+") is missing."};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {false, "Malformed \"pos\" in TEXS block index (" + std::to_string(index) + "). Must be an array with 2 numbers."};
|
|
|
|
}
|
|
|
|
|
2023-11-28 13:51:40 +01:00
|
|
|
//* Scale.
|
|
|
|
if (t.contains("scale") && t["scale"].is_array()) {
|
|
|
|
|
|
|
|
irr::core::vector2df scale {0,0};
|
|
|
|
|
|
|
|
auto scaleSuccess = grabVec2f(t, "scale", scale);
|
|
|
|
|
|
|
|
// Something went horribly wrong.
|
|
|
|
if (!std::get<0>(scaleSuccess)) {
|
|
|
|
|
|
|
|
return {false, "TEXS: " + std::get<1>(scaleSuccess)};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success.
|
|
|
|
B3DTexture.Xscale = scale.X;
|
|
|
|
B3DTexture.Yscale = scale.Y;
|
|
|
|
|
2023-11-28 13:54:35 +01:00
|
|
|
// println(std::to_string(B3DTexture.Xscale) + " " + std::to_string(B3DTexture.Yscale));
|
|
|
|
|
2023-11-28 13:51:40 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
if (t.contains("scale") && !t["scale"].is_array()) {
|
|
|
|
return {false, "\"scale\" in TEXS block index(" + std::to_string(index)+") is not an array."};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!t.contains("scale") ) {
|
|
|
|
return {false, "\"scale\" in TEXS block index(" + std::to_string(index)+") is missing."};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {false, "Malformed \"scale\" in TEXS block index (" + std::to_string(index) + "). Must be an array with 2 numbers."};
|
|
|
|
}
|
2023-11-28 13:43:54 +01:00
|
|
|
|
2023-11-28 13:54:35 +01:00
|
|
|
//* Angle.
|
|
|
|
if (t.contains("angle") && t["angle"].is_number()) {
|
|
|
|
|
|
|
|
B3DTexture.Angle = t["angle"];
|
|
|
|
|
|
|
|
} else {
|
2023-11-28 14:03:34 +01:00
|
|
|
|
2023-11-28 13:54:35 +01:00
|
|
|
if (t.contains("angle")) {
|
|
|
|
return {false, "\"angle\" in TEXS block index (" + std::to_string(index) + ") is not a number."};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {false, "\"angle\" in TEXS block index (" + std::to_string(index) + ") is missing."};
|
|
|
|
}
|
2023-11-28 12:42:47 +01:00
|
|
|
|
2023-11-28 13:08:17 +01:00
|
|
|
|
|
|
|
index++;
|
2023-11-28 12:08:17 +01:00
|
|
|
}
|
2023-11-28 13:08:17 +01:00
|
|
|
// } else {
|
|
|
|
// // it is malformed.
|
|
|
|
// //todo: return
|
|
|
|
// return {false, "Malformed TEXS block. \"textures\" is not an array."} ;
|
|
|
|
// }
|
2023-11-28 12:14:29 +01:00
|
|
|
|
|
|
|
// Everything succeeds, yay!
|
2023-11-28 12:53:23 +01:00
|
|
|
return {true, ""};
|
2023-11-28 12:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-28 11:21:34 +01:00
|
|
|
/**
|
|
|
|
* Returns (success, failure reason).
|
|
|
|
*/
|
|
|
|
std::tuple<bool, std::string> 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) {
|
2023-11-28 11:22:11 +01:00
|
|
|
return {false, "Wrong version in B3D JSON! Expected: 1"};
|
2023-11-28 11:21:34 +01:00
|
|
|
}
|
2023-11-26 11:10:04 +01:00
|
|
|
|
2023-11-28 14:03:34 +01:00
|
|
|
// Grab TEXS (textures).
|
2023-11-28 12:08:17 +01:00
|
|
|
std::tuple<bool, std::string> texturesSuccess = this->readChunkTEXS();
|
2023-11-28 13:34:32 +01:00
|
|
|
|
|
|
|
if (!std::get<0>(texturesSuccess)) {
|
|
|
|
return {false, std::get<1>(texturesSuccess)};
|
|
|
|
}
|
2023-11-28 12:08:17 +01:00
|
|
|
|
2023-11-28 14:03:34 +01:00
|
|
|
// Grab BRUS (brushes). Defines where materials go.
|
|
|
|
std::tuple<bool, std::string> brushesSuccess = this->readChunkBRUS();
|
|
|
|
|
|
|
|
if (!std::get<0>(brushesSuccess)) {
|
|
|
|
return {false, std::get<1>(brushesSuccess)};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-11-28 11:43:22 +01:00
|
|
|
|
2023-11-26 12:52:22 +01:00
|
|
|
|
2023-11-28 10:55:39 +01:00
|
|
|
// return animatedMesh;
|
2023-11-28 12:53:23 +01:00
|
|
|
return {true, ""};
|
2023-11-28 10:55:39 +01:00
|
|
|
}
|
|
|
|
|
2023-11-28 11:11:40 +01:00
|
|
|
/**
|
|
|
|
* Automatically cleans and returns the nullptr so it can be inlined or chained.
|
|
|
|
*/
|
2023-11-28 11:09:46 +01:00
|
|
|
CSkinnedMesh* CB3DJSONMeshFileLoader::cleanUp(std::string failure) {
|
2023-11-28 13:35:01 +01:00
|
|
|
os::Printer::log(failure.c_str(), ELL_WARNING);
|
2023-11-28 12:53:23 +01:00
|
|
|
if (AnimatedMesh != nullptr) {
|
|
|
|
AnimatedMesh->drop();
|
|
|
|
AnimatedMesh = 0;
|
|
|
|
}
|
2023-11-28 11:09:46 +01:00
|
|
|
return AnimatedMesh;
|
2023-11-26 11:10:04 +01:00
|
|
|
}
|
|
|
|
|
2023-11-28 11:06:58 +01:00
|
|
|
/**
|
2023-11-28 11:38:24 +01:00
|
|
|
* Returns (success, failure reason).
|
2023-11-28 11:06:58 +01:00
|
|
|
*/
|
2023-11-28 11:38:24 +01:00
|
|
|
std::tuple<bool, std::string> CB3DJSONMeshFileLoader::parseJSONFile(io::IReadFile* file) {
|
2023-11-26 09:37:35 +01:00
|
|
|
// So here we turn this mangled disaster into a C string.
|
2023-11-26 11:22:57 +01:00
|
|
|
// Please consider this the equivalent of duct taping a chainsaw onto a car to cut your lawn.
|
2023-11-26 15:30:26 +01:00
|
|
|
auto buffer = std::make_unique<char[]>(file->getSize());
|
2023-11-26 09:50:44 +01:00
|
|
|
|
|
|
|
// Now we read that dang JSON.
|
2023-11-26 15:30:26 +01:00
|
|
|
file->read(buffer.get(), file->getSize());
|
|
|
|
|
2023-11-28 10:55:39 +01:00
|
|
|
// This is the same pointer as buffer, but it's easier to read as "output".
|
2023-11-26 15:30:26 +01:00
|
|
|
char* output = buffer.get();
|
2023-11-26 09:50:44 +01:00
|
|
|
|
2023-11-26 15:30:26 +01:00
|
|
|
// Irrlicht doesn't null terminate the raw output. So we must doe it ourself.
|
2023-11-26 15:46:25 +01:00
|
|
|
output[file->getSize()] = '\0';
|
2023-11-26 09:50:44 +01:00
|
|
|
|
2023-11-26 15:32:28 +01:00
|
|
|
// We have to catch a JSON parse error or else the game will segfault.
|
2023-11-26 15:30:26 +01:00
|
|
|
try {
|
2023-11-28 11:40:07 +01:00
|
|
|
//! This is pointing to static memory. It will only be valid during the loading of this model.
|
2023-11-28 11:06:58 +01:00
|
|
|
JSONDataContainer = json::parse(output);
|
2023-11-26 15:30:26 +01:00
|
|
|
} catch (const json::parse_error& e) {
|
2023-11-28 11:38:24 +01:00
|
|
|
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';
|
|
|
|
return {false, failureReason};
|
2023-11-26 15:30:26 +01:00
|
|
|
}
|
2023-11-28 11:09:46 +01:00
|
|
|
|
|
|
|
// I'm not sure if buffer and output gets dropped here.
|
2023-11-28 12:53:23 +01:00
|
|
|
return {true, ""};
|
2023-11-28 11:06:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IAnimatedMesh* CB3DJSONMeshFileLoader::createMesh(io::IReadFile* file) {
|
|
|
|
|
|
|
|
// Less than zero? What is this file a black hole?
|
|
|
|
if (file->getSize() <= 0) {
|
2023-11-28 11:10:07 +01:00
|
|
|
return(this->cleanUp("B3D JSON severe error! File size is 0!"));
|
2023-11-28 11:06:58 +01:00
|
|
|
}
|
|
|
|
|
2023-11-28 11:07:48 +01:00
|
|
|
// Try to read this file.
|
2023-11-28 11:38:24 +01:00
|
|
|
const std::tuple<bool, std::string> 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)));
|
2023-11-28 11:06:58 +01:00
|
|
|
}
|
|
|
|
|
2023-11-28 11:11:40 +01:00
|
|
|
//? Now JSONDataContainer exists on this object.
|
|
|
|
//? So we can commence loading it.
|
2023-11-26 11:10:04 +01:00
|
|
|
|
2023-11-28 11:21:34 +01:00
|
|
|
// 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<bool, std::string> loadResult = load();
|
2023-11-26 09:50:44 +01:00
|
|
|
|
2023-11-28 11:21:34 +01:00
|
|
|
// 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)));
|
2023-11-28 11:32:28 +01:00
|
|
|
} else {
|
|
|
|
//todo: We finalize the model here.
|
2023-11-28 11:21:34 +01:00
|
|
|
}
|
2023-11-26 09:37:35 +01:00
|
|
|
|
2023-11-28 11:02:31 +01:00
|
|
|
println("We got to the end.");
|
2023-11-28 11:32:28 +01:00
|
|
|
//todo: We return the object's AnimatedMesh.
|
2023-11-28 11:02:31 +01:00
|
|
|
return nullptr;
|
2023-11-26 08:53:55 +01:00
|
|
|
}
|
|
|
|
|
2023-11-26 07:48:13 +01:00
|
|
|
} // namespace scene
|
2023-11-28 11:43:22 +01:00
|
|
|
} // namespace irr
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
2023-11-28 11:43:43 +01:00
|
|
|
! deprecated
|
|
|
|
|
2023-11-28 11:43:22 +01:00
|
|
|
? This is old reference material. Remember to remove this before PR is made.
|
|
|
|
|
|
|
|
//! Remember: This is basically a linked tree.
|
|
|
|
|
|
|
|
// SMesh* baseMesh(new SMesh {});
|
|
|
|
|
|
|
|
// SMeshBuffer* meshBuffer(new SMeshBuffer {});
|
|
|
|
|
|
|
|
// And now we begin the recursion!
|
|
|
|
|
|
|
|
// std::cout << data["NODE"] << "\n";
|
|
|
|
|
|
|
|
// if (data.contains("NODE") && data["NODE"].is_object()) {
|
|
|
|
// println("Yep, that's a node!");
|
|
|
|
|
|
|
|
// // If it fails, give up basically.
|
|
|
|
// if (parseNode(data["NODE"], meshBuffer)) {
|
|
|
|
// return nullptr;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// SAnimatedMesh* animatedMesh(new SAnimatedMesh {});
|
|
|
|
// animatedMesh->addMesh(baseMesh);
|
|
|
|
|
|
|
|
*/
|