simConsoleEvent.cc 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "sim/simConsoleEvent.h"
  23. #include "platform/platform.h"
  24. #include "console/consoleInternal.h"
  25. //-----------------------------------------------------------------------------
  26. extern ExprEvalState gEvalState;
  27. //-----------------------------------------------------------------------------
  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. }