Ver Fonte

Reimplemented computation of installed mod size for Launcher

Ivan Savenko há 11 meses atrás
pai
commit
ac3aecba81

+ 3 - 3
launcher/modManager/cmodlistview_moc.cpp

@@ -320,10 +320,10 @@ QString CModListView::genModInfoText(const ModState & mod)
 		result += replaceIfNotEmpty(mod.getRepositoryVersion(), lineTemplate.arg(tr("Latest version")));
 	}
 	else
-		result += replaceIfNotEmpty(mod.getVersion(), lineTemplate.arg(tr("Version")));
+		result += replaceIfNotEmpty(mod.getVersion(), lineTemplate.arg(tr("Installed version")));
 
-	if(!mod.getLocalSizeFormatted().isEmpty())
-		result += replaceIfNotEmpty(mod.getLocalSizeFormatted(), lineTemplate.arg(tr("Size")));
+	if (mod.isInstalled())
+		result += replaceIfNotEmpty(modStateModel->getInstalledModSizeFormatted(mod.getID()), lineTemplate.arg(tr("Size")));
 
 	if((!mod.isInstalled() || mod.isUpdateAvailable()) && !mod.getDownloadSizeFormatted().isEmpty())
 		result += replaceIfNotEmpty(mod.getDownloadSizeFormatted(), lineTemplate.arg(tr("Download size")));

+ 0 - 5
launcher/modManager/modstate.cpp

@@ -140,11 +140,6 @@ QString ModState::getDownloadSizeFormatted() const
 	return QCoreApplication::translate("File size", "%1 MiB").arg(QString::number(getDownloadSizeMegabytes(), 'f', 1));
 }
 
-QString ModState::getLocalSizeFormatted() const
-{
-	return {}; // TODO
-}
-
 QString ModState::getAuthors() const
 {
 	return QString::fromStdString(impl.getValue("author").String());

+ 0 - 1
launcher/modManager/modstate.h

@@ -46,7 +46,6 @@ public:
 	double getDownloadSizeMegabytes() const;
 	size_t getDownloadSizeBytes() const;
 	QString getDownloadSizeFormatted() const;
-	QString getLocalSizeFormatted() const;
 	QString getAuthors() const;
 	QString getContact() const;
 	QString getLicenseUrl() const;

+ 0 - 32
launcher/modManager/modstatecontroller.cpp

@@ -76,38 +76,6 @@ void ModStateController::appendRepositories(const JsonNode & repomap)
 	modList->appendRepositories(repomap);
 }
 
