Files
chronos-save-updater/main.cpp
2022-10-17 20:01:27 +02:00

105 lines
3.3 KiB
C++

#include <QCoreApplication>
#include <QStandardPaths>
#include <QDir>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDateTime>
#include <iostream>
#include "version2upgrader.h"
#include "version3upgrader.h"
#include "version4upgrader.h"
#define SAVE_FILENAME "data.json"
#define BACKUP_FILENAME "backup_%1.json"
QString get_save_file_path(QString name) {
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
if (!QDir(path).exists()) {
QDir().mkpath(path);
}
path += "/data/";
if (!QDir(path).exists()) {
std::cerr << "[ERROR] Chronos data folder not found. Chronos never ran on this computer ?????" << std::endl;
exit(-1);
}
path += name;
return path;
}
void make_backup() {
std::cout << "[INFO] Making backup..." << std::endl;
QFile* file = new QFile(get_save_file_path(SAVE_FILENAME));
bool res = file->open(QIODevice::ReadOnly);
if (!res) {
std::cerr << "[ERROR] Chronos data file not found. Chronos never ran on this computer ?????" << std::endl;
exit(-1);
}
QString backup_file_name = QString(BACKUP_FILENAME).arg(QDateTime::currentSecsSinceEpoch());
QFile *f = new QFile(get_save_file_path(backup_file_name));
if (f->open(QIODevice::WriteOnly)) {
f->write(file->readAll());
f->close();
}
file->close();
delete file;
delete f;
std::cout << "[INFO] Backup was created on " << backup_file_name.toStdString() << std::endl;
}
QJsonObject open_save() {
QFile* file = new QFile(get_save_file_path(SAVE_FILENAME));
bool res = file->open(QIODevice::ReadOnly);
if (!res) {
std::cerr << "[ERROR] Chronos data file not found. Chronos never ran on this computer ?????" << std::endl;
exit(-1);
}
auto json = QString(file->readAll());
file->close();
delete file;
return QJsonDocument::fromJson(json.toUtf8()).object();
}
void save_to_file(QJsonObject save) {
QJsonDocument doc(save);
QFile *f = new QFile(get_save_file_path(SAVE_FILENAME));
if (f->open(QIODevice::WriteOnly)) {
f->write(doc.toJson());
f->close();
}
delete f;
}
int main(int argc, char *argv[])
{
std::cout << "Chronos Save Upgrader v2.1.1" << std::endl;
std::cout << "by Aurélie Delhaie - https://github.com/mojitaurelie/chronos-save-updater" << std::endl << std::endl;
QCoreApplication::setApplicationName("Chronos");
make_backup();
QJsonObject save = open_save();
switch (save["version"].toInt()) {
case 1: {
std::cout << "[INFO] Upgrading to version 2" << std::endl;
save = Version2Upgrader::Upgrade(save);
std::cout << "[INFO] Save upgraded to version 2" << std::endl;
}
case 2: {
std::cout << "[INFO] Upgrading to version 3" << std::endl;
save = Version3Upgrader::Upgrade(save);
std::cout << "[INFO] Save upgraded to version 3" << std::endl;
}
case 3: {
std::cout << "[INFO] Upgrading to version 4" << std::endl;
save = Version4Upgrader::Upgrade(save);
std::cout << "[INFO] Save upgraded to version 4" << std::endl;
}
}
std::cout << "[INFO] Saving..." << std::endl;
save_to_file(save);
std::cout << "[INFO] Saved" << std::endl;
return 0;
}