assetQuery.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _ASSET_QUERY_H_
  23. #include "assetQuery.h"
  24. #endif
  25. #ifndef _CONSOLETYPES_H_
  26. #include "console/consoleTypes.h"
  27. #endif
  28. #ifndef _TAML_CUSTOM_H_
  29. #include "persistence/taml/tamlCustom.h"
  30. #endif
  31. // Script bindings.
  32. #include "assetQuery_ScriptBinding.h"
  33. //-----------------------------------------------------------------------------
  34. IMPLEMENT_CONOBJECT( AssetQuery );
  35. //-----------------------------------------------------------------------------
  36. void AssetQuery::initPersistFields()
  37. {
  38. // Call parent.
  39. Parent::initPersistFields();
  40. addProtectedField( "Count", TypeS32, 0, &defaultProtectedNotSetFn, &getCount, &writeCount, "Gets the number of results in the asset query." );
  41. }
  42. //-----------------------------------------------------------------------------
  43. void AssetQuery::onTamlCustomWrite( TamlCustomProperties& customProperties )
  44. {
  45. // Call parent.
  46. Parent::onTamlCustomWrite( customProperties );
  47. // Add property.
  48. TamlCustomProperty* pProperty = customProperties.addProperty( ASSETQUERY_CUSTO_PROPERTY_NAME );
  49. // Finish if no assets.
  50. if ( size() == 0 )
  51. return;
  52. // Iterate asset.
  53. for( Vector<StringTableEntry>::iterator assetItr = begin(); assetItr != end(); ++assetItr )
  54. {
  55. // Add alias.
  56. TamlPropertyAlias* pAlias = pProperty->addAlias( ASSETQUERY_TYPE_NAME );
  57. // Add fields.
  58. pAlias->addField( ASSETQUERY_ASSETID_FIELD_NAME, *assetItr );
  59. }
  60. }
  61. //-----------------------------------------------------------------------------
  62. void AssetQuery::onTamlCustomRead( const TamlCustomProperties& customProperties )
  63. {
  64. // Call parent.
  65. Parent::onTamlCustomRead( customProperties );
  66. // Find custom property name.
  67. const TamlCustomProperty* pProperty = customProperties.findProperty( ASSETQUERY_CUSTO_PROPERTY_NAME );
  68. // Finish if we don't have a property name.
  69. if ( pProperty == NULL )
  70. return;
  71. // Fetch alias name.
  72. StringTableEntry typeAliasName = StringTable->insert( ASSETQUERY_TYPE_NAME );
  73. // Iterate property alias.
  74. for( TamlCustomProperty::const_iterator propertyAliasItr = pProperty->begin(); propertyAliasItr != pProperty->end(); ++propertyAliasItr )
  75. {
  76. // Fetch property alias.
  77. const TamlPropertyAlias* pAlias = *propertyAliasItr;
  78. // Skip if an unknown alias name.
  79. if ( pAlias->mAliasName != typeAliasName )
  80. continue;
  81. // Fetch field.
  82. const TamlPropertyField* pField = pAlias->findField( ASSETQUERY_ASSETID_FIELD_NAME );
  83. // Do we find the field?
  84. if ( pField == NULL )
  85. {
  86. // No, so warn.
  87. Con::warnf( "AssetQuery::onTamlCustomRead() - Could not find '%s' field.", ASSETQUERY_ASSETID_FIELD_NAME );
  88. continue;
  89. }
  90. // Store asset.
  91. push_back( pField->getFieldValue() );
  92. }
  93. }