From 64ca96f470efda6ee643e52e80105eff2790c22b Mon Sep 17 00:00:00 2001 From: Sefa Eyeoglu Date: Thu, 31 Mar 2022 18:45:17 +0200 Subject: [PATCH] feat: track and display world size --- launcher/minecraft/World.cpp | 22 ++++++++++++++++++++++ launcher/minecraft/World.h | 6 ++++++ launcher/minecraft/WorldList.cpp | 18 +++++++++++++++++- launcher/minecraft/WorldList.h | 4 +++- 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/launcher/minecraft/World.cpp b/launcher/minecraft/World.cpp index 2937c116d..e45f042d4 100644 --- a/launcher/minecraft/World.cpp +++ b/launcher/minecraft/World.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "World.h" #include "GZip.h" @@ -187,6 +188,25 @@ bool putLevelDatDataToFS(const QFileInfo &file, QByteArray & data) return f.commit(); } +int64_t calculateWorldSize(const QFileInfo &file) +{ + if (file.isFile() && file.suffix() == "zip") + { + return file.size(); + } + else if(file.isDir()) + { + QDirIterator it(file.absolutePath(), QDirIterator::Subdirectories); + int64_t total = 0; + while (it.hasNext()) { + total += it.fileInfo().size(); + it.next(); + } + return total; + } + return -1; +} + World::World(const QFileInfo &file) { repath(file); @@ -196,6 +216,7 @@ void World::repath(const QFileInfo &file) { m_containerFile = file; m_folderName = file.fileName(); + m_size = calculateWorldSize(file); if(file.isFile() && file.suffix() == "zip") { m_iconFile = QString(); @@ -482,6 +503,7 @@ void World::loadFromLevelDat(QByteArray data) if(randomSeed) { qDebug() << "Seed:" << *randomSeed; } + qDebug() << "Size:" << m_size; qDebug() << "GameType:" << m_gameType.toLogString(); } diff --git a/launcher/minecraft/World.h b/launcher/minecraft/World.h index 35e327880..62af744ab 100644 --- a/launcher/minecraft/World.h +++ b/launcher/minecraft/World.h @@ -17,6 +17,7 @@ #include #include #include +#include struct GameType { GameType() = default; @@ -52,6 +53,10 @@ public: { return m_iconFile; } + int64_t bytes() const + { + return m_size; + } QDateTime lastPlayed() const { return m_lastPlayed; @@ -105,6 +110,7 @@ protected: QString m_iconFile; QDateTime levelDatTime; QDateTime m_lastPlayed; + int64_t m_size; int64_t m_randomSeed = 0; GameType m_gameType; bool is_valid = false; diff --git a/launcher/minecraft/WorldList.cpp b/launcher/minecraft/WorldList.cpp index dcdbc3214..344bea63f 100644 --- a/launcher/minecraft/WorldList.cpp +++ b/launcher/minecraft/WorldList.cpp @@ -14,6 +14,8 @@ */ #include "WorldList.h" + +#include "Application.h" #include #include #include @@ -150,7 +152,7 @@ bool WorldList::resetIcon(int row) int WorldList::columnCount(const QModelIndex &parent) const { - return 3; + return 4; } QVariant WorldList::data(const QModelIndex &index, int role) const @@ -164,6 +166,8 @@ QVariant WorldList::data(const QModelIndex &index, int role) const if (row < 0 || row >= worlds.size()) return QVariant(); + QLocale locale; + auto & world = worlds[row]; switch (role) { @@ -179,6 +183,9 @@ QVariant WorldList::data(const QModelIndex &index, int role) const case LastPlayedColumn: return world.lastPlayed(); + case SizeColumn: + return locale.formattedDataSize(world.bytes()); + default: return QVariant(); } @@ -207,6 +214,10 @@ QVariant WorldList::data(const QModelIndex &index, int role) const { return world.lastPlayed(); } + case SizeRole: + { + return locale.formattedDataSize(world.bytes()); + } case IconFileRole: { return world.iconFile(); @@ -229,6 +240,9 @@ QVariant WorldList::headerData(int section, Qt::Orientation orientation, int rol return tr("Game Mode"); case LastPlayedColumn: return tr("Last Played"); + case SizeColumn: + //: World size on disk + return tr("Size"); default: return QVariant(); } @@ -242,6 +256,8 @@ QVariant WorldList::headerData(int section, Qt::Orientation orientation, int rol return tr("Game mode of the world."); case LastPlayedColumn: return tr("Date and time the world was last played."); + case SizeColumn: + return tr("Size of the world on disk."); default: return QVariant(); } diff --git a/launcher/minecraft/WorldList.h b/launcher/minecraft/WorldList.h index 8e238ee36..5138e5837 100644 --- a/launcher/minecraft/WorldList.h +++ b/launcher/minecraft/WorldList.h @@ -32,7 +32,8 @@ public: { NameColumn, GameModeColumn, - LastPlayedColumn + LastPlayedColumn, + SizeColumn }; enum Roles @@ -43,6 +44,7 @@ public: NameRole, GameModeRole, LastPlayedRole, + SizeRole, IconFileRole };