assetQuery.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. docsURL;
  39. // Call parent.
  40. Parent::initPersistFields();
  41. addProtectedField("Count", TypeS32, 0, &defaultProtectedSetFn, &getCount, &writeCount, "Gets the number of results in the asset query.");
  42. }
  43. //-----------------------------------------------------------------------------
  44. void AssetQuery::onTamlCustomWrite( TamlCustomNodes& customNodes )
  45. {
  46. // Call parent.
  47. Parent::onTamlCustomWrite( customNodes );
  48. // Add node.
  49. TamlCustomNode* pCustomNode = customNodes.addNode( ASSETQUERY_RESULTS_NODE_NAME );
  50. // Finish if no assets.
  51. if (mAssetList.size() == 0)
  52. return;
  53. // Iterate asset.
  54. for (Vector<StringTableEntry>::iterator assetItr = mAssetList.begin(); assetItr != mAssetList.end(); ++assetItr)
  55. {
  56. // Add asset node.
  57. TamlCustomNode* pAssetNode = pCustomNode->addNode( ASSETQUERY_ASSETNODE_NAME );
  58. // Add field.
  59. pAssetNode->addField( ASSETQUERY_ASSETID_FIELD_NAME, *assetItr );
  60. }
  61. }
  62. //-----------------------------------------------------------------------------
  63. void AssetQuery::onTamlCustomRead( const TamlCustomNodes& customNodes )
  64. {
  65. // Call parent.
  66. Parent::onTamlCustomRead( customNodes );
  67. // Find custom node name.
  68. const TamlCustomNode* pResultsNode = customNodes.findNode( ASSETQUERY_RESULTS_NODE_NAME );
  69. // Finish if we don't have a results name.
  70. if ( pResultsNode == NULL )
  71. return;
  72. // Fetch node name.
  73. StringTableEntry assetNodeName = StringTable->insert( ASSETQUERY_ASSETNODE_NAME );
  74. // Fetch children asset nodes.
  75. const TamlCustomNodeVector& assetNodes = pResultsNode->getChildren();
  76. // Iterate asset nodes.
  77. for( TamlCustomNodeVector::const_iterator assetNodeItr = assetNodes.begin(); assetNodeItr != assetNodes.end(); ++assetNodeItr )
  78. {
  79. // Fetch asset node.
  80. const TamlCustomNode* pAssetNode = *assetNodeItr;
  81. // Skip if an unknown node name.
  82. if ( pAssetNode->getNodeName() != assetNodeName )
  83. continue;
  84. // Fetch field.
  85. const TamlCustomField* pField = pAssetNode->findField( ASSETQUERY_ASSETID_FIELD_NAME );
  86. // Do we find the field?
  87. if ( pField == NULL )
  88. {
  89. // No, so warn.
  90. Con::warnf( "AssetQuery::onTamlCustomRead() - Could not find '%s' field.", ASSETQUERY_ASSETID_FIELD_NAME );
  91. continue;
  92. }
  93. // Store asset.
  94. mAssetList.push_back(pField->getFieldValue());
  95. }
  96. }