simEvents.cpp 4.2 KB

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