2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
#include "ScriptGlue.h"
|
2004-07-15 21:29:56 +02:00
|
|
|
#include "CLogger.h"
|
2004-08-15 22:57:31 +02:00
|
|
|
#include "CConsole.h"
|
2004-06-11 00:24:03 +02:00
|
|
|
#include "CStr.h"
|
|
|
|
#include "EntityHandles.h"
|
|
|
|
#include "Entity.h"
|
|
|
|
#include "EntityManager.h"
|
|
|
|
#include "BaseEntityCollection.h"
|
2004-07-20 21:30:35 +02:00
|
|
|
#include "Scheduler.h"
|
2004-08-05 15:07:51 +02:00
|
|
|
#include "Game.h"
|
2004-06-11 00:24:03 +02:00
|
|
|
#include "scripting/JSInterface_Entity.h"
|
|
|
|
#include "scripting/JSInterface_BaseEntity.h"
|
|
|
|
#include "scripting/JSInterface_Vector3D.h"
|
2004-07-08 17:23:47 +02:00
|
|
|
#include "gui/scripting/JSInterface_IGUIObject.h"
|
2004-07-22 18:18:12 +02:00
|
|
|
#include "scripting/JSInterface_Selection.h"
|
2004-08-03 01:14:54 +02:00
|
|
|
#include "scripting/JSInterface_Camera.h"
|
|
|
|
#include "scripting/JSInterface_Console.h"
|
2004-06-11 00:24:03 +02:00
|
|
|
|
|
|
|
extern CConsole* g_Console;
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
// Parameters for the table are:
|
|
|
|
|
2004-07-07 20:27:25 +02:00
|
|
|
// 0: The name the function will be called as from script
|
|
|
|
// 1: The function which will be called
|
|
|
|
// 2: The number of arguments this function expects
|
|
|
|
// 3: Flags (deprecated, always zero)
|
|
|
|
// 4: Extra (reserved for future use, always zero)
|
2004-05-30 02:46:58 +02:00
|
|
|
|
|
|
|
JSFunctionSpec ScriptFunctionTable[] =
|
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
{"writeLog", WriteLog, 1, 0, 0},
|
|
|
|
{"writeConsole", JSI_Console::writeConsole, 1, 0, 0 }, // Keep this variant available for now.
|
2004-06-11 00:24:03 +02:00
|
|
|
{"getEntityByHandle", getEntityByHandle, 1, 0, 0 },
|
|
|
|
{"getEntityTemplate", getEntityTemplate, 1, 0, 0 },
|
2004-07-20 21:30:35 +02:00
|
|
|
{"setTimeout", setTimeout, 2, 0, 0 },
|
|
|
|
{"setInterval", setInterval, 2, 0, 0 },
|
|
|
|
{"cancelInterval", cancelInterval, 0, 0, 0 },
|
2004-07-08 17:23:47 +02:00
|
|
|
{"getGUIObjectByName", JSI_IGUIObject::getByName, 1, 0, 0 },
|
2004-07-07 20:27:25 +02:00
|
|
|
{"getGlobal", getGlobal, 0, 0, 0 },
|
2004-07-11 18:22:35 +02:00
|
|
|
{"getGUIGlobal", getGUIGlobal, 0, 0, 0 },
|
2004-08-14 13:21:29 +02:00
|
|
|
{"setCursor", setCursor, 1, 0, 0 },
|
2004-08-05 15:07:51 +02:00
|
|
|
{"startGame", startGame, 0, 0, 0 },
|
|
|
|
{"endGame", endGame, 0, 0, 0 },
|
2004-07-09 14:44:12 +02:00
|
|
|
{"exit", exitProgram, 0, 0, 0 },
|
2004-08-14 13:21:29 +02:00
|
|
|
{"crash", crash, 0, 0, 0 },
|
|
|
|
{"_mem", js_mem, 0, 0, 0 }, // Intentionally undocumented
|
2004-05-30 02:46:58 +02:00
|
|
|
{0, 0, 0, 0, 0},
|
|
|
|
};
|
|
|
|
|
2004-07-20 21:30:35 +02:00
|
|
|
enum ScriptGlobalTinyIDs
|
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
GLOBAL_SELECTION,
|
|
|
|
GLOBAL_GROUPSARRAY,
|
|
|
|
GLOBAL_CAMERA,
|
|
|
|
GLOBAL_CONSOLE,
|
2004-07-20 21:30:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
JSPropertySpec ScriptGlobalTable[] =
|
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
{ "selection", GLOBAL_SELECTION, JSPROP_PERMANENT, JSI_Selection::getSelection, JSI_Selection::setSelection },
|
|
|
|
{ "groups", GLOBAL_GROUPSARRAY, JSPROP_PERMANENT, JSI_Selection::getGroups, JSI_Selection::setGroups },
|
|
|
|
{ "camera", GLOBAL_CAMERA, JSPROP_PERMANENT, JSI_Camera::getCamera, JSI_Camera::setCamera },
|
|
|
|
{ "console", GLOBAL_CONSOLE, JSPROP_PERMANENT | JSPROP_READONLY, JSI_Console::getConsole, NULL },
|
2004-07-20 21:30:35 +02:00
|
|
|
{ 0, 0, 0, 0, 0 },
|
|
|
|
};
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
// Allow scripts to output to the global log file
|
2004-07-24 16:04:40 +02:00
|
|
|
JSBool WriteLog(JSContext* context, JSObject* UNUSEDPARAM(globalObject), unsigned int argc, jsval* argv, jsval* UNUSEDPARAM(rval))
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
|
|
|
if (argc < 1)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
CStr logMessage;
|
|
|
|
|
2004-05-30 02:46:58 +02:00
|
|
|
for (int i = 0; i < (int)argc; i++)
|
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
try
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
CStr arg = g_ScriptingHost.ValueToString( argv[i] );
|
|
|
|
logMessage += arg;
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
2004-08-03 01:14:54 +02:00
|
|
|
catch( PSERROR_Scripting_ConversionFailed )
|
2004-05-30 02:46:58 +02:00
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
// Do nothing.
|
2004-05-30 02:46:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
// We should perhaps unicodify (?) the logger at some point.
|
2004-05-30 02:46:58 +02:00
|
|
|
|
2004-08-15 22:57:31 +02:00
|
|
|
LOG( NORMAL, "script", logMessage );
|
2004-06-11 00:24:03 +02:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
return JS_TRUE;
|
2004-06-11 00:24:03 +02:00
|
|
|
}
|
|
|
|
|
2004-07-24 16:04:40 +02:00
|
|
|
JSBool getEntityByHandle( JSContext* context, JSObject* UNUSEDPARAM(globalObject), unsigned int argc, jsval* argv, jsval* rval )
|
2004-06-11 00:24:03 +02:00
|
|
|
{
|
|
|
|
assert( argc >= 1 );
|
|
|
|
i32 handle;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
handle = g_ScriptingHost.ValueToInt( argv[0] );
|
|
|
|
}
|
2004-07-17 19:09:33 +02:00
|
|
|
catch( PSERROR_Scripting_ConversionFailed )
|
2004-06-11 00:24:03 +02:00
|
|
|
{
|
|
|
|
*rval = JSVAL_NULL;
|
2004-07-20 21:30:35 +02:00
|
|
|
JS_ReportError( context, "Invalid handle" );
|
2004-06-11 00:24:03 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
HEntity* v = g_EntityManager.getByHandle( (u16)handle );
|
|
|
|
if( !v )
|
|
|
|
{
|
2004-07-20 21:30:35 +02:00
|
|
|
JS_ReportError( context, "No entity occupying handle: %d", handle );
|
2004-06-11 00:24:03 +02:00
|
|
|
*rval = JSVAL_NULL;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
JSObject* entity = JS_NewObject( context, &JSI_Entity::JSI_class, NULL, NULL );
|
|
|
|
JS_SetPrivate( context, entity, v );
|
|
|
|
*rval = OBJECT_TO_JSVAL( entity );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2004-07-24 16:04:40 +02:00
|
|
|
JSBool getEntityTemplate( JSContext* context, JSObject* UNUSEDPARAM(globalObject), unsigned int argc, jsval* argv, jsval* rval )
|
2004-06-11 00:24:03 +02:00
|
|
|
{
|
|
|
|
assert( argc >= 1 );
|
2004-08-03 01:14:54 +02:00
|
|
|
CStrW templateName;
|
2004-06-11 00:24:03 +02:00
|
|
|
try
|
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
templateName = g_ScriptingHost.ValueToUCString( argv[0] );
|
2004-06-11 00:24:03 +02:00
|
|
|
}
|
2004-07-17 19:09:33 +02:00
|
|
|
catch( PSERROR_Scripting_ConversionFailed )
|
2004-06-11 00:24:03 +02:00
|
|
|
{
|
|
|
|
*rval = JSVAL_NULL;
|
2004-07-20 21:30:35 +02:00
|
|
|
JS_ReportError( context, "Invalid template identifier" );
|
2004-06-11 00:24:03 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
CBaseEntity* v = g_EntityTemplateCollection.getTemplate( templateName );
|
|
|
|
if( !v )
|
|
|
|
{
|
|
|
|
*rval = JSVAL_NULL;
|
2004-08-03 01:14:54 +02:00
|
|
|
JS_ReportError( context, "No such template: %ls", (const wchar_t*)templateName );
|
2004-06-11 00:24:03 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
JSObject* baseEntity = JS_NewObject( context, &JSI_BaseEntity::JSI_class, NULL, NULL );
|
|
|
|
JS_SetPrivate( context, baseEntity, v );
|
|
|
|
*rval = OBJECT_TO_JSVAL( baseEntity );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2004-07-24 16:04:40 +02:00
|
|
|
JSBool setTimeout( JSContext* context, JSObject* UNUSEDPARAM(globalObject), unsigned int argc, jsval* argv, jsval* UNUSEDPARAM(rval) )
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
|
|
|
assert( argc >= 2 );
|
|
|
|
size_t delay;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
delay = g_ScriptingHost.ValueToInt( argv[1] );
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
JS_ReportError( context, "Invalid timer parameters" );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( JS_TypeOfValue( context, argv[0] ) )
|
|
|
|
{
|
|
|
|
case JSTYPE_STRING:
|
|
|
|
{
|
|
|
|
CStr16 fragment = g_ScriptingHost.ValueToUCString( argv[0] );
|
|
|
|
g_Scheduler.pushTime( delay, fragment, JS_GetScopeChain( context ) );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
case JSTYPE_FUNCTION:
|
|
|
|
{
|
|
|
|
JSFunction* fn = JS_ValueToFunction( context, argv[0] );
|
|
|
|
g_Scheduler.pushTime( delay, fn, JS_GetScopeChain( context ) );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
JS_ReportError( context, "Invalid timer script" );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-24 16:04:40 +02:00
|
|
|
JSBool setInterval( JSContext* context, JSObject* UNUSEDPARAM(globalObject), unsigned int argc, jsval* argv, jsval* UNUSEDPARAM(rval) )
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
|
|
|
assert( argc >= 2 );
|
|
|
|
size_t first, interval;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
first = g_ScriptingHost.ValueToInt( argv[1] );
|
|
|
|
if( argc == 3 )
|
|
|
|
{
|
|
|
|
// toDo, first, interval
|
|
|
|
interval = g_ScriptingHost.ValueToInt( argv[2] );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// toDo, interval (first = interval)
|
|
|
|
interval = first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
JS_ReportError( context, "Invalid timer parameters" );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( JS_TypeOfValue( context, argv[0] ) )
|
|
|
|
{
|
|
|
|
case JSTYPE_STRING:
|
|
|
|
{
|
|
|
|
CStr16 fragment = g_ScriptingHost.ValueToUCString( argv[0] );
|
|
|
|
g_Scheduler.pushInterval( first, interval, fragment, JS_GetScopeChain( context ) );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
case JSTYPE_FUNCTION:
|
|
|
|
{
|
|
|
|
JSFunction* fn = JS_ValueToFunction( context, argv[0] );
|
|
|
|
g_Scheduler.pushInterval( first, interval, fn, JS_GetScopeChain( context ) );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
JS_ReportError( context, "Invalid timer script" );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-24 16:04:40 +02:00
|
|
|
JSBool cancelInterval( JSContext* UNUSEDPARAM(context), JSObject* UNUSEDPARAM(globalObject), unsigned int UNUSEDPARAM(argc), jsval* UNUSEDPARAM(argv), jsval* UNUSEDPARAM(rval) )
|
2004-07-20 21:30:35 +02:00
|
|
|
{
|
|
|
|
g_Scheduler.m_abortInterval = true;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2004-07-24 16:04:40 +02:00
|
|
|
JSBool getGUIGlobal( JSContext* UNUSEDPARAM(context), JSObject* UNUSEDPARAM(globalObject), unsigned int UNUSEDPARAM(argc), jsval* UNUSEDPARAM(argv), jsval* rval )
|
2004-07-11 18:22:35 +02:00
|
|
|
{
|
|
|
|
*rval = OBJECT_TO_JSVAL( g_GUI.GetScriptObject() );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2004-07-24 16:04:40 +02:00
|
|
|
JSBool getGlobal( JSContext* UNUSEDPARAM(context), JSObject* globalObject, unsigned int UNUSEDPARAM(argc), jsval* UNUSEDPARAM(argv), jsval* rval )
|
2004-07-07 20:27:25 +02:00
|
|
|
{
|
|
|
|
*rval = OBJECT_TO_JSVAL( globalObject );
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2004-07-09 14:44:12 +02:00
|
|
|
|
2004-07-31 14:45:30 +02:00
|
|
|
extern CStr g_CursorName; // from main.cpp
|
|
|
|
JSBool setCursor(JSContext* UNUSEDPARAM(context), JSObject* UNUSEDPARAM(globalObject), unsigned int argc, jsval* argv, jsval* UNUSEDPARAM(rval))
|
|
|
|
{
|
|
|
|
if (argc != 1)
|
|
|
|
{
|
|
|
|
assert(! "Invalid parameter count to setCursor");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
g_CursorName = g_ScriptingHost.ValueToString(argv[0]);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2004-08-05 15:07:51 +02:00
|
|
|
// From main.cpp
|
|
|
|
extern void StartGame();
|
|
|
|
extern CGameAttributes g_GameAttributes;
|
|
|
|
|
|
|
|
JSBool startGame(JSContext* cx, JSObject* UNUSEDPARAM(globalObject), unsigned int argc, jsval* argv, jsval* rval)
|
|
|
|
{
|
|
|
|
if (argc == 1)
|
|
|
|
{
|
|
|
|
JSObject *obj;
|
|
|
|
if (JS_ConvertArguments(cx, 1, argv, "o", &obj))
|
|
|
|
{
|
|
|
|
g_GameAttributes.FillFromJS(cx, obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StartGame();
|
|
|
|
*rval=BOOLEAN_TO_JSVAL(JS_TRUE);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void EndGame();
|
|
|
|
JSBool endGame(JSContext* UNUSEDPARAM(context), JSObject* UNUSEDPARAM(globalObject), unsigned int UNUSEDPARAM(argc), jsval* UNUSEDPARAM(argv), jsval* UNUSEDPARAM(rval))
|
|
|
|
{
|
|
|
|
EndGame();
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2004-07-31 14:45:30 +02:00
|
|
|
|
2004-07-09 14:44:12 +02:00
|
|
|
extern void kill_mainloop(); // from main.cpp
|
|
|
|
|
2004-07-24 16:04:40 +02:00
|
|
|
JSBool exitProgram(JSContext* UNUSEDPARAM(context), JSObject* UNUSEDPARAM(globalObject), unsigned int UNUSEDPARAM(argc), jsval* UNUSEDPARAM(argv), jsval* UNUSEDPARAM(rval))
|
2004-07-09 14:44:12 +02:00
|
|
|
{
|
|
|
|
kill_mainloop();
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2004-07-15 21:29:56 +02:00
|
|
|
|
2004-07-24 16:04:40 +02:00
|
|
|
JSBool crash(JSContext* UNUSEDPARAM(context), JSObject* UNUSEDPARAM(globalObject), unsigned int UNUSEDPARAM(argc), jsval* UNUSEDPARAM(argv), jsval* UNUSEDPARAM(rval))
|
2004-07-15 21:29:56 +02:00
|
|
|
{
|
2004-07-15 21:31:09 +02:00
|
|
|
MICROLOG(L"Crashing at user's request.");
|
2004-07-15 21:29:56 +02:00
|
|
|
uintptr_t ptr = 0xDEADC0DE;
|
|
|
|
return *(JSBool*) ptr;
|
|
|
|
}
|
2004-08-14 13:21:29 +02:00
|
|
|
|
|
|
|
JSBool js_mem(JSContext* UNUSEDPARAM(context), JSObject* UNUSEDPARAM(globalObject), unsigned int UNUSEDPARAM(argc), jsval* UNUSEDPARAM(argv), jsval* UNUSEDPARAM(rval))
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
int left, total;
|
|
|
|
extern int GetVRAMInfo(int&, int&);
|
|
|
|
if (GetVRAMInfo(left, total))
|
2004-08-15 22:57:31 +02:00
|
|
|
g_Console->InsertMessage(L"VRAM: used %d, total %d, free %d", total-left, total, left);
|
2004-08-14 13:21:29 +02:00
|
|
|
else
|
2004-08-15 22:57:31 +02:00
|
|
|
g_Console->InsertMessage(L"VRAM: failed to detect");
|
2004-08-14 13:21:29 +02:00
|
|
|
#else
|
2004-08-15 22:57:31 +02:00
|
|
|
g_Console->InsertMessage(L"VRAM: [not available on non-Windows]");
|
2004-08-14 13:21:29 +02:00
|
|
|
#endif
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|