assetQuery.cpp 4.1 KB

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