Coverage Report

Created: 2024-08-02 07:04

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