2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2022-01-10 20:13:43 +00:00
|
|
|
Copyright (c) 2006-2022, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2017-05-09 17:57:36 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
2015-05-19 03:48:29 +00:00
|
|
|
following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-05-19 03:48:29 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file BlenderDNA.cpp
|
|
|
|
* @brief Implementation of the Blender `DNA`, that is its own
|
|
|
|
* serialized set of data structures.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_BLEND_IMPORTER
|
|
|
|
#include "BlenderDNA.h"
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/StreamReader.h>
|
|
|
|
#include <assimp/TinyFormatter.h>
|
2020-05-02 13:14:38 +00:00
|
|
|
#include <assimp/fast_atof.h>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
using namespace Assimp::Blender;
|
|
|
|
using namespace Assimp::Formatter;
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
static bool match4(StreamReaderAny &stream, const char *string) {
|
|
|
|
ai_assert(nullptr != string);
|
2018-02-07 19:21:05 +00:00
|
|
|
char tmp[4];
|
2020-05-02 13:14:38 +00:00
|
|
|
tmp[0] = (stream).GetI1();
|
|
|
|
tmp[1] = (stream).GetI1();
|
|
|
|
tmp[2] = (stream).GetI1();
|
|
|
|
tmp[3] = (stream).GetI1();
|
|
|
|
return (tmp[0] == string[0] && tmp[1] == string[1] && tmp[2] == string[2] && tmp[3] == string[3]);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Type {
|
2015-05-19 03:57:13 +00:00
|
|
|
size_t size;
|
|
|
|
std::string name;
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-05-02 13:14:38 +00:00
|
|
|
void DNAParser::Parse() {
|
2022-11-08 16:03:55 +00:00
|
|
|
StreamReaderAny &stream = *db.reader;
|
2020-05-02 13:14:38 +00:00
|
|
|
DNA &dna = db.dna;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
if (!match4(stream, "SDNA")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
throw DeadlyImportError("BlenderDNA: Expected SDNA chunk");
|
|
|
|
}
|
|
|
|
|
|
|
|
// name dictionary
|
2020-05-02 13:14:38 +00:00
|
|
|
if (!match4(stream, "NAME")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
throw DeadlyImportError("BlenderDNA: Expected NAME field");
|
|
|
|
}
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
std::vector<std::string> names(stream.GetI4());
|
|
|
|
for (std::string &s : names) {
|
2015-05-19 03:57:13 +00:00
|
|
|
while (char c = stream.GetI1()) {
|
|
|
|
s += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// type dictionary
|
2020-05-02 13:14:38 +00:00
|
|
|
for (; stream.GetCurrentPos() & 0x3; stream.GetI1())
|
|
|
|
;
|
|
|
|
if (!match4(stream, "TYPE")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
throw DeadlyImportError("BlenderDNA: Expected TYPE field");
|
|
|
|
}
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
std::vector<Type> types(stream.GetI4());
|
|
|
|
for (Type &s : types) {
|
2015-05-19 03:57:13 +00:00
|
|
|
while (char c = stream.GetI1()) {
|
|
|
|
s.name += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// type length dictionary
|
2020-05-02 13:14:38 +00:00
|
|
|
for (; stream.GetCurrentPos() & 0x3; stream.GetI1())
|
|
|
|
;
|
|
|
|
if (!match4(stream, "TLEN")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
throw DeadlyImportError("BlenderDNA: Expected TLEN field");
|
|
|
|
}
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
for (Type &s : types) {
|
2015-05-19 03:57:13 +00:00
|
|
|
s.size = stream.GetI2();
|
|
|
|
}
|
|
|
|
|
|
|
|
// structures dictionary
|
2020-05-02 13:14:38 +00:00
|
|
|
for (; stream.GetCurrentPos() & 0x3; stream.GetI1())
|
|
|
|
;
|
|
|
|
if (!match4(stream, "STRC")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
throw DeadlyImportError("BlenderDNA: Expected STRC field");
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t end = stream.GetI4(), fields = 0;
|
|
|
|
|
|
|
|
dna.structures.reserve(end);
|
2020-05-02 13:14:38 +00:00
|
|
|
for (size_t i = 0; i != end; ++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
uint16_t n = stream.GetI2();
|
|
|
|
if (n >= types.size()) {
|
2020-08-18 15:32:34 +00:00
|
|
|
throw DeadlyImportError("BlenderDNA: Invalid type index in structure name", n, " (there are only ", types.size(), " entries)");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// maintain separate indexes
|
|
|
|
dna.indices[types[n].name] = dna.structures.size();
|
|
|
|
|
|
|
|
dna.structures.push_back(Structure());
|
2020-05-02 13:14:38 +00:00
|
|
|
Structure &s = dna.structures.back();
|
|
|
|
s.name = types[n].name;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
n = stream.GetI2();
|
|
|
|
s.fields.reserve(n);
|
|
|
|
|
|
|
|
size_t offset = 0;
|
|
|
|
for (size_t m = 0; m < n; ++m, ++fields) {
|
|
|
|
|
|
|
|
uint16_t j = stream.GetI2();
|
|
|
|
if (j >= types.size()) {
|
2020-08-18 15:32:34 +00:00
|
|
|
throw DeadlyImportError("BlenderDNA: Invalid type index in structure field ", j, " (there are only ", types.size(), " entries)");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
s.fields.push_back(Field());
|
2020-05-02 13:14:38 +00:00
|
|
|
Field &f = s.fields.back();
|
2015-05-19 03:57:13 +00:00
|
|
|
f.offset = offset;
|
|
|
|
|
|
|
|
f.type = types[j].name;
|
|
|
|
f.size = types[j].size;
|
|
|
|
|
|
|
|
j = stream.GetI2();
|
|
|
|
if (j >= names.size()) {
|
2020-08-18 15:32:34 +00:00
|
|
|
throw DeadlyImportError("BlenderDNA: Invalid name index in structure field ", j, " (there are only ", names.size(), " entries)");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f.name = names[j];
|
|
|
|
f.flags = 0u;
|
|
|
|
|
|
|
|
// pointers always specify the size of the pointee instead of their own.
|
|
|
|
// The pointer asterisk remains a property of the lookup name.
|
|
|
|
if (f.name[0] == '*') {
|
|
|
|
f.size = db.i64bit ? 8 : 4;
|
|
|
|
f.flags |= FieldFlag_Pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// arrays, however, specify the size of a single element so we
|
|
|
|
// need to parse the (possibly multi-dimensional) array declaration
|
|
|
|
// in order to obtain the actual size of the array in the file.
|
|
|
|
// Also we need to alter the lookup name to include no array
|
|
|
|
// brackets anymore or size fixup won't work (if our size does
|
|
|
|
// not match the size read from the DNA).
|
|
|
|
if (*f.name.rbegin() == ']') {
|
|
|
|
const std::string::size_type rb = f.name.find('[');
|
|
|
|
if (rb == std::string::npos) {
|
2020-08-18 15:32:34 +00:00
|
|
|
throw DeadlyImportError("BlenderDNA: Encountered invalid array declaration ", f.name);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f.flags |= FieldFlag_Array;
|
2020-05-02 13:14:38 +00:00
|
|
|
DNA::ExtractArraySize(f.name, f.array_sizes);
|
|
|
|
f.name = f.name.substr(0, rb);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
f.size *= f.array_sizes[0] * f.array_sizes[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// maintain separate indexes
|
2020-05-02 13:14:38 +00:00
|
|
|
s.indices[f.name] = s.fields.size() - 1;
|
2015-05-19 03:57:13 +00:00
|
|
|
offset += f.size;
|
|
|
|
}
|
|
|
|
s.size = offset;
|
|
|
|
}
|
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_DEBUG("BlenderDNA: Got ", dna.structures.size(), " structures with totally ", fields, " fields");
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2021-05-05 23:30:29 +00:00
|
|
|
#if ASSIMP_BUILD_BLENDER_DEBUG_DNA
|
2015-05-19 03:57:13 +00:00
|
|
|
dna.DumpToFile();
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
dna.AddPrimitiveStructures();
|
|
|
|
dna.RegisterConverters();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 23:30:29 +00:00
|
|
|
#if ASSIMP_BUILD_BLENDER_DEBUG_DNA
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-05-02 13:14:38 +00:00
|
|
|
void DNA ::DumpToFile() {
|
2018-05-13 14:35:03 +00:00
|
|
|
// we don't bother using the VFS here for this is only for debugging.
|
2015-05-19 03:57:13 +00:00
|
|
|
// (and all your bases are belong to us).
|
|
|
|
|
|
|
|
std::ofstream f("dna.txt");
|
|
|
|
if (f.fail()) {
|
2018-04-19 14:48:43 +00:00
|
|
|
ASSIMP_LOG_ERROR("Could not dump dna to dna.txt");
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-05-02 13:14:38 +00:00
|
|
|
f << "Field format: type name offset size"
|
|
|
|
<< "\n";
|
|
|
|
f << "Structure format: name size"
|
|
|
|
<< "\n";
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
for (const Structure &s : structures) {
|
2015-05-19 03:57:13 +00:00
|
|
|
f << s.name << " " << s.size << "\n\n";
|
2020-05-02 13:14:38 +00:00
|
|
|
for (const Field &ff : s.fields) {
|
2016-04-27 15:58:40 +00:00
|
|
|
f << "\t" << ff.type << " " << ff.name << " " << ff.offset << " " << ff.size << "\n";
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2016-04-27 15:58:40 +00:00
|
|
|
f << "\n";
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2016-04-27 15:58:40 +00:00
|
|
|
f << std::flush;
|
|
|
|
|
2018-04-19 14:48:43 +00:00
|
|
|
ASSIMP_LOG_INFO("BlenderDNA: Dumped dna to dna.txt");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2021-05-05 23:30:29 +00:00
|
|
|
#endif // ASSIMP_BUILD_BLENDER_DEBUG_DNA
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-05-02 13:14:38 +00:00
|
|
|
/*static*/ void DNA ::ExtractArraySize(
|
|
|
|
const std::string &out,
|
|
|
|
size_t array_sizes[2]) {
|
2015-05-19 03:57:13 +00:00
|
|
|
array_sizes[0] = array_sizes[1] = 1;
|
|
|
|
std::string::size_type pos = out.find('[');
|
|
|
|
if (pos++ == std::string::npos) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
array_sizes[0] = strtoul10(&out[pos]);
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
pos = out.find('[', pos);
|
2015-05-19 03:57:13 +00:00
|
|
|
if (pos++ == std::string::npos) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
array_sizes[1] = strtoul10(&out[pos]);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-05-02 13:14:38 +00:00
|
|
|
std::shared_ptr<ElemBase> DNA ::ConvertBlobToStructure(
|
|
|
|
const Structure &structure,
|
|
|
|
const FileDatabase &db) const {
|
|
|
|
std::map<std::string, FactoryPair>::const_iterator it = converters.find(structure.name);
|
2015-05-19 03:57:13 +00:00
|
|
|
if (it == converters.end()) {
|
2020-05-02 13:14:38 +00:00
|
|
|
return std::shared_ptr<ElemBase>();
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
std::shared_ptr<ElemBase> ret = (structure.*((*it).second.first))();
|
|
|
|
(structure.*((*it).second.second))(ret, db);
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
return ret;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-05-02 13:14:38 +00:00
|
|
|
DNA::FactoryPair DNA ::GetBlobToStructureConverter(
|
|
|
|
const Structure &structure,
|
|
|
|
const FileDatabase & /*db*/
|
|
|
|
) const {
|
|
|
|
std::map<std::string, FactoryPair>::const_iterator it = converters.find(structure.name);
|
2015-05-19 03:57:13 +00:00
|
|
|
return it == converters.end() ? FactoryPair() : (*it).second;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// basing on http://www.blender.org/development/architecture/notes-on-sdna/
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-05-02 13:14:38 +00:00
|
|
|
void DNA ::AddPrimitiveStructures() {
|
2015-05-19 03:57:13 +00:00
|
|
|
// NOTE: these are just dummies. Their presence enforces
|
|
|
|
// Structure::Convert<target_type> to be called on these
|
|
|
|
// empty structures. These converters are special
|
|
|
|
// overloads which scan the name of the structure and
|
|
|
|
// perform the required data type conversion if one
|
|
|
|
// of these special names is found in the structure
|
|
|
|
// in question.
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
indices["int"] = structures.size();
|
2020-05-02 13:14:38 +00:00
|
|
|
structures.push_back(Structure());
|
2015-05-19 03:57:13 +00:00
|
|
|
structures.back().name = "int";
|
|
|
|
structures.back().size = 4;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
indices["short"] = structures.size();
|
2020-05-02 13:14:38 +00:00
|
|
|
structures.push_back(Structure());
|
2015-05-19 03:57:13 +00:00
|
|
|
structures.back().name = "short";
|
|
|
|
structures.back().size = 2;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
indices["char"] = structures.size();
|
2020-05-02 13:14:38 +00:00
|
|
|
structures.push_back(Structure());
|
2015-05-19 03:57:13 +00:00
|
|
|
structures.back().name = "char";
|
|
|
|
structures.back().size = 1;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
indices["float"] = structures.size();
|
2020-05-02 13:14:38 +00:00
|
|
|
structures.push_back(Structure());
|
2015-05-19 03:57:13 +00:00
|
|
|
structures.back().name = "float";
|
|
|
|
structures.back().size = 4;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
indices["double"] = structures.size();
|
2020-05-02 13:14:38 +00:00
|
|
|
structures.push_back(Structure());
|
2015-05-19 03:57:13 +00:00
|
|
|
structures.back().name = "double";
|
|
|
|
structures.back().size = 8;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// no long, seemingly.
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-05-02 13:14:38 +00:00
|
|
|
void SectionParser ::Next() {
|
2015-05-19 03:57:13 +00:00
|
|
|
stream.SetCurrentPos(current.start + current.size);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
const char tmp[] = {
|
2022-07-08 16:12:41 +00:00
|
|
|
(char)stream.GetI1(),
|
|
|
|
(char)stream.GetI1(),
|
|
|
|
(char)stream.GetI1(),
|
|
|
|
(char)stream.GetI1()
|
2015-05-19 03:57:13 +00:00
|
|
|
};
|
2020-05-02 13:14:38 +00:00
|
|
|
current.id = std::string(tmp, tmp[3] ? 4 : tmp[2] ? 3 : tmp[1] ? 2 : 1);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
current.size = stream.GetI4();
|
|
|
|
current.address.val = ptr64 ? stream.GetU8() : stream.GetU4();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
current.dna_index = stream.GetI4();
|
|
|
|
current.num = stream.GetI4();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
current.start = stream.GetCurrentPos();
|
|
|
|
if (stream.GetRemainingSizeToLimit() < current.size) {
|
|
|
|
throw DeadlyImportError("BLEND: invalid size of file block");
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
#ifdef ASSIMP_BUILD_BLENDER_DEBUG
|
2020-05-15 15:59:42 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(current.id);
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|