Switch to C++ and Qt
This commit is contained in:
70
main.cpp
Normal file
70
main.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
#include <QCoreApplication>
|
||||
#include <QStandardPaths>
|
||||
#include <QDir>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <iostream>
|
||||
|
||||
#include "version2upgrader.h"
|
||||
#include "version3upgrader.h"
|
||||
|
||||
#define SAVE_FILENAME "data.json"
|
||||
|
||||
|
||||
|
||||
QString get_save_file_path() {
|
||||
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
||||
if (!QDir(path).exists()) {
|
||||
QDir().mkpath(path);
|
||||
}
|
||||
path += "/data/";
|
||||
if (!QDir(path).exists()) {
|
||||
QDir().mkpath(path);
|
||||
}
|
||||
path += SAVE_FILENAME;
|
||||
return path;
|
||||
}
|
||||
|
||||
QJsonObject open_save() {
|
||||
QFile* file = new QFile(get_save_file_path());
|
||||
file->open(QIODevice::ReadOnly);
|
||||
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());
|
||||
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" << std::endl;
|
||||
std::cout << "by Aurélie Delhaie - https://github.com/mojitaurelie/chronos-save-updater" << std::endl << std::endl;
|
||||
QCoreApplication::setApplicationName("Chronos");
|
||||
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;
|
||||
}
|
||||
}
|
||||
std::cout << "[INFO] Saving..." << std::endl;
|
||||
save_to_file(save);
|
||||
std::cout << "[INFO] Saved" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user