forked from PixarAnimationStudios/OpenUSD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileFormat.cpp
401 lines (347 loc) · 9.3 KB
/
fileFormat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
///
/// \file Sdf/fileFormat.cpp
#include "pxr/pxr.h"
#include "pxr/usd/sdf/fileFormat.h"
#include "pxr/usd/sdf/assetPathResolver.h"
#include "pxr/usd/sdf/data.h"
#include "pxr/usd/sdf/fileFormatRegistry.h"
#include "pxr/usd/sdf/layer.h"
#include "pxr/usd/sdf/layerHints.h"
#include "pxr/usd/ar/resolver.h"
#include "pxr/base/trace/trace.h"
#include "pxr/base/tf/registryManager.h"
#include "pxr/base/tf/staticData.h"
#include "pxr/base/tf/stringUtils.h"
#include "pxr/base/tf/type.h"
PXR_NAMESPACE_OPEN_SCOPE
static TfStaticData<Sdf_FileFormatRegistry> _FileFormatRegistry;
TF_REGISTRY_FUNCTION(TfType)
{
TfType::Define<SdfFileFormat>();
}
TF_DEFINE_PUBLIC_TOKENS(SdfFileFormatTokens, SDF_FILE_FORMAT_TOKENS);
SdfFileFormat::SdfFileFormat(
const TfToken& formatId,
const TfToken& versionString,
const TfToken& target,
const std::string& extension,
const SdfSchemaBase& schema)
: SdfFileFormat(
formatId, versionString, target, std::vector<std::string>{extension},
schema)
{
}
SdfFileFormat::SdfFileFormat(
const TfToken& formatId,
const TfToken& versionString,
const TfToken& target,
const std::string& extension)
: SdfFileFormat(
formatId, versionString, target, std::vector<std::string>{extension},
SdfSchema::GetInstance())
{
}
SdfFileFormat::SdfFileFormat(
const TfToken& formatId,
const TfToken& versionString,
const TfToken& target,
const std::vector<std::string> &extensions)
: SdfFileFormat(
formatId, versionString, target, extensions, SdfSchema::GetInstance())
{
}
SdfFileFormat::SdfFileFormat(
const TfToken& formatId,
const TfToken& versionString,
const TfToken& target,
const std::vector<std::string>& extensions,
const SdfSchemaBase& schema)
: _schema(schema)
, _formatId(formatId)
, _target(target)
, _cookie("#" + formatId.GetString())
, _versionString(versionString)
, _extensions(extensions)
// If a file format is marked as primary, then it must be the
// primary format for all of the extensions it supports. So,
// it's sufficient to just check the first extension in the list.
, _isPrimaryFormat(
_FileFormatRegistry
->GetPrimaryFormatForExtension(extensions[0]) == formatId)
{
// Do Nothing.
}
SdfFileFormat::~SdfFileFormat()
{
// Do Nothing.
}
SdfFileFormat::FileFormatArguments
SdfFileFormat::GetDefaultFileFormatArguments() const
{
return FileFormatArguments();
}
SdfAbstractDataRefPtr
SdfFileFormat::InitData(const FileFormatArguments& args) const
{
SdfData* metadata = new SdfData;
// The pseudo-root spec must always exist in a layer's SdfData, so
// add it here.
metadata->CreateSpec(SdfPath::AbsoluteRootPath(), SdfSpecTypePseudoRoot);
return TfCreateRefPtr(metadata);
}
SdfLayerRefPtr
SdfFileFormat::NewLayer(const SdfFileFormatConstPtr &fileFormat,
const std::string &identifier,
const std::string &realPath,
const ArAssetInfo& assetInfo,
const FileFormatArguments &args) const
{
return TfCreateRefPtr(
_InstantiateNewLayer(
fileFormat, identifier, realPath, assetInfo, args));
}
bool
SdfFileFormat::ShouldSkipAnonymousReload() const
{
return _ShouldSkipAnonymousReload();
}
bool
SdfFileFormat::ShouldReadAnonymousLayers() const
{
return _ShouldReadAnonymousLayers();
}
const SdfSchemaBase&
SdfFileFormat::GetSchema() const
{
return _schema;
}
const TfToken&
SdfFileFormat::GetFormatId() const
{
return _formatId;
}
const TfToken&
SdfFileFormat::GetTarget() const
{
return _target;
}
const std::string&
SdfFileFormat::GetFileCookie() const
{
return _cookie;
}
const TfToken&
SdfFileFormat::GetVersionString() const
{
return _versionString;
}
bool
SdfFileFormat::IsPrimaryFormatForExtensions() const
{
return _isPrimaryFormat;
}
const std::vector<std::string>&
SdfFileFormat::GetFileExtensions() const {
return _extensions;
}
const std::string&
SdfFileFormat::GetPrimaryFileExtension() const
{
static std::string emptyString;
return TF_VERIFY(!_extensions.empty()) ? _extensions[0] : emptyString;
}
bool
SdfFileFormat::IsSupportedExtension(
const std::string& extension) const
{
std::string ext = GetFileExtension(extension);
return ext.empty() ?
false : std::count(_extensions.begin(), _extensions.end(), ext);
}
bool
SdfFileFormat::IsPackage() const
{
return false;
}
std::string
SdfFileFormat::GetPackageRootLayerPath(
const std::string& resolvedPath) const
{
return std::string();
}
bool
SdfFileFormat::WriteToFile(
const SdfLayer&,
const std::string&,
const std::string&,
const FileFormatArguments&) const
{
return false;
}
bool
SdfFileFormat::ReadFromString(
SdfLayer* layer,
const std::string& str) const
{
return false;
}
bool
SdfFileFormat::WriteToStream(
const SdfSpecHandle &spec,
std::ostream& out,
size_t indent) const
{
return false;
}
bool
SdfFileFormat::WriteToString(
const SdfLayer& layer,
std::string* str,
const std::string& comment) const
{
return false;
}
std::set<std::string>
SdfFileFormat::GetExternalAssetDependencies(
const SdfLayer& layer) const
{
return std::set<std::string>();
}
/* static */
std::string
SdfFileFormat::GetFileExtension(
const std::string& s)
{
if (s.empty()) {
return s;
}
const std::string extension = Sdf_GetExtension(s);
return extension.empty() ? s : extension;
}
/* static */
std::set<std::string>
SdfFileFormat::FindAllFileFormatExtensions()
{
return _FileFormatRegistry->FindAllFileFormatExtensions();
}
/* static */
SdfFileFormatConstPtr
SdfFileFormat::FindById(
const TfToken& formatId)
{
return _FileFormatRegistry->FindById(formatId);
}
/* static */
SdfFileFormatConstPtr
SdfFileFormat::FindByExtension(
const std::string& extension,
const std::string& target)
{
return _FileFormatRegistry->FindByExtension(extension, target);
}
/* static */
SdfFileFormatConstPtr
SdfFileFormat::FindByExtension(
const std::string &path,
const FileFormatArguments &args)
{
// Find a file format that can handle this extension and the
// specified target (if any).
const std::string* targets =
TfMapLookupPtr(args, SdfFileFormatTokens->TargetArg);
if (targets) {
for (std::string& target : TfStringTokenize(*targets, ",")) {
target = TfStringTrim(target);
if (target.empty()) {
continue;
}
if (const SdfFileFormatConstPtr format =
SdfFileFormat::FindByExtension(path, target)) {
return format;
}
}
return TfNullPtr;
}
return SdfFileFormat::FindByExtension(path);
}
bool
SdfFileFormat::_ShouldSkipAnonymousReload() const
{
return true;
}
bool
SdfFileFormat::_ShouldReadAnonymousLayers() const
{
return false;
}
void
SdfFileFormat::_SetLayerData(
SdfLayer* layer,
SdfAbstractDataRefPtr& data)
{
_SetLayerData(layer, data, SdfLayerHints{});
}
void
SdfFileFormat::_SetLayerData(
SdfLayer* layer,
SdfAbstractDataRefPtr& data,
SdfLayerHints hints)
{
// If layer initialization has not completed, then this
// is being loaded as a new layer; otherwise we are loading
// data into an existing layer.
//
// Note that this is an optional::bool and we are checking if it has
// been set, not what its held value is.
//
const bool layerIsLoadingAsNew = !layer->_initializationWasSuccessful;
if (layerIsLoadingAsNew) {
layer->_SwapData(data);
}
else {
layer->_SetData(data);
}
layer->_hints = hints;
}
SdfAbstractDataConstPtr
SdfFileFormat::_GetLayerData(const SdfLayer& layer)
{
return layer._GetData();
}
/* virtual */
SdfLayer*
SdfFileFormat::_InstantiateNewLayer(
const SdfFileFormatConstPtr &fileFormat,
const std::string &identifier,
const std::string &realPath,
const ArAssetInfo& assetInfo,
const FileFormatArguments &args) const
{
return new SdfLayer(fileFormat, identifier, realPath, assetInfo, args);
}
// ------------------------------------------------------------
Sdf_FileFormatFactoryBase::~Sdf_FileFormatFactoryBase() = default;
PXR_NAMESPACE_CLOSE_SCOPE