GH-1227 add world copy and rename

This commit is contained in:
Petr Mrázek 2015-09-14 23:49:32 +02:00
parent dd8eacee1b
commit 8d3f13c447
7 changed files with 220 additions and 46 deletions

View File

@ -22,6 +22,7 @@
#include <QClipboard> #include <QClipboard>
#include <QMessageBox> #include <QMessageBox>
#include <QTreeView> #include <QTreeView>
#include <QInputDialog>
#include "MultiMC.h" #include "MultiMC.h"
@ -35,6 +36,7 @@ WorldListPage::WorldListPage(BaseInstance *inst, std::shared_ptr<WorldList> worl
ui->setupUi(this); ui->setupUi(this);
ui->tabWidget->tabBar()->hide(); ui->tabWidget->tabBar()->hide();
QSortFilterProxyModel * proxy = new QSortFilterProxyModel(this); QSortFilterProxyModel * proxy = new QSortFilterProxyModel(this);
proxy->setSortCaseSensitivity(Qt::CaseInsensitive);
proxy->setSourceModel(m_worlds.get()); proxy->setSourceModel(m_worlds.get());
ui->worldTreeView->setSortingEnabled(true); ui->worldTreeView->setSortingEnabled(true);
ui->worldTreeView->setModel(proxy); ui->worldTreeView->setModel(proxy);
@ -207,6 +209,8 @@ void WorldListPage::worldChanged(const QModelIndex &current, const QModelIndex &
ui->copySeedBtn->setEnabled(enable); ui->copySeedBtn->setEnabled(enable);
ui->mcEditBtn->setEnabled(enable); ui->mcEditBtn->setEnabled(enable);
ui->rmWorldBtn->setEnabled(enable); ui->rmWorldBtn->setEnabled(enable);
ui->copyBtn->setEnabled(enable);
ui->renameBtn->setEnabled(enable);
} }
void WorldListPage::on_addBtn_clicked() void WorldListPage::on_addBtn_clicked()
@ -225,3 +229,40 @@ void WorldListPage::on_addBtn_clicked()
m_worlds->startWatching(); m_worlds->startWatching();
} }
} }
void WorldListPage::on_copyBtn_clicked()
{
QModelIndex index = getSelectedWorld();
if (!index.isValid())
{
return;
}
auto worldVariant = m_worlds->data(index, WorldList::ObjectRole);
auto world = (World *) worldVariant.value<void *>();
bool ok = false;
QString name = QInputDialog::getText(this, tr("World name"), tr("Enter a new name for the copy."), QLineEdit::Normal, world->name(), &ok);
if (ok && name.length() > 0)
{
world->install(m_worlds->dir().absolutePath(), name);
}
}
void WorldListPage::on_renameBtn_clicked()
{
QModelIndex index = getSelectedWorld();
if (!index.isValid())
{
return;
}
auto worldVariant = m_worlds->data(index, WorldList::ObjectRole);
auto world = (World *) worldVariant.value<void *>();
bool ok = false;
QString name = QInputDialog::getText(this, tr("World name"), tr("Enter a new world name."), QLineEdit::Normal, world->name(), &ok);
if (ok && name.length() > 0)
{
world->rename(name);
}
}

View File

@ -82,6 +82,8 @@ private slots:
void on_mcEditBtn_clicked(); void on_mcEditBtn_clicked();
void on_rmWorldBtn_clicked(); void on_rmWorldBtn_clicked();
void on_addBtn_clicked(); void on_addBtn_clicked();
void on_copyBtn_clicked();
void on_renameBtn_clicked();
void on_viewFolderBtn_clicked(); void on_viewFolderBtn_clicked();
void worldChanged(const QModelIndex &current, const QModelIndex &previous); void worldChanged(const QModelIndex &current, const QModelIndex &previous);
}; };

View File

@ -70,6 +70,20 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QPushButton" name="copyBtn">
<property name="text">
<string>Copy</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="renameBtn">
<property name="text">
<string>Rename</string>
</property>
</widget>
</item>
<item> <item>
<widget class="QPushButton" name="rmWorldBtn"> <widget class="QPushButton" name="rmWorldBtn">
<property name="text"> <property name="text">
@ -134,6 +148,8 @@
<tabstop>tabWidget</tabstop> <tabstop>tabWidget</tabstop>
<tabstop>worldTreeView</tabstop> <tabstop>worldTreeView</tabstop>
<tabstop>addBtn</tabstop> <tabstop>addBtn</tabstop>
<tabstop>copyBtn</tabstop>
<tabstop>renameBtn</tabstop>
<tabstop>rmWorldBtn</tabstop> <tabstop>rmWorldBtn</tabstop>
<tabstop>mcEditBtn</tabstop> <tabstop>mcEditBtn</tabstop>
<tabstop>copySeedBtn</tabstop> <tabstop>copySeedBtn</tabstop>

