c_scripting.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "console/compiler.h"
  24. #include "console/console.h"
  25. #include "console/consoleInternal.h"
  26. #include "core/util/tDictionary.h"
  27. #include "app/mainLoop.h"
  28. // External scripting cinterface, suitable for import into any scripting system which support "C" interfaces (C#, Python, Lua, Java, etc)
  29. #ifdef TORQUE_OS_WIN
  30. #include "windowManager/win32/win32Window.h"
  31. #include "windowManager/win32/winDispatch.h"
  32. #endif
  33. extern "C" {
  34. struct MarshalNativeEntry
  35. {
  36. const char* nameSpace;
  37. const char* name;
  38. Namespace::Entry* entry;
  39. S32 minArgs;
  40. S32 maxArgs;
  41. S32 cbType;
  42. };
  43. static Namespace::Entry* GetEntry(const char* nameSpace, const char* name)
  44. {
  45. Namespace* ns = NULL;
  46. if (!nameSpace || !dStrlen(nameSpace))
  47. ns = Namespace::mGlobalNamespace;
  48. else
  49. {
  50. nameSpace = StringTable->insert(nameSpace);
  51. ns = Namespace::find(nameSpace); //can specify a package here, maybe need, maybe not
  52. }
  53. if (!ns)
  54. return NULL;
  55. name = StringTable->insert(name);
  56. Namespace::Entry* entry = ns->lookupRecursive(name);
  57. return entry;
  58. }
  59. const char * script_getconsolexml()
  60. {
  61. Namespace::Entry* entry = GetEntry("", "consoleExportXML");
  62. if (!entry)
  63. return "";
  64. ConsoleValueRef argv[] = {"consoleExportXML"};
  65. return entry->cb.mStringCallbackFunc(NULL, 1, argv);
  66. }
  67. MarshalNativeEntry* script_get_namespace_entry(const char* nameSpace, const char* name)
  68. {
  69. static MarshalNativeEntry mentry;
  70. Namespace::Entry* e = GetEntry(nameSpace, name);
  71. if (!e)
  72. return NULL;
  73. mentry.nameSpace = e->mNamespace->mName;
  74. mentry.name = e->mFunctionName;
  75. mentry.minArgs = e->mMinArgs;
  76. mentry.maxArgs = e->mMaxArgs;
  77. mentry.cbType = e->mType;
  78. mentry.entry = e;
  79. return &mentry;
  80. }
  81. void* script_get_stringtable_entry(const char* string)
  82. {
  83. return (void*)StringTable->insert(string);
  84. }
  85. // FIELD ACCESS
  86. // fieldNames must be from stringTable coming in! See Engine.stringTable
  87. const char* script_simobject_getfield_string(U32 id, const char* fieldName)
  88. {
  89. SimObject *object = Sim::findObject( id );
  90. if( object )
  91. {
  92. return (const char *) object->getDataField(fieldName, "");
  93. }
  94. return "";
  95. }
  96. void script_simobject_setfield_string(U32 objectId, const char* fieldName, const char* v)
  97. {
  98. SimObject *object = Sim::findObject( objectId );
  99. if( object )
  100. {
  101. object->setDataField(fieldName, "", v);
  102. }
  103. }
  104. bool script_simobject_getfield_bool(U32 objectId, const char* fieldName)
  105. {
  106. SimObject *object = Sim::findObject( objectId );
  107. if( object )
  108. {
  109. const char *v = object->getDataField(fieldName, "");
  110. return dAtob(v);
  111. }
  112. return false;
  113. }
  114. void script_simobject_setfield_bool(U32 objectId, const char* fieldName, bool v)
  115. {
  116. SimObject *object = Sim::findObject( objectId );
  117. if( object )
  118. {
  119. object->setDataField(fieldName, "", v ? "1" : "0");
  120. }
  121. }
  122. S32 script_simobject_getfield_int(U32 objectId, const char* fieldName)
  123. {
  124. SimObject *object = Sim::findObject( objectId );
  125. if( object )
  126. {
  127. const char *v = object->getDataField(fieldName, "");
  128. return dAtoi(v);
  129. }
  130. return false;
  131. }
  132. void script_simobject_setfield_int(U32 objectId, const char* fieldName, S32 v)
  133. {
  134. SimObject *object = Sim::findObject( objectId );
  135. if( object )
  136. {
  137. // this seems pretty lame, though it is how it is handled in consoleType.cpp
  138. char buf[256];
  139. dSprintf(buf, 256, "%d", v );
  140. object->setDataField(fieldName, "", buf);
  141. }
  142. }
  143. F32 script_simobject_getfield_float(U32 objectId, const char* fieldName)
  144. {
  145. SimObject *object = Sim::findObject( objectId );
  146. if( object )
  147. {
  148. const char *v = object->getDataField(fieldName, "");
  149. return dAtof(v);
  150. }
  151. return false;
  152. }
  153. void script_simobject_setfield_float(U32 objectId, const char* fieldName, F32 v)
  154. {
  155. SimObject *object = Sim::findObject( objectId );
  156. if( object )
  157. {
  158. char buf[256];
  159. dSprintf(buf, 256, "%g", v );
  160. object->setDataField(fieldName, "", buf);
  161. }
  162. }
  163. const char* script_call_namespace_entry_string(Namespace::Entry* entry, S32 argc, const char** argv)
  164. {
  165. // maxArgs improper on a number of console function/methods
  166. if (argc < entry->mMinArgs)// || argc > entry->mMaxArgs)
  167. return "";
  168. SimObject* o = NULL;
  169. if (entry->mNamespace && entry->mNamespace->isClass())
  170. {
  171. o = Sim::findObject(dAtoi(argv[1]));
  172. if (!o)
  173. return "";
  174. }
  175. StringStackConsoleWrapper args(argc, argv);
  176. return entry->cb.mStringCallbackFunc(o, args.count(), args);
  177. }
  178. bool script_call_namespace_entry_bool(Namespace::Entry* entry, S32 argc, const char** argv)
  179. {
  180. // maxArgs improper on a number of console function/methods
  181. if (argc < entry->mMinArgs)// || argc > entry->mMaxArgs)
  182. return false;
  183. SimObject* o = NULL;
  184. if (entry->mNamespace && entry->mNamespace->isClass())
  185. {
  186. o = Sim::findObject(dAtoi(argv[1]));
  187. if (!o)
  188. return false;
  189. }
  190. StringStackConsoleWrapper args(argc, argv);
  191. return entry->cb.mBoolCallbackFunc(o, args.count(), args);
  192. }
  193. S32 script_call_namespace_entry_int(Namespace::Entry* entry, S32 argc, const char** argv)
  194. {
  195. // maxArgs improper on a number of console function/methods
  196. if (argc < entry->mMinArgs)// || argc > entry->mMaxArgs)
  197. return 0;
  198. SimObject* o = NULL;
  199. if (entry->mNamespace && entry->mNamespace->isClass())
  200. {
  201. o = Sim::findObject(dAtoi(argv[1]));
  202. if (!o)
  203. return 0;
  204. }
  205. StringStackConsoleWrapper args(argc, argv);
  206. return entry->cb.mIntCallbackFunc(o, args.count(), args);
  207. }
  208. F32 script_call_namespace_entry_float(Namespace::Entry* entry, S32 argc, const char** argv)
  209. {
  210. // maxArgs improper on a number of console function/methods
  211. if (argc < entry->mMinArgs)// || argc > entry->mMaxArgs)
  212. return 0.0f;
  213. SimObject* o = NULL;
  214. if (entry->mNamespace && entry->mNamespace->isClass())
  215. {
  216. o = Sim::findObject(dAtoi(argv[1]));
  217. if (!o)
  218. return 0.0f;
  219. }
  220. StringStackConsoleWrapper args(argc, argv);
  221. return entry->cb.mFloatCallbackFunc(o, args.count(), args);
  222. }
  223. void script_call_namespace_entry_void(Namespace::Entry* entry, S32 argc, const char** argv)
  224. {
  225. // maxArgs improper on a number of console function/methods
  226. if (argc < entry->mMinArgs)// || argc > entry->mMaxArgs)
  227. return;
  228. SimObject* o = NULL;
  229. if (entry->mNamespace && entry->mNamespace->isClass())
  230. {
  231. Sim::findObject(dAtoi(argv[1]));
  232. if (!o)
  233. return;
  234. }
  235. StringStackConsoleWrapper args(argc, argv);
  236. entry->cb.mVoidCallbackFunc(o, args.count(), args);
  237. }
  238. S32 script_simobject_get_id(SimObject* so)
  239. {
  240. return so->getId();
  241. }
  242. S32 script_simobject_find(const char* classname, const char* name)
  243. {
  244. SimObject *object;
  245. if( Sim::findObject( name, object ) )
  246. {
  247. // if we specified a classname do type checking
  248. if (classname && dStrlen(classname))
  249. {
  250. AbstractClassRep* ocr = object->getClassRep();
  251. while (ocr)
  252. {
  253. if (!dStricmp(ocr->getClassName(), classname))
  254. return object->getId();
  255. ocr = ocr->getParentClass();
  256. }
  257. }
  258. // invalid type
  259. return 0;
  260. }
  261. // didn't find object
  262. return 0;
  263. }
  264. void script_export_callback_string(StringCallback cb, const char *nameSpace, const char *funcName, const char* usage, S32 minArgs, S32 maxArgs)
  265. {
  266. if (!nameSpace || !dStrlen(nameSpace))
  267. Con::addCommand(funcName, cb, usage, minArgs + 1, maxArgs + 1);
  268. else
  269. Con::addCommand(nameSpace, funcName, cb, usage, minArgs + 1, maxArgs + 1);
  270. }
  271. void script_export_callback_void(VoidCallback cb, const char *nameSpace, const char *funcName, const char* usage, S32 minArgs, S32 maxArgs)
  272. {
  273. if (!nameSpace || !dStrlen(nameSpace))
  274. Con::addCommand(funcName, cb, usage, minArgs + 1, maxArgs + 1);
  275. else
  276. Con::addCommand(nameSpace, funcName, cb, usage, minArgs + 1, maxArgs + 1);
  277. // example of package support
  278. // note that Parent:: does not work with this, at least not yet anyway
  279. /*
  280. Namespace* ns;
  281. StringTableEntry nspace = NULL;
  282. if (nameSpace && dStrlen(nameSpace))
  283. nspace = StringTable->insert(nameSpace);
  284. Namespace::unlinkPackages();
  285. ns = Namespace::find(nspace, StringTable->insert("fps"));
  286. ns->addCommand(StringTable->insert(funcName), cb, StringTable->insert(usage), minArgs + 1, maxArgs + 1 );
  287. Namespace::relinkPackages();
  288. */
  289. }
  290. void script_export_callback_bool(BoolCallback cb, const char *nameSpace, const char *funcName, const char* usage, S32 minArgs, S32 maxArgs)
  291. {
  292. if (!nameSpace || !dStrlen(nameSpace))
  293. Con::addCommand(funcName, cb, usage, minArgs + 1, maxArgs + 1);
  294. else
  295. Con::addCommand(nameSpace, funcName, cb, usage, minArgs + 1, maxArgs + 1);
  296. }
  297. void script_export_callback_int(IntCallback cb, const char *nameSpace, const char *funcName, const char* usage, S32 minArgs, S32 maxArgs)
  298. {
  299. if (!nameSpace || !dStrlen(nameSpace))
  300. Con::addCommand(funcName, cb, usage, minArgs + 1, maxArgs + 1);
  301. else
  302. Con::addCommand(nameSpace, funcName, cb, usage, minArgs + 1, maxArgs + 1);
  303. }
  304. void script_export_callback_float(FloatCallback cb, const char *nameSpace, const char *funcName, const char* usage, S32 minArgs, S32 maxArgs)
  305. {
  306. if (!nameSpace || !dStrlen(nameSpace))
  307. Con::addCommand(funcName, cb, usage, minArgs + 1, maxArgs + 1);
  308. else
  309. Con::addCommand(nameSpace, funcName, cb, usage, minArgs + 1, maxArgs + 1);
  310. }
  311. #ifdef TORQUE_OS_WIN
  312. void script_input_event(S32 type, S32 value1, S32 value2)
  313. {
  314. if (PlatformWindowManager::get() && PlatformWindowManager::get()->getFirstWindow())
  315. {
  316. Win32Window* w = (Win32Window*) PlatformWindowManager::get()->getFirstWindow();
  317. WindowId devId = w->getWindowId();
  318. switch (type)
  319. {
  320. case 0:
  321. w->mouseEvent.trigger(devId,0,value1,value2,w->isMouseLocked());
  322. break;
  323. case 1:
  324. if (value2)
  325. w->buttonEvent.trigger(devId,0,IA_MAKE,value1);
  326. else
  327. w->buttonEvent.trigger(devId,0,IA_BREAK,value1);
  328. break;
  329. }
  330. }
  331. }
  332. #endif
  333. }
  334. ConsoleFunction(TestFunction2Args, const char *, 3, 3, "testFunction(arg1, arg2)")
  335. {
  336. return "Return Value";
  337. }