main.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. #ifdef TORQUE_SHARED
  23. #ifdef WIN32
  24. #include <windows.h>
  25. #include <string>
  26. extern "C"
  27. {
  28. int (*torque_winmain)( HINSTANCE hInstance, HINSTANCE h, LPSTR lpszCmdLine, int nShow) = NULL;
  29. };
  30. bool getDllName(std::wstring& dllName, const std::wstring suffix)
  31. {
  32. wchar_t filenameBuf[MAX_PATH];
  33. DWORD length = GetModuleFileNameW( NULL, filenameBuf, MAX_PATH );
  34. if(length == 0) return false;
  35. dllName = std::wstring(filenameBuf);
  36. size_t dotPos = dllName.find_last_of(L".");
  37. if(dotPos == std::wstring::npos)
  38. {
  39. dllName.clear();
  40. return false;
  41. }
  42. dllName.erase(dotPos);
  43. dllName += suffix + L".dll";
  44. return true;
  45. }
  46. int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCommandShow)
  47. {
  48. // Try to find the game DLL, which may have one of several file names.
  49. HMODULE hGame = NULL;
  50. std::wstring dllName = std::wstring();
  51. // The file name is the same as this executable's name, plus a suffix.
  52. const std::wstring dllSuffices[] = {L" DLL", L""};
  53. const unsigned int numSuffices = sizeof(dllSuffices) / sizeof(std::wstring);
  54. for (unsigned int i = 0; i < numSuffices; i++)
  55. {
  56. // Attempt to glue the suffix onto the current filename.
  57. if(!getDllName(dllName, dllSuffices[i]))
  58. continue;
  59. // Load the DLL at that address.
  60. hGame = LoadLibraryW(dllName.c_str());
  61. if (hGame)
  62. break;
  63. }
  64. if(!dllName.length())
  65. {
  66. MessageBoxW(NULL, L"Unable to find game dll", L"Error", MB_OK|MB_ICONWARNING);
  67. return -1;
  68. }
  69. if (!hGame)
  70. {
  71. wchar_t error[4096];
  72. _swprintf_l(error, sizeof(error), L"Unable to load game library: %s. Please make sure it exists and the latest DirectX is installed.", _get_current_locale(), dllName.c_str());
  73. MessageBoxW(NULL, error, L"Error", MB_OK|MB_ICONWARNING);
  74. return -1;
  75. }
  76. torque_winmain = (int (*)(HINSTANCE hInstance, HINSTANCE h, LPSTR lpszCmdLine, int nShow))GetProcAddress(hGame, "torque_winmain");
  77. if (!torque_winmain)
  78. {
  79. wchar_t error[4096];
  80. _swprintf_l(error, sizeof(error), L"Missing torque_winmain export in game library: %s. Please make sure that it exists and the latest DirectX is installed.", _get_current_locale(), dllName.c_str());
  81. MessageBoxW(NULL, error, L"Error", MB_OK|MB_ICONWARNING);
  82. return -1;
  83. }
  84. int ret = torque_winmain(hInstance, hPrevInstance, lpszCmdLine, nCommandShow);
  85. FreeLibrary(hGame);
  86. return ret;
  87. }
  88. #endif // WIN32
  89. #ifdef __MACOSX__
  90. #include <dlfcn.h>
  91. #include <stdio.h>
  92. #include <unistd.h>
  93. #include <Carbon/Carbon.h>
  94. extern "C" {
  95. int (*torque_macmain)(int argc, const char **argv) = 0;
  96. }
  97. void GetBasePath(const char** cpath, const char** cname)
  98. {
  99. static char path[2049];
  100. static char name[2049];
  101. ProcessSerialNumber PSN;
  102. ProcessInfoRec pinfo;
  103. FSSpec pspec;
  104. FSRef fsr;
  105. OSStatus err;
  106. path[0] = 0;
  107. name[0] = 0;
  108. *cpath = path;
  109. *cname = name;
  110. // set up process serial number
  111. PSN.highLongOfPSN = 0;
  112. PSN.lowLongOfPSN = kCurrentProcess;
  113. // set up info block
  114. pinfo.processInfoLength = sizeof(pinfo);
  115. pinfo.processName = NULL;
  116. pinfo.processAppSpec = &pspec;
  117. // grab the vrefnum and directory
  118. err = GetProcessInformation(&PSN, &pinfo);
  119. if (! err ) {
  120. FSSpec fss2;
  121. strcpy(name, &pspec.name[1]);
  122. err = FSMakeFSSpec(pspec.vRefNum, pspec.parID, 0, &fss2);
  123. if ( ! err ) {
  124. err = FSpMakeFSRef(&fss2, &fsr);
  125. if ( ! err ) {
  126. err = (OSErr)FSRefMakePath(&fsr, (UInt8*)path, 2048);
  127. }
  128. }
  129. }
  130. }
  131. int main(int argc, const char **argv)
  132. {
  133. void *gameBundle = 0;
  134. char gameBundleFilename[2049];
  135. const char* basePath;
  136. const char* appName;
  137. // Get the path to our app binary and the app name
  138. GetBasePath(&basePath, &appName);
  139. if (!basePath[0] || !appName[0])
  140. return;
  141. char appNameNoDebug[2049];
  142. strcpy(appNameNoDebug, appName);
  143. int i = strlen(appName);
  144. while (i > 0)
  145. {
  146. if (!strcmp(&appName[i], "_DEBUG"))
  147. {
  148. appNameNoDebug[i] = 0;
  149. break;
  150. }
  151. i--;
  152. }
  153. sprintf(gameBundleFilename, "%s.app/Contents/Frameworks/%s Bundle.bundle/Contents/MacOS/%s Bundle", appName, appNameNoDebug, appNameNoDebug);
  154. // first see if the current directory is set properly
  155. gameBundle = dlopen(gameBundleFilename, RTLD_LAZY | RTLD_LOCAL);
  156. if (!gameBundle)
  157. {
  158. // Couldn't load the game bundle... so, using the path to the bundle binary fix up the cwd
  159. if (basePath[0]) {
  160. chdir( basePath );
  161. chdir( "../../../" );
  162. }
  163. // and try again
  164. gameBundle = dlopen( gameBundleFilename, RTLD_LAZY | RTLD_LOCAL);
  165. }
  166. if (!gameBundle)
  167. return -1;
  168. torque_macmain = (int (*)(int argc, const char **argv)) dlsym(gameBundle, "torque_macmain");
  169. if (!torque_macmain)
  170. return -1;
  171. return torque_macmain(argc, argv);
  172. }
  173. #endif // __MACOSX
  174. #ifdef __linux__
  175. #include <dlfcn.h>
  176. #include <stdio.h>
  177. #include <unistd.h>
  178. #include <string.h>
  179. extern "C"
  180. {
  181. int (*torque_unixmain)(int argc, const char **argv) = NULL;
  182. void(*setExePathName)(const char *exePathName) = NULL;
  183. }
  184. int main(int argc, const char **argv)
  185. {
  186. // assume bin name is in argv[0]
  187. int len = strlen(argv[0]);
  188. char *libName = new char[len+4]; // len + .so + NUL
  189. strcpy(libName, argv[0]);
  190. strcat(libName, ".so");
  191. // try to load the game lib
  192. void *gameLib = dlopen(libName, RTLD_LAZY | RTLD_LOCAL);
  193. delete [] libName;
  194. if(gameLib == NULL)
  195. {
  196. printf("%s\n", dlerror());
  197. return -1;
  198. }
  199. // set the filename of the exe image
  200. setExePathName = (void(*)(const char *)) dlsym(gameLib, "setExePathName");
  201. if(setExePathName == NULL)
  202. {
  203. printf("%s\n", dlerror());
  204. return -1;
  205. }
  206. setExePathName(argv[0]);
  207. // try to load the lib entry point
  208. torque_unixmain = (int(*)(int argc, const char **argv)) dlsym(gameLib, "torque_unixmain");
  209. if(torque_unixmain == NULL)
  210. {
  211. printf("%s\n", dlerror());
  212. return -1;
  213. }
  214. // Go!
  215. return torque_unixmain(argc, argv);
  216. }
  217. #endif // __linux__
  218. #else //static exe build
  219. #include "platform/platform.h"
  220. #include "app/mainLoop.h"
  221. #include "T3D/gameFunctions.h"
  222. // Entry point for your game.
  223. //
  224. // This is build by default using the "StandardMainLoop" toolkit. Feel free
  225. // to bring code over directly as you need to modify or extend things. You
  226. // will need to merge against future changes to the SML code if you do this.
  227. S32 TorqueMain(S32 argc, const char **argv)
  228. {
  229. // Some handy debugging code:
  230. // if (argc == 1) {
  231. // static const char* argvFake[] = { "dtest.exe", "-jload", "test.jrn" };
  232. // argc = 3;
  233. // argv = argvFake;
  234. // }
  235. // Memory::enableLogging("testMem.log");
  236. // Memory::setBreakAlloc(104717);
  237. // Initialize the subsystems.
  238. StandardMainLoop::init();
  239. // Handle any command line args.
  240. if(!StandardMainLoop::handleCommandLine(argc, argv))
  241. {
  242. Platform::AlertOK("Error", "Failed to initialize game, shutting down.");
  243. return 1;
  244. }
  245. // Main loop
  246. while(StandardMainLoop::doMainLoop());
  247. // Clean everything up.
  248. StandardMainLoop::shutdown();
  249. // Do we need to restart?
  250. if( StandardMainLoop::requiresRestart() )
  251. Platform::restartInstance();
  252. // Return.
  253. return 0;
  254. }
  255. #endif //TORQUE_SHARED