forked from OpenAssetIO/usdOpenAssetIOResolver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolver.cpp
More file actions
282 lines (254 loc) · 10.4 KB
/
resolver.cpp
File metadata and controls
282 lines (254 loc) · 10.4 KB
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023 The Foundry Visionmongers Ltd
#include <stdexcept>
#include <pxr/base/tf/debug.h>
#include <pxr/base/tf/diagnostic.h>
#include <pxr/usd/ar/assetInfo.h>
#include <pxr/usd/ar/defaultResolver.h>
#include <pxr/usd/ar/defineResolver.h>
#include <openassetio/Context.hpp>
#include <openassetio/access.hpp>
#include <openassetio/hostApi/HostInterface.hpp>
#include <openassetio/hostApi/Manager.hpp>
#include <openassetio/hostApi/ManagerFactory.hpp>
#include <openassetio/log/LoggerInterface.hpp>
#include <openassetio/log/SeverityFilter.hpp>
#include <openassetio/pluginSystem/CppPluginSystemManagerImplementationFactory.hpp>
#include <openassetio/pluginSystem/HybridPluginSystemManagerImplementationFactory.hpp>
#include <openassetio/python/hostApi.hpp>
#include <openassetio/utils/path.hpp>
#include <openassetio_mediacreation/traits/content/LocatableContentTrait.hpp>
// NOLINTNEXTLINE
PXR_NAMESPACE_USING_DIRECTIVE
PXR_NAMESPACE_OPEN_SCOPE
TF_DEBUG_CODES(OPENASSETIO_RESOLVER)
PXR_NAMESPACE_CLOSE_SCOPE
namespace {
/*
* OpenAssetIO LoggerInterface implementation
*
* Converter logger from OpenAssetIO log framing to USD log outputs.
*/
class UsdOpenAssetIOResolverLogger final : public openassetio::log::LoggerInterface {
public:
void log(const Severity severity, const openassetio::Str &message) override {
switch (severity) {
case Severity::kCritical:
TF_ERROR(TfDiagnosticType::TF_DIAGNOSTIC_FATAL_ERROR_TYPE, message);
break;
case Severity::kDebug:
case Severity::kDebugApi:
TF_DEBUG(OPENASSETIO_RESOLVER).Msg(message + "\n");
break;
case Severity::kError:
TF_ERROR(TfDiagnosticType::TF_DIAGNOSTIC_NONFATAL_ERROR_TYPE, message);
break;
case Severity::kInfo:
case Severity::kProgress:
TF_INFO(OPENASSETIO_RESOLVER).Msg(message + "\n");
break;
case Severity::kWarning:
TF_WARN(TfDiagnosticType::TF_DIAGNOSTIC_WARNING_TYPE, message);
break;
}
}
};
/**
* OpenAssetIO HostInterface implementation
*
* This identifies the Ar2 plugin uniquely should the manager plugin
* wish to adapt its behaviour.
*/
class UsdOpenAssetIOHostInterface final : public openassetio::hostApi::HostInterface {
public:
[[nodiscard]] openassetio::Identifier identifier() const override {
return "org.openassetio.usdresolver";
}
[[nodiscard]] openassetio::Str displayName() const override {
return "OpenAssetIO USD Resolver";
}
};
} // namespace
// ---------------------------------------------------------------------
/*
* Ar Resolver Implementation
*/
class UsdOpenAssetIOResolver final : public PXR_NS::ArDefaultResolver {
public:
UsdOpenAssetIOResolver() {
logger_ =
openassetio::log::SeverityFilter::make(std::make_shared<UsdOpenAssetIOResolverLogger>());
// Python plugin system.
const auto pythonManagerImplementationFactory =
openassetio::python::hostApi::createPythonPluginSystemManagerImplementationFactory(
logger_);
// C++ plugin system.
const auto cppManagerImplementationFactory =
openassetio::pluginSystem::CppPluginSystemManagerImplementationFactory::make(logger_);
// Combined Python/C++ plugin system. Note: C++ is listed first so
// has priority.
const auto hybridManagerImplementationFactory =
openassetio::pluginSystem::HybridPluginSystemManagerImplementationFactory::make(
{cppManagerImplementationFactory, pythonManagerImplementationFactory}, logger_);
const auto hostInterface = std::make_shared<UsdOpenAssetIOHostInterface>();
// Find the plugin configured using the OPENASSETIO_DEFAULT_CONFIG
// file, initialise it, and create the host-facing Manager
// middleware for us to communicate with it.
manager_ = openassetio::hostApi::ManagerFactory::defaultManagerForInterface(
hostInterface, hybridManagerImplementationFactory, logger_);
if (!manager_) {
throw std::invalid_argument{
"No default manager configured, " +
openassetio::hostApi::ManagerFactory::kDefaultManagerConfigEnvVarName};
}
// This USD plugin is useless unless the manager plugin has the
// basic ability to `resolve(...)`.
if (!manager_->hasCapability(openassetio::hostApi::Manager::Capability::kResolution)) {
throw std::invalid_argument{manager_->displayName() +
" is not capable of resolving entity references"};
}
context_ = manager_->createContext();
}
~UsdOpenAssetIOResolver() override = default;
protected:
[[nodiscard]] std::string _CreateIdentifier(
const std::string &assetPath, const ArResolvedPath &anchorAssetPath) const override {
return catchAndLogExceptions(
[&] {
std::string identifier;
if (manager_->isEntityReferenceString(assetPath)) {
identifier = assetPath;
} else {
identifier = ArDefaultResolver::_CreateIdentifier(assetPath, anchorAssetPath);
}
return identifier;
},
TF_FUNC_NAME());
}
[[nodiscard]] ArResolvedPath _Resolve(const std::string &assetPath) const override {
return catchAndLogExceptions(
[&] {
if (const auto entityReference = manager_->createEntityReferenceIfValid(assetPath)) {
return ArResolvedPath{resolveToPath(*entityReference)};
}
return ArDefaultResolver::_Resolve(assetPath);
},
TF_FUNC_NAME());
}
[[nodiscard]] std::string _CreateIdentifierForNewAsset(
const std::string &assetPath, const ArResolvedPath &anchorAssetPath) const override {
if (manager_->isEntityReferenceString(assetPath)) {
std::string message = "Writes to OpenAssetIO entity references are not currently supported ";
message += assetPath;
logger_->critical(message);
return "";
}
return ArDefaultResolver::_CreateIdentifierForNewAsset(assetPath, anchorAssetPath);
}
[[nodiscard]] ArResolvedPath _ResolveForNewAsset(const std::string &assetPath) const override {
if (manager_->isEntityReferenceString(assetPath)) {
std::string message = "Writes to OpenAssetIO entity references are not currently supported ";
message += assetPath;
logger_->critical(message);
return ArResolvedPath("");
}
return ArDefaultResolver::_ResolveForNewAsset(assetPath);
}
[[nodiscard]] std::string _GetExtension(const std::string &assetPath) const override {
return ArDefaultResolver::_GetExtension(assetPath);
}
[[nodiscard]] ArAssetInfo _GetAssetInfo(const std::string &assetPath,
const ArResolvedPath &resolvedPath) const override {
return ArDefaultResolver::_GetAssetInfo(assetPath, resolvedPath);
}
[[nodiscard]] ArTimestamp _GetModificationTimestamp(
const std::string &assetPath, const ArResolvedPath &resolvedPath) const override {
return ArDefaultResolver::_GetModificationTimestamp(assetPath, resolvedPath);
}
[[nodiscard]] std::shared_ptr<ArAsset> _OpenAsset(
const ArResolvedPath &resolvedPath) const override {
return ArDefaultResolver::_OpenAsset(resolvedPath);
}
[[nodiscard]] bool _CanWriteAssetToPath(const ArResolvedPath &resolvedPath,
std::string *whyNot) const override {
return ArDefaultResolver::_CanWriteAssetToPath(resolvedPath, whyNot);
}
[[nodiscard]] std::shared_ptr<ArWritableAsset> _OpenAssetForWrite(
const ArResolvedPath &resolvedPath, const WriteMode writeMode) const override {
return ArDefaultResolver::_OpenAssetForWrite(resolvedPath, writeMode);
}
private:
/**
* Retrieve the resolved file path of an entity reference.
*
* This will resolve the "locateableContent" trait of the entity and
* return the "location" property of it, converted from a URL to a
* file path.
*
* @param entityReference The reference to resolve.
* @return Resolved file path.
*/
[[nodiscard]] std::string resolveToPath(
const openassetio::EntityReference &entityReference) const {
using openassetio::access::ResolveAccess;
using openassetio_mediacreation::traits::content::LocatableContentTrait;
// Resolve the locatable content trait, this will provide a URL
// that points to the final content
const openassetio::trait::TraitsDataPtr traitsData = manager_->resolve(
entityReference, {LocatableContentTrait::kId}, ResolveAccess::kRead, context_);
const std::optional<openassetio::Str> url = LocatableContentTrait(traitsData).getLocation();
if (!url) {
throw std::invalid_argument{"Entity reference does not have a location: " +
entityReference.toString()};
}
// OpenAssetIO is URL based, but we need a path. Note: will throw if
// the URL is not valid.
return fileUrlPathConverter_.pathFromUrl(*url);
}
/**
* Decorator to stop propagation of all exceptions.
*
* Exceptions occurring within the wrapped callable will be caught and
* logged, and a default-constructed value (of the same type as the
* callable's return type) returned instead of propagating the
* exception.
*
* The callable must take no arguments (but may return a value).
*
* This is needed since USD reacts badly if an exception escapes an Ar
* plugin (segfault, sigabrt).
*
* @tparam Fn Callable type.
* @param func Callable instance.
* @param name Name of function/process that caused the exception, to
* append to log messages.
* @return Result of callable if no exception occurred, otherwise a
* default-constructed object.
*/
template <typename Fn>
auto catchAndLogExceptions(Fn &&func, const std::string_view name) const
-> std::invoke_result_t<Fn> {
try {
return func();
} catch (const std::exception &exc) {
std::string msg = "OpenAssetIO error in ";
msg += name;
msg += ": ";
msg += exc.what();
logger_->critical(msg);
} catch (...) {
std::string msg = "OpenAssetIO error in ";
msg += name;
msg += ": unknown non-exception type caught";
logger_->critical(msg);
}
return {};
}
openassetio::log::LoggerInterfacePtr logger_;
openassetio::hostApi::ManagerPtr manager_;
openassetio::ContextConstPtr context_;
openassetio::utils::FileUrlPathConverter fileUrlPathConverter_;
};
PXR_NAMESPACE_OPEN_SCOPE
AR_DEFINE_RESOLVER(UsdOpenAssetIOResolver, ArResolver)
PXR_NAMESPACE_CLOSE_SCOPE