Add backup, fix upgrade v4
This commit is contained in:
42
main.cpp
42
main.cpp
@@ -4,6 +4,7 @@
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QDateTime>
|
||||
#include <iostream>
|
||||
|
||||
#include "version2upgrader.h"
|
||||
@@ -11,25 +12,51 @@
|
||||
#include "version4upgrader.h"
|
||||
|
||||
#define SAVE_FILENAME "data.json"
|
||||
#define BACKUP_FILENAME "backup_%1.json"
|
||||
|
||||
|
||||
|
||||
QString get_save_file_path() {
|
||||
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()) {
|
||||
QDir().mkpath(path);
|
||||
std::cerr << "[ERROR] Chronos data folder not found. Chronos never ran on this computer ?????" << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
path += SAVE_FILENAME;
|
||||
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());
|
||||
file->open(QIODevice::ReadOnly);
|
||||
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;
|
||||
@@ -38,7 +65,7 @@ QJsonObject open_save() {
|
||||
|
||||
void save_to_file(QJsonObject save) {
|
||||
QJsonDocument doc(save);
|
||||
QFile *f = new QFile(get_save_file_path());
|
||||
QFile *f = new QFile(get_save_file_path(SAVE_FILENAME));
|
||||
if (f->open(QIODevice::WriteOnly)) {
|
||||
f->write(doc.toJson());
|
||||
f->close();
|
||||
@@ -48,9 +75,10 @@ void save_to_file(QJsonObject save) {
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
std::cout << "Chronos Save Upgrader v2.1" << std::endl;
|
||||
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: {
|
||||
|
||||
Reference in New Issue
Block a user