Skip to content

Commit 190614f

Browse files
committed
Fix for unicode paths failing to launch.
Using short paths + ACP code page (instead of UTF8).
1 parent 0979187 commit 190614f

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

Launcher.cpp

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ bool IsValidLuaFile(const std::wstring &path, std::string &firstLine)
7272
return true;
7373
}
7474

75+
bool InsertPath(std::vector<std::wstring> &commandLine, std::wstring path)
76+
{
77+
DWORD requiredLength = GetShortPathName(path.c_str(), nullptr, 0);
78+
if (requiredLength == 0)
79+
{
80+
return false;
81+
}
82+
83+
std::wstring shortPath(requiredLength, L'\0');
84+
requiredLength = GetShortPathName(path.c_str(), shortPath.data(), requiredLength);
85+
if (requiredLength == 0)
86+
{
87+
return false;
88+
}
89+
90+
commandLine.insert(commandLine.begin() + 1, shortPath);
91+
}
92+
7593
bool FindLaunchLua(std::wstring basePath, std::vector<std::wstring> &commandLine, std::string &firstLine)
7694
{
7795
// Unify path separator characters
@@ -93,16 +111,14 @@ bool FindLaunchLua(std::wstring basePath, std::vector<std::wstring> &commandLine
93111
std::wstring launchPath = basePath + L"\\Launch.lua";
94112
if (IsValidLuaFile(launchPath, firstLine))
95113
{
96-
commandLine.insert(commandLine.begin() + 1, launchPath);
97-
return true;
114+
return InsertPath(commandLine, launchPath);
98115
}
99116

100117
// Look for src\\Launch.lua
101118
launchPath = basePath + L"\\src\\Launch.lua";
102119
if (IsValidLuaFile(launchPath, firstLine))
103120
{
104-
commandLine.insert(commandLine.begin() + 1, launchPath);
105-
return true;
121+
return InsertPath(commandLine, launchPath);
106122
}
107123

108124
// If the base path ends with "runtime" then strip that off, append "src" and look for Launch.lua there
@@ -125,8 +141,7 @@ bool FindLaunchLua(std::wstring basePath, std::vector<std::wstring> &commandLine
125141
launchPath = parentPath + L"\\src\\Launch.lua";
126142
if (IsValidLuaFile(launchPath, firstLine))
127143
{
128-
commandLine.insert(commandLine.begin() + 1, launchPath);
129-
return true;
144+
return InsertPath(commandLine, launchPath);
130145
}
131146
}
132147
}
@@ -219,21 +234,21 @@ bool InsertLaunchLua(std::vector<std::wstring> &commandLine, std::string &firstL
219234
return false;
220235
}
221236

222-
std::vector<std::string> ConvertToUTF8(std::vector<std::wstring> commandLine)
237+
std::vector<std::string> ConvertToACP(std::vector<std::wstring> commandLine)
223238
{
224-
std::vector<std::string> commandLineUTF8;
239+
std::vector<std::string> commandLineACP;
225240
if (commandLine.size() > 0)
226241
{
227-
commandLineUTF8.reserve(commandLine.size());
242+
commandLineACP.reserve(commandLine.size());
228243
for (const std::wstring &param : commandLine)
229244
{
230-
int dwUTF8Size = WideCharToMultiByte(CP_UTF8, 0, param.c_str(), (int)param.size(), NULL, 0, NULL, NULL);
231-
std::string paramUTF8(dwUTF8Size, 0);
232-
WideCharToMultiByte(CP_UTF8, 0, param.c_str(), (int)param.size(), paramUTF8.data(), dwUTF8Size, NULL, NULL);
233-
commandLineUTF8.emplace_back(std::move(paramUTF8));
245+
int dwACPSize = WideCharToMultiByte(CP_ACP, 0, param.c_str(), (int)param.size(), NULL, 0, NULL, NULL);
246+
std::string paramACP(dwACPSize, 0);
247+
WideCharToMultiByte(CP_ACP, 0, param.c_str(), (int)param.size(), paramACP.data(), dwACPSize, NULL, NULL);
248+
commandLineACP.emplace_back(std::move(paramACP));
234249
}
235250
}
236-
return commandLineUTF8;
251+
return commandLineACP;
237252
}
238253

239254
void InitConsole()
@@ -300,18 +315,18 @@ int CALLBACK wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance
300315
}
301316

302317
// Create a utf8 version of the commandline parameters
303-
std::vector<std::string> commandLineUTF8 = ConvertToUTF8(commandLine);
318+
std::vector<std::string> commandLineACP = ConvertToACP(commandLine);
304319

305320
// Remove the first commandline argument as the scripts don't care about that.
306-
commandLineUTF8.erase(commandLineUTF8.begin());
321+
commandLineACP.erase(commandLineACP.begin());
307322

308323
// Convert the commandline parameters to a form the DLL can understand
309324
size_t dwTotalParamSize = 0;
310-
for (const std::string &param : commandLineUTF8)
325+
for (const std::string &param : commandLineACP)
311326
{
312327
dwTotalParamSize += param.size() + 1;
313328
}
314-
size_t dwNumParams = commandLineUTF8.size();
329+
size_t dwNumParams = commandLineACP.size();
315330
std::unique_ptr<char[]> pParamBuf = std::make_unique<char[]>(dwTotalParamSize);
316331
char *pCurParamBufLoc = pParamBuf.get();
317332

@@ -320,7 +335,7 @@ int CALLBACK wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance
320335
{
321336
ppParamList[i] = pCurParamBufLoc;
322337

323-
const std::string &param = commandLineUTF8[i];
338+
const std::string &param = commandLineACP[i];
324339
memcpy(pCurParamBufLoc, param.c_str(), param.size() + 1);
325340
pCurParamBufLoc += param.size() + 1;
326341
}

0 commit comments

Comments
 (0)