dynamicConsoleMethodComponent.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #include "component/dynamicConsoleMethodComponent.h"
  23. #include "dynamicConsoleMethodComponent_ScriptBinding.h"
  24. IMPLEMENT_CONOBJECT(DynamicConsoleMethodComponent);
  25. //-----------------------------------------------------------
  26. // Function name: SimComponent::handlesConsoleMethod
  27. // Summary:
  28. //-----------------------------------------------------------
  29. bool DynamicConsoleMethodComponent::handlesConsoleMethod( const char *fname, S32 *routingId )
  30. {
  31. // CodeReview: Host object is now given priority over components for method
  32. // redirection. [6/23/2007 Pat]
  33. // On this object?
  34. if( isMethod( fname ) )
  35. {
  36. *routingId = -1; // -1 denotes method on object
  37. return true;
  38. }
  39. // on this objects components?
  40. S32 nI = 0;
  41. VectorPtr<SimComponent*> &componentList = lockComponentList();
  42. for( SimComponentIterator nItr = componentList.begin(); nItr != componentList.end(); nItr++, nI++ )
  43. {
  44. SimObject *pComponent = dynamic_cast<SimObject*>(*nItr);
  45. if( pComponent != NULL && pComponent->isMethod( fname ) )
  46. {
  47. *routingId = -2; // -2 denotes method on component
  48. unlockComponentList();
  49. return true;
  50. }
  51. }
  52. unlockComponentList();
  53. return false;
  54. }
  55. const char *DynamicConsoleMethodComponent::callMethod( S32 argc, const char* methodName, ... )
  56. {
  57. const char *argv[128];
  58. methodName = StringTable->insert( methodName );
  59. argc++;
  60. va_list args;
  61. va_start(args, methodName);
  62. for(S32 i = 0; i < argc; i++)
  63. argv[i+2] = va_arg(args, const char *);
  64. va_end(args);
  65. // FIXME: the following seems a little excessive. I wonder why it's needed?
  66. argv[0] = methodName;
  67. argv[1] = methodName;
  68. argv[2] = methodName;
  69. return callMethodArgList( argc , argv );
  70. }
  71. const char* DynamicConsoleMethodComponent::callMethodArgList( U32 argc, const char *argv[], bool callThis /* = true */ )
  72. {
  73. return _callMethod( argc, argv, callThis );
  74. }
  75. // Call all components that implement methodName giving them a chance to operate
  76. // Components are called in reverse order of addition
  77. const char *DynamicConsoleMethodComponent::_callMethod( U32 argc, const char *argv[], bool callThis /* = true */ )
  78. {
  79. // Set Owner
  80. SimObject *pThis = dynamic_cast<SimObject *>( this );
  81. AssertFatal( pThis, "DynamicConsoleMethodComponent::callMethod : this should always exist!" );
  82. const char *cbName = StringTable->insert(argv[0]);
  83. if( getComponentCount() > 0 )
  84. {
  85. VectorPtr<SimComponent *>&componentList = lockComponentList();
  86. for( SimComponentIterator nItr = (componentList.end()-1); nItr >= componentList.begin(); nItr-- )
  87. {
  88. argv[0] = cbName;
  89. SimComponent *pComponent = (*nItr);
  90. AssertFatal( pComponent, "DynamicConsoleMethodComponent::callMethod - NULL component in list!" );
  91. DynamicConsoleMethodComponent *pThisComponent = dynamic_cast<DynamicConsoleMethodComponent*>( pComponent );
  92. AssertFatal( pThisComponent, "DynamicConsoleMethodComponent::callMethod - Non DynamicConsoleMethodComponent component attempting to callback!");
  93. // Only call on first depth components
  94. // Should isMethod check these calls? [11/22/2006 justind]
  95. if(pComponent->isEnabled())
  96. Con::execute( pThisComponent, argc, argv );
  97. // Bail if this was the first element
  98. //if( nItr == componentList.begin() )
  99. // break;
  100. }
  101. unlockComponentList();
  102. }
  103. // Set Owner Field
  104. const char* result = "";
  105. if(callThis)
  106. result = Con::execute( pThis, argc, argv, true ); // true - exec method onThisOnly, not on DCMCs
  107. return result;
  108. }
  109. const char *DynamicConsoleMethodComponent::callOnBehaviors( U32 argc, const char *argv[] )
  110. {
  111. // Set Owner
  112. SimObject *pThis;
  113. pThis = dynamic_cast<SimObject *>( this );
  114. AssertFatal( pThis, "DynamicConsoleMethodComponent::callOnBehaviors : this should always exist!" );
  115. const char* result = "";
  116. bool handled = false;
  117. if( getComponentCount() > 0 )
  118. {
  119. VectorPtr<SimComponent *>&componentList = lockComponentList();
  120. for( SimComponentIterator nItr = (componentList.end()-1); nItr >= componentList.begin(); nItr-- )
  121. {
  122. argv[0] = StringTable->insert(argv[0]);
  123. SimComponent *pComponent = (*nItr);
  124. AssertFatal( pComponent, "DynamicConsoleMethodComponent::callOnBehaviors - NULL component in list!" );
  125. handled = pComponent->callMethodOnComponents(argc, argv, &result);
  126. if (handled)
  127. break;
  128. }
  129. unlockComponentList();
  130. }
  131. if (!handled)
  132. {
  133. result = "ERR_CALL_NOT_HANDLED";
  134. }
  135. return result;
  136. }