cinterface.cpp 13 KB

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