Skip to content

Commit

Permalink
Win32: Use wide strings to parse most command-line arguments.
Browse files Browse the repository at this point in the history
Also do a tiny bit of cleanup in main.cpp (replacing strcmps with simple string equality checks).
  • Loading branch information
thedax committed Aug 31, 2014
1 parent cdbcc12 commit 3590352
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 26 deletions.
2 changes: 2 additions & 0 deletions Common/CommonWindows.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ extern "C" void _ReadBarrier();

#else
#include <Windows.h>
#include <vector>
extern std::vector<std::wstring> GetWideCmdLine();
#endif

#undef min
Expand Down
7 changes: 5 additions & 2 deletions Core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,11 @@ std::map<std::string, std::pair<std::string, int>> GetLangValuesMapping() {
}

void Config::Load(const char *iniFileName, const char *controllerIniFilename) {
iniFilename_ = FindConfigFile(iniFileName != NULL ? iniFileName : "ppsspp.ini");
controllerIniFilename_ = FindConfigFile(controllerIniFilename != NULL ? controllerIniFilename : "controls.ini");
const bool useIniFilename = (iniFileName != NULL) && (strlen(iniFileName) > 0);
iniFilename_ = FindConfigFile(useIniFilename ? iniFileName : "ppsspp.ini");

const bool useControllerIniFilename = (controllerIniFilename != NULL) && (strlen(controllerIniFilename) > 0);
controllerIniFilename_ = FindConfigFile(useControllerIniFilename ? controllerIniFilename : "controls.ini");

INFO_LOG(LOADER, "Loading config: %s", iniFilename_.c_str());
bSaveSettings = true;
Expand Down
22 changes: 21 additions & 1 deletion Windows/EmuThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,27 @@ unsigned int WINAPI TheThread(void *)

Host *oldHost = host;

NativeInit(__argc, (const char **)__argv, "1234", "1234", "1234");
// Convert the command-line arguments to Unicode, then to proper UTF-8
// (the benefit being that we don't have to pollute the UI project with win32 ifdefs and lots of Convert<whatever>To<whatever>).
// This avoids issues with PPSSPP inadvertently destroying paths with Unicode glyphs
// (using the ANSI args resulted in Japanese/Chinese glyphs being turned into question marks, at least for me..).
// -TheDax
std::vector<std::wstring> wideArgs = GetWideCmdLine();
std::vector<std::string> argsUTF8;
for (auto& i : wideArgs)
{
argsUTF8.push_back(ConvertWStringToUTF8(i));
}

std::vector<const char *> args;

for (auto& string: argsUTF8)
{
args.push_back(string.c_str());
}

NativeInit(args.size(), &args[0], "1234", "1234", "1234");

Host *nativeHost = host;
host = oldHost;

Expand Down
60 changes: 37 additions & 23 deletions Windows/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,16 @@ void MakePPSSPPDPIAware()
}
}

std::vector<std::wstring> GetWideCmdLine() {
wchar_t **wargv;
int wargc = -1;
wargv = CommandLineToArgvW(GetCommandLineW(), &wargc);

std::vector<std::wstring> wideArgs(wargv, wargv + wargc);

return wideArgs;
}

int WINAPI WinMain(HINSTANCE _hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
{
setCurrentThreadName("Main");
Expand Down Expand Up @@ -385,23 +395,29 @@ int WINAPI WinMain(HINSTANCE _hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLin

osName = GetWindowsVersion() + " " + GetWindowsSystemArchitecture();

const char *configFilename = NULL;
const char *configOption = "--config=";
char configFilename[MAX_PATH] = { 0 };
const std::wstring configOption = L"--config=";

char controlsConfigFilename[MAX_PATH] = { 0 };
const std::wstring controlsOption = L"--controlconfig=";

const char *controlsConfigFilename = NULL;
const char *controlsOption = "--controlconfig=";
std::vector<std::wstring> wideArgs = GetWideCmdLine();

for (int i = 1; i < __argc; ++i)
for (size_t i = 1; i < wideArgs.size(); ++i)
{
if (__argv[i][0] == '\0')
if (wideArgs[i][0] == L'\0')
continue;
if (__argv[i][0] == '-')
{
if (!strncmp(__argv[i], configOption, strlen(configOption)) && strlen(__argv[i]) > strlen(configOption)) {
configFilename = __argv[i] + strlen(configOption);
if (wideArgs[i][0] == L'-') {
if (wideArgs[i].find(configOption) != std::wstring::npos && wideArgs[i].size() > configOption.size()) {
const std::wstring tempWide = wideArgs[i].substr(configOption.size());
const std::string tempStr = ConvertWStringToUTF8(tempWide);
std::strncpy(configFilename, tempStr.c_str(), MAX_PATH);
}
if (!strncmp(__argv[i], controlsOption, strlen(controlsOption)) && strlen(__argv[i]) > strlen(controlsOption)) {
controlsConfigFilename = __argv[i] + strlen(controlsOption);

if (wideArgs[i].find(controlsOption) != std::wstring::npos && wideArgs[i].size() > controlsOption.size()) {
const std::wstring tempWide = wideArgs[i].substr(configOption.size());
const std::string tempStr = ConvertWStringToUTF8(tempWide);
std::strncpy(configFilename, tempStr.c_str(), MAX_PATH);
}
}
}
Expand All @@ -420,32 +436,30 @@ int WINAPI WinMain(HINSTANCE _hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLin
bool debugLogLevel = false;

// The rest is handled in NativeInit().
for (int i = 1; i < __argc; ++i)
for (size_t i = 1; i < wideArgs.size(); ++i)
{
if (__argv[i][0] == '\0')
if (wideArgs[i][0] == L'\0')
continue;

if (__argv[i][0] == '-')
{
switch (__argv[i][1])
{
case 'l':
if (wideArgs[i][0] == L'-') {
switch (wideArgs[i][1]) {
case L'l':
showLog = true;
g_Config.bEnableLogging = true;
break;
case 's':
case L's':
g_Config.bAutoRun = false;
g_Config.bSaveSettings = false;
break;
case 'd':
case L'd':
debugLogLevel = true;
break;
}

if (!strncmp(__argv[i], "--fullscreen", strlen("--fullscreen")))
if (wideArgs[i] == L"--fullscreen")
g_Config.bFullScreen = true;

if (!strncmp(__argv[i], "--windowed", strlen("--windowed")))
if (wideArgs[i] == L"--windowed")
g_Config.bFullScreen = false;
}
}
Expand Down

0 comments on commit 3590352

Please sign in to comment.