win32: correctly parse unicode command line arguments

This commit is contained in:
OV2 2014-03-22 15:07:15 +01:00
parent 6d74746342
commit 8fb48d0f60
2 changed files with 34 additions and 19 deletions

View File

@ -293,8 +293,6 @@ static bool try_save(const char *fname, ConfigFile &conf){
return false; return false;
} }
static char rom_filename [MAX_PATH] = {0};
static bool S9xSaveConfigFile(ConfigFile &conf){ static bool S9xSaveConfigFile(ConfigFile &conf){
configMutex = CreateMutex(NULL, FALSE, TEXT("Snes9xConfigMutex")); configMutex = CreateMutex(NULL, FALSE, TEXT("Snes9xConfigMutex"));
@ -344,18 +342,30 @@ static inline char *SkipSpaces (char *p)
return (p); return (p);
} }
const char* WinParseCommandLineAndLoadConfigFile (char *line) const TCHAR* WinParseCommandLineAndLoadConfigFile (TCHAR *line)
{ {
// Break the command line up into an array of string pointers, each pointer // Break the command line up into an array of string pointers, each pointer
// points at a separate word or character sequence enclosed in quotes. // points at a separate word or character sequence enclosed in quotes.
int count = 0;
static TCHAR return_filename[MAX_PATH];
#ifdef UNICODE
// split params into argv
TCHAR **params = CommandLineToArgvW(line, &count);
// convert all parameters to utf8
char **parameters = new char*[count];
for(int i = 0; i < count; i++) {
int requiredChars = WideCharToMultiByte(CP_UTF8, 0, params[i], -1, NULL, 0, NULL, NULL);
parameters[i] = new char[requiredChars];
WideCharToMultiByte(CP_UTF8, 0, params[i], -1, parameters[i], requiredChars, NULL, NULL);
}
LocalFree(params);
#else
#define MAX_PARAMETERS 100 #define MAX_PARAMETERS 100
char *p = line; char *p = line;
static char *parameters [MAX_PARAMETERS]; char *parameters[MAX_PARAMETERS];
int count = 0;
//parameters [count++] = "Snes9X";
while (count < MAX_PARAMETERS && *p) while (count < MAX_PARAMETERS && *p)
{ {
p = SkipSpaces (p); p = SkipSpaces (p);
@ -387,6 +397,7 @@ const char* WinParseCommandLineAndLoadConfigFile (char *line)
} }
} }
#endif
configMutex = CreateMutex(NULL, FALSE, TEXT("Snes9xConfigMutex")); configMutex = CreateMutex(NULL, FALSE, TEXT("Snes9xConfigMutex"));
int times = 0; int times = 0;
DWORD waitVal = WAIT_TIMEOUT; DWORD waitVal = WAIT_TIMEOUT;
@ -427,12 +438,19 @@ const char* WinParseCommandLineAndLoadConfigFile (char *line)
CloseHandle(configMutex); CloseHandle(configMutex);
const char* rf = S9xParseArgs (parameters, count); const char* rf = S9xParseArgs (parameters, count);
/*if(rf)
strcpy(rom_filename, rf);
else
rom_filename[0] = '\0';*/
return rf; if(rf) // save rom_filename as TCHAR if available
lstrcpy(return_filename, _tFromChar(rf));
#ifdef UNICODE
// free parameters
for(int i = 0; i < count; i++) {
delete [] parameters[i];
}
delete [] parameters;
#endif
return rf ? return_filename : NULL;
} }
#define S(x) GAMEDEVICE_VK_##x #define S(x) GAMEDEVICE_VK_##x
@ -955,7 +973,6 @@ void WinRegisterConfigItems()
AddBoolC("Cheat", Settings.ApplyCheats, true, "true to allow enabled cheats to be applied"); AddBoolC("Cheat", Settings.ApplyCheats, true, "true to allow enabled cheats to be applied");
AddInvBoolC("Patch", Settings.NoPatch, true, "true to allow IPS/UPS patches to be applied (\"soft patching\")"); AddInvBoolC("Patch", Settings.NoPatch, true, "true to allow IPS/UPS patches to be applied (\"soft patching\")");
AddBoolC("BS", Settings.BS, false, "Broadcast Satellaview emulation"); AddBoolC("BS", Settings.BS, false, "Broadcast Satellaview emulation");
AddStringC("Filename", rom_filename, MAX_PATH, "", "filename of ROM to run when Snes9x opens");
#undef CATEGORY #undef CATEGORY
#ifdef NETPLAY_SUPPORT #ifdef NETPLAY_SUPPORT
#define CATEGORY "Netplay" #define CATEGORY "Netplay"

View File

@ -635,7 +635,7 @@ bool8 S9xLoadROMImage (const TCHAR *string);
static void EnableServer (bool8 enable); static void EnableServer (bool8 enable);
#endif #endif
void WinDeleteRecentGamesList (); void WinDeleteRecentGamesList ();
const char* WinParseCommandLineAndLoadConfigFile (char *line); const TCHAR* WinParseCommandLineAndLoadConfigFile (TCHAR *line);
void WinRegisterConfigItems (); void WinRegisterConfigItems ();
void WinSaveConfigFile (); void WinSaveConfigFile ();
void WinSetDefaultValues (); void WinSetDefaultValues ();
@ -3330,7 +3330,6 @@ int WINAPI WinMain(
LPSTR lpCmdLine, LPSTR lpCmdLine,
int nCmdShow) int nCmdShow)
{ {
char cmdLine[MAX_PATH];
Settings.StopEmulation = TRUE; Settings.StopEmulation = TRUE;
SetCurrentDirectory(S9xGetDirectoryT(DEFAULT_DIR)); SetCurrentDirectory(S9xGetDirectoryT(DEFAULT_DIR));
@ -3352,8 +3351,7 @@ int WINAPI WinMain(
ChangeInputDevice(); ChangeInputDevice();
strcpy(cmdLine,_tToChar(GetCommandLine())); const TCHAR *rom_filename = WinParseCommandLineAndLoadConfigFile (GetCommandLine());
const char *rom_filename = WinParseCommandLineAndLoadConfigFile (cmdLine);
WinSaveConfigFile (); WinSaveConfigFile ();
WinLockConfigFile (); WinLockConfigFile ();
@ -3422,7 +3420,7 @@ int WINAPI WinMain(
} }
if(rom_filename) if(rom_filename)
LoadROM(_tFromChar(rom_filename)); LoadROM(rom_filename);
S9xUnmapAllControls(); S9xUnmapAllControls();
S9xSetupDefaultKeymap(); S9xSetupDefaultKeymap();