cinterface.cpp 13 KB

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