fixed main method not working on unicode systems. 3 spaces -> 4 spaces

This commit is contained in:
Thomas Fischer 2014-03-11 10:57:39 +01:00
parent 2142d452d4
commit f1bb8a0a73

View file

@ -25,48 +25,54 @@
#ifdef WIN32 #ifdef WIN32
#include <windows.h> #include <windows.h>
#include <stdio.h> #include <string>
extern "C" extern "C"
{ {
int (*torque_winmain)( HINSTANCE hInstance, HINSTANCE h, LPSTR lpszCmdLine, int nShow) = NULL; int (*torque_winmain)(HINSTANCE hInstance, HINSTANCE h, LPSTR lpszCmdLine, int nShow) = NULL;
}; };
bool getDllName(std::wstring& dllName)
{
wchar_t filenameBuf[MAX_PATH];
DWORD length = GetModuleFileNameW( NULL, filenameBuf, MAX_PATH );
if(length == 0) return false;
dllName = std::wstring(filenameBuf);
size_t dotPos = dllName.find_last_of(L".");
if(dotPos == std::wstring::npos) return false;
dllName.erase(dotPos);
dllName += L".dll";
return true;
}
int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCommandShow) int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCommandShow)
{ {
char filename[4096]; std::wstring dllName = std::wstring();
char gameLib[4096]; if(!getDllName(dllName)) {
MessageBoxW(NULL, L"Unable to find game dll", L"Error", MB_OK|MB_ICONWARNING);
return -1;
}
HMODULE hGame = LoadLibraryW(dllName.c_str());
if (!hGame) {
wchar_t error[4096];
_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());
MessageBoxW(NULL, error, L"Error", MB_OK|MB_ICONWARNING);
return -1;
}
GetModuleFileNameA(NULL, filename, 4096); torque_winmain = (int (*)(HINSTANCE hInstance, HINSTANCE h, LPSTR lpszCmdLine, int nShow))GetProcAddress(hGame, "torque_winmain");
filename[strlen(filename)-4] = 0; if (!torque_winmain) {
sprintf(gameLib, "%s.dll", filename); wchar_t error[4096];
_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());
MessageBoxW(NULL, error, L"Error", MB_OK|MB_ICONWARNING);
return -1;
}
HMODULE hGame = LoadLibraryA(gameLib); int ret = torque_winmain(hInstance, hPrevInstance, lpszCmdLine, nCommandShow);
if (hGame)
torque_winmain = (int (*)(HINSTANCE hInstance, HINSTANCE h, LPSTR lpszCmdLine, int nShow))GetProcAddress(hGame, "torque_winmain");
char error[4096];
if (!hGame)
{
sprintf(error, "Unable to load game library: %s. Please make sure it exists and the latest DirectX is installed.", gameLib);
MessageBoxA(NULL, error, "Error", MB_OK|MB_ICONWARNING);
return -1;
}
if (!torque_winmain)
{
sprintf(error, "Missing torque_winmain export in game library: %s. Please make sure that it exists and the latest DirectX is installed.", gameLib);
MessageBoxA(NULL, error, "Error", MB_OK|MB_ICONWARNING);
return -1;
}
int ret = torque_winmain(hInstance, hPrevInstance, lpszCmdLine, nCommandShow );
FreeLibrary(hGame);
return ret;
FreeLibrary(hGame);
return ret;
} }
#endif // WIN32 #endif // WIN32
@ -80,113 +86,113 @@ int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdL
extern "C" { extern "C" {
int (*torque_macmain)(int argc, const char **argv) = 0; int (*torque_macmain)(int argc, const char **argv) = 0;
} }
void GetBasePath(const char** cpath, const char** cname) void GetBasePath(const char** cpath, const char** cname)
{ {
static char path[2049]; static char path[2049];
static char name[2049]; static char name[2049];
ProcessSerialNumber PSN; ProcessSerialNumber PSN;
ProcessInfoRec pinfo; ProcessInfoRec pinfo;
FSSpec pspec; FSSpec pspec;
FSRef fsr; FSRef fsr;
OSStatus err; OSStatus err;
path[0] = 0; path[0] = 0;
name[0] = 0; name[0] = 0;
*cpath = path; *cpath = path;
*cname = name; *cname = name;
// set up process serial number // set up process serial number
PSN.highLongOfPSN = 0; PSN.highLongOfPSN = 0;
PSN.lowLongOfPSN = kCurrentProcess; PSN.lowLongOfPSN = kCurrentProcess;
// set up info block // set up info block
pinfo.processInfoLength = sizeof(pinfo); pinfo.processInfoLength = sizeof(pinfo);
pinfo.processName = NULL; pinfo.processName = NULL;
pinfo.processAppSpec = &pspec; pinfo.processAppSpec = &pspec;
// grab the vrefnum and directory // grab the vrefnum and directory
err = GetProcessInformation(&PSN, &pinfo); err = GetProcessInformation(&PSN, &pinfo);
if (! err ) { if (! err ) {
FSSpec fss2; FSSpec fss2;
strcpy(name, &pspec.name[1]); strcpy(name, &pspec.name[1]);
err = FSMakeFSSpec(pspec.vRefNum, pspec.parID, 0, &fss2); err = FSMakeFSSpec(pspec.vRefNum, pspec.parID, 0, &fss2);
if ( ! err ) { if ( ! err ) {
err = FSpMakeFSRef(&fss2, &fsr); err = FSpMakeFSRef(&fss2, &fsr);
if ( ! err ) { if ( ! err ) {
err = (OSErr)FSRefMakePath(&fsr, (UInt8*)path, 2048); err = (OSErr)FSRefMakePath(&fsr, (UInt8*)path, 2048);
} }
} }
} }
} }
int main(int argc, const char **argv) int main(int argc, const char **argv)
{ {
void *gameBundle = 0; void *gameBundle = 0;
char gameBundleFilename[2049]; char gameBundleFilename[2049];
const char* basePath; const char* basePath;
const char* appName; const char* appName;
// Get the path to our app binary and the app name // Get the path to our app binary and the app name
GetBasePath(&basePath, &appName); GetBasePath(&basePath, &appName);
if (!basePath[0] || !appName[0]) if (!basePath[0] || !appName[0])
return; return;
char appNameNoDebug[2049]; char appNameNoDebug[2049];
strcpy(appNameNoDebug, appName); strcpy(appNameNoDebug, appName);
int i = strlen(appName); int i = strlen(appName);
while (i > 0) while (i > 0)
{ {
if (!strcmp(&appName[i], "_DEBUG")) if (!strcmp(&appName[i], "_DEBUG"))
{ {
appNameNoDebug[i] = 0; appNameNoDebug[i] = 0;
break; break;
} }
i--; i--;
} }
sprintf(gameBundleFilename, "%s.app/Contents/Frameworks/%s Bundle.bundle/Contents/MacOS/%s Bundle", appName, appNameNoDebug, appNameNoDebug); sprintf(gameBundleFilename, "%s.app/Contents/Frameworks/%s Bundle.bundle/Contents/MacOS/%s Bundle", appName, appNameNoDebug, appNameNoDebug);
// first see if the current directory is set properly // first see if the current directory is set properly
gameBundle = dlopen(gameBundleFilename, RTLD_LAZY | RTLD_LOCAL); gameBundle = dlopen(gameBundleFilename, RTLD_LAZY | RTLD_LOCAL);
if (!gameBundle) if (!gameBundle)
{ {
// Couldn't load the game bundle... so, using the path to the bundle binary fix up the cwd // Couldn't load the game bundle... so, using the path to the bundle binary fix up the cwd
if (basePath[0]) { if (basePath[0]) {
chdir( basePath ); chdir( basePath );
chdir( "../../../" ); chdir( "../../../" );
} }
// and try again // and try again
gameBundle = dlopen( gameBundleFilename, RTLD_LAZY | RTLD_LOCAL); gameBundle = dlopen( gameBundleFilename, RTLD_LAZY | RTLD_LOCAL);
} }
if (!gameBundle) if (!gameBundle)
return -1; return -1;
torque_macmain = (int (*)(int argc, const char **argv)) dlsym(gameBundle, "torque_macmain"); torque_macmain = (int (*)(int argc, const char **argv)) dlsym(gameBundle, "torque_macmain");
if (!torque_macmain) if (!torque_macmain)
return -1; return -1;
return torque_macmain(argc, argv); return torque_macmain(argc, argv);
} }
#endif // __MACOSX #endif // __MACOSX
@ -200,49 +206,49 @@ int main(int argc, const char **argv)
extern "C" extern "C"
{ {
int (*torque_unixmain)(int argc, const char **argv) = NULL; int (*torque_unixmain)(int argc, const char **argv) = NULL;
void(*setExePathName)(const char *exePathName) = NULL; void(*setExePathName)(const char *exePathName) = NULL;
} }
int main(int argc, const char **argv) int main(int argc, const char **argv)
{ {
// assume bin name is in argv[0] // assume bin name is in argv[0]
int len = strlen(argv[0]); int len = strlen(argv[0]);
char *libName = new char[len+4]; // len + .so + NUL char *libName = new char[len+4]; // len + .so + NUL
strcpy(libName, argv[0]); strcpy(libName, argv[0]);
strcat(libName, ".so"); strcat(libName, ".so");
// try to load the game lib // try to load the game lib
void *gameLib = dlopen(libName, RTLD_LAZY | RTLD_LOCAL); void *gameLib = dlopen(libName, RTLD_LAZY | RTLD_LOCAL);
delete [] libName; delete [] libName;
if(gameLib == NULL) if(gameLib == NULL)
{ {
printf("%s\n", dlerror()); printf("%s\n", dlerror());
return -1; return -1;
} }
// set the filename of the exe image // set the filename of the exe image
setExePathName = (void(*)(const char *)) dlsym(gameLib, "setExePathName"); setExePathName = (void(*)(const char *)) dlsym(gameLib, "setExePathName");
if(setExePathName == NULL) if(setExePathName == NULL)
{ {
printf("%s\n", dlerror()); printf("%s\n", dlerror());
return -1; return -1;
} }
setExePathName(argv[0]); setExePathName(argv[0]);
// try to load the lib entry point // try to load the lib entry point
torque_unixmain = (int(*)(int argc, const char **argv)) dlsym(gameLib, "torque_unixmain"); torque_unixmain = (int(*)(int argc, const char **argv)) dlsym(gameLib, "torque_unixmain");
if(torque_unixmain == NULL) if(torque_unixmain == NULL)
{ {
printf("%s\n", dlerror()); printf("%s\n", dlerror());
return -1; return -1;
} }
// Go! // Go!
return torque_unixmain(argc, argv); return torque_unixmain(argc, argv);
} }
#endif // __linux__ #endif // __linux__
@ -260,39 +266,39 @@ int main(int argc, const char **argv)
// will need to merge against future changes to the SML code if you do this. // will need to merge against future changes to the SML code if you do this.
S32 TorqueMain(S32 argc, const char **argv) S32 TorqueMain(S32 argc, const char **argv)
{ {
// Some handy debugging code: // Some handy debugging code:
// if (argc == 1) { // if (argc == 1) {
// static const char* argvFake[] = { "dtest.exe", "-jload", "test.jrn" }; // static const char* argvFake[] = { "dtest.exe", "-jload", "test.jrn" };
// argc = 3; // argc = 3;
// argv = argvFake; // argv = argvFake;
// } // }
// Memory::enableLogging("testMem.log"); // Memory::enableLogging("testMem.log");
// Memory::setBreakAlloc(104717); // Memory::setBreakAlloc(104717);
// Initialize the subsystems. // Initialize the subsystems.
StandardMainLoop::init(); StandardMainLoop::init();
// Handle any command line args. // Handle any command line args.
if(!StandardMainLoop::handleCommandLine(argc, argv)) if(!StandardMainLoop::handleCommandLine(argc, argv))
{ {
Platform::AlertOK("Error", "Failed to initialize game, shutting down."); Platform::AlertOK("Error", "Failed to initialize game, shutting down.");
return 1; return 1;
} }
// Main loop // Main loop
while(StandardMainLoop::doMainLoop()); while(StandardMainLoop::doMainLoop());
// Clean everything up. // Clean everything up.
StandardMainLoop::shutdown(); StandardMainLoop::shutdown();
// Do we need to restart? // Do we need to restart?
if( StandardMainLoop::requiresRestart() ) if( StandardMainLoop::requiresRestart() )
Platform::restartInstance(); Platform::restartInstance();
// Return. // Return.
return 0; return 0;
} }
#endif //TORQUE_SHARED #endif //TORQUE_SHARED