Coverage Report

Created: 2025-08-03 06:54

/src/assimp/code/AssetLib/FBX/FBXProperties.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
Open Asset Import Library (assimp)
3
----------------------------------------------------------------------
4
5
Copyright (c) 2006-2025, assimp team
6
7
All rights reserved.
8
9
Redistribution and use of this software in source and binary forms,
10
with or without modification, are permitted provided that the
11
following conditions are met:
12
13
* Redistributions of source code must retain the above
14
  copyright notice, this list of conditions and the
15
  following disclaimer.
16
17
* Redistributions in binary form must reproduce the above
18
  copyright notice, this list of conditions and the
19
  following disclaimer in the documentation and/or other
20
  materials provided with the distribution.
21
22
* Neither the name of the assimp team, nor the names of its
23
  contributors may be used to endorse or promote products
24
  derived from this software without specific prior
25
  written permission of the assimp team.
26
27
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39
----------------------------------------------------------------------
40
*/
41
42
/** @file  FBXProperties.cpp
43
 *  @brief Implementation of the FBX dynamic properties system
44
 */
45
46
#ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
47
48
#include "FBXTokenizer.h"
49
#include "FBXParser.h"
50
#include "FBXDocument.h"
51
#include "FBXDocumentUtil.h"
52
#include "FBXProperties.h"
53
54
#include <utility>
55
56
namespace Assimp {
57
namespace FBX {
58
59
using namespace Util;
60
61
// ------------------------------------------------------------------------------------------------
62
63
namespace {
64
65
0
    void checkTokenCount(const TokenList &tok, unsigned int expectedCount) {
66
0
        ai_assert(expectedCount >= 2);
67
0
        if (tok.size() < expectedCount) {
68
0
            const std::string &s = ParseTokenAsString(*tok[1]);
69
0
            if (tok[1]->IsBinary()) {
70
0
                throw DeadlyImportError("Not enough tokens for property of type ", s, " at offset ", tok[1]->Offset());
71
0
            } else {
72
0
                throw DeadlyImportError("Not enough tokens for property of type ", s, " at line ", tok[1]->Line());
73
0
            }
74
0
        }
75
0
}
76
77
// ------------------------------------------------------------------------------------------------
78
// read a typed property out of a FBX element. The return value is nullptr if the property cannot be read.
79
Property* ReadTypedProperty(const Element& element)
80
0
{
81
0
    ai_assert(element.KeyToken().StringContents() == "P");
82
83
0
    const TokenList& tok = element.Tokens();
84
0
    if (tok.size() < 2) {
85
0
        return nullptr;
86
0
    }
87
88
0
    const std::string& s = ParseTokenAsString(*tok[1]);
89
0
    const char* const cs = s.c_str();
90
0
    if (!strcmp(cs,"KString")) {
91
0
        checkTokenCount(tok, 5);
92
0
        return new TypedProperty<std::string>(ParseTokenAsString(*tok[4]));
93
0
    }
94
0
    else if (!strcmp(cs,"bool") || !strcmp(cs,"Bool")) {
95
0
        checkTokenCount(tok, 5);
96
0
        return new TypedProperty<bool>(ParseTokenAsInt(*tok[4]) != 0);
97
0
    }
98
0
    else if (!strcmp(cs, "int") || !strcmp(cs, "Int") || !strcmp(cs, "enum") || !strcmp(cs, "Enum") || !strcmp(cs, "Integer")) {
99
0
        checkTokenCount(tok, 5);
100
0
        return new TypedProperty<int>(ParseTokenAsInt(*tok[4]));
101
0
    }
102
0
    else if (!strcmp(cs, "ULongLong")) {
103
0
        checkTokenCount(tok, 5);
104
0
        return new TypedProperty<uint64_t>(ParseTokenAsID(*tok[4]));
105
0
    }
106
0
    else if (!strcmp(cs, "KTime")) {
107
0
        checkTokenCount(tok, 5);
108
0
        return new TypedProperty<int64_t>(ParseTokenAsInt64(*tok[4]));
109
0
    }
110
0
    else if (!strcmp(cs,"Vector3D") ||
111
0
        !strcmp(cs,"ColorRGB") ||
112
0
        !strcmp(cs,"Vector") ||
113
0
        !strcmp(cs,"Color") ||
114
0
        !strcmp(cs,"Lcl Translation") ||
115
0
        !strcmp(cs,"Lcl Rotation") ||
116
0
        !strcmp(cs,"Lcl Scaling")
117
0
        ) {
118
0
        checkTokenCount(tok, 7);
119
0
        return new TypedProperty<aiVector3D>(aiVector3D(
120
0
            ParseTokenAsFloat(*tok[4]),
121
0
            ParseTokenAsFloat(*tok[5]),
122
0
            ParseTokenAsFloat(*tok[6]))
123
0
        );
124
0
    }
125
0
    else if (!strcmp(cs,"double") || !strcmp(cs,"Number") || !strcmp(cs,"float") || !strcmp(cs,"Float") || !strcmp(cs,"FieldOfView") || !strcmp( cs, "UnitScaleFactor" ) ) {
126
0
        checkTokenCount(tok, 5);
127
0
        return new TypedProperty<float>(ParseTokenAsFloat(*tok[4]));
128
0
    }
129
0
    else if (!strcmp(cs, "ColorAndAlpha")) {
130
0
        checkTokenCount(tok, 8);
131
0
        return new TypedProperty<aiColor4D>(aiColor4D(
132
0
            ParseTokenAsFloat(*tok[4]),
133
0
            ParseTokenAsFloat(*tok[5]),
134
0
            ParseTokenAsFloat(*tok[6]),
135
0
            ParseTokenAsFloat(*tok[7]))
136
0
        );
137
0
    }
138
0
    return nullptr;
139
0
}
140
141
142
// ------------------------------------------------------------------------------------------------
143
// peek into an element and check if it contains a FBX property, if so return its name.
144
0
std::string PeekPropertyName(const Element& element) {
145
0
    ai_assert(element.KeyToken().StringContents() == "P");
146
147
0
    const TokenList& tok = element.Tokens();
148
0
    if(tok.size() < 4) {
149
0
        return std::string();
150
0
    }
151
152
0
    return ParseTokenAsString(*tok[0]);
153
0
}
154
155
} //! anon
156
157
158
// ------------------------------------------------------------------------------------------------
159
PropertyTable::PropertyTable(const Element &element, std::shared_ptr<const PropertyTable> templateProps) :
160
0
        templateProps(std::move(templateProps)), element(&element) {
161
0
    const Scope& scope = GetRequiredScope(element);
162
0
    for(const ElementMap::value_type& v : scope.Elements()) {
163
0
        if(v.first != "P") {
164
0
            DOMWarning("expected only P elements in property table",v.second);
165
0
            continue;
166
0
        }
167
168
0
        const std::string& name = PeekPropertyName(*v.second);
169
0
        if(!name.length()) {
170
0
            DOMWarning("could not read property name",v.second);
171
0
            continue;
172
0
        }
173
174
0
        LazyPropertyMap::const_iterator it = lazyProps.find(name);
175
0
        if (it != lazyProps.end()) {
176
0
            DOMWarning("duplicate property name, will hide previous value: " + name,v.second);
177
0
            continue;
178
0
        }
179
180
0
        lazyProps[name] = v.second;
181
0
    }
182
0
}
183
184
// ------------------------------------------------------------------------------------------------
185
PropertyTable::~PropertyTable()
186
0
{
187
0
    for(PropertyMap::value_type& v : props) {
188
0
        delete v.second;
189
0
    }
190
0
}
191
192
193
// ------------------------------------------------------------------------------------------------
194
const Property* PropertyTable::Get(const std::string& name) const
195
0
{
196
0
    PropertyMap::const_iterator it = props.find(name);
197
0
    if (it == props.end()) {
198
        // hasn't been parsed yet?
199
0
        LazyPropertyMap::const_iterator lit = lazyProps.find(name);
200
0
        if(lit != lazyProps.end()) {
201
0
            props[name] = ReadTypedProperty(*(*lit).second);
202
0
            it = props.find(name);
203
204
0
            ai_assert(it != props.end());
205
0
        }
206
207
0
        if (it == props.end()) {
208
            // check property template
209
0
            if(templateProps) {
210
0
                return templateProps->Get(name);
211
0
            }
212
213
0
            return nullptr;
214
0
        }
215
0
    }
216
217
0
    return (*it).second;
218
0
}
219
220
DirectPropertyMap PropertyTable::GetUnparsedProperties() const
221
0
{
222
0
    DirectPropertyMap result;
223
224
    // Loop through all the lazy properties (which is all the properties)
225
0
    for(const LazyPropertyMap::value_type& currentElement : lazyProps) {
226
227
        // Skip parsed properties
228
0
        if (props.end() != props.find(currentElement.first)) {
229
0
            continue;
230
0
        }
231
232
        // Read the element's value.
233
        // Wrap the naked pointer (since the call site is required to acquire ownership)
234
0
        std::shared_ptr<Property> prop = std::shared_ptr<Property>(ReadTypedProperty(*currentElement.second));
235
236
        // Element could not be read. Skip it.
237
0
        if (!prop) {
238
0
            continue;
239
0
        }
240
241
        // Add to result
242
0
        result[currentElement.first] = prop;
243
0
    }
244
245
0
    return result;
246
0
}
247
248
} //! FBX
249
} //! Assimp
250
251
#endif