webCommon.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #ifndef _webcommon_h
  23. #define _webcommon_h
  24. // Common web functionality between IE/Safari/Firefox/Chrome
  25. // Platform specific includes
  26. #ifdef WIN32
  27. #include <windows.h>
  28. #else // Mac
  29. #include <Cocoa/Cocoa.h>
  30. #include <WebKit/npapi.h>
  31. #include <WebKit/npfunctions.h>
  32. #include <WebKit/npruntime.h>
  33. #include <dlfcn.h>
  34. #define OSCALL
  35. #endif
  36. // common includes
  37. #include <string>
  38. #include <algorithm>
  39. #include <vector>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. // Function prototype matching Torque 3D console callback convention
  43. typedef const char * (*StringCallback)(void *obj, int argc, const char *argv[]);
  44. namespace WebCommon
  45. {
  46. // loads the Torque 3D shared library, sets web deployment mode, retrieves engine "C" interface
  47. bool InitTorque3D(void *platformWindow, int clipLeft = 0, int clipTop= 0, int clipRight = 0, int clipBottom = 0);
  48. // sets the current directory to the game install path
  49. bool ChangeToGameDirectory();
  50. // unloads the Torque 3D shared library (first signaling a shutdown for clean exit)
  51. void ShutdownTorque3D();
  52. // checks a given domain against the allowed domains in webConfig.h
  53. bool CheckDomain(const char* url);
  54. // exposes TorqueScript functions marked as secure in webConfig.h, these functions can then be called on the page via Javascript
  55. void AddSecureFunctions();
  56. // bring up a platform specific message box (used for error reporting)
  57. void MessageBox(void* parentWindow, const char* msg, const char* caption);
  58. // a handy string function that eats white space
  59. void StringCopy(char* dst, const char* src, int count);
  60. void SetVariable(const char* variable, const char* value);
  61. const char* GetVariable(const char* variable);
  62. extern std::string gPluginMIMEType;
  63. #ifdef WIN32
  64. // the handle of our plugin's DLL
  65. extern HMODULE gPluginModule;
  66. // the handle of the Torque 3D DLL
  67. extern HMODULE gTorque3DModule;
  68. //string conversion to/from wstring and string
  69. std::string WStringToString( const std::wstring& wstr);
  70. std::wstring StringToWString( const std::string& str);
  71. void ConvertToWindowsPathSep(std::string& path);
  72. #else //Mac
  73. // ptr to the Torque 3D Bundle
  74. extern void* gTorque3DModule;
  75. #endif
  76. };
  77. // C Interface exported from the Torque 3D DLL (or Bundle on Mac)
  78. extern "C"
  79. {
  80. // initialize Torque 3D including argument handling
  81. extern bool (*torque_engineinit)(int argc, const char **argv);
  82. // tick Torque 3D's main loop
  83. extern int (*torque_enginetick)();
  84. // set Torque 3D into web deployment mode (disable fullscreen exlusive mode, etc)
  85. extern int (*torque_setwebdeployment)();
  86. // shutdown the engine
  87. extern bool (*torque_engineshutdown)();
  88. // signal an engine shutdown (as with the quit(); console command)
  89. extern void (*torque_enginesignalshutdown)();
  90. // reset the engine, unloading any current level and returning to the main menu
  91. extern void (*torque_reset)();
  92. // Evaluate arbitrary TorqueScript (ONLY CALL torque_evaluate FROM TRUSTED CODE!!!)
  93. extern const char* (*torque_evaluate)(const char* code);
  94. // Get a console variable
  95. // For security, restricted to "Javascript::" namespace
  96. extern const char* (*torque_getvariable)(const char* name);
  97. // Set a console variable
  98. // For security, restricted to "Javascript::" namespace
  99. extern void (*torque_setvariable)(const char* name, const char* value);
  100. // Export a function to the Torque 3D console system which matches the StringCallback function prototype
  101. // specify the nameSpace, functionName, usage, min and max arguments
  102. extern void (*torque_exportstringcallback)(StringCallback cb, const char *nameSpace, const char *funcName, const char* usage, int minArgs, int maxArgs);
  103. // Set a TorqueScript console function as secure and available for JavaScript via the callScript plugin method
  104. extern void (*torque_addsecurefunction)(const char* nameSpace, const char* fname);
  105. // Call a TorqueScript console function that has been marked as secure
  106. extern const char* (*torque_callsecurefunction)(const char* nameSpace, const char* name, int argc, const char ** argv);
  107. // resize the Torque 3D child window to the specified width and height
  108. extern void (*torque_resizewindow)(int width, int height);
  109. #ifndef WIN32
  110. // On Mac, handle the parent safari window
  111. extern void (*torque_setsafariwindow)(NSWindow* window, int32 x, int32 y, int32 width, int32 height);
  112. // On Mac, sets the executable path
  113. extern void (*torque_setexecutablepath)(const char *path);
  114. #else
  115. // retrieve the render windows hwnd
  116. extern void* (*torque_gethwnd)();
  117. // directly add a message to the Torque 3D event queue, bypassing the Windows event queue
  118. // this is useful in the case of the IE plugin, where we are hooking into an application
  119. // level message, and posting to the windows queue would cause a hang
  120. extern void (*torque_directmessage)(unsigned int message, unsigned int wparam, unsigned int lparam);
  121. #endif
  122. };
  123. #endif