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"
static CLauncherDirs launcherDirsGlobal;
CLauncherDirs::CLauncherDirs()
namespace CLauncherDirs
{
QDir().mkdir(downloadsPath());
QDir().mkdir(modsPath());
void prepare()
{
for(auto path : {downloadsPath(), modsPath(), mapsPath()})
QDir{}.mkdir(path);
}
CLauncherDirs & CLauncherDirs::get()
{
return launcherDirsGlobal;
}
QString CLauncherDirs::downloadsPath()
QString downloadsPath()
{
return pathToQString(VCMIDirs::get().userCachePath() / "downloads");
}
QString CLauncherDirs::modsPath()
QString modsPath()
{
return pathToQString(VCMIDirs::get().userDataPath() / "Mods");
}
QString CLauncherDirs::mapsPath()
QString mapsPath()
{
return pathToQString(VCMIDirs::get().userDataPath() / "Maps");
}
}

View File

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

View File

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

View File

@@ -22,7 +22,7 @@ void CDownloadManager::downloadFile(const QUrl & url, const QString & file, qint
{
QNetworkRequest request(url);
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.totalSize = bytesTotal;
entry.filename = file;

View File

@@ -841,7 +841,7 @@ void CModListView::installMods(QStringList archives)
void CModListView::installMaps(QStringList maps)
{
QString destDir = CLauncherDirs::get().mapsPath() + "/";
const auto destDir = CLauncherDirs::mapsPath() + QChar{'/'};
for(QString map : maps)
{
@@ -890,18 +890,18 @@ void CModListView::loadScreenshots()
QString modName = ui->allModsView->currentIndex().data(ModRoles::ModNameRole).toString();
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
auto hashed = QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md5);
auto hashedStr = QString::fromUtf8(hashed.toHex());
const auto hashed = QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md5);
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);
if(pixmap.isNull())
{
// image file not exists or corrupted - try to redownload
downloadFile(hashedStr + ".png", url, "screenshots");
downloadFile(fileName, url, "screenshots");
}
else
{

View File

@@ -271,7 +271,7 @@ bool CModManager::doEnableMod(QString mod, bool on)
bool CModManager::doInstallMod(QString modname, QString archivePath)
{
QString destDir = CLauncherDirs::get().modsPath() + "/";
const auto destDir = CLauncherDirs::modsPath() + QChar{'/'};
if(!QFile(archivePath).exists())
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]()
{
const auto destDirFsPath = qstringToPath(destDir);
ZipArchive archive(qstringToPath(archivePath));
for (auto const & file : filesToExtract)
{
if (!archive.extract(qstringToPath(destDir), file))
if (!archive.extract(destDirFsPath, file))
return false;
++filesCounter;
}