cinterface.cpp 13 KB

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