diff --git a/source/simulation/EventHandlers.h b/source/simulation/EventHandlers.h new file mode 100755 index 0000000000..21d4318660 --- /dev/null +++ b/source/simulation/EventHandlers.h @@ -0,0 +1,21 @@ +// List of event handlers (for entities) the engine will call in to. +// Using integer tags should be ever-so-slightly faster than the hashmap lookup +// Also allows events to be renamed without affecting other code. + +#ifndef EVENT_HANDLERS_INCLUDED +#define EVENT_HANDLERS_INCLUDED + +enum EEventType +{ + EVENT_INITIALIZE, + EVENT_TICK, + EVENT_LAST, +}; + +static const wchar_t* EventNames[] = +{ + /* EVENT_INITIALIZE */ L"onInitialize", + /* EVENT_TICK */ L"onTick" +}; + +#endif \ No newline at end of file diff --git a/source/simulation/ScriptObject.cpp b/source/simulation/ScriptObject.cpp new file mode 100755 index 0000000000..154e087679 --- /dev/null +++ b/source/simulation/ScriptObject.cpp @@ -0,0 +1,100 @@ +#include "ScriptObject.h" +#include "Entity.h" + +CScriptObject::CScriptObject() +{ + Type = UNDEFINED; + Function = NULL; + Script = NULL; +} + +CScriptObject::CScriptObject( JSFunction* _Function ) +{ + SetFunction( _Function ); +} + +CScriptObject::CScriptObject( JSScript* _Script ) +{ + SetScript( _Script ); +} + +CScriptObject::CScriptObject( jsval v ) +{ + SetJSVal( v ); +} + +void CScriptObject::SetFunction( JSFunction* _Function ) +{ + Type = FUNCTION; + Function = _Function; + Script = NULL; +} + +void CScriptObject::SetScript( JSScript* _Script ) +{ + Type = SCRIPT; + Function = NULL; + Script = _Script; +} + +void CScriptObject::SetJSVal( jsval v ) +{ + switch( JS_TypeOfValue( g_ScriptingHost.GetContext(), v ) ) + { + case JSTYPE_STRING: + { + CStrW Source = g_ScriptingHost.ValueToUCString( v ); + JSScript* Script = JS_CompileUCScript( g_ScriptingHost.GetContext(), JS_GetGlobalObject( g_ScriptingHost.GetContext() ), Source.c_str(), Source.Length(), "subset query script", 0 ); + SetScript( Script ); + break; + } + case JSTYPE_FUNCTION: + { + JSFunction* fn = JS_ValueToFunction( g_ScriptingHost.GetContext(), v ); + SetFunction( fn ); + break; + } + default: + Type = UNDEFINED; + Function = NULL; + Script = NULL; + } +} + +bool CScriptObject::Defined() +{ + return( Type != UNDEFINED ); +} + +// Executes a script attached to a JS object. +// Returns false if the script isn't defined, if the script can't be executed, +// otherwise true. Script return value is in rval. +bool CScriptObject::Run( JSObject* Context, jsval* rval ) +{ + switch( Type ) + { + case UNDEFINED: + return( false ); + case FUNCTION: + return( JS_TRUE == JS_CallFunction( g_ScriptingHost.GetContext(), Context, Function, 0, NULL, rval ) ); + case SCRIPT: + return( JS_TRUE == JS_ExecuteScript( g_ScriptingHost.GetContext(), Context, Script, rval ) ); + default: + return( false ); + } +} + +// This variant casts script return value to a boolean, and passes it back. +bool CScriptObject::Run( JSObject* Context ) +{ + jsval Temp; + if( !Run( Context, &Temp ) ) + return( false ); + return( g_ScriptingHost.ValueToBool( Temp ) ); +} + +void CScriptObject::CompileScript( CStrW FileNameTag, CStrW Script ) +{ + Type = FUNCTION; + Function = JS_CompileUCFunction( g_ScriptingHost.GetContext(), NULL, NULL, 0, NULL, Script, Script.Length(), (CStr)FileNameTag, 0 ); +} diff --git a/source/simulation/ScriptObject.h b/source/simulation/ScriptObject.h new file mode 100755 index 0000000000..cd766936c4 --- /dev/null +++ b/source/simulation/ScriptObject.h @@ -0,0 +1,45 @@ +// A generic type and some helper functions +// for scripts + +// Mark Thompson (mark@wildfiregames.com / mot20@cam.ac.uk) + +#ifndef SCRIPTOBJECT_INCLUDED +#define SCRIPTOBJECT_INCLUDED + +#include "CStr.h" +#include "scripting/ScriptingHost.h" +#include "EntityHandles.h" +#include "scripting/JSInterface_Entity.h" + +class CScriptObject +{ +public: + enum EScriptType + { + UNDEFINED, + FUNCTION, + SCRIPT + } Type; + + JSFunction* Function; + JSScript* Script; + + CScriptObject(); + CScriptObject( JSFunction* _Function ); + CScriptObject( JSScript* _Script ); + CScriptObject( jsval v ); + void SetFunction( JSFunction* _Function ); + void SetScript( JSScript* _Script ); + void SetJSVal( jsval v ); + inline bool Defined(); + // Executes a script attached to a JS object. + // Returns false if the script isn't defined, if the script can't be executed, + // otherwise true. Script return value is in rval. + bool Run( JSObject* Context, jsval* rval ); + // This variant casts script return value to a boolean, and passes it back. + bool Run( JSObject* Context ); + + void CompileScript( CStrW FileNameTag, CStrW Script ); +}; + +#endif \ No newline at end of file