dynamicConsoleMethodComponent.cpp 6.8 KB

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