cinterface.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 INT 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(int 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. int 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. int 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. // signal an engine shutdown (as with the quit(); console command)
  104. void torque_enginesignalshutdown()
  105. {
  106. Con::evaluate("quit();");
  107. }
  108. // shutdown the engine
  109. int torque_engineshutdown()
  110. {
  111. #if defined( TORQUE_MINIDUMP ) && defined( TORQUE_RELEASE )
  112. __try {
  113. #endif
  114. // Clean everything up.
  115. StandardMainLoop::shutdown();
  116. #if !defined(TORQUE_OS_XENON) && !defined(TORQUE_OS_PS3) && defined(_MSC_VER)
  117. createFontShutdown();
  118. #endif
  119. #ifdef TORQUE_OS_MAC
  120. torque_mac_engineshutdown();
  121. #endif
  122. #if defined( TORQUE_MINIDUMP ) && defined( TORQUE_RELEASE )
  123. }
  124. __except( CreateMiniDump(GetExceptionInformation()) )
  125. {
  126. _exit(0);
  127. }
  128. #endif
  129. // Return.
  130. return true;
  131. }
  132. bool torque_isdebugbuild()
  133. {
  134. #ifdef _DEBUG
  135. return true;
  136. #else
  137. return false;
  138. #endif
  139. }
  140. int torque_getconsolebool(const char* name)
  141. {
  142. return Con::getBoolVariable(name);
  143. }
  144. void torque_setconsolebool(const char* name, bool value)
  145. {
  146. Con::setBoolVariable(name, value);
  147. }
  148. static char* gExecutablePath = NULL;
  149. const char* torque_getexecutablepath()
  150. {
  151. return gExecutablePath;
  152. }
  153. void torque_setexecutablepath(const char* directory)
  154. {
  155. gExecutablePath = new char[strlen(directory)+1];
  156. strcpy(gExecutablePath, directory);
  157. }
  158. // set Torque 3D into web deployment mode (disable fullscreen exlusive mode, etc)
  159. void torque_setwebdeployment()
  160. {
  161. Platform::setWebDeployment(true);
  162. }
  163. // Get a console variable
  164. const char* torque_getvariable(const char* name)
  165. {
  166. return Con::getVariable(StringTable->insert(name));
  167. }
  168. // Set a console variable
  169. void torque_setvariable(const char* name, const char* value)
  170. {
  171. Con::setVariable(StringTable->insert(name), StringTable->insert(value));
  172. }
  173. static Namespace::Entry* GetEntry(const char* nameSpace, const char* name)
  174. {
  175. Namespace* ns = NULL;
  176. if (!nameSpace || !dStrlen(nameSpace))
  177. ns = Namespace::mGlobalNamespace;
  178. else
  179. {
  180. nameSpace = StringTable->insert(nameSpace);
  181. ns = Namespace::find(nameSpace); //can specify a package here, maybe need, maybe not
  182. }
  183. if (!ns)
  184. return NULL;
  185. name = StringTable->insert(name);
  186. Namespace::Entry* entry = ns->lookupRecursive(name);
  187. return entry;
  188. }
  189. // Export a function to the Torque 3D console system which matches the StringCallback function prototype
  190. // specify the nameSpace, functionName, usage, min and max arguments
  191. void torque_exportstringcallback(StringCallback cb, const char *nameSpace, const char *funcName, const char* usage, S32 minArgs, S32 maxArgs)
  192. {
  193. if (!nameSpace || !dStrlen(nameSpace))
  194. Con::addCommand(funcName, cb, usage, minArgs + 1, maxArgs + 1);
  195. else
  196. Con::addCommand(nameSpace, funcName, cb, usage, minArgs + 1, maxArgs + 1);
  197. }
  198. void torque_callvoidfunction(const char* nameSpace, const char* name, S32 argc, const char ** argv)
  199. {
  200. Namespace::Entry* entry = GetEntry(nameSpace, name);
  201. if (!entry)
  202. return;
  203. entry->cb.mVoidCallbackFunc(NULL, argc, argv);
  204. }
  205. F32 torque_callfloatfunction(const char* nameSpace, const char* name, S32 argc, const char ** argv)
  206. {
  207. Namespace::Entry* entry = GetEntry(nameSpace, name);
  208. if (!entry)
  209. return 0.0f;
  210. return entry->cb.mFloatCallbackFunc(NULL, argc, argv);
  211. }
  212. S32 torque_callintfunction(const char* nameSpace, const char* name, S32 argc, const char ** argv)
  213. {
  214. Namespace::Entry* entry = GetEntry(nameSpace, name);
  215. if (!entry)
  216. return 0;
  217. return entry->cb.mIntCallbackFunc(NULL, argc, argv);
  218. }
  219. const char * torque_callstringfunction(const char* nameSpace, const char* name, S32 argc, const char ** argv)
  220. {
  221. Namespace::Entry* entry = GetEntry(nameSpace, name);
  222. if (!entry)
  223. return "";
  224. return entry->cb.mStringCallbackFunc(NULL, argc, argv);
  225. }
  226. bool torque_callboolfunction(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. return entry->cb.mBoolCallbackFunc(NULL, argc, argv);
  232. }
  233. const char * torque_callscriptfunction(const char* nameSpace, const char* name, S32 argc, const char ** argv)
  234. {
  235. Namespace::Entry* entry = GetEntry(nameSpace, name);
  236. if (!entry)
  237. return "";
  238. if(!entry->mFunctionOffset)
  239. return "";
  240. const char* ret = entry->mCode->exec(entry->mFunctionOffset, StringTable->insert(name), entry->mNamespace, argc, argv, false, entry->mPackage);
  241. if (!ret || !dStrlen(ret))
  242. return "";
  243. return ret;
  244. }
  245. // Call a TorqueScript console function that has been marked as secure
  246. const char* torque_callsecurefunction(const char* nameSpace, const char* name, S32 argc, const char ** argv)
  247. {
  248. static const char* invalidChars = "()=:{}";
  249. String s = nameSpace;
  250. s += "::";
  251. s += name;
  252. s = String::ToUpper(s);
  253. if (!gSecureScript.count(StringTable->insert(s.c_str())))
  254. {
  255. Con::warnf("\nAttempt to call insecure script: %s\n", s.c_str());
  256. return "";
  257. }
  258. // scan through for invalid characters
  259. for (S32 i = 0; i < argc ; i++)
  260. for (S32 j = 0; j < dStrlen(invalidChars) ; j++)
  261. for (S32 k = 0; k < dStrlen(argv[i]); k++)
  262. if (invalidChars[j] == argv[i][k])
  263. {
  264. Con::warnf("\nInvalid parameter passed to secure script: %s, %s\n", s.c_str(), argv[i]);
  265. return "";
  266. }
  267. Namespace::Entry* entry = GetEntry(nameSpace, name);
  268. if (!entry)
  269. return "";
  270. static char returnBuffer[32];
  271. switch(entry->mType)
  272. {
  273. case Namespace::Entry::ConsoleFunctionType:
  274. return torque_callscriptfunction(nameSpace, name, argc, argv);
  275. case Namespace::Entry::StringCallbackType:
  276. return torque_callstringfunction(nameSpace, name, argc, argv);
  277. case Namespace::Entry::IntCallbackType:
  278. dSprintf(returnBuffer, sizeof(returnBuffer), "%d", torque_callintfunction(nameSpace, name, argc, argv));
  279. return returnBuffer;
  280. case Namespace::Entry::FloatCallbackType:
  281. dSprintf(returnBuffer, sizeof(returnBuffer), "%g", torque_callfloatfunction(nameSpace, name, argc, argv));
  282. return returnBuffer;
  283. case Namespace::Entry::VoidCallbackType:
  284. torque_callvoidfunction(nameSpace, name, argc, argv);
  285. return "";
  286. case Namespace::Entry::BoolCallbackType:
  287. dSprintf(returnBuffer, sizeof(returnBuffer), "%d", (U32) torque_callboolfunction(nameSpace, name, argc, argv));
  288. return returnBuffer;
  289. };
  290. return "";
  291. }
  292. // Set a TorqueScript console function as secure and available for JavaScript via the callScript plugin method
  293. void torque_addsecurefunction(const char* nameSpace, const char* fname)
  294. {
  295. String s = nameSpace;
  296. s += "::";
  297. s += fname;
  298. s = String::ToUpper(s);
  299. gSecureScript.insertEqual(StringTable->insert(s.c_str()), StringTable->insert(s.c_str()));
  300. }
  301. // Evaluate arbitrary TorqueScript (ONLY CALL torque_evaluate FROM TRUSTED CODE!!!)
  302. const char* torque_evaluate(const char* code)
  303. {
  304. return Con::evaluate(code);
  305. }
  306. // resize the Torque 3D child window to the specified width and height
  307. void torque_resizewindow(S32 width, S32 height)
  308. {
  309. if (PlatformWindowManager::get() && PlatformWindowManager::get()->getFirstWindow())
  310. PlatformWindowManager::get()->getFirstWindow()->setSize(Point2I(width,height));
  311. }
  312. #ifdef TORQUE_OS_WIN
  313. // retrieve the hwnd of our render window
  314. void* torque_gethwnd()
  315. {
  316. if (PlatformWindowManager::get() && PlatformWindowManager::get()->getFirstWindow())
  317. {
  318. Win32Window* w = (Win32Window*) PlatformWindowManager::get()->getFirstWindow();
  319. return (void *) w->getHWND();
  320. }
  321. return NULL;
  322. }
  323. // directly add a message to the Torque 3D event queue, bypassing the Windows event queue
  324. // this is useful in the case of the IE plugin, where we are hooking into an application
  325. // level message, and posting to the windows queue would cause a hang
  326. void torque_directmessage(U32 message, U32 wparam, U32 lparam)
  327. {
  328. if (PlatformWindowManager::get() && PlatformWindowManager::get()->getFirstWindow())
  329. {
  330. Win32Window* w = (Win32Window*) PlatformWindowManager::get()->getFirstWindow();
  331. Dispatch(DelayedDispatch,w->getHWND(),message,wparam,lparam);
  332. }
  333. }
  334. #endif
  335. }
  336. // This function is solely to test the TorqueScript <-> Javascript binding
  337. // By default, it is marked as secure by the web plugins and then can be called from
  338. // Javascript on the web page to ensure that function calls across the language
  339. // boundry are working with arguments and return values
  340. ConsoleFunction(testJavaScriptBridge, const char *, 4, 4, "testBridge(arg1, arg2, arg3)")
  341. {
  342. S32 failed = 0;
  343. if(argc != 4)
  344. failed = 1;
  345. else
  346. {
  347. if (dStrcmp(argv[1],"one"))
  348. failed = 2;
  349. if (dStrcmp(argv[2],"two"))
  350. failed = 2;
  351. if (dStrcmp(argv[3],"three"))
  352. failed = 2;
  353. }
  354. //attempt to call from TorqueScript -> JavaScript
  355. const char* jret = Con::evaluate("JS::bridgeCallback(\"one\",\"two\",\"three\");");
  356. if (dStrcmp(jret,"42"))
  357. failed = 3;
  358. char *ret = Con::getReturnBuffer(256);
  359. dSprintf(ret, 256, "%i", failed);
  360. return ret;
  361. }