simEvents.cpp

Engine/source/console/simEvents.cpp

More...

Public Variables

Detailed Description

Public Variables

ExprEvalState gEvalState 
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "console/console.h"
 25#include "console/consoleInternal.h"
 26#include "platform/threads/semaphore.h"
 27#include "console/simEvents.h"
 28
 29// Stupid globals not declared in a header
 30extern ExprEvalState gEvalState;
 31
 32SimConsoleEvent::SimConsoleEvent(S32 argc, ConsoleValueRef *argv, bool onObject)
 33{
 34   mOnObject = onObject;
 35   mArgc = argc;
 36
 37   mArgv = new ConsoleValueRef[argc];
 38   for (int i=0; i<argc; i++)
 39   {
 40      mArgv[i].value = new ConsoleValue();
 41      mArgv[i].value->type = ConsoleValue::TypeInternalString;
 42      mArgv[i].value->init();
 43     if (argv)
 44     {
 45      mArgv[i].value->setStringValue((const char*)argv[i]);
 46     }
 47   }
 48}
 49
 50SimConsoleEvent::~SimConsoleEvent()
 51{
 52   for (int i=0; i<mArgc; i++)
 53   {
 54      delete mArgv[i].value;
 55   }
 56   delete[] mArgv;
 57}
 58
 59void SimConsoleEvent::process(SimObject* object)
 60{
 61   // #ifdef DEBUG
 62   //    Con::printf("Executing schedule: %d", sequenceCount);
 63   // #endif
 64   if(mOnObject)
 65      Con::execute(object, mArgc, mArgv );
 66   else
 67   {
 68      // Grab the function name. If '::' doesn't exist, then the schedule is
 69      // on a global function.
 70      char funcName[256];
 71      dStrncpy(funcName, (const char*)mArgv[0], 256);
 72      char* func = dStrstr( funcName, (char*)"::" );
 73      if( func )
 74      {
 75         // Set the first colon to NULL, so we can reference the namespace.
 76         // This is okay because events are deleted immediately after
 77         // processing. Maybe a bad idea anyway?
 78         func[0] = '\0';
 79
 80         // Move the pointer forward to the function name.
 81         func += 2;
 82
 83         // Lookup the namespace and function entry.
 84         Namespace* ns = Namespace::find( StringTable->insert( funcName ) );
 85         if( ns )
 86         {
 87            Namespace::Entry* nse = ns->lookup( StringTable->insert( func ) );
 88            if( nse )
 89               // Execute.
 90               nse->execute( mArgc, mArgv, &gEvalState );
 91         }
 92      }
 93
 94      else
 95         Con::execute(mArgc, mArgv );
 96   }
 97}
 98
 99void SimConsoleEvent::populateArgs(ConsoleValueRef *argv)
100{
101   for (U32 i=0; i<mArgc; i++)
102   {
103      argv[i].value = mArgv[i].value;
104   }
105}
106
107//-----------------------------------------------------------------------------
108
109SimConsoleThreadExecCallback::SimConsoleThreadExecCallback()
110{
111   retVal.value = NULL;
112   sem = new Semaphore(0);
113}
114
115SimConsoleThreadExecCallback::~SimConsoleThreadExecCallback()
116{
117   delete sem;
118}
119
120void SimConsoleThreadExecCallback::handleCallback(ConsoleValueRef ret)
121{
122   retVal = ret;
123   sem->release();
124}
125
126ConsoleValueRef SimConsoleThreadExecCallback::waitForResult()
127{
128   if(sem->acquire(true))
129   {
130      return retVal;
131   }
132
133   return ConsoleValueRef();
134}
135
136//-----------------------------------------------------------------------------
137
138SimConsoleThreadExecEvent::SimConsoleThreadExecEvent(S32 argc, ConsoleValueRef *argv, bool onObject, SimConsoleThreadExecCallback *callback) :
139   SimConsoleEvent(argc, argv, onObject), cb(callback)
140{
141}
142
143void SimConsoleThreadExecEvent::process(SimObject* object)
144{
145   ConsoleValueRef retVal;
146   if(mOnObject)
147      retVal = Con::execute(object, mArgc, mArgv);
148   else
149      retVal = Con::execute(mArgc, mArgv);
150
151   if(cb)
152      cb->handleCallback(retVal);
153}
154