From 583e5946f4cd29f0a18bcc4fad5608de739aa113 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 18 Aug 2015 19:10:17 -0400 Subject: GH-1047 World management for instances. Removal only currently. --- application/CMakeLists.txt | 3 + application/InstancePageProvider.h | 3 + application/pages/WorldListPage.cpp | 94 +++++++++++++++ application/pages/WorldListPage.h | 69 +++++++++++ application/pages/WorldListPage.ui | 94 +++++++++++++++ logic/CMakeLists.txt | 4 + logic/minecraft/LegacyInstance.cpp | 11 ++ logic/minecraft/LegacyInstance.h | 2 + logic/minecraft/MinecraftInstance.h | 5 + logic/minecraft/OneSixInstance.cpp | 16 +++ logic/minecraft/OneSixInstance.h | 3 + logic/minecraft/World.cpp | 72 ++++++++++++ logic/minecraft/World.h | 47 ++++++++ logic/minecraft/WorldList.cpp | 226 ++++++++++++++++++++++++++++++++++++ logic/minecraft/WorldList.h | 101 ++++++++++++++++ 15 files changed, 750 insertions(+) create mode 100644 application/pages/WorldListPage.cpp create mode 100644 application/pages/WorldListPage.h create mode 100644 application/pages/WorldListPage.ui create mode 100644 logic/minecraft/World.cpp create mode 100644 logic/minecraft/World.h create mode 100644 logic/minecraft/WorldList.cpp create mode 100644 logic/minecraft/WorldList.h diff --git a/application/CMakeLists.txt b/application/CMakeLists.txt index 1cc37a5d..f72b6a16 100644 --- a/application/CMakeLists.txt +++ b/application/CMakeLists.txt @@ -177,6 +177,8 @@ SET(MULTIMC_SOURCES pages/LegacyJarModPage.h pages/LegacyUpgradePage.cpp pages/LegacyUpgradePage.h + pages/WorldListPage.cpp + pages/WorldListPage.h # GUI - global settings pages pages/global/AccountListPage.cpp @@ -272,6 +274,7 @@ SET(MULTIMC_UIS pages/OtherLogsPage.ui pages/LegacyJarModPage.ui pages/LegacyUpgradePage.ui + pages/WorldListPage.ui # Global settings pages pages/global/AccountListPage.ui diff --git a/application/InstancePageProvider.h b/application/InstancePageProvider.h index 1da4c9f1..c3ae17c1 100644 --- a/application/InstancePageProvider.h +++ b/application/InstancePageProvider.h @@ -12,6 +12,7 @@ #include "pages/OtherLogsPage.h" #include "pages/BasePageProvider.h" #include "pages/LegacyJarModPage.h" +#include "pages/WorldListPage.h" #include @@ -39,6 +40,7 @@ public: values.append(new ResourcePackPage(onesix.get())); values.append(new TexturePackPage(onesix.get())); values.append(new NotesPage(onesix.get())); + values.append(new WorldListPage(onesix.get(), onesix->worldList(), "worlds", "worlds", tr("Worlds"), "Worlds")); values.append(new ScreenshotsPage(PathCombine(onesix->minecraftRoot(), "screenshots"))); values.append(new InstanceSettingsPage(onesix.get())); } @@ -54,6 +56,7 @@ public: values.append(new ModFolderPage(legacy.get(), legacy->coreModList(), "coremods", "coremods", tr("Core mods"), "Loader-mods")); values.append(new TexturePackPage(legacy.get())); values.append(new NotesPage(legacy.get())); + values.append(new WorldListPage(legacy.get(), legacy->worldList(), "worlds", "worlds", tr("Worlds"), "Worlds")); values.append(new ScreenshotsPage(PathCombine(legacy->minecraftRoot(), "screenshots"))); values.append(new InstanceSettingsPage(legacy.get())); } diff --git a/application/pages/WorldListPage.cpp b/application/pages/WorldListPage.cpp new file mode 100644 index 00000000..662b7452 --- /dev/null +++ b/application/pages/WorldListPage.cpp @@ -0,0 +1,94 @@ +// +// Created by robotbrain on 8/18/15. +// + +#include "WorldListPage.h" +#include "ui_WorldListPage.h" +#include "minecraft/WorldList.h" +#include "dialogs/ModEditDialogCommon.h" +#include +#include + +WorldListPage::WorldListPage(BaseInstance *inst, std::shared_ptr worlds, QString id, + QString iconName, QString displayName, QString helpPage, + QWidget *parent) + : QWidget(parent), ui(new Ui::WorldListPage) +{ + ui->setupUi(this); + ui->tabWidget->tabBar()->hide(); + m_inst = inst; + m_worlds = worlds; + m_id = id; + m_displayName = displayName; + m_iconName = iconName; + m_helpName = helpPage; + ui->worldTreeView->setModel(m_worlds.get()); + ui->worldTreeView->installEventFilter(this); + auto smodel = ui->worldTreeView->selectionModel(); + connect(smodel, SIGNAL(currentChanged(QModelIndex, QModelIndex)), + SLOT(modCurrent(QModelIndex, QModelIndex))); +} + +void WorldListPage::opened() +{ + m_worlds->startWatching(); +} + +void WorldListPage::closed() +{ + m_worlds->stopWatching(); +} + +WorldListPage::~WorldListPage() +{ + m_worlds->stopWatching(); + delete ui; +} + +bool WorldListPage::shouldDisplay() const +{ + if (m_inst) + return !m_inst->isRunning(); + return true; +} + +bool WorldListPage::worldListFilter(QKeyEvent *keyEvent) +{ + switch (keyEvent->key()) + { + case Qt::Key_Delete: + on_rmWorldBtn_clicked(); + return true; + default: + break; + } + return QWidget::eventFilter(ui->worldTreeView, keyEvent); +} + +bool WorldListPage::eventFilter(QObject *obj, QEvent *ev) +{ + if (ev->type() != QEvent::KeyPress) + { + return QWidget::eventFilter(obj, ev); + } + QKeyEvent *keyEvent = static_cast(ev); + if (obj == ui->worldTreeView) + return worldListFilter(keyEvent); + return QWidget::eventFilter(obj, ev); +} +void WorldListPage::on_rmWorldBtn_clicked() +{ + int first, last; + auto list = ui->worldTreeView->selectionModel()->selectedRows(); + + if (!lastfirst(list, first, last)) + return; + m_worlds->stopWatching(); + m_worlds->deleteWorlds(first, last); + m_worlds->startWatching(); +} + +void WorldListPage::on_viewFolderBtn_clicked() +{ + openDirInDefaultProgram(m_worlds->dir().absolutePath(), true); +} diff --git a/application/pages/WorldListPage.h b/application/pages/WorldListPage.h new file mode 100644 index 00000000..f55f2999 --- /dev/null +++ b/application/pages/WorldListPage.h @@ -0,0 +1,69 @@ +// +// Created by robotbrain on 8/18/15. +// + +#pragma once + +#include + +#include "minecraft/OneSixInstance.h" +#include "BasePage.h" +#include +#include + +class WorldList; +namespace Ui +{ +class WorldListPage; +} + +class WorldListPage : public QWidget, public BasePage +{ + Q_OBJECT + +public: + explicit WorldListPage(BaseInstance *inst, std::shared_ptr worlds, QString id, + QString iconName, QString displayName, QString helpPage = "", + QWidget *parent = 0); + virtual ~WorldListPage(); + + virtual QString displayName() const override + { + return m_displayName; + } + virtual QIcon icon() const override + { + return MMC->getThemedIcon(m_iconName); + } + virtual QString id() const override + { + return m_id; + } + virtual QString helpPage() const override + { + return m_helpName; + } + virtual bool shouldDisplay() const; + + virtual void opened(); + virtual void closed(); + +protected: + bool eventFilter(QObject *obj, QEvent *ev); + bool worldListFilter(QKeyEvent *ev); + +protected: + BaseInstance *m_inst; + +private: + Ui::WorldListPage *ui; + std::shared_ptr m_worlds; + QString m_iconName; + QString m_id; + QString m_displayName; + QString m_helpName; + +private slots: + void on_rmWorldBtn_clicked(); + void on_viewFolderBtn_clicked(); +}; diff --git a/application/pages/WorldListPage.ui b/application/pages/WorldListPage.ui new file mode 100644 index 00000000..b8100acd --- /dev/null +++ b/application/pages/WorldListPage.ui @@ -0,0 +1,94 @@ + + + WorldListPage + + + + 0 + 0 + 723 + 532 + + + + Mods + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + 0 + + + + Tab 1 + + + + + + + 0 + 0 + + + + true + + + QAbstractItemView::DropOnly + + + + + + + + + &Remove + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + &View Folder + + + + + + + + + + + + + + diff --git a/logic/CMakeLists.txt b/logic/CMakeLists.txt index 1f6b62bf..df2aea28 100644 --- a/logic/CMakeLists.txt +++ b/logic/CMakeLists.txt @@ -199,6 +199,10 @@ set(LOGIC_SOURCES minecraft/Mod.cpp minecraft/ModList.h minecraft/ModList.cpp + minecraft/World.h + minecraft/World.cpp + minecraft/WorldList.h + minecraft/WorldList.cpp # FTB ftb/OneSixFTBInstance.h diff --git a/logic/minecraft/LegacyInstance.cpp b/logic/minecraft/LegacyInstance.cpp index 9dd50c80..ff345033 100644 --- a/logic/minecraft/LegacyInstance.cpp +++ b/logic/minecraft/LegacyInstance.cpp @@ -33,6 +33,7 @@ #include #include #include "minecraft/ModList.h" +#include "minecraft/WorldList.h" #include LegacyInstance::LegacyInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir) @@ -319,6 +320,16 @@ std::shared_ptr LegacyInstance::texturePackList() const return texture_pack_list; } +std::shared_ptr LegacyInstance::worldList() const +{ + if (!m_world_list) + { + m_world_list.reset(new WorldList(savesDir())); + } + m_world_list->update(); + return m_world_list; +} + QString LegacyInstance::jarModsDir() const { return PathCombine(instanceRoot(), "instMods"); diff --git a/logic/minecraft/LegacyInstance.h b/logic/minecraft/LegacyInstance.h index 8663daba..724fcd33 100644 --- a/logic/minecraft/LegacyInstance.h +++ b/logic/minecraft/LegacyInstance.h @@ -49,6 +49,7 @@ public: std::shared_ptr coreModList() const; std::shared_ptr loaderModList() const; std::shared_ptr texturePackList() const override; + std::shared_ptr worldList() const override; ////// Directories ////// QString libDir() const; @@ -126,6 +127,7 @@ protected: mutable std::shared_ptr core_mod_list; mutable std::shared_ptr loader_mod_list; mutable std::shared_ptr texture_pack_list; + mutable std::shared_ptr m_world_list; std::shared_ptr m_lwjglFolderSetting; protected slots: diff --git a/logic/minecraft/MinecraftInstance.h b/logic/minecraft/MinecraftInstance.h index b3af2fe8..8c6838e7 100644 --- a/logic/minecraft/MinecraftInstance.h +++ b/logic/minecraft/MinecraftInstance.h @@ -6,6 +6,7 @@ #include "multimc_logic_export.h" class ModList; +class WorldList; class MULTIMC_LOGIC_EXPORT MinecraftInstance: public BaseInstance { @@ -25,6 +26,10 @@ public: { return nullptr; } + virtual std::shared_ptr worldList() const + { + return nullptr; + } /// get all jar mods applicable to this instance's jar virtual QList getJarMods() const { diff --git a/logic/minecraft/OneSixInstance.cpp b/logic/minecraft/OneSixInstance.cpp index 57fa0a68..b0fab8af 100644 --- a/logic/minecraft/OneSixInstance.cpp +++ b/logic/minecraft/OneSixInstance.cpp @@ -35,6 +35,7 @@ #include "minecraft/AssetsUtils.h" #include "icons/IconList.h" +#include "minecraft/WorldList.h" OneSixInstance::OneSixInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir) : MinecraftInstance(globalSettings, settings, rootDir) @@ -392,6 +393,16 @@ std::shared_ptr OneSixInstance::texturePackList() const return m_texture_pack_list; } +std::shared_ptr OneSixInstance::worldList() const +{ + if (!m_world_list) + { + m_world_list.reset(new WorldList(worldDir())); + } + m_world_list->update(); + return m_world_list; +} + bool OneSixInstance::setIntendedVersionId(QString version) { settings()->set("IntendedVersion", version); @@ -556,6 +567,11 @@ QString OneSixInstance::libDir() const return PathCombine(minecraftRoot(), "lib"); } +QString OneSixInstance::worldDir() const +{ + return PathCombine(minecraftRoot(), "saves"); +} + QStringList OneSixInstance::extraArguments() const { auto list = BaseInstance::extraArguments(); diff --git a/logic/minecraft/OneSixInstance.h b/logic/minecraft/OneSixInstance.h index 963bd152..68be1d4d 100644 --- a/logic/minecraft/OneSixInstance.h +++ b/logic/minecraft/OneSixInstance.h @@ -36,6 +36,7 @@ public: std::shared_ptr coreModList() const; std::shared_ptr resourcePackList() const override; std::shared_ptr texturePackList() const override; + std::shared_ptr worldList() const override; virtual QList getJarMods() const override; virtual void createProfile(); @@ -48,6 +49,7 @@ public: QString loaderModsDir() const; QString coreModsDir() const; QString libDir() const; + QString worldDir() const; virtual QString instanceConfigFolder() const override; virtual std::shared_ptr createUpdateTask() override; @@ -102,6 +104,7 @@ protected: mutable std::shared_ptr m_core_mod_list; mutable std::shared_ptr m_resource_pack_list; mutable std::shared_ptr m_texture_pack_list; + mutable std::shared_ptr m_world_list; }; Q_DECLARE_METATYPE(std::shared_ptr) diff --git a/logic/minecraft/World.cpp b/logic/minecraft/World.cpp new file mode 100644 index 00000000..0f7e0db1 --- /dev/null +++ b/logic/minecraft/World.cpp @@ -0,0 +1,72 @@ +/* Copyright 2013-2015 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "World.h" +#include + +World::World(const QFileInfo &file) +{ + repath(file); +} + +void World::repath(const QFileInfo &file) +{ + m_file = file; + m_name = file.fileName(); + is_valid = file.isDir() && QDir(file.filePath()).exists("level.dat"); +} + +bool World::replace(World &with) +{ + if (!destroy()) + return false; + bool success = copyPath(with.m_file.filePath(), m_file.path()); + if (success) + { + m_name = with.m_name; + m_file.refresh(); + } + return success; +} + +bool World::destroy() +{ + if(!is_valid) return false; + if (m_file.isDir()) + { + QDir d(m_file.filePath()); + if (d.removeRecursively()) + { + return true; + } + return false; + } + else + { + return false; + } + return true; +} + +bool World::operator==(const World &other) const +{ + return is_valid == other.is_valid && name() == other.name(); +} +bool World::strongCompare(const World &other) const +{ + return is_valid == other.is_valid && name() == other.name(); +} diff --git a/logic/minecraft/World.h b/logic/minecraft/World.h new file mode 100644 index 00000000..d00062e7 --- /dev/null +++ b/logic/minecraft/World.h @@ -0,0 +1,47 @@ +/* Copyright 2013-2015 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once +#include + +class World +{ +public: + World(const QFileInfo &file); + QString name() const + { + return m_name; + } + bool isValid() const + { + return is_valid; + } + // delete all the files of this mod + bool destroy(); + // replace this mod with a copy of the other + bool replace(World &with); + // change the mod's filesystem path (used by mod lists for *MAGIC* purposes) + void repath(const QFileInfo &file); + + // WEAK compare operator - used for replacing mods + bool operator==(const World &other) const; + bool strongCompare(const World &other) const; + +protected: + + QFileInfo m_file; + QString m_name; + bool is_valid; +}; diff --git a/logic/minecraft/WorldList.cpp b/logic/minecraft/WorldList.cpp new file mode 100644 index 00000000..ae0077e9 --- /dev/null +++ b/logic/minecraft/WorldList.cpp @@ -0,0 +1,226 @@ +/* Copyright 2013-2015 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "WorldList.h" +#include +#include +#include +#include +#include +#include +#include + +WorldList::WorldList(const QString &dir) + : QAbstractListModel(), m_dir(dir) +{ + ensureFolderPathExists(m_dir.absolutePath()); + m_dir.setFilter(QDir::Readable | QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs | + QDir::NoSymLinks); + m_dir.setSorting(QDir::Name | QDir::IgnoreCase | QDir::LocaleAware); + m_watcher = new QFileSystemWatcher(this); + is_watching = false; + connect(m_watcher, SIGNAL(directoryChanged(QString)), this, + SLOT(directoryChanged(QString))); +} + +void WorldList::startWatching() +{ + update(); + is_watching = m_watcher->addPath(m_dir.absolutePath()); + if (is_watching) + { + qDebug() << "Started watching " << m_dir.absolutePath(); + } + else + { + qDebug() << "Failed to start watching " << m_dir.absolutePath(); + } +} + +void WorldList::stopWatching() +{ + is_watching = !m_watcher->removePath(m_dir.absolutePath()); + if (!is_watching) + { + qDebug() << "Stopped watching " << m_dir.absolutePath(); + } + else + { + qDebug() << "Failed to stop watching " << m_dir.absolutePath(); + } +} + +void WorldList::internalSort(QList &what) +{ + auto predicate = [](const World &left, const World &right) + { + return left.name().localeAwareCompare(right.name()) < 0; + }; + std::sort(what.begin(), what.end(), predicate); +} + +bool WorldList::update() +{ + if (!isValid()) + return false; + + QList orderedWorlds; + QList newWorlds; + m_dir.refresh(); + auto folderContents = m_dir.entryInfoList(); + // if there are any untracked files... + if (folderContents.size()) + { + // the order surely changed! + for (auto entry : folderContents) + { + World w(entry); + if(w.isValid()) { + newWorlds.append(w); + } + } + internalSort(newWorlds); + orderedWorlds.append(newWorlds); + } + beginResetModel(); + worlds.swap(orderedWorlds); + endResetModel(); + return true; +} + +void WorldList::directoryChanged(QString path) +{ + update(); +} + +bool WorldList::isValid() +{ + return m_dir.exists() && m_dir.isReadable(); +} + +bool WorldList::deleteWorld(int index) +{ + if (index >= worlds.size() || index < 0) + return false; + World &m = worlds[index]; + if (m.destroy()) + { + beginRemoveRows(QModelIndex(), index, index); + worlds.removeAt(index); + endRemoveRows(); + emit changed(); + return true; + } + return false; +} + +bool WorldList::deleteWorlds(int first, int last) +{ + for (int i = first; i <= last; i++) + { + World &m = worlds[i]; + m.destroy(); + } + beginRemoveRows(QModelIndex(), first, last); + worlds.erase(worlds.begin() + first, worlds.begin() + last + 1); + endRemoveRows(); + emit changed(); + return true; +} + +int WorldList::columnCount(const QModelIndex &parent) const +{ + return 1; +} + +QVariant WorldList::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + int row = index.row(); + int column = index.column(); + + if (row < 0 || row >= worlds.size()) + return QVariant(); + + switch (role) + { + case Qt::DisplayRole: + switch (column) + { + case NameColumn: + return worlds[row].name(); + + default: + return QVariant(); + } + + case Qt::ToolTipRole: + return worlds[row].name(); + default: + return QVariant(); + } +} + +QVariant WorldList::headerData(int section, Qt::Orientation orientation, int role) const +{ + switch (role) + { + case Qt::DisplayRole: + switch (section) + { + case NameColumn: + return tr("Name"); + default: + return QVariant(); + } + + case Qt::ToolTipRole: + switch (section) + { + case NameColumn: + return tr("The name of the world."); + default: + return QVariant(); + } + default: + return QVariant(); + } + return QVariant(); +} + +QStringList WorldList::mimeTypes() const +{ + QStringList types; + types << "text/plain"; + return types; +} + +QMimeData *WorldList::mimeData(const QModelIndexList &indexes) const +{ + QMimeData *data = new QMimeData(); + + if (indexes.size() == 0) + return data; + + auto idx = indexes[0]; + int row = idx.row(); + if (row < 0 || row >= worlds.size()) + return data; + + data->setText(QString::number(row)); + return data; +} diff --git a/logic/minecraft/WorldList.h b/logic/minecraft/WorldList.h new file mode 100644 index 00000000..3f337c72 --- /dev/null +++ b/logic/minecraft/WorldList.h @@ -0,0 +1,101 @@ +/* + * Copyright 2015 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#pragma once + +#include +#include +#include +#include +#include "minecraft/World.h" + +class QFileSystemWatcher; + +class WorldList : public QAbstractListModel +{ + Q_OBJECT +public: + enum Columns { + NameColumn + }; + + WorldList ( const QString &dir ); + + virtual QVariant data ( const QModelIndex &index, int role = Qt::DisplayRole ) const; + + virtual int rowCount ( const QModelIndex &parent = QModelIndex() ) const { + return size(); + } + ; + virtual QVariant headerData ( int section, Qt::Orientation orientation, + int role = Qt::DisplayRole ) const; + virtual int columnCount ( const QModelIndex &parent ) const; + + size_t size() const { + return worlds.size(); + } + ; + bool empty() const { + return size() == 0; + } + World &operator[] ( size_t index ) { + return worlds[index]; + } + + /// Reloads the mod list and returns true if the list changed. + virtual bool update(); + + /// Deletes the mod at the given index. + virtual bool deleteWorld ( int index ); + + /// Deletes all the selected mods + virtual bool deleteWorlds ( int first, int last ); + + /// get data for drag action + virtual QMimeData *mimeData ( const QModelIndexList &indexes ) const; + /// get the supported mime types + virtual QStringList mimeTypes() const; + + void startWatching(); + void stopWatching(); + + virtual bool isValid(); + + QDir dir() { + return m_dir; + } + + const QList & allWorlds() { + return worlds; + } + +private: + void internalSort ( QList & what ); + private +slots: + void directoryChanged ( QString path ); + +signals: + void changed(); + +protected: + QFileSystemWatcher *m_watcher; + bool is_watching; + QDir m_dir; + QList worlds; +}; + -- cgit v1.2.3