1 //
2 // Copyright 2020 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #include "pxr/pxr.h"
25 
26 #include "TestArURIResolver_plugin.h"
27 
28 #include "pxr/usd/ar/defaultResolver.h"
29 #include "pxr/usd/ar/defineResolver.h"
30 #include "pxr/usd/ar/defineResolverContext.h"
31 
32 #include "pxr/base/tf/diagnosticLite.h"
33 #include "pxr/base/tf/pathUtils.h"
34 #include "pxr/base/tf/stringUtils.h"
35 #include "pxr/base/vt/value.h"
36 
37 #include <map>
38 #include <string>
39 #include <vector>
40 
41 PXR_NAMESPACE_USING_DIRECTIVE
42 
43 // Base class for test URI resolvers
44 class _TestURIResolverBase
45     : public ArResolver
46 {
47 protected:
_TestURIResolverBase(const std::string & uriScheme)48     _TestURIResolverBase(const std::string& uriScheme)
49         : _uriScheme(uriScheme + ":")
50     {
51     }
52 
_CreateIdentifier(const std::string & assetPath,const ArResolvedPath & anchorAssetPath) const53     std::string _CreateIdentifier(
54         const std::string& assetPath,
55         const ArResolvedPath& anchorAssetPath) const final
56     {
57         TF_AXIOM(
58             TfStringStartsWith(TfStringToLower(assetPath), _uriScheme) ||
59             TfStringStartsWith(TfStringToLower(anchorAssetPath), _uriScheme));
60         return assetPath;
61     }
62 
_CreateIdentifierForNewAsset(const std::string & assetPath,const ArResolvedPath & anchorAssetPath) const63     std::string _CreateIdentifierForNewAsset(
64         const std::string& assetPath,
65         const ArResolvedPath& anchorAssetPath) const final
66     {
67         TF_AXIOM(
68             TfStringStartsWith(TfStringToLower(assetPath), _uriScheme) ||
69             TfStringStartsWith(TfStringToLower(anchorAssetPath), _uriScheme));
70         return assetPath;
71     }
72 
_Resolve(const std::string & assetPath) const73     ArResolvedPath _Resolve(
74         const std::string& assetPath) const final
75     {
76         TF_AXIOM(TfStringStartsWith(TfStringToLower(assetPath), _uriScheme));
77 
78         const _TestURIResolverContext* uriContext = _GetCurrentContextPtr();
79         if (uriContext && !uriContext->data.empty()) {
80             return ArResolvedPath(assetPath + "?" + uriContext->data);
81         }
82 
83         return ArResolvedPath(assetPath);
84     }
85 
_ResolveForNewAsset(const std::string & assetPath) const86     ArResolvedPath _ResolveForNewAsset(
87         const std::string& assetPath) const final
88     {
89         return _Resolve(assetPath);
90     }
91 
_CreateDefaultContext() const92     ArResolverContext _CreateDefaultContext() const final
93     {
94         return ArResolverContext(
95             _TestURIResolverContext("CreateDefaultContext"));
96     }
97 
_CreateDefaultContextForAsset(const std::string & assetPath) const98     ArResolverContext _CreateDefaultContextForAsset(
99         const std::string& assetPath) const final
100     {
101         return ArResolverContext(
102             _TestURIResolverContext(TfAbsPath(assetPath)));
103     }
104 
_OpenAsset(const ArResolvedPath & resolvedPath) const105     std::shared_ptr<ArAsset> _OpenAsset(
106         const ArResolvedPath& resolvedPath) const final
107     {
108         TF_AXIOM(TfStringStartsWith(TfStringToLower(resolvedPath), _uriScheme));
109         return nullptr;
110     }
111 
_CreateContextFromString(const std::string & contextStr) const112     ArResolverContext _CreateContextFromString(
113         const std::string& contextStr) const final
114     {
115         return ArResolverContext(_TestURIResolverContext(contextStr));
116     };
117 
118     std::shared_ptr<ArWritableAsset>
_OpenAssetForWrite(const ArResolvedPath & resolvedPath,WriteMode writeMode) const119     _OpenAssetForWrite(
120         const ArResolvedPath& resolvedPath,
121         WriteMode writeMode) const final
122     {
123         TF_AXIOM(TfStringStartsWith(TfStringToLower(resolvedPath), _uriScheme));
124         return nullptr;
125     }
126 
127 private:
_GetCurrentContextPtr() const128     const _TestURIResolverContext* _GetCurrentContextPtr() const
129     {
130         return _GetCurrentContextObject<_TestURIResolverContext>();
131     }
132 
133     std::string _uriScheme;
134 };
135 
136 // Test resolver that handles asset paths of the form "test://...."
137 class _TestURIResolver
138     : public _TestURIResolverBase
139 {
140 public:
_TestURIResolver()141     _TestURIResolver()
142         : _TestURIResolverBase("test")
143     {
144     }
145 };
146 
147 // Test resolver that handles asset paths of the form "test_other://...."
148 class _TestOtherURIResolver
149     : public _TestURIResolverBase
150 {
151 public:
_TestOtherURIResolver()152     _TestOtherURIResolver()
153         : _TestURIResolverBase("test_other")
154     {
155     }
156 };
157 
158 // XXX: Should have a AR_DEFINE_ABSTRACT_RESOLVER macro like
159 // AR_DEFINE_ABSTRACT_RESOLVER(_TestURIResolverBase, ArResolver)
160 // to take care of this registration.
TF_REGISTRY_FUNCTION(TfType)161 TF_REGISTRY_FUNCTION(TfType)
162 {
163     TfType::Define<_TestURIResolverBase, TfType::Bases<ArResolver>>();
164 }
165 
166 AR_DEFINE_RESOLVER(_TestURIResolver, _TestURIResolverBase);
167 AR_DEFINE_RESOLVER(_TestOtherURIResolver, _TestURIResolverBase);
168