Explorar o código

get rid of CLauncherDirs global static

std::call_once causes crash
Andrey Filipenkov hai 1 ano
pai
achega
74ecbec1c7

+ 8 - 12
launcher/launcherdirs.cpp

@@ -12,30 +12,26 @@
 
 #include "../lib/VCMIDirs.h"
 
-static CLauncherDirs launcherDirsGlobal;
-
-CLauncherDirs::CLauncherDirs()
+namespace CLauncherDirs
 {
-	QDir().mkdir(downloadsPath());
-	QDir().mkdir(modsPath());
-}
-
-CLauncherDirs & CLauncherDirs::get()
+void prepare()
 {
-	return launcherDirsGlobal;
+	for(auto path : {downloadsPath(), modsPath(), mapsPath()})
+		QDir{}.mkdir(path);
 }
 
-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");
 }
+}

+ 3 - 6
launcher/launcherdirs.h

@@ -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();
-};
+}

+ 6 - 1
launcher/main.cpp

@@ -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
 	}

+ 1 - 1
launcher/modManager/cdownloadmanager_moc.cpp

@@ -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;

+ 6 - 6
launcher/modManager/cmodlistview_moc.cpp

@@ -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
 			{

+ 3 - 2
launcher/modManager/cmodmanager.cpp

@@ -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;
 		}