-//void ModStateController::loadMods()
-//{
-//	CModHandler handler;
-//	auto installedMods = handler.getAllMods();
-//	localMods.clear();
-//
-//	for(auto modname : installedMods)
-//	{
-//			//calculate mod size
-//			qint64 total = 0;
-//			ResourcePath resDir(CModInfo::getModDir(modname), EResType::DIRECTORY);
-//			if(CResourceHandler::get()->existsResource(resDir))
-//			{
-//				for(QDirIterator iter(QString::fromStdString(CResourceHandler::get()->getResourceName(resDir)->string()), QDirIterator::Subdirectories); iter.hasNext(); iter.next())
-//					total += iter.fileInfo().size();
-//			}
-//
-//			boost::filesystem::path name = *CResourceHandler::get()->getResourceName(resID);
-//			auto mod = JsonUtils::JsonFromFile(pathToQString(name));
-//			auto json = JsonUtils::toJson(mod);
-//			json["localSizeBytes"].Float() = total;
-//			if(!name.is_absolute())
-//				json["storedLocally"].Bool() = true;
-//
-//			mod = JsonUtils::toVariant(json);
-//			QString modNameQt = QString::fromUtf8(modname.c_str()).toLower();
-//			localMods.insert(modNameQt, mod);
-//			modSettings->registerNewMod(modNameQt, json["keepDisabled"].Bool());
-//	}
-//	modList->setLocalModList(localMods);
-//}
-
 bool ModStateController::addError(QString modname, QString message)
 {
 	recentErrors.push_back(QString("%1: %2").arg(modname).arg(message));

+ 10 - 0
launcher/modManager/modstatemodel.cpp

@@ -82,3 +82,13 @@ bool ModStateModel::isModVisible(QString modName) const
 {
 	return getMod(modName).isVisible();
 }
+
+QString ModStateModel::getInstalledModSizeFormatted(QString modName) const
+{
+	return QCoreApplication::translate("File size", "%1 MiB").arg(QString::number(getInstalledModSizeMegabytes(modName), 'f', 1));
+}
+
+double ModStateModel::getInstalledModSizeMegabytes(QString modName) const
+{
+	return modManager->getInstalledModSizeMegabytes(modName.toStdString());
+}

+ 3 - 0
launcher/modManager/modstatemodel.h

@@ -34,6 +34,9 @@ public:
 	QStringList getAllMods() const;
 	QStringList getSubmods(QString modName) const;
 
+	QString getInstalledModSizeFormatted(QString modName) const;
+	double getInstalledModSizeMegabytes(QString modName) const;
+
 	bool isModExists(QString modName) const;
 	bool isModInstalled(QString modName) const;
 	bool isModEnabled(QString modName) const;

+ 29 - 6
lib/modding/ModManager.cpp

@@ -20,20 +20,22 @@
 
 VCMI_LIB_NAMESPACE_BEGIN
 
-static std::string getModSettingsDirectory(const TModID & modName)
+static std::string getModDirectory(const TModID & modName)
 {
 	std::string result = modName;
 	boost::to_upper(result);
 	boost::algorithm::replace_all(result, ".", "/MODS/");
-	return "MODS/" + result + "/MODS/";
+	return "MODS/" + result;
+}
+
+static std::string getModSettingsDirectory(const TModID & modName)
+{
+	return getModDirectory(modName) + "/MODS/";
 }
 
 static JsonPath getModDescriptionFile(const TModID & modName)
 {
-	std::string result = modName;
-	boost::to_upper(result);
-	boost::algorithm::replace_all(result, ".", "/MODS/");
-	return JsonPath::builtin("MODS/" + result + "/mod");
+	return JsonPath::builtin(getModDirectory(modName) + "/mod");
 }
 
 ModsState::ModsState()
@@ -89,6 +91,22 @@ uint32_t ModsState::computeChecksum(const TModID & modName) const
 	return modChecksum.checksum();
 }
 
+double ModsState::getInstalledModSizeMegabytes(const TModID & modName) const
+{
+	ResourcePath resDir(getModDirectory(modName), EResType::DIRECTORY);
+	std::string path = CResourceHandler::get()->getResourceName(resDir)->string();
+
+	size_t sizeBytes = 0;
+	for(boost::filesystem::recursive_directory_iterator it(path); it != boost::filesystem::recursive_directory_iterator(); ++it)
+	{
+		if(!boost::filesystem::is_directory(*it))
+			sizeBytes += boost::filesystem::file_size(*it);
+	}
+
+	double sizeMegabytes = sizeBytes / double(1024*1024);
+	return sizeMegabytes;
+}
+
 std::vector<TModID> ModsState::scanModsDirectory(const std::string & modDir) const
 {
 	size_t depth = boost::range::count(modDir, '/');
@@ -372,6 +390,11 @@ TModList ModManager::getAllMods() const
 	return modsStorage->getAllMods();
 }
 
+double ModManager::getInstalledModSizeMegabytes(const TModID & modName) const
+{
+	return modsState->getInstalledModSizeMegabytes(modName);
+}
+
 void ModManager::eraseMissingModsFromPreset()
 {
 	const TModList & installedMods = modsState->getInstalledMods();

+ 2 - 0
lib/modding/ModManager.h

@@ -32,6 +32,7 @@ public:
 	ModsState();
 
 	TModList getInstalledMods() const;
+	double getInstalledModSizeMegabytes(const TModID & modName) const;
 
 	uint32_t computeChecksum(const TModID & modName) const;
 };
@@ -111,6 +112,7 @@ public:
 	std::optional<uint32_t> getValidatedChecksum(const TModID & modName) const;
 	void setValidatedChecksum(const TModID & modName, std::optional<uint32_t> value);
 	void saveConfigurationState() const;
+	double getInstalledModSizeMegabytes(const TModID & modName) const;
 };
 
 VCMI_LIB_NAMESPACE_END