simEvents.cpp 4.4 KB

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