cinterface.cpp
Engine/source/cinterface/cinterface.cpp
Public Variables
Public Functions
DefineConsoleFunction(testJavaScriptBridge , const char * , (const char *arg1, const char *arg2, const char *arg3) , "testBridge(arg1, arg2, arg3)" )
torque_addsecurefunction(const char * nameSpace, const char * fname)
bool
torque_callboolfunction(const char * nameSpace, const char * name, S32 argc, const char ** argv)
torque_callfloatfunction(const char * nameSpace, const char * name, S32 argc, const char ** argv)
const char *
torque_callscriptfunction(const char * nameSpace, const char * name, S32 argc, const char ** argv)
const char *
torque_callsecurefunction(const char * nameSpace, const char * name, S32 argc, const char ** argv)
const char *
torque_callstringfunction(const char * nameSpace, const char * name, S32 argc, const char ** argv)
torque_callvoidfunction(const char * nameSpace, const char * name, S32 argc, const char ** argv)
torque_engineinit(S32 argc, const char ** argv)
const char *
torque_evaluate(const char * code)
torque_exportstringcallback(StringCallback cb, const char * nameSpace, const char * funcName, const char * usage, S32 minArgs, S32 maxArgs)
torque_getconsolebool(const char * name)
const char *
const char *
torque_getvariable(const char * name)
bool
torque_resizewindow(S32 width, S32 height)
torque_setconsolebool(const char * name, bool value)
torque_setexecutablepath(const char * directory)
torque_setvariable(const char * name, const char * value)
Detailed Description
Public Variables
char * gExecutablePath
HashTable< StringTableEntry, StringTableEntry > gSecureScript
bool LinkConsoleFunctions
Public Functions
DefineConsoleFunction(testJavaScriptBridge , const char * , (const char *arg1, const char *arg2, const char *arg3) , "testBridge(arg1, arg2, arg3)" )
GetEntry(const char * nameSpace, const char * name)
torque_addsecurefunction(const char * nameSpace, const char * fname)
torque_callboolfunction(const char * nameSpace, const char * name, S32 argc, const char ** argv)
torque_callfloatfunction(const char * nameSpace, const char * name, S32 argc, const char ** argv)
torque_callintfunction(const char * nameSpace, const char * name, S32 argc, const char ** argv)
torque_callscriptfunction(const char * nameSpace, const char * name, S32 argc, const char ** argv)
torque_callsecurefunction(const char * nameSpace, const char * name, S32 argc, const char ** argv)
torque_callstringfunction(const char * nameSpace, const char * name, S32 argc, const char ** argv)
torque_callvoidfunction(const char * nameSpace, const char * name, S32 argc, const char ** argv)
torque_engineinit(S32 argc, const char ** argv)
torque_engineshutdown()
torque_enginesignalshutdown()
torque_enginetick()
torque_evaluate(const char * code)
torque_exportstringcallback(StringCallback cb, const char * nameSpace, const char * funcName, const char * usage, S32 minArgs, S32 maxArgs)
torque_getconsolebool(const char * name)
torque_getexecutablepath()
torque_getreturnstatus()
torque_getvariable(const char * name)
torque_isdebugbuild()
torque_reset()
torque_resizewindow(S32 width, S32 height)
torque_setconsolebool(const char * name, bool value)
torque_setexecutablepath(const char * directory)
torque_setvariable(const char * name, const char * value)
torque_setwebdeployment()
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 "platform/platform.h" 25#include "console/compiler.h" 26#include "console/consoleInternal.h" 27#include "console/engineAPI.h" 28#include "core/util/tDictionary.h" 29#include "core/strings/stringFunctions.h" 30#include "app/mainLoop.h" 31#include "windowManager/platformWindow.h" 32#include "windowManager/platformWindowMgr.h" 33 34#ifdef TORQUE_OS_WIN 35#include "windowManager/win32/win32Window.h" 36#include "windowManager/win32/winDispatch.h" 37extern void createFontInit(void); 38extern void createFontShutdown(void); 39#endif 40 41#if defined( TORQUE_MINIDUMP ) && defined( TORQUE_RELEASE ) 42 extern S32 CreateMiniDump(LPEXCEPTION_POINTERS ExceptionInfo); 43#endif 44 45static HashTable<StringTableEntry,StringTableEntry> gSecureScript; 46 47extern bool LinkConsoleFunctions; 48 49extern "C" { 50 51 // reset the engine, unloading any current level and returning to the main menu 52 void torque_reset() 53 { 54 Con::evaluate("disconnect();"); 55 } 56 57 // initialize Torque 3D including argument handling 58 S32 torque_engineinit(S32 argc, const char **argv) 59 { 60 61#if defined( TORQUE_MINIDUMP ) && defined( TORQUE_RELEASE ) 62 __try { 63#endif 64 65 LinkConsoleFunctions = true; 66 67#if !defined(TORQUE_OS_XENON) && !defined(TORQUE_OS_PS3) && defined(_MSC_VER) 68 createFontInit(); 69#endif 70 71 // Initialize the subsystems. 72 StandardMainLoop::init(); 73 74 // Handle any command line args. 75 if(!StandardMainLoop::handleCommandLine(argc, argv)) 76 { 77 Platform::AlertOK("Error", "Failed to initialize game, shutting down."); 78 return false; 79 } 80 81#if defined( TORQUE_MINIDUMP ) && defined( TORQUE_RELEASE ) 82 } 83 84 __except( CreateMiniDump(GetExceptionInformation()) ) 85 { 86 _exit(0); 87 } 88#endif 89 90 return true; 91 92 } 93 94 // tick Torque 3D's main loop 95 S32 torque_enginetick() 96 { 97 98#if defined( TORQUE_MINIDUMP ) && defined( TORQUE_RELEASE ) 99 __try { 100#endif 101 102 bool ret = StandardMainLoop::doMainLoop(); 103 return ret; 104 105#if defined( TORQUE_MINIDUMP ) && defined( TORQUE_RELEASE ) 106 } 107 __except( CreateMiniDump(GetExceptionInformation()) ) 108 { 109 _exit(0); 110 } 111#endif 112 113 114 115 } 116 117 S32 torque_getreturnstatus() 118 { 119 return StandardMainLoop::getReturnStatus(); 120 } 121 122 // signal an engine shutdown (as with the quit(); console command) 123 void torque_enginesignalshutdown() 124 { 125 Con::evaluate("quit();"); 126 } 127 128 // shutdown the engine 129 S32 torque_engineshutdown() 130 { 131 132#if defined( TORQUE_MINIDUMP ) && defined( TORQUE_RELEASE ) 133 __try { 134#endif 135 136 // Clean everything up. 137 StandardMainLoop::shutdown(); 138 139#if !defined(TORQUE_OS_XENON) && !defined(TORQUE_OS_PS3) && defined(_MSC_VER) 140 createFontShutdown(); 141#endif 142 143#if defined( TORQUE_MINIDUMP ) && defined( TORQUE_RELEASE ) 144 } 145 146 __except( CreateMiniDump(GetExceptionInformation()) ) 147 { 148 _exit(0); 149 } 150#endif 151 152 // Return. 153 return true; 154 155 } 156 157 bool torque_isdebugbuild() 158 { 159#ifdef _DEBUG 160 return true; 161#else 162 return false; 163#endif 164 165 } 166 167 S32 torque_getconsolebool(const char* name) 168 { 169 return Con::getBoolVariable(name); 170 } 171 172 void torque_setconsolebool(const char* name, bool value) 173 { 174 Con::setBoolVariable(name, value); 175 } 176 177 static char* gExecutablePath = NULL; 178 179 const char* torque_getexecutablepath() 180 { 181 return gExecutablePath; 182 } 183 184 void torque_setexecutablepath(const char* directory) 185 { 186 gExecutablePath = new char[strlen(directory)+1]; 187 strcpy(gExecutablePath, directory); 188 } 189 190 // set Torque 3D into web deployment mode (disable fullscreen exlusive mode, etc) 191 void torque_setwebdeployment() 192 { 193 Platform::setWebDeployment(true); 194 } 195 196 // Get a console variable 197 const char* torque_getvariable(const char* name) 198 { 199 return Con::getVariable(StringTable->insert(name)); 200 } 201 202 // Set a console variable 203 void torque_setvariable(const char* name, const char* value) 204 { 205 Con::setVariable(StringTable->insert(name), StringTable->insert(value)); 206 } 207 208 static Namespace::Entry* GetEntry(const char* nameSpace, const char* name) 209 { 210 Namespace* ns = NULL; 211 212 if (!nameSpace || !dStrlen(nameSpace)) 213 ns = Namespace::mGlobalNamespace; 214 else 215 { 216 nameSpace = StringTable->insert(nameSpace); 217 ns = Namespace::find(nameSpace); //can specify a package here, maybe need, maybe not 218 } 219 220 if (!ns) 221 return NULL; 222 223 name = StringTable->insert(name); 224 225 Namespace::Entry* entry = ns->lookupRecursive(name); 226 227 return entry; 228 } 229 230 // Export a function to the Torque 3D console system which matches the StringCallback function prototype 231 // specify the nameSpace, functionName, usage, min and max arguments 232 void torque_exportstringcallback(StringCallback cb, const char *nameSpace, const char *funcName, const char* usage, S32 minArgs, S32 maxArgs) 233 { 234 if (!nameSpace || !dStrlen(nameSpace)) 235 Con::addCommand(funcName, cb, usage, minArgs + 1, maxArgs + 1); 236 else 237 Con::addCommand(nameSpace, funcName, cb, usage, minArgs + 1, maxArgs + 1); 238 } 239 240 void torque_callvoidfunction(const char* nameSpace, const char* name, S32 argc, const char ** argv) 241 { 242 243 Namespace::Entry* entry = GetEntry(nameSpace, name); 244 245 if (!entry) 246 return; 247 248 StringStackConsoleWrapper args(argc, argv); 249 entry->cb.mVoidCallbackFunc(NULL, args.count(), args); 250 } 251 252 F32 torque_callfloatfunction(const char* nameSpace, const char* name, S32 argc, const char ** argv) 253 { 254 255 Namespace::Entry* entry = GetEntry(nameSpace, name); 256 257 if (!entry) 258 return 0.0f; 259 260 StringStackConsoleWrapper args(argc, argv); 261 return entry->cb.mFloatCallbackFunc(NULL, args.count(), args); 262 } 263 264 S32 torque_callintfunction(const char* nameSpace, const char* name, S32 argc, const char ** argv) 265 { 266 267 Namespace::Entry* entry = GetEntry(nameSpace, name); 268 269 if (!entry) 270 return 0; 271 272 StringStackConsoleWrapper args(argc, argv); 273 return entry->cb.mIntCallbackFunc(NULL, args.count(), args); 274 } 275 276 277 const char * torque_callstringfunction(const char* nameSpace, const char* name, S32 argc, const char ** argv) 278 { 279 Namespace::Entry* entry = GetEntry(nameSpace, name); 280 281 if (!entry) 282 return ""; 283 284 StringStackConsoleWrapper args(argc, argv); 285 return entry->cb.mStringCallbackFunc(NULL, args.count(), args); 286 } 287 288 bool torque_callboolfunction(const char* nameSpace, const char* name, S32 argc, const char ** argv) 289 { 290 Namespace::Entry* entry = GetEntry(nameSpace, name); 291 292 if (!entry) 293 return false; 294 295 StringStackConsoleWrapper args(argc, argv); 296 return entry->cb.mBoolCallbackFunc(NULL, args.count(), args); 297 } 298 299 300 const char * torque_callscriptfunction(const char* nameSpace, const char* name, S32 argc, const char ** argv) 301 { 302 Namespace::Entry* entry = GetEntry(nameSpace, name); 303 304 if (!entry) 305 return ""; 306 307 if(!entry->mFunctionOffset) 308 return ""; 309 310 StringStackConsoleWrapper args(argc, argv); 311 const char* ret = entry->mCode->exec(entry->mFunctionOffset, StringTable->insert(name), entry->mNamespace, args.count(), args, false, entry->mPackage); 312 313 if (!ret || !dStrlen(ret)) 314 return ""; 315 316 return ret; 317 318 } 319 320 321 // Call a TorqueScript console function that has been marked as secure 322 const char* torque_callsecurefunction(const char* nameSpace, const char* name, S32 argc, const char ** argv) 323 { 324 static const char* invalidChars = "()=:{}"; 325 String s = nameSpace; 326 s += "::"; 327 s += name; 328 s = String::ToUpper(s); 329 330 if (!gSecureScript.count(StringTable->insert(s.c_str()))) 331 { 332 Con::warnf("\nAttempt to call insecure script: %s\n", s.c_str()); 333 return ""; 334 } 335 336 // scan through for invalid characters 337 for (S32 i = 0; i < argc ; i++) 338 for (S32 j = 0; j < dStrlen(invalidChars) ; j++) 339 for (S32 k = 0; k < dStrlen(argv[i]); k++) 340 if (invalidChars[j] == argv[i][k]) 341 { 342 Con::warnf("\nInvalid parameter passed to secure script: %s, %s\n", s.c_str(), argv[i]); 343 return ""; 344 } 345 346 Namespace::Entry* entry = GetEntry(nameSpace, name); 347 348 if (!entry) 349 return ""; 350 351 static char returnBuffer[32]; 352 353 switch(entry->mType) 354 { 355 case Namespace::Entry::ConsoleFunctionType: 356 return torque_callscriptfunction(nameSpace, name, argc, argv); 357 358 case Namespace::Entry::StringCallbackType: 359 return torque_callstringfunction(nameSpace, name, argc, argv); 360 361 case Namespace::Entry::IntCallbackType: 362 dSprintf(returnBuffer, sizeof(returnBuffer), "%d", torque_callintfunction(nameSpace, name, argc, argv)); 363 return returnBuffer; 364 365 case Namespace::Entry::FloatCallbackType: 366 dSprintf(returnBuffer, sizeof(returnBuffer), "%g", torque_callfloatfunction(nameSpace, name, argc, argv)); 367 return returnBuffer; 368 369 case Namespace::Entry::VoidCallbackType: 370 torque_callvoidfunction(nameSpace, name, argc, argv); 371 return ""; 372 373 case Namespace::Entry::BoolCallbackType: 374 dSprintf(returnBuffer, sizeof(returnBuffer), "%d", (U32) torque_callboolfunction(nameSpace, name, argc, argv)); 375 return returnBuffer; 376 }; 377 378 return ""; 379 380 } 381 382 // Set a TorqueScript console function as secure and available for JavaScript via the callScript plugin method 383 void torque_addsecurefunction(const char* nameSpace, const char* fname) 384 { 385 String s = nameSpace; 386 s += "::"; 387 s += fname; 388 s = String::ToUpper(s); 389 390 gSecureScript.insertEqual(StringTable->insert(s.c_str()), StringTable->insert(s.c_str())); 391 } 392 393 394 // Evaluate arbitrary TorqueScript (ONLY CALL torque_evaluate FROM TRUSTED CODE!!!) 395 const char* torque_evaluate(const char* code) 396 { 397 return Con::evaluate(code); 398 } 399 400 // resize the Torque 3D child window to the specified width and height 401 void torque_resizewindow(S32 width, S32 height) 402 { 403 if (PlatformWindowManager::get() && PlatformWindowManager::get()->getFirstWindow()) 404 PlatformWindowManager::get()->getFirstWindow()->setSize(Point2I(width,height)); 405 } 406 407#if defined(TORQUE_OS_WIN) && !defined(TORQUE_SDL) 408 // retrieve the hwnd of our render window 409 void* torque_gethwnd() 410 { 411 if (PlatformWindowManager::get() && PlatformWindowManager::get()->getFirstWindow()) 412 { 413 Win32Window* w = (Win32Window*) PlatformWindowManager::get()->getFirstWindow(); 414 return (void *) w->getHWND(); 415 } 416 417 return NULL; 418 } 419 420 // directly add a message to the Torque 3D event queue, bypassing the Windows event queue 421 // this is useful in the case of the IE plugin, where we are hooking into an application 422 // level message, and posting to the windows queue would cause a hang 423 void torque_directmessage(U32 message, U32 wparam, U32 lparam) 424 { 425 if (PlatformWindowManager::get() && PlatformWindowManager::get()->getFirstWindow()) 426 { 427 Win32Window* w = (Win32Window*) PlatformWindowManager::get()->getFirstWindow(); 428 Dispatch(DelayedDispatch,w->getHWND(),message,wparam,lparam); 429 } 430 } 431 432#endif 433} 434 435// This function is solely to test the TorqueScript <-> Javascript binding 436// By default, it is marked as secure by the web plugins and then can be called from 437// Javascript on the web page to ensure that function calls across the language 438// boundry are working with arguments and return values 439DefineConsoleFunction( testJavaScriptBridge, const char *, (const char* arg1, const char* arg2, const char* arg3), , "testBridge(arg1, arg2, arg3)") 440{ 441 S32 failed = 0; 442 if (dStrcmp(arg1,"one")) 443 failed = 2; 444 if (dStrcmp(arg2,"two")) 445 failed = 2; 446 if (dStrcmp(arg3,"three")) 447 failed = 2; 448 449 450 //attempt to call from TorqueScript -> JavaScript 451 const char* jret = Con::evaluate("JS::bridgeCallback(\"one\",\"two\",\"three\");"); 452 453 if (dStrcmp(jret,"42")) 454 failed = 3; 455 456 static const U32 bufSize = 256; 457 char *ret = Con::getReturnBuffer(bufSize); 458 459 dSprintf(ret, bufSize, "%i", failed); 460 461 return ret; 462} 463 464 465 466 467 468
