diff --git a/TODO b/TODO index ac3f8e4..e4ec6a3 100644 --- a/TODO +++ b/TODO @@ -6,7 +6,7 @@ TODO • DONE: Ability to add multiple mods to a single instance • TODO: Ability to select version for each mod added • TODO: Interface is not refreshed for mod versions the first time -• TODO: Instance edition +• DONE: Instance edition • DONE: Retrieve users and news from API • DONE: Display username in list views • DONE: News tab diff --git a/source/InstanceEditWindow.cpp b/source/InstanceEditWindow.cpp index 14c2af4..d6ad0ae 100644 --- a/source/InstanceEditWindow.cpp +++ b/source/InstanceEditWindow.cpp @@ -30,7 +30,6 @@ #include #include #include -#include #include #include @@ -38,6 +37,7 @@ #include "InstanceEditModTab.hpp" #include "InstanceEditVersionTab.hpp" #include "InstanceEditWindow.hpp" +#include "PathUtils.hpp" #include "Utils.hpp" InstanceEditWindow::InstanceEditWindow(ContentData &data, ContentInstance *instance, QWidget *parent) @@ -95,44 +95,13 @@ void InstanceEditWindow::saveChanges() { } } - QString appData = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); - QString oldInstancePath = appData + "/instances/" + m_instance->name(); - QString newInstancePath = appData + "/instances/" + m_nameEdit->text(); - - // Update the instance - + QString oldName = m_instance->name(); m_instance->setName(m_nameEdit->text()); m_instance->setEngineVersionID(m_versionTab->engineVersionID()); m_instance->setMods(m_modTab->modList()); - // Reinstallation of the instance - - Utils::copyDirectory(oldInstancePath, newInstancePath); - - QDir oldInstanceDir{oldInstancePath}; - if (!oldInstanceDir.removeRecursively()) - qDebug() << "Failed to remove" << oldInstanceDir.path(); - - QDir resourcesDir{newInstancePath + "/resources"}; - if (!resourcesDir.removeRecursively()) - qDebug() << "Failed to remove" << resourcesDir.path(); - - QDir modsDir{newInstancePath + "/mods"}; - if (!modsDir.removeRecursively()) - qDebug() << "Failed to remove" << modsDir.path(); - - QString versionPath = appData + "/versions/" + QString::number(m_instance->engineVersionID()) + "/openminer"; - Utils::copyDirectory(versionPath + "/resources", newInstancePath + "/resources"); - - auto mods = m_instance->mods(); - for (auto &it : mods) { - ContentMod *mod = m_data.getMod(it); - QString modPath = appData + "/mods/" + QString::number(mod->id()) + "/" - + QString::number(mod->latestVersionID()) + "/"; - - // FIXME: Use a mod string ID instead of the name - Utils::copyDirectory(modPath + mod->name(), newInstancePath + "/mods/" + mod->name()); - } + PathUtils::renameInstance(oldName, m_instance->name()); + PathUtils::reinstallInstance(*m_instance, m_data); accept(); diff --git a/source/PathUtils.cpp b/source/PathUtils.cpp new file mode 100644 index 0000000..b9d473a --- /dev/null +++ b/source/PathUtils.cpp @@ -0,0 +1,82 @@ +/* + * ===================================================================================== + * + * OpenMiner Launcher + * + * Copyright (C) 2018-2020 Unarelith, Quentin Bazin + * + * This file is part of OpenMiner. + * + * OpenMiner is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * OpenMiner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with OpenMiner; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * ===================================================================================== + */ +#include +#include +#include + +#include "ContentData.hpp" +#include "PathUtils.hpp" +#include "Utils.hpp" + +QString PathUtils::getInstancePath(const QString &instanceName) { + QString appData = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); + return appData + "/instances/" + instanceName; +} + +QString PathUtils::getEnginePath(int versionID) { + QString appData = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); + return appData + "/versions/" + QString::number(versionID) + "/openminer"; +} + +QString PathUtils::getModPath(int modID, int versionID, const QString &modName) { + QString appData = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); + return appData + "/mods/" + QString::number(modID) + "/" + QString::number(versionID) + "/" + modName; +} + +void PathUtils::renameInstance(const QString &oldName, const QString &newName) { + QString oldInstancePath = getInstancePath(oldName); + QString newInstancePath = getInstancePath(newName); + + Utils::copyDirectory(oldInstancePath, newInstancePath); + + QDir oldInstanceDir{oldInstancePath}; + if (!oldInstanceDir.removeRecursively()) + qDebug() << "Failed to remove" << oldInstanceDir.path(); + + QDir resourcesDir{newInstancePath + "/resources"}; + if (!resourcesDir.removeRecursively()) + qDebug() << "Failed to remove" << resourcesDir.path(); + + QDir modsDir{newInstancePath + "/mods"}; + if (!modsDir.removeRecursively()) + qDebug() << "Failed to remove" << modsDir.path(); +} + +void PathUtils::reinstallInstance(const ContentInstance &instance, ContentData &data) { + QString instancePath = getInstancePath(instance.name()); + QString versionPath = getEnginePath(instance.engineVersionID()); + Utils::copyDirectory(versionPath + "/resources", instancePath + "/resources"); + + auto mods = instance.mods(); + for (auto &it : mods) { + ContentMod *mod = data.getMod(it); + QString modPath = PathUtils::getModPath(mod->id(), mod->latestVersionID(), mod->name()); + + // FIXME: Use a mod string ID instead of the name + Utils::copyDirectory(modPath, instancePath + "/mods/" + mod->name()); + } +} + diff --git a/source/PathUtils.hpp b/source/PathUtils.hpp new file mode 100644 index 0000000..bfaa0b7 --- /dev/null +++ b/source/PathUtils.hpp @@ -0,0 +1,43 @@ +/* + * ===================================================================================== + * + * OpenMiner Launcher + * + * Copyright (C) 2018-2020 Unarelith, Quentin Bazin + * + * This file is part of OpenMiner. + * + * OpenMiner is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * OpenMiner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with OpenMiner; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * ===================================================================================== + */ +#ifndef PATHUTILS_HPP_ +#define PATHUTILS_HPP_ + +#include + +class ContentData; +class ContentInstance; + +namespace PathUtils { + QString getInstancePath(const QString &instanceName); + QString getEnginePath(int versionID); + QString getModPath(int modID, int versionID, const QString &modName); + + void renameInstance(const QString &oldName, const QString &newName); + void reinstallInstance(const ContentInstance &instance, ContentData &data); +} + +#endif // PATHUTILS_HPP_