2
0

simEvents.cpp 4.2 KB

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