simEvents.cpp 4.3 KB

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