Skip to content

Commit

Permalink
[PathUtils] Added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed May 5, 2020
1 parent 17c3655 commit 1c80369
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 36 deletions.
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 4 additions & 35 deletions source/InstanceEditWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
#include <QMessageBox>
#include <QPushButton>
#include <QRegExpValidator>
#include <QStandardPaths>
#include <QTabWidget>
#include <QVBoxLayout>

#include "ContentData.hpp"
#include "InstanceEditModTab.hpp"
#include "InstanceEditVersionTab.hpp"
#include "InstanceEditWindow.hpp"
#include "PathUtils.hpp"
#include "Utils.hpp"

InstanceEditWindow::InstanceEditWindow(ContentData &data, ContentInstance *instance, QWidget *parent)
Expand Down Expand Up @@ -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();

Expand Down
82 changes: 82 additions & 0 deletions source/PathUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* =====================================================================================
*
* OpenMiner Launcher
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
*
* 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 <QDebug>
#include <QDir>
#include <QStandardPaths>

#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());
}
}

43 changes: 43 additions & 0 deletions source/PathUtils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* =====================================================================================
*
* OpenMiner Launcher
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
*
* 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 <QString>

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_

0 comments on commit 1c80369

Please sign in to comment.