Refactor into InstanceDirUpdate.h

Signed-off-by: Yihe Li <winmikedows@hotmail.com>
This commit is contained in:
Yihe Li 2025-03-27 05:47:29 +08:00
parent b550a6c5c4
commit 7b511f4c67
No known key found for this signature in database
6 changed files with 146 additions and 60 deletions

View File

@ -329,62 +329,6 @@ QString BaseInstance::instanceRoot() const
return m_rootDir;
}
bool BaseInstance::updateInstanceRoot(QWidget* parent)
{
QString renamingMode = globalSettings()->get("InstRenamingMode").toString();
if (renamingMode == "MetadataOnly")
return false;
auto oldRoot = instanceRoot();
auto oldName = QFileInfo(oldRoot).baseName();
auto newRoot = FS::PathCombine(QFileInfo(oldRoot).dir().absolutePath(), name());
if (oldRoot == newRoot)
return false;
// Check for conflict
if (QDir(newRoot).exists()) {
QMessageBox::warning(parent, tr("Cannot rename instance"),
tr("New instance root (%1) already exists. <br />Only the metadata will be renamed.").arg(newRoot));
return false;
}
// Ask if we should rename
if (renamingMode == "AskEverytime") {
QMessageBox messageBox(parent);
messageBox.setText(tr("Would you also like to rename the instance folder?"));
messageBox.setInformativeText(tr("Renaming \'%1\' -> \'%2\'").arg(oldName, name()));
messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
messageBox.setDefaultButton(QMessageBox::Yes);
messageBox.setIcon(QMessageBox::Question);
auto checkBox = new QCheckBox(tr("&Remember my choice"), parent);
messageBox.setCheckBox(checkBox);
auto res = messageBox.exec();
if (checkBox->isChecked()) {
if (res == QMessageBox::Yes)
globalSettings()->set("InstRenamingMode", "PhysicalDir");
else
globalSettings()->set("InstRenamingMode", "MetadataOnly");
}
if (res == QMessageBox::No)
return false;
}
// Now we can confirm that a renaming is happening
auto ret = QFile::rename(oldRoot, newRoot);
if (!ret) {
QMessageBox::warning(parent, tr("Cannot rename instance"),
tr("An error occurred when performing the following renaming operation: <br/>"
" - Old instance root: %1<br/>"
" - New instance root: %2<br/>"
"Only the metadata is renamed.")
.arg(oldRoot, newRoot));
return false;
}
return true;
}
SettingsObjectPtr BaseInstance::settings()
{
loadSpecificSettings();

View File

@ -117,9 +117,6 @@ class BaseInstance : public QObject, public std::enable_shared_from_this<BaseIns
/// Path to the instance's root directory.
QString instanceRoot() const;
/// Update instanceRoot to make it sync with name/id; return true if a refresh is needed
bool updateInstanceRoot(QWidget* parent);
/// Path to the instance's game root directory.
virtual QString gameRoot() const { return instanceRoot(); }

View File

@ -21,6 +21,8 @@ set(CORE_SOURCES
BaseVersion.h
BaseInstance.h
BaseInstance.cpp
InstanceDirUpdate.h
InstanceDirUpdate.cpp
NullInstance.h
MMCZip.h
MMCZip.cpp

View File

@ -0,0 +1,99 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
* Copyright (c) 2022 Jamie Mansfield <jmansfield@cadixdev.org>
* Copyright (C) 2023 TheKodeToad <TheKodeToad@proton.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright 2013-2021 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 "InstanceDirUpdate.h"
#include <QCheckBox>
#include <QMessageBox>
#include "FileSystem.h"
bool askToUpdateInstanceDirName(SettingsObjectPtr globalSettings, InstancePtr instance, QWidget* parent)
{
QString renamingMode = globalSettings->get("InstRenamingMode").toString();
if (renamingMode == "MetadataOnly")
return false;
auto oldRoot = instance->instanceRoot();
auto oldName = QFileInfo(oldRoot).baseName();
auto newRoot = FS::PathCombine(QFileInfo(oldRoot).dir().absolutePath(), instance->name());
if (oldRoot == newRoot)
return false;
// Check for conflict
if (QDir(newRoot).exists()) {
QMessageBox::warning(parent, QObject::tr("Cannot rename instance"),
QObject::tr("New instance root (%1) already exists. <br />Only the metadata will be renamed.").arg(newRoot));
return false;
}
// Ask if we should rename
if (renamingMode == "AskEverytime") {
QMessageBox messageBox(parent);
messageBox.setText(QObject::tr("Would you also like to rename the instance folder?"));
messageBox.setInformativeText(QObject::tr("Renaming \'%1\' -> \'%2\'").arg(oldName, instance->name()));
messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
messageBox.setDefaultButton(QMessageBox::Yes);
messageBox.setIcon(QMessageBox::Question);
auto checkBox = new QCheckBox(QObject::tr("&Remember my choice"), parent);
messageBox.setCheckBox(checkBox);
auto res = messageBox.exec();
if (checkBox->isChecked()) {
if (res == QMessageBox::Yes)
globalSettings->set("InstRenamingMode", "PhysicalDir");
else
globalSettings->set("InstRenamingMode", "MetadataOnly");
}
if (res == QMessageBox::No)
return false;
}
// Now we can confirm that a renaming is happening
auto ret = QFile::rename(oldRoot, newRoot);
if (!ret) {
QMessageBox::warning(parent, QObject::tr("Cannot rename instance"),
QObject::tr("An error occurred when performing the following renaming operation: <br/>"
" - Old instance root: %1<br/>"
" - New instance root: %2<br/>"
"Only the metadata is renamed.")
.arg(oldRoot, newRoot));
return false;
}
return true;
}

View File

@ -0,0 +1,43 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
* Copyright (c) 2022 Jamie Mansfield <jmansfield@cadixdev.org>
* Copyright (C) 2023 TheKodeToad <TheKodeToad@proton.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright 2013-2021 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 "BaseInstance.h"
/// Update instanceRoot to make it sync with name/id; return true if a refresh is needed
bool askToUpdateInstanceDirName(SettingsObjectPtr globalSettings, InstancePtr instance, QWidget* parent);

View File

@ -124,6 +124,7 @@
#include "KonamiCode.h"
#include "InstanceCopyTask.h"
#include "InstanceDirUpdate.h"
#include "Json.h"
@ -1704,7 +1705,7 @@ void MainWindow::instanceDataChanged(const QModelIndex& topLeft, const QModelInd
QItemSelection test(topLeft, bottomRight);
if (test.contains(current)) {
instanceChanged(current, current);
if (m_selectedInstance && m_selectedInstance->updateInstanceRoot(this)) {
if (m_selectedInstance && askToUpdateInstanceDirName(APPLICATION->settings(), m_selectedInstance, this)) {
auto newID = m_selectedInstance->name();
refreshInstances();
setSelectedInstanceById(newID);