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