aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/common/include/ApexAuthorableObject.h
blob: d2fa7262e9f1f06a7ce2f43b1a9ce1b5f498c89e (plain) (blame)
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
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of NVIDIA CORPORATION nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2018 NVIDIA Corporation. All rights reserved.



#ifndef __APEXAUTHORABLEOBJECT_H__
#define __APEXAUTHORABLEOBJECT_H__

#include "Asset.h"

#include "AuthorableObjectIntl.h"
#include "nvparameterized/NvParameterizedTraits.h"
#include "nvparameterized/NvParameterized.h"

namespace nvidia
{
namespace apex
{

class ResourceList;

/**
 *	ApexAuthorableObject
 *	This class is the implementation for AuthorableObjectIntl (except for the
 *	ApexResource stuff).  It's disappointing that it has to be templated like
 *	this, but there were issues with multiple inheritance (an Resource ptr
 *	cannot be cast to an Asset ptr - Asset should inherit from Resource
 *	in the future.
 *
 *	Template expectations:
 *	T_Module -	must inherit from ModuleIntl
 *				the T_Asset type typically uses T_Module->mSdk
 *
 *	T_Asset -	T_Asset( T_Module *m, ResourceList &list, const char *name )
 *				must inherit from Asset
 *
 *	T_AssetAuthoring -	T_AssetAuthoring( T_Module *m, ResourceList &list )
 *						must inherit from AssetAuthoring
 */

template <class T_Module, class T_Asset, class T_AssetAuthoring>
class ApexAuthorableObject : public AuthorableObjectIntl
{
public:
	ApexAuthorableObject(ModuleIntl* m, ResourceList& list)
		:	AuthorableObjectIntl(m, list, T_Asset::getClassName())
	{
		// Register the authorable object type name in the NRP
		mAOResID = GetInternalApexSDK()->getInternalResourceProvider()->createNameSpace(mAOTypeName.c_str());
		mAOPtrResID = GetInternalApexSDK()->registerAuthObjType(mAOTypeName.c_str(), this);

		PX_ASSERT(!"This constructor is no longer valid, you MUST provide a parameterizedName!");
	}

	// This constructor is for assets that are based on NvParameterized, they provide the string
	// defined in the NvParameterized structure.  This will be used to map the NvParameterized object
	// to the AuthorableObject class to create the assets after they are deserialized
	ApexAuthorableObject(ModuleIntl* m, ResourceList& list, const char* parameterizedName)
		:	AuthorableObjectIntl(m, list, T_Asset::getClassName())
	{
		mParameterizedName = parameterizedName;

		// Register the authorable object type name in the NRP
		mAOResID = GetInternalApexSDK()->getInternalResourceProvider()->createNameSpace(mAOTypeName.c_str());
		mAOPtrResID = GetInternalApexSDK()->registerAuthObjType(mAOTypeName.c_str(), this);

		// Register the parameterized name in the NRP to point to this authorable object
		GetInternalApexSDK()->registerNvParamAuthType(mParameterizedName.c_str(), this);
	}

	virtual Asset* 			createAsset(AssetAuthoring& author, const char* name);
	virtual Asset* 			createAsset(NvParameterized::Interface* params, const char* name);
	virtual void					releaseAsset(Asset& nxasset);

	virtual AssetAuthoring* 	createAssetAuthoring();
	virtual AssetAuthoring* 	createAssetAuthoring(const char* name);
	virtual AssetAuthoring* 	createAssetAuthoring(NvParameterized::Interface* params, const char* name);
	virtual void					releaseAssetAuthoring(AssetAuthoring& nxauthor);

	virtual uint32_t					forceLoadAssets();

	virtual uint32_t					getAssetCount()
	{
		return mAssets.getSize();
	}
	virtual bool					getAssetList(Asset** outAssets, uint32_t& outAssetCount, uint32_t inAssetCount);

	virtual ResID					getResID()
	{
		return mAOResID;
	}

	virtual ApexSimpleString&		getName()
	{
		return mAOTypeName;
	}

