Fixed crash on non-latin instance name

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2024-04-03 19:34:35 +03:00
parent 90097f1309
commit 450b73328e
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318

View File

@ -74,16 +74,36 @@ QString shortPathName(const QString& file)
auto input = file.toStdWString();
std::wstring output;
long length = GetShortPathNameW(input.c_str(), NULL, 0);
if (length == 0)
return {};
// NOTE: this resizing might seem weird...
// when GetShortPathNameW fails, it returns length including null character
// when it succeeds, it returns length excluding null character
// See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa364989(v=vs.85).aspx
output.resize(length);
GetShortPathNameW(input.c_str(), (LPWSTR)output.c_str(), length);
if (GetShortPathNameW(input.c_str(), (LPWSTR)output.c_str(), length) == 0)
return {};
output.resize(length - 1);
QString ret = QString::fromStdWString(output);
return ret;
}
QString getShortPathName(const QString& file)
{
auto path = shortPathName(file);
if (!path.isEmpty())
return path;
// the path can not be getted due to the file/folder not existing
// so create the parrent folder
// and assume that we can concatenate the short path of the parent folder with the file name
// usually the 8 bit characters are in the instance name not in the name of the end files/folders we need
FS::ensureFilePathExists(file);
QFileInfo a(file);
auto partialShortPath = shortPathName(a.path());
if (!partialShortPath.isEmpty())
return FS::PathCombine(partialShortPath, a.fileName());
return file;
}
#endif
// if the string survives roundtrip through local 8bit encoding...
@ -137,7 +157,7 @@ void LauncherPartLaunch::executeTask()
auto natPath = minecraftInstance->getNativePath();
#ifdef Q_OS_WIN
if (!fitsInLocal8bit(natPath)) {
args << "-Djava.library.path=" + shortPathName(natPath);
args << "-Djava.library.path=" + getShortPathName(natPath);
} else {
args << "-Djava.library.path=" + natPath;
}
@ -150,7 +170,7 @@ void LauncherPartLaunch::executeTask()
QStringList processed;
for (auto& item : classPath) {
if (!fitsInLocal8bit(item)) {
processed << shortPathName(item);
processed << getShortPathName(item);
} else {
processed << item;
}