1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-23 22:37:55 +02:00

get rid of CLauncherDirs global static

std::call_once causes crash
This commit is contained in:
Andrey Filipenkov
2024-02-06 01:33:47 +03:00
parent c210b488f3
commit 74ecbec1c7
6 changed files with 28 additions and 29 deletions

View File

@@ -12,30 +12,26 @@
#include "../lib/VCMIDirs.h" #include "../lib/VCMIDirs.h"
static CLauncherDirs launcherDirsGlobal; namespace CLauncherDirs
CLauncherDirs::CLauncherDirs()
{ {
QDir().mkdir(downloadsPath()); void prepare()
QDir().mkdir(modsPath()); {
for(auto path : {downloadsPath(), modsPath(), mapsPath()})
QDir{}.mkdir(path);
} }
CLauncherDirs & CLauncherDirs::get() QString downloadsPath()
{
return launcherDirsGlobal;
}
QString CLauncherDirs::downloadsPath()
{ {
return pathToQString(VCMIDirs::get().userCachePath() / "downloads"); return pathToQString(VCMIDirs::get().userCachePath() / "downloads");
} }
QString CLauncherDirs::modsPath() QString modsPath()
{ {
return pathToQString(VCMIDirs::get().userDataPath() / "Mods"); return pathToQString(VCMIDirs::get().userDataPath() / "Mods");
} }
QString CLauncherDirs::mapsPath() QString mapsPath()
{ {
return pathToQString(VCMIDirs::get().userDataPath() / "Maps"); return pathToQString(VCMIDirs::get().userDataPath() / "Maps");
} }
}

View File

@@ -10,14 +10,11 @@
#pragma once #pragma once
/// similar to lib/VCMIDirs, controls where all launcher-related data will be stored /// similar to lib/VCMIDirs, controls where all launcher-related data will be stored
class CLauncherDirs namespace CLauncherDirs
{ {
public: void prepare();
CLauncherDirs();
static CLauncherDirs & get();
QString downloadsPath(); QString downloadsPath();
QString modsPath(); QString modsPath();
QString mapsPath(); QString mapsPath();
}; }

View File

@@ -10,11 +10,13 @@
#include "StdInc.h" #include "StdInc.h"
#include "main.h" #include "main.h"
#include "mainwindow_moc.h" #include "mainwindow_moc.h"
#include "launcherdirs.h"
#include "../lib/VCMIDirs.h"
#include <QApplication> #include <QApplication>
#include <QProcess> #include <QProcess>
#include <QMessageBox> #include <QMessageBox>
#include "../lib/VCMIDirs.h"
// Conan workaround https://github.com/conan-io/conan-center-index/issues/13332 // Conan workaround https://github.com/conan-io/conan-center-index/issues/13332
#ifdef VCMI_IOS #ifdef VCMI_IOS
@@ -33,8 +35,11 @@ int main(int argc, char * argv[])
#endif #endif
QApplication vcmilauncher(argc, argv); QApplication vcmilauncher(argc, argv);
CLauncherDirs::prepare();
MainWindow mainWindow; MainWindow mainWindow;
mainWindow.show(); mainWindow.show();
result = vcmilauncher.exec(); result = vcmilauncher.exec();
#ifdef VCMI_IOS #ifdef VCMI_IOS
} }

View File

@@ -22,7 +22,7 @@ void CDownloadManager::downloadFile(const QUrl & url, const QString & file, qint
{ {
QNetworkRequest request(url); QNetworkRequest request(url);
FileEntry entry; FileEntry entry;
entry.file.reset(new QFile(CLauncherDirs::get().downloadsPath() + '/' + file)); entry.file.reset(new QFile(QString{QLatin1String{"%1/%2"}}.arg(CLauncherDirs::downloadsPath(), file)));
entry.bytesReceived = 0; entry.bytesReceived = 0;
entry.totalSize = bytesTotal; entry.totalSize = bytesTotal;
entry.filename = file; entry.filename = file;

View File

@@ -841,7 +841,7 @@ void CModListView::installMods(QStringList archives)
void CModListView::installMaps(QStringList maps) void CModListView::installMaps(QStringList maps)
{ {
QString destDir = CLauncherDirs::get().mapsPath() + "/"; const auto destDir = CLauncherDirs::mapsPath() + QChar{'/'};
for(QString map : maps) for(QString map : maps)
{ {
@@ -890,18 +890,18 @@ void CModListView::loadScreenshots()
QString modName = ui->allModsView->currentIndex().data(ModRoles::ModNameRole).toString(); QString modName = ui->allModsView->currentIndex().data(ModRoles::ModNameRole).toString();
assert(modModel->hasMod(modName)); //should be filtered out by check above assert(modModel->hasMod(modName)); //should be filtered out by check above
for(QString & url : modModel->getMod(modName).getValue("screenshots").toStringList()) for(QString url : modModel->getMod(modName).getValue("screenshots").toStringList())
{ {
// URL must be encoded to something else to get rid of symbols illegal in file names // URL must be encoded to something else to get rid of symbols illegal in file names
auto hashed = QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md5); const auto hashed = QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md5);
auto hashedStr = QString::fromUtf8(hashed.toHex()); const auto fileName = QString{QLatin1String{"%1.png"}}.arg(QLatin1String{hashed.toHex()});
QString fullPath = CLauncherDirs::get().downloadsPath() + '/' + hashedStr + ".png"; const auto fullPath = QString{QLatin1String{"%1/%2"}}.arg(CLauncherDirs::downloadsPath(), fileName);
QPixmap pixmap(fullPath); QPixmap pixmap(fullPath);
if(pixmap.isNull()) if(pixmap.isNull())
{ {
// image file not exists or corrupted - try to redownload // image file not exists or corrupted - try to redownload
downloadFile(hashedStr + ".png", url, "screenshots"); downloadFile(fileName, url, "screenshots");
} }
else else
{ {

View File

@@ -271,7 +271,7 @@ bool CModManager::doEnableMod(QString mod, bool on)
bool CModManager::doInstallMod(QString modname, QString archivePath) bool CModManager::doInstallMod(QString modname, QString archivePath)
{ {
QString destDir = CLauncherDirs::get().modsPath() + "/"; const auto destDir = CLauncherDirs::modsPath() + QChar{'/'};
if(!QFile(archivePath).exists()) if(!QFile(archivePath).exists())
return addError(modname, "Mod archive is missing"); return addError(modname, "Mod archive is missing");
@@ -288,10 +288,11 @@ bool CModManager::doInstallMod(QString modname, QString archivePath)
auto futureExtract = std::async(std::launch::async, [&archivePath, &destDir, &filesCounter, &filesToExtract]() auto futureExtract = std::async(std::launch::async, [&archivePath, &destDir, &filesCounter, &filesToExtract]()
{ {
const auto destDirFsPath = qstringToPath(destDir);
ZipArchive archive(qstringToPath(archivePath)); ZipArchive archive(qstringToPath(archivePath));
for (auto const & file : filesToExtract) for (auto const & file : filesToExtract)
{ {
if (!archive.extract(qstringToPath(destDir), file)) if (!archive.extract(destDirFsPath, file))
return false; return false;
++filesCounter; ++filesCounter;
} }