simEvents.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "console/console.h"
  23. #include "console/consoleInternal.h"
  24. #include "platform/threads/semaphore.h"
  25. #include "console/simEvents.h"
  26. // Stupid globals not declared in a header
  27. extern ExprEvalState gEvalState;
  28. SimConsoleEvent::SimConsoleEvent(S32 argc, ConsoleValueRef *argv, bool onObject)
  29. {
  30. mOnObject = onObject;
  31. mArgc = argc;
  32. mArgv = new ConsoleValueRef[argc];
  33. for (int i=0; i<argc; i++) {
  34. mArgv[i].value = new ConsoleValue();
  35. mArgv[i].value->type = ConsoleValue::TypeInternalString;
  36. mArgv[i].value->init();
  37. mArgv[i].value->setStringValue((const char*)argv[i]);
  38. }
  39. }
  40. SimConsoleEvent::~SimConsoleEvent()
  41. {
  42. for (int i=0; i<mArgc; i++) {
  43. delete mArgv[i].value;
  44. }
  45. delete[] mArgv;
  46. }
  47. void SimConsoleEvent::process(SimObject* object)
  48. {
  49. // #ifdef DEBUG
  50. // Con::printf("Executing schedule: %d", sequenceCount);
  51. // #endif
  52. if(mOnObject)
  53. Con::execute(object, mArgc, mArgv );
  54. else
  55. {
  56. // Grab the function name. If '::' doesn't exist, then the schedule is
  57. // on a global function.
  58. char funcName[256];
  59. dStrncpy(funcName, (const char*)mArgv[0], 256);
  60. char* func = dStrstr( funcName, (char*)"::" );
  61. if( func )
  62. {
  63. // Set the first colon to NULL, so we can reference the namespace.
  64. // This is okay because events are deleted immediately after
  65. // processing. Maybe a bad idea anyway?
  66. func[0] = '\0';
  67. // Move the pointer forward to the function name.
  68. func += 2;
  69. // Lookup the namespace and function entry.
  70. Namespace* ns = Namespace::find( StringTable->insert( funcName ) );
  71. if( ns )
  72. {
  73. Namespace::Entry* nse = ns->lookup( StringTable->insert( func ) );
  74. if( nse )
  75. // Execute.
  76. nse->execute( mArgc, mArgv, &gEvalState );
  77. }
  78. }
  79. else
  80. Con::execute(mArgc, mArgv );
  81. }
  82. }
  83. //-----------------------------------------------------------------------------
  84. SimConsoleThreadExecCallback::SimConsoleThreadExecCallback() : retVal(NULL)
  85. {
  86. sem = new Semaphore(0);
  87. }
  88. SimConsoleThreadExecCallback::~SimConsoleThreadExecCallback()
  89. {
  90. delete sem;
  91. }
  92. void SimConsoleThreadExecCallback::handleCallback(const char *ret)
  93. {
  94. retVal = ret;
  95. sem->release();
  96. }
  97. const char *SimConsoleThreadExecCallback::waitForResult()
  98. {
  99. if(sem->acquire(true))
  100. {
  101. return retVal;
  102. }
  103. return NULL;
  104. }
  105. //-----------------------------------------------------------------------------
  106. SimConsoleThreadExecEvent::SimConsoleThreadExecEvent(S32 argc, ConsoleValueRef *argv, bool onObject, SimConsoleThreadExecCallback *callback) :
  107. SimConsoleEvent(argc, argv, onObject), cb(callback)
  108. {
  109. }
  110. void SimConsoleThreadExecEvent::process(SimObject* object)
  111. {
  112. const char *retVal;
  113. if(mOnObject)
  114. retVal = Con::execute(object, mArgc, mArgv);
  115. else
  116. retVal = Con::execute(mArgc, mArgv);
  117. if(cb)
  118. cb->handleCallback(retVal);
  119. }