2015-06-28 22:11:05 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2018-01-28 18:42:05 +00:00
|
|
|
Copyright (c) 2006-2018, assimp team
|
|
|
|
|
2017-05-09 17:57:36 +00:00
|
|
|
|
2015-06-28 22:11:05 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
2016-04-05 21:23:53 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
2015-06-28 22:11:05 +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.
|
|
|
|
|
2016-04-05 21:23:53 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-06-28 22:11:05 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2016-04-05 21:23:53 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-06-28 22:11:05 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2016-04-05 21:23:53 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-06-28 22:11:05 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2016-04-05 21:23:53 +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-06-28 22:11:05 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file BlenderDNA.inl
|
2016-04-05 21:23:53 +00:00
|
|
|
* @brief Blender `DNA` (file format specification embedded in
|
2015-06-28 22:11:05 +00:00
|
|
|
* blend file itself) loader.
|
|
|
|
*/
|
|
|
|
#ifndef INCLUDED_AI_BLEND_DNA_INL
|
|
|
|
#define INCLUDED_AI_BLEND_DNA_INL
|
|
|
|
|
2016-04-05 21:23:53 +00:00
|
|
|
#include <memory>
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/TinyFormatter.h>
|
2015-06-28 22:11:05 +00:00
|
|
|
|
2015-06-28 22:12:51 +00:00
|
|
|
namespace Assimp {
|
2017-02-22 16:20:26 +00:00
|
|
|
namespace Blender {
|
2015-06-28 22:11:05 +00:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
const Field& Structure :: operator [] (const std::string& ss) const
|
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
std::map<std::string, size_t>::const_iterator it = indices.find(ss);
|
|
|
|
if (it == indices.end()) {
|
|
|
|
throw Error((Formatter::format(),
|
|
|
|
"BlendDNA: Did not find a field named `",ss,"` in structure `",name,"`"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return fields[(*it).second];
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
const Field* Structure :: Get (const std::string& ss) const
|
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
std::map<std::string, size_t>::const_iterator it = indices.find(ss);
|
|
|
|
return it == indices.end() ? NULL : &fields[(*it).second];
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
2016-04-05 21:23:53 +00:00
|
|
|
const Field& Structure :: operator [] (const size_t i) const
|
2015-06-28 22:11:05 +00:00
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
if (i >= fields.size()) {
|
|
|
|
throw Error((Formatter::format(),
|
|
|
|
"BlendDNA: There is no field with index `",i,"` in structure `",name,"`"
|
|
|
|
));
|
|
|
|
}
|
2015-06-28 22:11:05 +00:00
|
|
|
|
2015-06-28 22:12:51 +00:00
|
|
|
return fields[i];
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
2016-04-05 21:23:53 +00:00
|
|
|
template <typename T> std::shared_ptr<ElemBase> Structure :: Allocate() const
|
2015-06-28 22:11:05 +00:00
|
|
|
{
|
2016-04-05 21:23:53 +00:00
|
|
|
return std::shared_ptr<T>(new T());
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
template <typename T> void Structure :: Convert(
|
2016-04-05 21:23:53 +00:00
|
|
|
std::shared_ptr<ElemBase> in,
|
|
|
|
const FileDatabase& db) const
|
2015-06-28 22:11:05 +00:00
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
Convert<T> (*static_cast<T*> ( in.get() ),db);
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
template <int error_policy, typename T, size_t M>
|
|
|
|
void Structure :: ReadFieldArray(T (& out)[M], const char* name, const FileDatabase& db) const
|
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
const StreamReaderAny::pos old = db.reader->GetCurrentPos();
|
|
|
|
try {
|
|
|
|
const Field& f = (*this)[name];
|
|
|
|
const Structure& s = db.dna[f.type];
|
|
|
|
|
|
|
|
// is the input actually an array?
|
|
|
|
if (!(f.flags & FieldFlag_Array)) {
|
|
|
|
throw Error((Formatter::format(),"Field `",name,"` of structure `",
|
|
|
|
this->name,"` ought to be an array of size ",M
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
db.reader->IncPtr(f.offset);
|
|
|
|
|
|
|
|
// size conversions are always allowed, regardless of error_policy
|
|
|
|
unsigned int i = 0;
|
|
|
|
for(; i < std::min(f.array_sizes[0],M); ++i) {
|
|
|
|
s.Convert(out[i],db);
|
|
|
|
}
|
|
|
|
for(; i < M; ++i) {
|
|
|
|
_defaultInitializer<ErrorPolicy_Igno>()(out[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Error& e) {
|
|
|
|
_defaultInitializer<error_policy>()(out,e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
// and recover the previous stream position
|
|
|
|
db.reader->SetCurrentPos(old);
|
2015-06-28 22:11:05 +00:00
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_BLENDER_NO_STATS
|
2015-06-28 22:12:51 +00:00
|
|
|
++db.stats().fields_read;
|
2015-06-28 22:11:05 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
template <int error_policy, typename T, size_t M, size_t N>
|
|
|
|
void Structure :: ReadFieldArray2(T (& out)[M][N], const char* name, const FileDatabase& db) const
|
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
const StreamReaderAny::pos old = db.reader->GetCurrentPos();
|
|
|
|
try {
|
|
|
|
const Field& f = (*this)[name];
|
|
|
|
const Structure& s = db.dna[f.type];
|
|
|
|
|
|
|
|
// is the input actually an array?
|
|
|
|
if (!(f.flags & FieldFlag_Array)) {
|
|
|
|
throw Error((Formatter::format(),"Field `",name,"` of structure `",
|
|
|
|
this->name,"` ought to be an array of size ",M,"*",N
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
db.reader->IncPtr(f.offset);
|
|
|
|
|
|
|
|
// size conversions are always allowed, regardless of error_policy
|
|
|
|
unsigned int i = 0;
|
|
|
|
for(; i < std::min(f.array_sizes[0],M); ++i) {
|
|
|
|
unsigned int j = 0;
|
|
|
|
for(; j < std::min(f.array_sizes[1],N); ++j) {
|
|
|
|
s.Convert(out[i][j],db);
|
|
|
|
}
|
|
|
|
for(; j < N; ++j) {
|
|
|
|
_defaultInitializer<ErrorPolicy_Igno>()(out[i][j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(; i < M; ++i) {
|
|
|
|
_defaultInitializer<ErrorPolicy_Igno>()(out[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Error& e) {
|
|
|
|
_defaultInitializer<error_policy>()(out,e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
// and recover the previous stream position
|
|
|
|
db.reader->SetCurrentPos(old);
|
2015-06-28 22:11:05 +00:00
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_BLENDER_NO_STATS
|
2015-06-28 22:12:51 +00:00
|
|
|
++db.stats().fields_read;
|
2015-06-28 22:11:05 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
template <int error_policy, template <typename> class TOUT, typename T>
|
|
|
|
bool Structure :: ReadFieldPtr(TOUT<T>& out, const char* name, const FileDatabase& db,
|
2015-06-28 22:12:51 +00:00
|
|
|
bool non_recursive /*= false*/) const
|
2015-06-28 22:11:05 +00:00
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
const StreamReaderAny::pos old = db.reader->GetCurrentPos();
|
|
|
|
Pointer ptrval;
|
|
|
|
const Field* f;
|
|
|
|
try {
|
|
|
|
f = &(*this)[name];
|
|
|
|
|
|
|
|
// sanity check, should never happen if the genblenddna script is right
|
|
|
|
if (!(f->flags & FieldFlag_Pointer)) {
|
|
|
|
throw Error((Formatter::format(),"Field `",name,"` of structure `",
|
|
|
|
this->name,"` ought to be a pointer"));
|
|
|
|
}
|
|
|
|
|
|
|
|
db.reader->IncPtr(f->offset);
|
|
|
|
Convert(ptrval,db);
|
|
|
|
// actually it is meaningless on which Structure the Convert is called
|
|
|
|
// because the `Pointer` argument triggers a special implementation.
|
|
|
|
}
|
|
|
|
catch (const Error& e) {
|
|
|
|
_defaultInitializer<error_policy>()(out,e.what());
|
|
|
|
|
|
|
|
out.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve the pointer and load the corresponding structure
|
|
|
|
const bool res = ResolvePointer(out,ptrval,db,*f, non_recursive);
|
|
|
|
|
|
|
|
if(!non_recursive) {
|
|
|
|
// and recover the previous stream position
|
|
|
|
db.reader->SetCurrentPos(old);
|
|
|
|
}
|
2015-06-28 22:11:05 +00:00
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_BLENDER_NO_STATS
|
2015-06-28 22:12:51 +00:00
|
|
|
++db.stats().fields_read;
|
2015-06-28 22:11:05 +00:00
|
|
|
#endif
|
|
|
|
|
2015-06-28 22:12:51 +00:00
|
|
|
return res;
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
template <int error_policy, template <typename> class TOUT, typename T, size_t N>
|
2016-04-05 21:23:53 +00:00
|
|
|
bool Structure :: ReadFieldPtr(TOUT<T> (&out)[N], const char* name,
|
2015-06-28 22:12:51 +00:00
|
|
|
const FileDatabase& db) const
|
2015-06-28 22:11:05 +00:00
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
// XXX see if we can reduce this to call to the 'normal' ReadFieldPtr
|
|
|
|
const StreamReaderAny::pos old = db.reader->GetCurrentPos();
|
|
|
|
Pointer ptrval[N];
|
|
|
|
const Field* f;
|
|
|
|
try {
|
|
|
|
f = &(*this)[name];
|
|
|
|
|
|
|
|
// sanity check, should never happen if the genblenddna script is right
|
|
|
|
if ((FieldFlag_Pointer|FieldFlag_Pointer) != (f->flags & (FieldFlag_Pointer|FieldFlag_Pointer))) {
|
|
|
|
throw Error((Formatter::format(),"Field `",name,"` of structure `",
|
|
|
|
this->name,"` ought to be a pointer AND an array"));
|
|
|
|
}
|
|
|
|
|
|
|
|
db.reader->IncPtr(f->offset);
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
for(; i < std::min(f->array_sizes[0],N); ++i) {
|
|
|
|
Convert(ptrval[i],db);
|
|
|
|
}
|
|
|
|
for(; i < N; ++i) {
|
|
|
|
_defaultInitializer<ErrorPolicy_Igno>()(ptrval[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// actually it is meaningless on which Structure the Convert is called
|
|
|
|
// because the `Pointer` argument triggers a special implementation.
|
|
|
|
}
|
|
|
|
catch (const Error& e) {
|
|
|
|
_defaultInitializer<error_policy>()(out,e.what());
|
|
|
|
for(size_t i = 0; i < N; ++i) {
|
|
|
|
out[i].reset();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool res = true;
|
|
|
|
for(size_t i = 0; i < N; ++i) {
|
|
|
|
// resolve the pointer and load the corresponding structure
|
|
|
|
res = ResolvePointer(out[i],ptrval[i],db,*f) && res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// and recover the previous stream position
|
|
|
|
db.reader->SetCurrentPos(old);
|
2015-06-28 22:11:05 +00:00
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_BLENDER_NO_STATS
|
2015-06-28 22:12:51 +00:00
|
|
|
++db.stats().fields_read;
|
2015-06-28 22:11:05 +00:00
|
|
|
#endif
|
2015-06-28 22:12:51 +00:00
|
|
|
return res;
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
template <int error_policy, typename T>
|
|
|
|
void Structure :: ReadField(T& out, const char* name, const FileDatabase& db) const
|
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
const StreamReaderAny::pos old = db.reader->GetCurrentPos();
|
|
|
|
try {
|
|
|
|
const Field& f = (*this)[name];
|
|
|
|
// find the structure definition pertaining to this field
|
|
|
|
const Structure& s = db.dna[f.type];
|
|
|
|
|
|
|
|
db.reader->IncPtr(f.offset);
|
|
|
|
s.Convert(out,db);
|
|
|
|
}
|
|
|
|
catch (const Error& e) {
|
|
|
|
_defaultInitializer<error_policy>()(out,e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
// and recover the previous stream position
|
|
|
|
db.reader->SetCurrentPos(old);
|
2015-06-28 22:11:05 +00:00
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_BLENDER_NO_STATS
|
2015-06-28 22:12:51 +00:00
|
|
|
++db.stats().fields_read;
|
2015-06-28 22:11:05 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
template <template <typename> class TOUT, typename T>
|
2016-04-05 21:23:53 +00:00
|
|
|
bool Structure :: ResolvePointer(TOUT<T>& out, const Pointer & ptrval, const FileDatabase& db,
|
|
|
|
const Field& f,
|
|
|
|
bool non_recursive /*= false*/) const
|
2015-06-28 22:11:05 +00:00
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
out.reset(); // ensure null pointers work
|
2016-04-05 21:23:53 +00:00
|
|
|
if (!ptrval.val) {
|
2015-06-28 22:12:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const Structure& s = db.dna[f.type];
|
|
|
|
// find the file block the pointer is pointing to
|
|
|
|
const FileBlockHead* block = LocateFileBlockForAddress(ptrval,db);
|
|
|
|
|
|
|
|
// also determine the target type from the block header
|
|
|
|
// and check if it matches the type which we expect.
|
|
|
|
const Structure& ss = db.dna[block->dna_index];
|
|
|
|
if (ss != s) {
|
|
|
|
throw Error((Formatter::format(),"Expected target to be of type `",s.name,
|
|
|
|
"` but seemingly it is a `",ss.name,"` instead"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to retrieve the object from the cache
|
2016-04-05 21:23:53 +00:00
|
|
|
db.cache(out).get(s,out,ptrval);
|
2015-06-28 22:12:51 +00:00
|
|
|
if (out) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// seek to this location, but save the previous stream pointer.
|
|
|
|
const StreamReaderAny::pos pold = db.reader->GetCurrentPos();
|
|
|
|
db.reader->SetCurrentPos(block->start+ static_cast<size_t>((ptrval.val - block->address.val) ));
|
|
|
|
// FIXME: basically, this could cause problems with 64 bit pointers on 32 bit systems.
|
|
|
|
// I really ought to improve StreamReader to work with 64 bit indices exclusively.
|
|
|
|
|
|
|
|
// continue conversion after allocating the required storage
|
2016-04-05 21:23:53 +00:00
|
|
|
size_t num = block->size / ss.size;
|
2015-06-28 22:12:51 +00:00
|
|
|
T* o = _allocate(out,num);
|
|
|
|
|
|
|
|
// cache the object before we convert it to avoid cyclic recursion.
|
2016-04-05 21:23:53 +00:00
|
|
|
db.cache(out).set(s,out,ptrval);
|
2015-06-28 22:12:51 +00:00
|
|
|
|
|
|
|
// if the non_recursive flag is set, we don't do anything but leave
|
|
|
|
// the cursor at the correct position to resolve the object.
|
|
|
|
if (!non_recursive) {
|
|
|
|
for (size_t i = 0; i < num; ++i,++o) {
|
|
|
|
s.Convert(*o,db);
|
|
|
|
}
|
|
|
|
|
|
|
|
db.reader->SetCurrentPos(pold);
|
|
|
|
}
|
2015-06-28 22:11:05 +00:00
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_BLENDER_NO_STATS
|
2015-06-28 22:12:51 +00:00
|
|
|
if(out) {
|
|
|
|
++db.stats().pointers_resolved;
|
|
|
|
}
|
2015-06-28 22:11:05 +00:00
|
|
|
#endif
|
2015-06-28 22:12:51 +00:00
|
|
|
return false;
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
2016-04-05 21:23:53 +00:00
|
|
|
inline bool Structure :: ResolvePointer( std::shared_ptr< FileOffset >& out, const Pointer & ptrval,
|
|
|
|
const FileDatabase& db,
|
2015-06-28 22:12:51 +00:00
|
|
|
const Field&,
|
|
|
|
bool) const
|
2015-06-28 22:11:05 +00:00
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
// Currently used exclusively by PackedFile::data to represent
|
2016-04-05 21:23:53 +00:00
|
|
|
// a simple offset into the mapped BLEND file.
|
2015-06-28 22:12:51 +00:00
|
|
|
out.reset();
|
2016-04-05 21:23:53 +00:00
|
|
|
if (!ptrval.val) {
|
2015-06-28 22:12:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the file block the pointer is pointing to
|
|
|
|
const FileBlockHead* block = LocateFileBlockForAddress(ptrval,db);
|
|
|
|
|
2016-04-05 21:23:53 +00:00
|
|
|
out = std::shared_ptr< FileOffset > (new FileOffset());
|
2015-06-28 22:12:51 +00:00
|
|
|
out->val = block->start+ static_cast<size_t>((ptrval.val - block->address.val) );
|
|
|
|
return false;
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
template <template <typename> class TOUT, typename T>
|
2016-04-05 21:23:53 +00:00
|
|
|
bool Structure :: ResolvePointer(vector< TOUT<T> >& out, const Pointer & ptrval,
|
|
|
|
const FileDatabase& db,
|
2015-06-28 22:12:51 +00:00
|
|
|
const Field& f,
|
2016-04-05 21:23:53 +00:00
|
|
|
bool) const
|
2015-06-28 22:11:05 +00:00
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
// This is a function overload, not a template specialization. According to
|
|
|
|
// the partial ordering rules, it should be selected by the compiler
|
|
|
|
// for array-of-pointer inputs, i.e. Object::mats.
|
|
|
|
|
|
|
|
out.reset();
|
2016-04-05 21:23:53 +00:00
|
|
|
if (!ptrval.val) {
|
2015-06-28 22:12:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the file block the pointer is pointing to
|
|
|
|
const FileBlockHead* block = LocateFileBlockForAddress(ptrval,db);
|
2016-04-05 21:23:53 +00:00
|
|
|
const size_t num = block->size / (db.i64bit?8:4);
|
2015-06-28 22:12:51 +00:00
|
|
|
|
|
|
|
// keep the old stream position
|
|
|
|
const StreamReaderAny::pos pold = db.reader->GetCurrentPos();
|
|
|
|
db.reader->SetCurrentPos(block->start+ static_cast<size_t>((ptrval.val - block->address.val) ));
|
|
|
|
|
|
|
|
bool res = false;
|
|
|
|
// allocate raw storage for the array
|
|
|
|
out.resize(num);
|
|
|
|
for (size_t i = 0; i< num; ++i) {
|
|
|
|
Pointer val;
|
|
|
|
Convert(val,db);
|
|
|
|
|
|
|
|
// and resolve the pointees
|
2016-04-05 21:23:53 +00:00
|
|
|
res = ResolvePointer(out[i],val,db,f) && res;
|
2015-06-28 22:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
db.reader->SetCurrentPos(pold);
|
|
|
|
return res;
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
2016-04-05 21:23:53 +00:00
|
|
|
template <> bool Structure :: ResolvePointer<std::shared_ptr,ElemBase>(std::shared_ptr<ElemBase>& out,
|
|
|
|
const Pointer & ptrval,
|
|
|
|
const FileDatabase& db,
|
2015-06-28 22:12:51 +00:00
|
|
|
const Field&,
|
|
|
|
bool
|
2016-04-05 21:23:53 +00:00
|
|
|
) const
|
2015-06-28 22:11:05 +00:00
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
// Special case when the data type needs to be determined at runtime.
|
|
|
|
// Less secure than in the `strongly-typed` case.
|
|
|
|
|
|
|
|
out.reset();
|
2016-04-05 21:23:53 +00:00
|
|
|
if (!ptrval.val) {
|
2015-06-28 22:12:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the file block the pointer is pointing to
|
|
|
|
const FileBlockHead* block = LocateFileBlockForAddress(ptrval,db);
|
|
|
|
|
|
|
|
// determine the target type from the block header
|
|
|
|
const Structure& s = db.dna[block->dna_index];
|
|
|
|
|
|
|
|
// try to retrieve the object from the cache
|
2016-04-05 21:23:53 +00:00
|
|
|
db.cache(out).get(s,out,ptrval);
|
2015-06-28 22:12:51 +00:00
|
|
|
if (out) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// seek to this location, but save the previous stream pointer.
|
|
|
|
const StreamReaderAny::pos pold = db.reader->GetCurrentPos();
|
|
|
|
db.reader->SetCurrentPos(block->start+ static_cast<size_t>((ptrval.val - block->address.val) ));
|
|
|
|
// FIXME: basically, this could cause problems with 64 bit pointers on 32 bit systems.
|
|
|
|
// I really ought to improve StreamReader to work with 64 bit indices exclusively.
|
|
|
|
|
|
|
|
// continue conversion after allocating the required storage
|
|
|
|
DNA::FactoryPair builders = db.dna.GetBlobToStructureConverter(s,db);
|
|
|
|
if (!builders.first) {
|
|
|
|
// this might happen if DNA::RegisterConverters hasn't been called so far
|
|
|
|
// or if the target type is not contained in `our` DNA.
|
|
|
|
out.reset();
|
|
|
|
DefaultLogger::get()->warn((Formatter::format(),
|
|
|
|
"Failed to find a converter for the `",s.name,"` structure"
|
|
|
|
));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate the object hull
|
|
|
|
out = (s.*builders.first)();
|
2016-04-05 21:23:53 +00:00
|
|
|
|
|
|
|
// cache the object immediately to prevent infinite recursion in a
|
2015-06-28 22:12:51 +00:00
|
|
|
// circular list with a single element (i.e. a self-referencing element).
|
|
|
|
db.cache(out).set(s,out,ptrval);
|
|
|
|
|
|
|
|
// and do the actual conversion
|
|
|
|
(s.*builders.second)(out,db);
|
|
|
|
db.reader->SetCurrentPos(pold);
|
2016-04-05 21:23:53 +00:00
|
|
|
|
2015-06-28 22:12:51 +00:00
|
|
|
// store a pointer to the name string of the actual type
|
|
|
|
// in the object itself. This allows the conversion code
|
|
|
|
// to perform additional type checking.
|
|
|
|
out->dna_type = s.name.c_str();
|
2015-06-28 22:11:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_BLENDER_NO_STATS
|
2015-06-28 22:12:51 +00:00
|
|
|
++db.stats().pointers_resolved;
|
2015-06-28 22:11:05 +00:00
|
|
|
#endif
|
2015-06-28 22:12:51 +00:00
|
|
|
return false;
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
2016-04-05 21:23:53 +00:00
|
|
|
const FileBlockHead* Structure :: LocateFileBlockForAddress(const Pointer & ptrval, const FileDatabase& db) const
|
2015-06-28 22:11:05 +00:00
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
// the file blocks appear in list sorted by
|
2016-04-05 21:23:53 +00:00
|
|
|
// with ascending base addresses so we can run a
|
2018-02-11 16:43:49 +00:00
|
|
|
// binary search to locate the pointer quickly.
|
2015-06-28 22:12:51 +00:00
|
|
|
|
|
|
|
// NOTE: Blender seems to distinguish between side-by-side
|
|
|
|
// data (stored in the same data block) and far pointers,
|
|
|
|
// which are only used for structures starting with an ID.
|
|
|
|
// We don't need to make this distinction, our algorithm
|
|
|
|
// works regardless where the data is stored.
|
|
|
|
vector<FileBlockHead>::const_iterator it = std::lower_bound(db.entries.begin(),db.entries.end(),ptrval);
|
|
|
|
if (it == db.entries.end()) {
|
|
|
|
// this is crucial, pointers may not be invalid.
|
|
|
|
// this is either a corrupted file or an attempted attack.
|
|
|
|
throw DeadlyImportError((Formatter::format(),"Failure resolving pointer 0x",
|
|
|
|
std::hex,ptrval.val,", no file block falls into this address range"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
if (ptrval.val >= (*it).address.val + (*it).size) {
|
|
|
|
throw DeadlyImportError((Formatter::format(),"Failure resolving pointer 0x",
|
|
|
|
std::hex,ptrval.val,", nearest file block starting at 0x",
|
|
|
|
(*it).address.val," ends at 0x",
|
|
|
|
(*it).address.val + (*it).size
|
|
|
|
));
|
|
|
|
}
|
|
|
|
return &*it;
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-04-05 21:23:53 +00:00
|
|
|
// NOTE: The MSVC debugger keeps showing up this annoying `a cast to a smaller data type has
|
2015-06-28 22:11:05 +00:00
|
|
|
// caused a loss of data`-warning. Avoid this warning by a masking with an appropriate bitmask.
|
|
|
|
|
|
|
|
template <typename T> struct signless;
|
|
|
|
template <> struct signless<char> {typedef unsigned char type;};
|
|
|
|
template <> struct signless<short> {typedef unsigned short type;};
|
|
|
|
template <> struct signless<int> {typedef unsigned int type;};
|
2016-07-24 08:23:59 +00:00
|
|
|
template <> struct signless<unsigned char> { typedef unsigned char type; };
|
2015-06-28 22:11:05 +00:00
|
|
|
template <typename T>
|
2016-04-05 21:23:53 +00:00
|
|
|
struct static_cast_silent {
|
2015-06-28 22:12:51 +00:00
|
|
|
template <typename V>
|
|
|
|
T operator()(V in) {
|
|
|
|
return static_cast<T>(in & static_cast<typename signless<T>::type>(-1));
|
|
|
|
}
|
2015-06-28 22:11:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct static_cast_silent<float> {
|
2015-06-28 22:12:51 +00:00
|
|
|
template <typename V> float operator()(V in) {
|
|
|
|
return static_cast<float> (in);
|
|
|
|
}
|
2015-06-28 22:11:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct static_cast_silent<double> {
|
2015-06-28 22:12:51 +00:00
|
|
|
template <typename V> double operator()(V in) {
|
|
|
|
return static_cast<double>(in);
|
|
|
|
}
|
2015-06-28 22:11:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-04-05 21:23:53 +00:00
|
|
|
template <typename T> inline void ConvertDispatcher(T& out, const Structure& in,const FileDatabase& db)
|
2015-06-28 22:11:05 +00:00
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
if (in.name == "int") {
|
|
|
|
out = static_cast_silent<T>()(db.reader->GetU4());
|
|
|
|
}
|
|
|
|
else if (in.name == "short") {
|
|
|
|
out = static_cast_silent<T>()(db.reader->GetU2());
|
|
|
|
}
|
|
|
|
else if (in.name == "char") {
|
|
|
|
out = static_cast_silent<T>()(db.reader->GetU1());
|
|
|
|
}
|
|
|
|
else if (in.name == "float") {
|
|
|
|
out = static_cast<T>(db.reader->GetF4());
|
|
|
|
}
|
|
|
|
else if (in.name == "double") {
|
|
|
|
out = static_cast<T>(db.reader->GetF8());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw DeadlyImportError("Unknown source for conversion to primitive data type: "+in.name);
|
|
|
|
}
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> inline void Structure :: Convert<int> (int& dest,const FileDatabase& db) const
|
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
ConvertDispatcher(dest,*this,db);
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2017-10-31 11:24:32 +00:00
|
|
|
template<> inline void Structure :: Convert<short> (short& dest,const FileDatabase& db) const
|
2015-06-28 22:11:05 +00:00
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
// automatic rescaling from short to float and vice versa (seems to be used by normals)
|
|
|
|
if (name == "float") {
|
2017-10-13 20:41:38 +00:00
|
|
|
float f = db.reader->GetF4();
|
|
|
|
if ( f > 1.0f )
|
|
|
|
f = 1.0f;
|
|
|
|
dest = static_cast<short>( f * 32767.f);
|
2015-06-28 22:12:51 +00:00
|
|
|
//db.reader->IncPtr(-4);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (name == "double") {
|
|
|
|
dest = static_cast<short>(db.reader->GetF8() * 32767.);
|
|
|
|
//db.reader->IncPtr(-8);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ConvertDispatcher(dest,*this,db);
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> inline void Structure :: Convert<char> (char& dest,const FileDatabase& db) const
|
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
// automatic rescaling from char to float and vice versa (seems useful for RGB colors)
|
|
|
|
if (name == "float") {
|
|
|
|
dest = static_cast<char>(db.reader->GetF4() * 255.f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (name == "double") {
|
|
|
|
dest = static_cast<char>(db.reader->GetF8() * 255.f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ConvertDispatcher(dest,*this,db);
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 08:23:59 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> inline void Structure::Convert<unsigned char>(unsigned char& dest, const FileDatabase& db) const
|
|
|
|
{
|
|
|
|
// automatic rescaling from char to float and vice versa (seems useful for RGB colors)
|
|
|
|
if (name == "float") {
|
|
|
|
dest = static_cast<unsigned char>(db.reader->GetF4() * 255.f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (name == "double") {
|
|
|
|
dest = static_cast<unsigned char>(db.reader->GetF8() * 255.f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ConvertDispatcher(dest, *this, db);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-28 22:11:05 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> inline void Structure :: Convert<float> (float& dest,const FileDatabase& db) const
|
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
// automatic rescaling from char to float and vice versa (seems useful for RGB colors)
|
|
|
|
if (name == "char") {
|
|
|
|
dest = db.reader->GetI1() / 255.f;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// automatic rescaling from short to float and vice versa (used by normals)
|
|
|
|
else if (name == "short") {
|
|
|
|
dest = db.reader->GetI2() / 32767.f;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ConvertDispatcher(dest,*this,db);
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> inline void Structure :: Convert<double> (double& dest,const FileDatabase& db) const
|
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
if (name == "char") {
|
|
|
|
dest = db.reader->GetI1() / 255.;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (name == "short") {
|
|
|
|
dest = db.reader->GetI2() / 32767.;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ConvertDispatcher(dest,*this,db);
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <> inline void Structure :: Convert<Pointer> (Pointer& dest,const FileDatabase& db) const
|
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
if (db.i64bit) {
|
|
|
|
dest.val = db.reader->GetU8();
|
|
|
|
//db.reader->IncPtr(-8);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dest.val = db.reader->GetU4();
|
|
|
|
//db.reader->IncPtr(-4);
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
const Structure& DNA :: operator [] (const std::string& ss) const
|
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
std::map<std::string, size_t>::const_iterator it = indices.find(ss);
|
|
|
|
if (it == indices.end()) {
|
|
|
|
throw Error((Formatter::format(),
|
|
|
|
"BlendDNA: Did not find a structure named `",ss,"`"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return structures[(*it).second];
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
const Structure* DNA :: Get (const std::string& ss) const
|
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
std::map<std::string, size_t>::const_iterator it = indices.find(ss);
|
|
|
|
return it == indices.end() ? NULL : &structures[(*it).second];
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
2016-04-05 21:23:53 +00:00
|
|
|
const Structure& DNA :: operator [] (const size_t i) const
|
2015-06-28 22:11:05 +00:00
|
|
|
{
|
2015-06-28 22:12:51 +00:00
|
|
|
if (i >= structures.size()) {
|
|
|
|
throw Error((Formatter::format(),
|
|
|
|
"BlendDNA: There is no structure with index `",i,"`"
|
|
|
|
));
|
|
|
|
}
|
2015-06-28 22:11:05 +00:00
|
|
|
|
2015-06-28 22:12:51 +00:00
|
|
|
return structures[i];
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
template <template <typename> class TOUT> template <typename T> void ObjectCache<TOUT> :: get (
|
2016-04-05 21:23:53 +00:00
|
|
|
const Structure& s,
|
|
|
|
TOUT<T>& out,
|
2015-06-28 22:12:51 +00:00
|
|
|
const Pointer& ptr
|
2015-06-28 22:11:05 +00:00
|
|
|
) const {
|
|
|
|
|
2015-06-28 22:12:51 +00:00
|
|
|
if(s.cache_idx == static_cast<size_t>(-1)) {
|
|
|
|
s.cache_idx = db.next_cache_idx++;
|
|
|
|
caches.resize(db.next_cache_idx);
|
|
|
|
return;
|
|
|
|
}
|
2015-06-28 22:11:05 +00:00
|
|
|
|
2015-06-28 22:12:51 +00:00
|
|
|
typename StructureCache::const_iterator it = caches[s.cache_idx].find(ptr);
|
|
|
|
if (it != caches[s.cache_idx].end()) {
|
2016-04-05 21:23:53 +00:00
|
|
|
out = std::static_pointer_cast<T>( (*it).second );
|
2015-06-28 22:11:05 +00:00
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_BLENDER_NO_STATS
|
2015-06-28 22:12:51 +00:00
|
|
|
++db.stats().cache_hits;
|
2015-06-28 22:11:05 +00:00
|
|
|
#endif
|
2015-06-28 22:12:51 +00:00
|
|
|
}
|
|
|
|
// otherwise, out remains untouched
|
2015-06-28 22:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
template <template <typename> class TOUT> template <typename T> void ObjectCache<TOUT> :: set (
|
2016-04-05 21:23:53 +00:00
|
|
|
const Structure& s,
|
2015-06-28 22:12:51 +00:00
|
|
|
const TOUT<T>& out,
|
|
|
|
const Pointer& ptr
|
2015-06-28 22:11:05 +00:00
|
|
|
) {
|
2015-06-28 22:12:51 +00:00
|
|
|
if(s.cache_idx == static_cast<size_t>(-1)) {
|
|
|
|
s.cache_idx = db.next_cache_idx++;
|
|
|
|
caches.resize(db.next_cache_idx);
|
|
|
|
}
|
2016-04-05 21:23:53 +00:00
|
|
|
caches[s.cache_idx][ptr] = std::static_pointer_cast<ElemBase>( out );
|
2015-06-28 22:11:05 +00:00
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_BLENDER_NO_STATS
|
2015-06-28 22:12:51 +00:00
|
|
|
++db.stats().cached_objects;
|
2015-06-28 22:11:05 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
}}
|
|
|
|
#endif
|