closes https://github.com/assimp/assimp/issues/3178: throw exception in case of invalid export stream.
parent
f8e6512a63
commit
3329e76263
|
@ -9,16 +9,16 @@ Licensed under a 3-clause BSD license. See the LICENSE file for more information
|
||||||
#ifndef ASSIMP_BUILD_NO_EXPORT
|
#ifndef ASSIMP_BUILD_NO_EXPORT
|
||||||
#ifndef ASSIMP_BUILD_NO_ASSJSON_EXPORTER
|
#ifndef ASSIMP_BUILD_NO_ASSJSON_EXPORTER
|
||||||
|
|
||||||
#include <assimp/Importer.hpp>
|
#include <assimp/scene.h>
|
||||||
#include <assimp/Exporter.hpp>
|
#include <assimp/Exporter.hpp>
|
||||||
#include <assimp/IOStream.hpp>
|
#include <assimp/IOStream.hpp>
|
||||||
#include <assimp/IOSystem.hpp>
|
#include <assimp/IOSystem.hpp>
|
||||||
#include <assimp/scene.h>
|
#include <assimp/Importer.hpp>
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
#include <limits>
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <limits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
#define CURRENT_FORMAT_VERSION 100
|
#define CURRENT_FORMAT_VERSION 100
|
||||||
|
|
||||||
|
@ -42,10 +42,8 @@ public:
|
||||||
Flag_WriteSpecialFloats = 0x2,
|
Flag_WriteSpecialFloats = 0x2,
|
||||||
};
|
};
|
||||||
|
|
||||||
JSONWriter(Assimp::IOStream& out, unsigned int flags = 0u)
|
JSONWriter(Assimp::IOStream &out, unsigned int flags = 0u) :
|
||||||
: out(out)
|
out(out), first(), flags(flags) {
|
||||||
, first()
|
|
||||||
, flags(flags) {
|
|
||||||
// make sure that all formatting happens using the standard, C locale and not the user's current locale
|
// make sure that all formatting happens using the standard, C locale and not the user's current locale
|
||||||
buff.imbue(std::locale("C"));
|
buff.imbue(std::locale("C"));
|
||||||
}
|
}
|
||||||
|
@ -156,8 +154,7 @@ public:
|
||||||
void Delimit() {
|
void Delimit() {
|
||||||
if (!first) {
|
if (!first) {
|
||||||
buff << ',';
|
buff << ',';
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
buff << ' ';
|
buff << ' ';
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
|
@ -468,8 +465,7 @@ void Write(JSONWriter& out, const aiMaterial& ai, bool is_elem = true) {
|
||||||
out.Element(reinterpret_cast<float *>(prop->mData)[ii]);
|
out.Element(reinterpret_cast<float *>(prop->mData)[ii]);
|
||||||
}
|
}
|
||||||
out.EndArray();
|
out.EndArray();
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
out.SimpleValue(*reinterpret_cast<float *>(prop->mData));
|
out.SimpleValue(*reinterpret_cast<float *>(prop->mData));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -486,19 +482,15 @@ void Write(JSONWriter& out, const aiMaterial& ai, bool is_elem = true) {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case aiPTI_String:
|
case aiPTI_String: {
|
||||||
{
|
|
||||||
aiString s;
|
aiString s;
|
||||||
aiGetMaterialString(&ai, prop->mKey.data, prop->mSemantic, prop->mIndex, &s);
|
aiGetMaterialString(&ai, prop->mKey.data, prop->mSemantic, prop->mIndex, &s);
|
||||||
out.SimpleValue(s);
|
out.SimpleValue(s);
|
||||||
}
|
} break;
|
||||||
break;
|
case aiPTI_Buffer: {
|
||||||
case aiPTI_Buffer:
|
|
||||||
{
|
|
||||||
// binary data is written as series of hex-encoded octets
|
// binary data is written as series of hex-encoded octets
|
||||||
out.SimpleValue(prop->mData, prop->mDataLength);
|
out.SimpleValue(prop->mData, prop->mDataLength);
|
||||||
}
|
} break;
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
|
@ -525,8 +517,7 @@ void Write(JSONWriter& out, const aiTexture& ai, bool is_elem = true) {
|
||||||
out.Key("data");
|
out.Key("data");
|
||||||
if (!ai.mHeight) {
|
if (!ai.mHeight) {
|
||||||
out.SimpleValue(ai.pcData, ai.mWidth);
|
out.SimpleValue(ai.pcData, ai.mWidth);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
out.StartArray();
|
out.StartArray();
|
||||||
for (unsigned int y = 0; y < ai.mHeight; ++y) {
|
for (unsigned int y = 0; y < ai.mHeight; ++y) {
|
||||||
out.StartArray(true);
|
out.StartArray(true);
|
||||||
|
@ -585,7 +576,6 @@ void Write(JSONWriter& out, const aiLight& ai, bool is_elem = true) {
|
||||||
if (ai.mType != aiLightSource_POINT) {
|
if (ai.mType != aiLightSource_POINT) {
|
||||||
out.Key("direction");
|
out.Key("direction");
|
||||||
Write(out, ai.mDirection, false);
|
Write(out, ai.mDirection, false);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ai.mType != aiLightSource_DIRECTIONAL) {
|
if (ai.mType != aiLightSource_DIRECTIONAL) {
|
||||||
|
@ -774,11 +764,10 @@ void Write(JSONWriter& out, const aiScene& ai) {
|
||||||
out.EndObj();
|
out.EndObj();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ExportAssimp2Json(const char *file, Assimp::IOSystem *io, const aiScene *scene, const Assimp::ExportProperties *) {
|
void ExportAssimp2Json(const char *file, Assimp::IOSystem *io, const aiScene *scene, const Assimp::ExportProperties *) {
|
||||||
std::unique_ptr<Assimp::IOStream> str(io->Open(file, "wt"));
|
std::unique_ptr<Assimp::IOStream> str(io->Open(file, "wt"));
|
||||||
if (!str) {
|
if (!str) {
|
||||||
//throw Assimp::DeadlyExportError("could not open output file");
|
throw Assimp::DeadlyExportError("could not open output file");
|
||||||
}
|
}
|
||||||
|
|
||||||
// get a copy of the scene so we can modify it
|
// get a copy of the scene so we can modify it
|
||||||
|
@ -795,15 +784,14 @@ void ExportAssimp2Json(const char* file, Assimp::IOSystem* io, const aiScene* sc
|
||||||
JSONWriter s(*str, JSONWriter::Flag_WriteSpecialFloats);
|
JSONWriter s(*str, JSONWriter::Flag_WriteSpecialFloats);
|
||||||
Write(s, *scenecopy_tmp);
|
Write(s, *scenecopy_tmp);
|
||||||
|
|
||||||
}
|
} catch (...) {
|
||||||
catch (...) {
|
|
||||||
aiFreeScene(scenecopy_tmp);
|
aiFreeScene(scenecopy_tmp);
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
aiFreeScene(scenecopy_tmp);
|
aiFreeScene(scenecopy_tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} // namespace Assimp
|
||||||
|
|
||||||
#endif // ASSIMP_BUILD_NO_ASSJSON_EXPORTER
|
#endif // ASSIMP_BUILD_NO_ASSJSON_EXPORTER
|
||||||
#endif // ASSIMP_BUILD_NO_EXPORT
|
#endif // ASSIMP_BUILD_NO_EXPORT
|
||||||
|
|
Loading…
Reference in New Issue