2012-09-19 15:15:01 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// Copyright (c) 2012 GarageGames, LLC
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to
|
|
|
|
|
// deal in the Software without restriction, including without limitation the
|
|
|
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
|
// IN THE SOFTWARE.
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
#include "console/console.h"
|
|
|
|
|
#include "console/consoleInternal.h"
|
|
|
|
|
#include "platform/threads/semaphore.h"
|
|
|
|
|
#include "console/simEvents.h"
|
|
|
|
|
|
|
|
|
|
// Stupid globals not declared in a header
|
|
|
|
|
extern ExprEvalState gEvalState;
|
|
|
|
|
|
2021-04-01 01:09:23 +00:00
|
|
|
SimConsoleEvent::SimConsoleEvent(S32 argc, ConsoleValue *argv, bool onObject)
|
2012-09-19 15:15:01 +00:00
|
|
|
{
|
|
|
|
|
mOnObject = onObject;
|
|
|
|
|
mArgc = argc;
|
|
|
|
|
|
2021-04-01 01:09:23 +00:00
|
|
|
mArgv = new ConsoleValue[argc];
|
2012-10-11 20:29:39 +00:00
|
|
|
for (int i=0; i<argc; i++)
|
|
|
|
|
{
|
2021-04-01 01:09:23 +00:00
|
|
|
if (argv)
|
|
|
|
|
{
|
|
|
|
|
mArgv->setString(argv[i].getString(), dStrlen(argv[i].getString()));
|
|
|
|
|
}
|
2012-09-19 15:15:01 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SimConsoleEvent::~SimConsoleEvent()
|
|
|
|
|
{
|
2012-09-23 08:59:48 +00:00
|
|
|
delete[] mArgv;
|
2012-09-19 15:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimConsoleEvent::process(SimObject* object)
|
|
|
|
|
{
|
|
|
|
|
// #ifdef DEBUG
|
|
|
|
|
// Con::printf("Executing schedule: %d", sequenceCount);
|
|
|
|
|
// #endif
|
|
|
|
|
if(mOnObject)
|
2012-09-23 08:59:48 +00:00
|
|
|
Con::execute(object, mArgc, mArgv );
|
2012-09-19 15:15:01 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Grab the function name. If '::' doesn't exist, then the schedule is
|
|
|
|
|
// on a global function.
|
2012-09-23 08:59:48 +00:00
|
|
|
char funcName[256];
|
2021-04-01 01:09:23 +00:00
|
|
|
dStrncpy(funcName, mArgv[0].getString(), 256);
|
2012-09-23 08:59:48 +00:00
|
|
|
char* func = dStrstr( funcName, (char*)"::" );
|
2012-09-19 15:15:01 +00:00
|
|
|
if( func )
|
|
|
|
|
{
|
|
|
|
|
// Set the first colon to NULL, so we can reference the namespace.
|
|
|
|
|
// This is okay because events are deleted immediately after
|
|
|
|
|
// processing. Maybe a bad idea anyway?
|
|
|
|
|
func[0] = '\0';
|
|
|
|
|
|
|
|
|
|
// Move the pointer forward to the function name.
|
|
|
|
|
func += 2;
|
|
|
|
|
|
|
|
|
|
// Lookup the namespace and function entry.
|
2012-09-23 08:59:48 +00:00
|
|
|
Namespace* ns = Namespace::find( StringTable->insert( funcName ) );
|
2012-09-19 15:15:01 +00:00
|
|
|
if( ns )
|
|
|
|
|
{
|
|
|
|
|
Namespace::Entry* nse = ns->lookup( StringTable->insert( func ) );
|
|
|
|
|
if( nse )
|
|
|
|
|
// Execute.
|
2012-09-23 08:59:48 +00:00
|
|
|
nse->execute( mArgc, mArgv, &gEvalState );
|
2012-09-19 15:15:01 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
2012-09-23 08:59:48 +00:00
|
|
|
Con::execute(mArgc, mArgv );
|
2012-09-19 15:15:01 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 01:09:23 +00:00
|
|
|
void SimConsoleEvent::populateArgs(ConsoleValue *argv)
|
2015-02-07 22:41:54 +00:00
|
|
|
{
|
|
|
|
|
for (U32 i=0; i<mArgc; i++)
|
|
|
|
|
{
|
2021-04-01 01:09:23 +00:00
|
|
|
argv[i].setString(mArgv[i].getString(), dStrlen(mArgv[i].getString()));
|
2015-02-07 22:41:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-19 15:15:01 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2015-02-07 22:41:54 +00:00
|
|
|
SimConsoleThreadExecCallback::SimConsoleThreadExecCallback()
|
2012-09-19 15:15:01 +00:00
|
|
|
{
|
|
|
|
|
sem = new Semaphore(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SimConsoleThreadExecCallback::~SimConsoleThreadExecCallback()
|
|
|
|
|
{
|
|
|
|
|
delete sem;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 01:09:23 +00:00
|
|
|
void SimConsoleThreadExecCallback::handleCallback(ConsoleValue ret)
|
2012-09-19 15:15:01 +00:00
|
|
|
{
|
2021-04-01 01:09:23 +00:00
|
|
|
// can we move this pls?
|
|
|
|
|
retVal.setString(ret.getString(), dStrlen(ret.getString()));
|
2012-09-19 15:15:01 +00:00
|
|
|
sem->release();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 01:09:23 +00:00
|
|
|
ConsoleValue SimConsoleThreadExecCallback::waitForResult()
|
2012-09-19 15:15:01 +00:00
|
|
|
{
|
|
|
|
|
if(sem->acquire(true))
|
|
|
|
|
{
|
2021-04-01 01:09:23 +00:00
|
|
|
return std::move(retVal);
|
2012-09-19 15:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-01 01:09:23 +00:00
|
|
|
return ConsoleValue();
|
2012-09-19 15:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2021-04-01 01:09:23 +00:00
|
|
|
SimConsoleThreadExecEvent::SimConsoleThreadExecEvent(S32 argc, ConsoleValue *argv, bool onObject, SimConsoleThreadExecCallback *callback) :
|
2012-09-19 15:15:01 +00:00
|
|
|
SimConsoleEvent(argc, argv, onObject), cb(callback)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimConsoleThreadExecEvent::process(SimObject* object)
|
|
|
|
|
{
|
2021-04-01 01:09:23 +00:00
|
|
|
if (cb)
|
|
|
|
|
{
|
|
|
|
|
if (mOnObject)
|
|
|
|
|
cb->handleCallback(std::move(Con::execute(object, mArgc, mArgv)));
|
|
|
|
|
else
|
|
|
|
|
cb->handleCallback(std::move(Con::execute(mArgc, mArgv)));
|
|
|
|
|
}
|
2012-09-19 15:15:01 +00:00
|
|
|
else
|
2021-04-01 01:09:23 +00:00
|
|
|
{
|
|
|
|
|
if (mOnObject)
|
|
|
|
|
Con::execute(object, mArgc, mArgv);
|
|
|
|
|
else
|
|
|
|
|
Con::execute(mArgc, mArgv);
|
|
|
|
|
}
|
2012-09-19 15:15:01 +00:00
|
|
|
}
|