View File

@ -16,6 +16,7 @@
#include <QDir> #include <QDir>
#include <QString> #include <QString>
#include <QDebug> #include <QDebug>
#include <QSaveFile>
#include "World.h" #include "World.h"
#include <pathutils.h> #include <pathutils.h>
@ -29,6 +30,83 @@
#include <quazipfile.h> #include <quazipfile.h>
#include <quazipdir.h> #include <quazipdir.h>
std::unique_ptr <nbt::tag_compound> parseLevelDat(QByteArray data)
{
QByteArray output;
if(!GZip::unzip(data, output))
{
return nullptr;
}
std::istringstream foo(std::string(output.constData(), output.size()));
auto pair = nbt::io::read_compound(foo);
if(pair.first != "")
return nullptr;
if(pair.second == nullptr)
return nullptr;
return std::move(pair.second);
}
QByteArray serializeLevelDat(nbt::tag_compound * levelInfo)
{
std::ostringstream s;
nbt::io::write_tag("", *levelInfo, s);
QByteArray val( s.str().data(), (int) s.str().size() );
return val;
}
QString getLevelDatFromFS(const QFileInfo &file)
{
QDir worldDir(file.filePath());
if(!file.isDir() || !worldDir.exists("level.dat"))
{
return QString();
}
return worldDir.absoluteFilePath("level.dat");
}
QByteArray getLevelDatDataFromFS(const QFileInfo &file)
{
auto fullFilePath = getLevelDatFromFS(file);
if(fullFilePath.isNull())
{
return QByteArray();
}
QFile f(fullFilePath);
if(!f.open(QIODevice::ReadOnly))
{
return QByteArray();
}
return f.readAll();
}
bool putLevelDatDataToFS(const QFileInfo &file, QByteArray & data)
{
auto fullFilePath = getLevelDatFromFS(file);
if(fullFilePath.isNull())
{
return false;
}
QSaveFile f(fullFilePath);
if(!f.open(QIODevice::WriteOnly))
{
return false;
}
QByteArray compressed;
if(!GZip::zip(data, compressed))
{
return false;
}
if(f.write(compressed) != compressed.size())
{
f.cancelWriting();
return false;
}
return f.commit();
}
World::World(const QFileInfo &file) World::World(const QFileInfo &file)
{ {
repath(file); repath(file);
@ -50,23 +128,14 @@ void World::repath(const QFileInfo &file)
void World::readFromFS(const QFileInfo &file) void World::readFromFS(const QFileInfo &file)
{ {
QDir worldDir(file.filePath()); auto bytes = getLevelDatDataFromFS(file);
is_valid = file.isDir() && worldDir.exists("level.dat"); if(bytes.isEmpty())
if(!is_valid)
{ {
is_valid = false;
return; return;
} }
loadFromLevelDat(bytes);
auto fullFilePath = worldDir.absoluteFilePath("level.dat"); levelDatTime = file.lastModified();
QFile f(fullFilePath);
is_valid = f.open(QIODevice::ReadOnly);
if(!is_valid)
{
return;
}
QFileInfo finfo(fullFilePath);
levelDatTime = finfo.lastModified();
parseLevelDat(f.readAll());
} }
void World::readFromZip(const QFileInfo &file) void World::readFromZip(const QFileInfo &file)
@ -104,17 +173,18 @@ void World::readFromZip(const QFileInfo &file)
{ {
return; return;
} }
parseLevelDat(zippedFile.readAll()); loadFromLevelDat(zippedFile.readAll());
zippedFile.close(); zippedFile.close();
} }
bool World::install(QString to) bool World::install(const QString &to, const QString &name)
{ {
auto finalPath = PathCombine(to, DirNameFromString(m_actualName, to)); auto finalPath = PathCombine(to, DirNameFromString(m_actualName, to));
if(!ensureFolderPathExists(finalPath)) if(!ensureFolderPathExists(finalPath))
{ {
return false; return false;
} }
bool ok = false;
if(m_containerFile.isFile()) if(m_containerFile.isFile())
{ {
QuaZip zip(m_containerFile.absoluteFilePath()); QuaZip zip(m_containerFile.absoluteFilePath());
@ -122,14 +192,63 @@ bool World::install(QString to)
{ {
return false; return false;
} }
return !MMCZip::extractSubDir(&zip, m_containerOffsetPath, finalPath).isEmpty(); ok = !MMCZip::extractSubDir(&zip, m_containerOffsetPath, finalPath).isEmpty();
} }
else if(m_containerFile.isDir()) else if(m_containerFile.isDir())
{ {
QString from = m_containerFile.filePath(); QString from = m_containerFile.filePath();
return copyPath(from, finalPath); ok = copyPath(from, finalPath);
} }
return false;
if(ok && !name.isEmpty() && m_actualName != name)
{
World newWorld(finalPath);
if(newWorld.isValid())
{
newWorld.rename(name);
}
}
return ok;
}
bool World::rename(const QString &newName)
{
if(m_containerFile.isFile())
{
return false;
}
auto data = getLevelDatDataFromFS(m_containerFile);
if(data.isEmpty())
{
return false;
}
auto worldData = parseLevelDat(data);
if(!worldData)
{
return false;
}
auto &val = worldData->at("Data");
if(val.get_type() != nbt::tag_type::Compound)
{
return false;
}
auto &dataCompound = val.as<nbt::tag_compound>();
dataCompound.put("LevelName", nbt::value_initializer(newName.toUtf8().data()));
data = serializeLevelDat(worldData.get());
putLevelDatDataToFS(m_containerFile, data);
m_actualName = newName;
QDir parentDir(m_containerFile.absoluteFilePath());
parentDir.cdUp();
QFile container(m_containerFile.absoluteFilePath());
auto dirName = DirNameFromString(m_actualName, parentDir.absolutePath());
container.rename(parentDir.absoluteFilePath(dirName));
return true;
} }
static QString read_string (nbt::value& parent, const char * name, const QString & fallback = QString()) static QString read_string (nbt::value& parent, const char * name, const QString & fallback = QString())
@ -184,34 +303,18 @@ static int64_t read_long (nbt::value& parent, const char * name, const int64_t &
} }
}; };
void World::parseLevelDat(QByteArray data) void World::loadFromLevelDat(QByteArray data)
{ {
QByteArray output;
is_valid = GZip::unzip(data, output);
if(!is_valid)
{
return;
}
try try
{ {
std::istringstream foo(std::string(output.constData(), output.size())); auto levelData = parseLevelDat(data);
auto pair = nbt::io::read_compound(foo); if(!levelData)
is_valid = pair.first == "";
if(!is_valid)
{ {
return; is_valid = false;
}
std::ostringstream ostr;
is_valid = pair.second != nullptr;
if(!is_valid)
{
qDebug() << "FAIL~!!!";
return; return;
} }
auto &val = pair.second->at("Data"); auto &val = levelData->at("Data");
is_valid = val.get_type() == nbt::tag_type::Compound; is_valid = val.get_type() == nbt::tag_type::Compound;
if(!is_valid) if(!is_valid)
return; return;

View File

@ -17,7 +17,9 @@
#include <QFileInfo> #include <QFileInfo>
#include <QDateTime> #include <QDateTime>
class World #include "multimc_logic_export.h"
class MULTIMC_LOGIC_EXPORT World
{ {
public: public:
World(const QFileInfo &file); World(const QFileInfo &file);
@ -56,7 +58,8 @@ public:
// change the world's filesystem path (used by world lists for *MAGIC* purposes) // change the world's filesystem path (used by world lists for *MAGIC* purposes)
void repath(const QFileInfo &file); void repath(const QFileInfo &file);
bool install(QString to); bool rename(const QString &to);
bool install(const QString &to, const QString &name= QString());
// WEAK compare operator - used for replacing worlds // WEAK compare operator - used for replacing worlds
bool operator==(const World &other) const; bool operator==(const World &other) const;
@ -65,7 +68,7 @@ public:
private: private:
void readFromZip(const QFileInfo &file); void readFromZip(const QFileInfo &file);
void readFromFS(const QFileInfo &file); void readFromFS(const QFileInfo &file);
void parseLevelDat(QByteArray data); void loadFromLevelDat(QByteArray data);
protected: protected:

View File

@ -164,6 +164,10 @@ QVariant WorldList::data(const QModelIndex &index, int role) const
{ {
return world.folderName(); return world.folderName();
} }
case ObjectRole:
{
return QVariant::fromValue<void *>((void *)&world);
}
case FolderRole: case FolderRole:
{ {
return QDir::toNativeSeparators(dir().absoluteFilePath(world.folderName())); return QDir::toNativeSeparators(dir().absoluteFilePath(world.folderName()));
@ -335,7 +339,11 @@ bool WorldList::dropMimeData(const QMimeData *data, Qt::DropAction action, int r
QString filename = url.toLocalFile(); QString filename = url.toLocalFile();
QFileInfo worldInfo(filename); QFileInfo worldInfo(filename);
installWorld(worldInfo);
if(!m_dir.entryInfoList().contains(worldInfo))
{
installWorld(worldInfo);
}
} }
if (was_watching) if (was_watching)
startWatching(); startWatching();

View File

@ -38,7 +38,8 @@ public:
enum Roles enum Roles
{ {
FolderRole = Qt::UserRole + 1, ObjectRole = Qt::UserRole + 1,
FolderRole,
SeedRole, SeedRole,
NameRole, NameRole,
LastPlayedRole LastPlayedRole