	// Resource methods
	virtual void					release();

	virtual void					destroy()
	{
		delete this;
	}

};

template <class T_Module, class T_Asset, class T_AssetAuthoring>
Asset* ApexAuthorableObject<T_Module, T_Asset, T_AssetAuthoring>::
createAsset(AssetAuthoring& author, const char* name)
{
	if (mParameterizedName.len())
	{
		NvParameterized::Interface* params = 0;
		NvParameterized::Traits* traits = GetInternalApexSDK()->getParameterizedTraits();
		params = traits->createNvParameterized(mParameterizedName.c_str());
		PX_ASSERT(params);
		if (params)
		{
			NvParameterized::Interface* authorParams = author.getNvParameterized();
			PX_ASSERT(authorParams);
			if (authorParams)
			{
				if (NvParameterized::ERROR_NONE != authorParams->callPreSerializeCallback())
				{
					return NULL;
				}

				NvParameterized::ErrorType err = params->copy(*authorParams);

				PX_ASSERT(err == NvParameterized::ERROR_NONE);

				if (err == NvParameterized::ERROR_NONE)
				{
					return createAsset(params, name);
				}
				else
				{
					return NULL;
				}
			}
			else
			{
				return NULL;
			}
		}
		else
		{
			return NULL;
		}
	}
	else
	{
		APEX_INVALID_OPERATION("Authorable Asset needs a parameterized name");
		return NULL;
	}
}

template <class T_Module, class T_Asset, class T_AssetAuthoring>
void ApexAuthorableObject<T_Module, T_Asset, T_AssetAuthoring>::
release()
{
	// test this by releasing the module before the individual assets

	// remove all assets that we loaded (must do now else we cannot unregister)
	mAssets.clear();
	mAssetAuthors.clear();

	// remove this AO's name from the authorable namespace
	GetInternalApexSDK()->unregisterAuthObjType(mAOTypeName.c_str());

	if (mParameterizedName.len())
	{
		GetInternalApexSDK()->unregisterNvParamAuthType(mParameterizedName.c_str());
	}
	destroy();
}

template <class T_Module, class T_Asset, class T_AssetAuthoring>
Asset* ApexAuthorableObject<T_Module, T_Asset, T_AssetAuthoring>::
createAsset(NvParameterized::Interface* params, const char* name)
{
	T_Asset* asset = PX_NEW(T_Asset)(DYNAMIC_CAST(T_Module*)(mModule), mAssets, params, name);
	if (asset)
	{
		GetInternalApexSDK()->getNamedResourceProvider()->setResource(mAOTypeName.c_str(), name, asset);
	}
	return asset;
}

template <class T_Module, class T_Asset, class T_AssetAuthoring>
void ApexAuthorableObject<T_Module, T_Asset, T_AssetAuthoring>::
releaseAsset(Asset& nxasset)
{
	T_Asset* asset = DYNAMIC_CAST(T_Asset*)(&nxasset);

	GetInternalApexSDK()->getInternalResourceProvider()->setResource(mAOTypeName.c_str(), nxasset.getName(), NULL, false, false);
	asset->destroy();
}

#ifdef WITHOUT_APEX_AUTHORING

// this should no longer be called now that we're auto-assigning names in createAssetAuthoring()
template <class T_Module, class T_Asset, class T_AssetAuthoring>
AssetAuthoring* ApexAuthorableObject<T_Module, T_Asset, T_AssetAuthoring>::
createAssetAuthoring()
{
	APEX_INVALID_OPERATION("Asset authoring has been disabled");
	return NULL;
}

template <class T_Module, class T_Asset, class T_AssetAuthoring>
AssetAuthoring* ApexAuthorableObject<T_Module, T_Asset, T_AssetAuthoring>::
createAssetAuthoring(const char*)
{
	APEX_INVALID_OPERATION("Asset authoring has been disabled");
	return NULL;
}

template <class T_Module, class T_Asset, class T_AssetAuthoring>
AssetAuthoring* ApexAuthorableObject<T_Module, T_Asset, T_AssetAuthoring>::
createAssetAuthoring(NvParameterized::Interface*, const char*)
{
	APEX_INVALID_OPERATION("Asset authoring has been disabled");
	return NULL;
}

#else // WITHOUT_APEX_AUTHORING

// this should no longer be called now that we're auto-assigning names in createAssetAuthoring()
template <class T_Module, class T_Asset, class T_AssetAuthoring>
AssetAuthoring* ApexAuthorableObject<T_Module, T_Asset, T_AssetAuthoring>::
createAssetAuthoring()
{
	return PX_NEW(T_AssetAuthoring)(DYNAMIC_CAST(T_Module*)(mModule), mAssetAuthors);
}

template <class T_Module, class T_Asset, class T_AssetAuthoring>
AssetAuthoring* ApexAuthorableObject<T_Module, T_Asset, T_AssetAuthoring>::
createAssetAuthoring(const char* name)
{
	T_AssetAuthoring* assetAuthor = PX_NEW(T_AssetAuthoring)(DYNAMIC_CAST(T_Module*)(mModule), mAssetAuthors, name);

	if (assetAuthor)
	{
		GetInternalApexSDK()->getNamedResourceProvider()->setResource(mAOTypeName.c_str(), name, assetAuthor);
	}
	return assetAuthor;
}

template <class T_Module, class T_Asset, class T_AssetAuthoring>
AssetAuthoring* ApexAuthorableObject<T_Module, T_Asset, T_AssetAuthoring>::
createAssetAuthoring(NvParameterized::Interface* params, const char* name)
{
	T_AssetAuthoring* assetAuthor = PX_NEW(T_AssetAuthoring)(DYNAMIC_CAST(T_Module*)(mModule), mAssetAuthors, params, name);

	if (assetAuthor)
	{
		GetInternalApexSDK()->getNamedResourceProvider()->setResource(mAOTypeName.c_str(), name, assetAuthor);
	}
	return assetAuthor;
}

#endif // WITHOUT_APEX_AUTHORING

template <class T_Module, class T_Asset, class T_AssetAuthoring>
void ApexAuthorableObject<T_Module, T_Asset, T_AssetAuthoring>::
releaseAssetAuthoring(AssetAuthoring& nxauthor)
{
	T_AssetAuthoring* author = DYNAMIC_CAST(T_AssetAuthoring*)(&nxauthor);

	GetInternalApexSDK()->getInternalResourceProvider()->setResource(mAOTypeName.c_str(), nxauthor.getName(), NULL, false, false);
	author->destroy();
}

template <class T_Module, class T_Asset, class T_AssetAuthoring>
uint32_t ApexAuthorableObject<T_Module, T_Asset, T_AssetAuthoring>::
forceLoadAssets()
{
	uint32_t loadedAssetCount = 0;

	for (uint32_t i = 0; i < mAssets.getSize(); i++)
	{
		T_Asset* asset = DYNAMIC_CAST(T_Asset*)(mAssets.getResource(i));
		loadedAssetCount += asset->forceLoadAssets();
	}
	return loadedAssetCount;
}

template <class T_Module, class T_Asset, class T_AssetAuthoring>
bool ApexAuthorableObject<T_Module, T_Asset, T_AssetAuthoring>::
getAssetList(Asset** outAssets, uint32_t& outAssetCount, uint32_t inAssetCount)
{
	PX_ASSERT(outAssets);
	PX_ASSERT(inAssetCount >= mAssets.getSize());

	if (!outAssets || inAssetCount < mAssets.getSize())
	{
		outAssetCount = 0;
		return false;
	}

	outAssetCount = mAssets.getSize();
	for (uint32_t i = 0; i < mAssets.getSize(); i++)
	{
		T_Asset* asset = DYNAMIC_CAST(T_Asset*)(mAssets.getResource(i));
		outAssets[i] = static_cast<Asset*>(asset);
	}

	return true;
}

}
} // end namespace nvidia::apex

#endif	// __APEXAUTHORABLEOBJECT_H__