c_scripting.cpp
Engine/source/cinterface/c_scripting.cpp
Classes:
class
Public Functions
ConsoleFunction(TestFunction2Args , const char * , 3 , 3 , "testFunction(arg1, arg2)" )
bool
script_call_namespace_entry_bool(Namespace::Entry * entry, S32 argc, const char ** argv)
script_call_namespace_entry_float(Namespace::Entry * entry, S32 argc, const char ** argv)
script_call_namespace_entry_int(Namespace::Entry * entry, S32 argc, const char ** argv)
const char *
script_call_namespace_entry_string(Namespace::Entry * entry, S32 argc, const char ** argv)
script_call_namespace_entry_void(Namespace::Entry * entry, S32 argc, const char ** argv)
script_export_callback_bool(BoolCallback cb, const char * nameSpace, const char * funcName, const char * usage, S32 minArgs, S32 maxArgs)
script_export_callback_float(FloatCallback cb, const char * nameSpace, const char * funcName, const char * usage, S32 minArgs, S32 maxArgs)
script_export_callback_int(IntCallback cb, const char * nameSpace, const char * funcName, const char * usage, S32 minArgs, S32 maxArgs)
script_export_callback_string(StringCallback cb, const char * nameSpace, const char * funcName, const char * usage, S32 minArgs, S32 maxArgs)
script_export_callback_void(VoidCallback cb, const char * nameSpace, const char * funcName, const char * usage, S32 minArgs, S32 maxArgs)
script_get_namespace_entry(const char * nameSpace, const char * name)
void *
script_get_stringtable_entry(const char * string)
const char *
script_simobject_find(const char * classname, const char * name)
bool
script_simobject_getfield_bool(U32 objectId, const char * fieldName)
script_simobject_getfield_float(U32 objectId, const char * fieldName)
script_simobject_getfield_int(U32 objectId, const char * fieldName)
const char *
script_simobject_getfield_string(U32 id, const char * fieldName)
script_simobject_setfield_bool(U32 objectId, const char * fieldName, bool v)
script_simobject_setfield_float(U32 objectId, const char * fieldName, F32 v)
script_simobject_setfield_int(U32 objectId, const char * fieldName, S32 v)
script_simobject_setfield_string(U32 objectId, const char * fieldName, const char * v)
Detailed Description
Public Functions
ConsoleFunction(TestFunction2Args , const char * , 3 , 3 , "testFunction(arg1, arg2)" )
GetEntry(const char * nameSpace, const char * name)
script_call_namespace_entry_bool(Namespace::Entry * entry, S32 argc, const char ** argv)
script_call_namespace_entry_float(Namespace::Entry * entry, S32 argc, const char ** argv)
script_call_namespace_entry_int(Namespace::Entry * entry, S32 argc, const char ** argv)
script_call_namespace_entry_string(Namespace::Entry * entry, S32 argc, const char ** argv)
script_call_namespace_entry_void(Namespace::Entry * entry, S32 argc, const char ** argv)
script_export_callback_bool(BoolCallback cb, const char * nameSpace, const char * funcName, const char * usage, S32 minArgs, S32 maxArgs)
script_export_callback_float(FloatCallback cb, const char * nameSpace, const char * funcName, const char * usage, S32 minArgs, S32 maxArgs)
script_export_callback_int(IntCallback cb, const char * nameSpace, const char * funcName, const char * usage, S32 minArgs, S32 maxArgs)
script_export_callback_string(StringCallback cb, const char * nameSpace, const char * funcName, const char * usage, S32 minArgs, S32 maxArgs)
script_export_callback_void(VoidCallback cb, const char * nameSpace, const char * funcName, const char * usage, S32 minArgs, S32 maxArgs)
script_get_namespace_entry(const char * nameSpace, const char * name)
script_get_stringtable_entry(const char * string)
script_getconsolexml()
script_simobject_find(const char * classname, const char * name)
script_simobject_get_id(SimObject * so)
script_simobject_getfield_bool(U32 objectId, const char * fieldName)
script_simobject_getfield_float(U32 objectId, const char * fieldName)
script_simobject_getfield_int(U32 objectId, const char * fieldName)
script_simobject_getfield_string(U32 id, const char * fieldName)
script_simobject_setfield_bool(U32 objectId, const char * fieldName, bool v)
script_simobject_setfield_float(U32 objectId, const char * fieldName, F32 v)
script_simobject_setfield_int(U32 objectId, const char * fieldName, S32 v)
script_simobject_setfield_string(U32 objectId, const char * fieldName, const char * v)
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/console.h" 27#include "console/consoleInternal.h" 28#include "core/util/tDictionary.h" 29#include "app/mainLoop.h" 30 31// External scripting cinterface, suitable for import into any scripting system which support "C" interfaces (C#, Python, Lua, Java, etc) 32 33#ifdef TORQUE_OS_WIN 34#include "windowManager/win32/win32Window.h" 35#include "windowManager/win32/winDispatch.h" 36#endif 37 38extern "C" { 39 40 struct MarshalNativeEntry 41 { 42 const char* nameSpace; 43 const char* name; 44 Namespace::Entry* entry; 45 S32 minArgs; 46 S32 maxArgs; 47 S32 cbType; 48 }; 49 50 51 static Namespace::Entry* GetEntry(const char* nameSpace, const char* name) 52 { 53 Namespace* ns = NULL; 54 55 if (!nameSpace || !dStrlen(nameSpace)) 56 ns = Namespace::mGlobalNamespace; 57 else 58 { 59 nameSpace = StringTable->insert(nameSpace); 60 ns = Namespace::find(nameSpace); //can specify a package here, maybe need, maybe not 61 } 62 63 if (!ns) 64 return NULL; 65 66 name = StringTable->insert(name); 67 68 Namespace::Entry* entry = ns->lookupRecursive(name); 69 70 return entry; 71 } 72 73 const char * script_getconsolexml() 74 { 75 Namespace::Entry* entry = GetEntry("", "consoleExportXML"); 76 77 if (!entry) 78 return ""; 79 80 static const char* exportArgv[1] = { "consoleExportXML" }; 81 static StringStackConsoleWrapper exportCmd(1, exportArgv); 82 83 return entry->cb.mStringCallbackFunc(NULL, exportCmd.argc, exportCmd.argv); 84 } 85 86 MarshalNativeEntry* script_get_namespace_entry(const char* nameSpace, const char* name) 87 { 88 static MarshalNativeEntry mentry; 89 90 Namespace::Entry* e = GetEntry(nameSpace, name); 91 92 if (!e) 93 return NULL; 94 95 mentry.nameSpace = e->mNamespace->mName; 96 mentry.name = e->mFunctionName; 97 mentry.minArgs = e->mMinArgs; 98 mentry.maxArgs = e->mMaxArgs; 99 mentry.cbType = e->mType; 100 mentry.entry = e; 101 102 return &mentry; 103 } 104 105 void* script_get_stringtable_entry(const char* string) 106 { 107 return (void*)StringTable->insert(string); 108 } 109 110 // FIELD ACCESS 111 112 // fieldNames must be from stringTable coming in! See Engine.stringTable 113 114 const char* script_simobject_getfield_string(U32 id, const char* fieldName) 115 { 116 SimObject *object = Sim::findObject( id ); 117 if( object ) 118 { 119 return (const char *) object->getDataField(fieldName, ""); 120 } 121 return ""; 122 } 123 124 void script_simobject_setfield_string(U32 objectId, const char* fieldName, const char* v) 125 { 126 SimObject *object = Sim::findObject( objectId ); 127 if( object ) 128 { 129 object->setDataField(fieldName, "", v); 130 } 131 } 132 133 134 bool script_simobject_getfield_bool(U32 objectId, const char* fieldName) 135 { 136 SimObject *object = Sim::findObject( objectId ); 137 if( object ) 138 { 139 const char *v = object->getDataField(fieldName, ""); 140 141 return dAtob(v); 142 } 143 144 return false; 145 } 146 147 void script_simobject_setfield_bool(U32 objectId, const char* fieldName, bool v) 148 { 149 SimObject *object = Sim::findObject( objectId ); 150 if( object ) 151 { 152 object->setDataField(fieldName, "", v ? "1" : "0"); 153 } 154 } 155 156 S32 script_simobject_getfield_int(U32 objectId, const char* fieldName) 157 { 158 SimObject *object = Sim::findObject( objectId ); 159 if( object ) 160 { 161 const char *v = object->getDataField(fieldName, ""); 162 163 return dAtoi(v); 164 } 165 166 return false; 167 } 168 169 void script_simobject_setfield_int(U32 objectId, const char* fieldName, S32 v) 170 { 171 SimObject *object = Sim::findObject( objectId ); 172 if( object ) 173 { 174 // this seems pretty lame, though it is how it is handled in consoleType.cpp 175 char buf[256]; 176 dSprintf(buf, 256, "%d", v ); 177 object->setDataField(fieldName, "", buf); 178 } 179 } 180 181 F32 script_simobject_getfield_float(U32 objectId, const char* fieldName) 182 { 183 SimObject *object = Sim::findObject( objectId ); 184 if( object ) 185 { 186 const char *v = object->getDataField(fieldName, ""); 187 188 return dAtof(v); 189 } 190 191 return false; 192 } 193 194 void script_simobject_setfield_float(U32 objectId, const char* fieldName, F32 v) 195 { 196 SimObject *object = Sim::findObject( objectId ); 197 if( object ) 198 { 199 char buf[256]; 200 dSprintf(buf, 256, "%g", v ); 201 object->setDataField(fieldName, "", buf); 202 } 203 } 204 205 const char* script_call_namespace_entry_string(Namespace::Entry* entry, S32 argc, const char** argv) 206 { 207 // maxArgs improper on a number of console function/methods 208 if (argc < entry->mMinArgs)// || argc > entry->mMaxArgs) 209 return ""; 210 211 SimObject* o = NULL; 212 213 if (entry->mNamespace && entry->mNamespace->isClass()) 214 { 215 o = Sim::findObject(dAtoi(argv[1])); 216 if (!o) 217 return ""; 218 } 219 220 StringStackConsoleWrapper args(argc, argv); 221 return entry->cb.mStringCallbackFunc(o, args.count(), args); 222 } 223 224 bool script_call_namespace_entry_bool(Namespace::Entry* entry, S32 argc, const char** argv) 225 { 226 // maxArgs improper on a number of console function/methods 227 if (argc < entry->mMinArgs)// || argc > entry->mMaxArgs) 228 return false; 229 230 SimObject* o = NULL; 231 232 if (entry->mNamespace && entry->mNamespace->isClass()) 233 { 234 o = Sim::findObject(dAtoi(argv[1])); 235 if (!o) 236 return false; 237 } 238 239 StringStackConsoleWrapper args(argc, argv); 240 return entry->cb.mBoolCallbackFunc(o, args.count(), args); 241 } 242 243 S32 script_call_namespace_entry_int(Namespace::Entry* entry, S32 argc, const char** argv) 244 { 245 // maxArgs improper on a number of console function/methods 246 if (argc < entry->mMinArgs)// || argc > entry->mMaxArgs) 247 return 0; 248 249 SimObject* o = NULL; 250 251 if (entry->mNamespace && entry->mNamespace->isClass()) 252 { 253 o = Sim::findObject(dAtoi(argv[1])); 254 if (!o) 255 return 0; 256 } 257 258 StringStackConsoleWrapper args(argc, argv); 259 return entry->cb.mIntCallbackFunc(o, args.count(), args); 260 } 261 262 F32 script_call_namespace_entry_float(Namespace::Entry* entry, S32 argc, const char** argv) 263 { 264 // maxArgs improper on a number of console function/methods 265 if (argc < entry->mMinArgs)// || argc > entry->mMaxArgs) 266 return 0.0f; 267 268 SimObject* o = NULL; 269 270 if (entry->mNamespace && entry->mNamespace->isClass()) 271 { 272 o = Sim::findObject(dAtoi(argv[1])); 273 if (!o) 274 return 0.0f; 275 } 276 277 StringStackConsoleWrapper args(argc, argv); 278 return entry->cb.mFloatCallbackFunc(o, args.count(), args); 279 } 280 281 282 void script_call_namespace_entry_void(Namespace::Entry* entry, S32 argc, const char** argv) 283 { 284 // maxArgs improper on a number of console function/methods 285 if (argc < entry->mMinArgs)// || argc > entry->mMaxArgs) 286 return; 287 288 SimObject* o = NULL; 289 290 if (entry->mNamespace && entry->mNamespace->isClass()) 291 { 292 Sim::findObject(dAtoi(argv[1])); 293 if (!o) 294 return; 295 } 296 297 StringStackConsoleWrapper args(argc, argv); 298 entry->cb.mVoidCallbackFunc(o, args.count(), args); 299 } 300 301 S32 script_simobject_get_id(SimObject* so) 302 { 303 return so->getId(); 304 } 305 306 S32 script_simobject_find(const char* classname, const char* name) 307 { 308 SimObject *object; 309 if( Sim::findObject( name, object ) ) 310 { 311 // if we specified a classname do type checking 312 if (classname && dStrlen(classname)) 313 { 314 AbstractClassRep* ocr = object->getClassRep(); 315 while (ocr) 316 { 317 if (!dStricmp(ocr->getClassName(), classname)) 318 return object->getId(); 319 ocr = ocr->getParentClass(); 320 } 321 322 } 323 324 // invalid type 325 return 0; 326 } 327 328 // didn't find object 329 return 0; 330 } 331 332 void script_export_callback_string(StringCallback cb, const char *nameSpace, const char *funcName, const char* usage, S32 minArgs, S32 maxArgs) 333 { 334 if (!nameSpace || !dStrlen(nameSpace)) 335 Con::addCommand(funcName, cb, usage, minArgs + 1, maxArgs + 1); 336 else 337 Con::addCommand(nameSpace, funcName, cb, usage, minArgs + 1, maxArgs + 1); 338 } 339 340 void script_export_callback_void(VoidCallback cb, const char *nameSpace, const char *funcName, const char* usage, S32 minArgs, S32 maxArgs) 341 { 342 343 if (!nameSpace || !dStrlen(nameSpace)) 344 Con::addCommand(funcName, cb, usage, minArgs + 1, maxArgs + 1); 345 else 346 Con::addCommand(nameSpace, funcName, cb, usage, minArgs + 1, maxArgs + 1); 347 348 349 350 // example of package support 351 // note that Parent:: does not work with this, at least not yet anyway 352 353 /* 354 Namespace* ns; 355 356 StringTableEntry nspace = NULL; 357 358 if (nameSpace && dStrlen(nameSpace)) 359 nspace = StringTable->insert(nameSpace); 360 361 Namespace::unlinkPackages(); 362 ns = Namespace::find(nspace, StringTable->insert("fps")); 363 ns->addCommand(StringTable->insert(funcName), cb, StringTable->insert(usage), minArgs + 1, maxArgs + 1 ); 364 Namespace::relinkPackages(); 365 */ 366 } 367 368 void script_export_callback_bool(BoolCallback cb, const char *nameSpace, const char *funcName, const char* usage, S32 minArgs, S32 maxArgs) 369 { 370 if (!nameSpace || !dStrlen(nameSpace)) 371 Con::addCommand(funcName, cb, usage, minArgs + 1, maxArgs + 1); 372 else 373 Con::addCommand(nameSpace, funcName, cb, usage, minArgs + 1, maxArgs + 1); 374 } 375 376 void script_export_callback_int(IntCallback cb, const char *nameSpace, const char *funcName, const char* usage, S32 minArgs, S32 maxArgs) 377 { 378 if (!nameSpace || !dStrlen(nameSpace)) 379 Con::addCommand(funcName, cb, usage, minArgs + 1, maxArgs + 1); 380 else 381 Con::addCommand(nameSpace, funcName, cb, usage, minArgs + 1, maxArgs + 1); 382 } 383 384 void script_export_callback_float(FloatCallback cb, const char *nameSpace, const char *funcName, const char* usage, S32 minArgs, S32 maxArgs) 385 { 386 if (!nameSpace || !dStrlen(nameSpace)) 387 Con::addCommand(funcName, cb, usage, minArgs + 1, maxArgs + 1); 388 else 389 Con::addCommand(nameSpace, funcName, cb, usage, minArgs + 1, maxArgs + 1); 390 } 391 392 393#ifdef TORQUE_OS_WIN 394 395 void script_input_event(S32 type, S32 value1, S32 value2) 396 { 397 if (PlatformWindowManager::get() && PlatformWindowManager::get()->getFirstWindow()) 398 { 399 Win32Window* w = (Win32Window*) PlatformWindowManager::get()->getFirstWindow(); 400 WindowId devId = w->getWindowId(); 401 402 switch (type) 403 { 404 case 0: 405 w->mouseEvent.trigger(devId,0,value1,value2,w->isMouseLocked()); 406 break; 407 case 1: 408 if (value2) 409 w->buttonEvent.trigger(devId,0,IA_MAKE,value1); 410 else 411 w->buttonEvent.trigger(devId,0,IA_BREAK,value1); 412 break; 413 414 } 415 416 } 417 418 } 419#endif 420 421} 422 423 424ConsoleFunction(TestFunction2Args, const char *, 3, 3, "testFunction(arg1, arg2)") 425{ 426 return "Return Value"; 427} 428
