pollymc/launcher/modplatform/helpers/OverrideUtils.cpp
flow 7024acac06
feat: add override helper functions
These help us keep track of relevant metadata information about
overrides, so that we know what they are when we update a pack.

Signed-off-by: flow <flowlnlnln@gmail.com>
2022-09-20 18:36:08 -03:00

60 lines
1.4 KiB
C++

#include "OverrideUtils.h"
#include <QDirIterator>
#include "FileSystem.h"
namespace Override {
void createOverrides(QString name, QString parent_folder, QString override_path)
{
QString file_path(FS::PathCombine(parent_folder, name + ".txt"));
if (QFile::exists(file_path))
QFile::remove(file_path);
FS::ensureFilePathExists(file_path);
QFile file(file_path);
file.open(QFile::WriteOnly);
QDirIterator override_iterator(override_path, QDirIterator::Subdirectories);
while (override_iterator.hasNext()) {
auto override_file_path = override_iterator.next();
QFileInfo info(override_file_path);
if (info.isFile()) {
// Absolute path with temp directory -> relative path
override_file_path = override_file_path.split(name).last().remove(0, 1);
file.write(override_file_path.toUtf8());
file.write("\n");
}
}
file.close();
}
QStringList readOverrides(QString name, QString parent_folder)
{
QString file_path(FS::PathCombine(parent_folder, name + ".txt"));
QFile file(file_path);
if (!file.exists())
return {};
QStringList previous_overrides;
file.open(QFile::ReadOnly);
QString entry;
do {
entry = file.readLine();
previous_overrides.append(entry.trimmed());
} while (!entry.isEmpty());
file.close();
return previous_overrides;
}
} // namespace Override