summaryrefslogtreecommitdiff
path: root/src/profile.cpp
diff options
context:
space:
mode:
authorEran Mizrahi <erasmux@gmail.com>2017-12-17 21:05:18 +0200
committerEran Mizrahi <erasmux@gmail.com>2017-12-18 20:04:44 +0200
commitff31ce636b68a19949d6e4049d20f7cf88e29f99 (patch)
tree1ef28878f4a56e2d86c0072c668db00a2434b389 /src/profile.cpp
parent90421277e7ed0044b499d81f06df95bf62111ce9 (diff)
Fix mod rename with custom profiles path
Diffstat (limited to 'src/profile.cpp')
-rw-r--r--src/profile.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/profile.cpp b/src/profile.cpp
index 3383ffee..00509ef7 100644
--- a/src/profile.cpp
+++ b/src/profile.cpp
@@ -39,6 +39,8 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>.
#include <QStringList> // for QStringList
#include <QtDebug> // for qDebug, qWarning, etc
#include <QtGlobal> // for qPrintable
+#include <QBuffer>
+#include <QDirIterator>
#include <Windows.h>
@@ -242,9 +244,83 @@ void Profile::createTweakedIniFile()
qDebug("%s saved", qPrintable(QDir::toNativeSeparators(tweakedIni)));
}
+// static
+void Profile::renameModInAllProfiles(const QString& oldName, const QString& newName)
+{
+ QDir profilesDir(Settings::instance().getProfileDirectory());
+ profilesDir.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
+ QDirIterator profileIter(profilesDir);
+ while (profileIter.hasNext()) {
+ profileIter.next();
+ QFile modList(profileIter.filePath() + "/modlist.txt");
+ if (modList.exists())
+ renameModInList(modList, oldName, newName);
+ else
+ qWarning("Profile has no modlist.txt : %s", qPrintable(profileIter.filePath()));
+ }
+}
+
+// static
+void Profile::renameModInList(QFile &modList, const QString &oldName, const QString &newName)
+{
+ if (!modList.open(QIODevice::ReadOnly)) {
+ reportError(tr("failed to open %1").arg(modList.fileName()));
+ return;
+ }
+
+ QBuffer outBuffer;
+ outBuffer.open(QIODevice::WriteOnly);
+
+ int renamed = 0;
+ while (!modList.atEnd()) {
+ QByteArray line = modList.readLine();
+
+ if (line.length() == 0) {
+ // ignore empty lines
+ qWarning("mod list contained invalid data: empty line");
+ continue;
+ }
+
+ char spec = line.at(0);
+ if (spec == '#') {
+ // don't touch comments
+ outBuffer.write(line);
+ continue;
+ }
+
+ QString modName = QString::fromUtf8(line).mid(1).trimmed();
+
+ if (modName.isEmpty()) {
+ // file broken?
+ qWarning("mod list contained invalid data: missing mod name");
+ continue;
+ }
+
+ outBuffer.write(QByteArray(1, spec));
+ if (modName == oldName) {
+ modName = newName;
+ ++renamed;
+ }
+ outBuffer.write(modName.toUtf8().constData());
+ outBuffer.write("\r\n");
+ }
+ modList.close();
+
+ if (renamed) {
+ modList.open(QIODevice::WriteOnly);
+ modList.write(outBuffer.buffer());
+ modList.close();
+ }
+
+ if (renamed)
+ qDebug("Renamed %d \"%s\" mod to \"%s\" in %s",
+ renamed, qPrintable(oldName), qPrintable(newName), qPrintable(modList.fileName()));
+}
void Profile::refreshModStatus()
{
+ writeModlistNow(true); // if there are pending changes write them first
+
QFile file(getModlistFileName());
if (!file.open(QIODevice::ReadOnly)) {
throw MyException(tr("\"%1\" is missing or inaccessible").arg(getModlistFileName()));