From 8ef07ec6346a4f9c078184c9497e7c12f5b8c33d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 13 Sep 2015 04:21:26 +0200 Subject: GH-1227 allow structured world zip import and drag and drop out of MultiMC --- logic/minecraft/World.cpp | 18 +++++++-- logic/minecraft/World.h | 9 +++++ logic/minecraft/WorldList.cpp | 85 ++++++++++++++++++++++++++----------------- 3 files changed, 76 insertions(+), 36 deletions(-) (limited to 'logic/minecraft') diff --git a/logic/minecraft/World.cpp b/logic/minecraft/World.cpp index 142839d2..bb99aa96 100644 --- a/logic/minecraft/World.cpp +++ b/logic/minecraft/World.cpp @@ -27,6 +27,7 @@ #include #include #include +#include World::World(const QFileInfo &file) { @@ -76,9 +77,16 @@ void World::readFromZip(const QFileInfo &file) { return; } + auto location = MMCZip::findFileInZip(&zip, "level.dat"); + is_valid = !location.isEmpty(); + if (!is_valid) + { + return; + } + m_containerOffsetPath = location; QuaZipFile zippedFile(&zip); // read the install profile - is_valid = zip.setCurrentFile("level.dat"); + is_valid = zip.setCurrentFile(location + "level.dat"); if (!is_valid) { return; @@ -109,8 +117,12 @@ bool World::install(QString to) } if(m_containerFile.isFile()) { - // FIXME: check if this is OK. - return !MMCZip::extractDir(m_containerFile.absoluteFilePath(), finalPath).isEmpty(); + QuaZip zip(m_containerFile.absoluteFilePath()); + if (!zip.open(QuaZip::mdUnzip)) + { + return false; + } + return !MMCZip::extractSubDir(&zip, m_containerOffsetPath, finalPath).isEmpty(); } else if(m_containerFile.isDir()) { diff --git a/logic/minecraft/World.h b/logic/minecraft/World.h index 27184e05..bed8f967 100644 --- a/logic/minecraft/World.h +++ b/logic/minecraft/World.h @@ -41,6 +41,14 @@ public: { return is_valid; } + bool isOnFS() const + { + return m_containerFile.isDir(); + } + QFileInfo container() const + { + return m_containerFile; + } // delete all the files of this world bool destroy(); // replace this world with a copy of the other @@ -62,6 +70,7 @@ private: protected: QFileInfo m_containerFile; + QString m_containerOffsetPath; QString m_folderName; QString m_actualName; QDateTime levelDatTime; diff --git a/logic/minecraft/WorldList.cpp b/logic/minecraft/WorldList.cpp index 1c25f3b1..e58ca064 100644 --- a/logic/minecraft/WorldList.cpp +++ b/logic/minecraft/WorldList.cpp @@ -219,25 +219,64 @@ QVariant WorldList::headerData(int section, Qt::Orientation orientation, int rol QStringList WorldList::mimeTypes() const { QStringList types; - types << "text/plain"; types << "text/uri-list"; return types; } -QMimeData *WorldList::mimeData(const QModelIndexList &indexes) const +class WorldMimeData : public QMimeData { - QMimeData *data = new QMimeData(); +Q_OBJECT - if (indexes.size() == 0) - return data; +public: + WorldMimeData(QList worlds) + { + m_worlds = worlds; - auto idx = indexes[0]; - int row = idx.row(); - if (row < 0 || row >= worlds.size()) - return data; + } + QStringList formats() const + { + return QMimeData::formats() << "text/uri-list"; + } + +protected: + QVariant retrieveData(const QString &mimetype, QVariant::Type type) const + { + QList urls; + for(auto &world: m_worlds) + { + if(!world.isValid() || !world.isOnFS()) + continue; + QString worldPath = world.container().absoluteFilePath(); + qDebug() << worldPath; + urls.append(QUrl::fromLocalFile(worldPath)); + } + const_cast(this)->setUrls(urls); + return QMimeData::retrieveData(mimetype, type); + } +private: + QList m_worlds; +}; + +QMimeData *WorldList::mimeData(const QModelIndexList &indexes) const +{ + if (indexes.size() == 0) + return new QMimeData(); - data->setText(QString::number(row)); - return data; + QList worlds; + for(auto idx : indexes) + { + if(idx.column() != 0) + continue; + int row = idx.row(); + if (row < 0 || row >= this->worlds.size()) + continue; + worlds.append(this->worlds[row]); + } + if(!worlds.size()) + { + return new QMimeData(); + } + return new WorldMimeData(worlds); } Qt::ItemFlags WorldList::flags(const QModelIndex &index) const @@ -302,27 +341,7 @@ bool WorldList::dropMimeData(const QMimeData *data, Qt::DropAction action, int r startWatching(); return true; } - /* - else if (data->hasText()) - { - QString sourcestr = data->text(); - auto list = sourcestr.split('|'); - if (list.size() != 2) - return false; - QString remoteId = list[0]; - int remoteIndex = list[1].toInt(); - qDebug() << "move: " << sourcestr; - // no moving of things between two lists - if (remoteId != m_list_id) - return false; - // no point moving to the same place... - if (row == remoteIndex) - return false; - // otherwise, move the mod :D - moveModTo(remoteIndex, row); - return true; - } - */ return false; - } + +#include "WorldList.moc" -- cgit v1.2.3