350 lines
12 KiB
C++
350 lines
12 KiB
C++
// MassBuilderSaveTool
|
|
// Copyright (C) 2021 Guillaume Jacquemin
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#include <cstdio>
|
|
|
|
#include <algorithm>
|
|
#include <chrono>
|
|
#include <iomanip>
|
|
#include <regex>
|
|
|
|
#include <Corrade/Containers/ScopeGuard.h>
|
|
#include <Corrade/Containers/StaticArray.h>
|
|
#include <Corrade/Utility/Directory.h>
|
|
#include <Corrade/Utility/FormatStl.h>
|
|
#include <Corrade/Utility/String.h>
|
|
|
|
#include <zip.h>
|
|
|
|
#include "ProfileManager.h"
|
|
|
|
using namespace Corrade;
|
|
|
|
ProfileManager::ProfileManager(const std::string& base_path):
|
|
_backupsDirectory{Utility::Directory::join(Utility::Directory::path(Utility::Directory::executableLocation()), "backups")}
|
|
{
|
|
_saveDirectory = Utility::Directory::join(base_path, "Saved/SaveGames");
|
|
|
|
if(!Utility::Directory::exists(_backupsDirectory)) {
|
|
Utility::Directory::mkpath(_backupsDirectory);
|
|
}
|
|
|
|
if(!Utility::Directory::exists(_saveDirectory)) {
|
|
_lastError = "Couldn't find the profile directory. Make sure you played enough of the game.";
|
|
return;
|
|
}
|
|
|
|
_ready = refreshProfiles();
|
|
}
|
|
|
|
auto ProfileManager::ready() const -> bool {
|
|
return _ready;
|
|
}
|
|
|
|
auto ProfileManager::lastError() -> std::string const& {
|
|
return _lastError;
|
|
}
|
|
|
|
auto ProfileManager::saveDirectory() -> std::string const& {
|
|
return _saveDirectory;
|
|
}
|
|
|
|
auto ProfileManager::backupsDirectory() -> std::string const& {
|
|
return _backupsDirectory;
|
|
}
|
|
|
|
auto ProfileManager::profiles() -> std::vector<Profile> const& {
|
|
return _profiles;
|
|
}
|
|
|
|
auto ProfileManager::refreshProfiles() -> bool {
|
|
_profiles.clear();
|
|
|
|
using Utility::Directory::Flag;
|
|
std::vector<std::string> files = Utility::Directory::list(_saveDirectory, Flag::SkipSpecial|Flag::SkipDirectories|Flag::SkipDotAndDotDot);
|
|
|
|
auto predicate = [](const std::string& file)->bool{
|
|
std::regex regex("(Demo)?Profile[0-9]{17}\\.sav", std::regex::nosubs);
|
|
std::cmatch m;
|
|
return !std::regex_match(file.c_str(), m, regex);
|
|
};
|
|
|
|
files.erase(std::remove_if(files.begin(), files.end(), predicate), files.end());
|
|
|
|
for(const std::string& file : files) {
|
|
Profile profile{Utility::Directory::join(_saveDirectory, file)};
|
|
|
|
if(!profile.valid()) {
|
|
Utility::Warning{} << "Profile" << file.c_str() << "is invalid:" << profile.lastError().c_str();
|
|
continue;
|
|
}
|
|
|
|
_profiles.push_back(std::move(profile));
|
|
}
|
|
|
|
if(_profiles.empty()) {
|
|
_lastError = "No profiles were found.";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
auto ProfileManager::getProfile(std::size_t index) -> Profile* {
|
|
return &(_profiles.at(index));
|
|
}
|
|
|
|
auto ProfileManager::deleteProfile(std::size_t index, bool delete_builds) -> bool {
|
|
if(!Utility::Directory::rm(Utility::Directory::join(_saveDirectory, _profiles.at(index).filename()))) {
|
|
_lastError = Utility::formatString("Couldn't delete {} (filename: {}).",
|
|
_profiles.at(index).companyName(),
|
|
_profiles.at(index).filename());
|
|
refreshProfiles();
|
|
return false;
|
|
}
|
|
|
|
if(delete_builds) {
|
|
for(std::uint8_t i = 0; i < 32; ++i) {
|
|
std::string filename = Utility::formatString("{}Unit{:.2d}{}.sav",
|
|
_profiles.at(index).type() == ProfileType::Demo ? "Demo": "",
|
|
i, _profiles.at(index).steamId());
|
|
Utility::Directory::rm(Utility::Directory::join(_saveDirectory, filename));
|
|
}
|
|
}
|
|
|
|
_profiles.erase(_profiles.cbegin() + index);
|
|
|
|
return true;
|
|
}
|
|
|
|
auto ProfileManager::backupProfile(std::size_t index, bool backup_builds) -> bool {
|
|
std::time_t timestamp = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
|
|
std::tm* time = std::localtime(×tamp);
|
|
|
|
std::string filename = Utility::formatString("{}_{}{:.2d}{:.2d}_{:.2d}{:.2d}{:.2d}.mbprofbackup",
|
|
Utility::String::replaceAll(_profiles.at(index).companyName(), " ", "_"),
|
|
time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
|
|
time->tm_hour, time->tm_min, time->tm_sec);
|
|
|
|
int error_code = 0;
|
|
zip_error_t error;
|
|
zip_t* zip = zip_open(Utility::Directory::join(_backupsDirectory, filename).c_str(), ZIP_CREATE|ZIP_TRUNCATE, &error_code);
|
|
if(zip == nullptr) {
|
|
zip_error_init_with_code(&error, error_code);
|
|
_lastError = zip_error_strerror(&error);
|
|
return false;
|
|
}
|
|
|
|
zip_source_t* profile_source = zip_source_file(zip, Utility::Directory::toNativeSeparators(Utility::Directory::join(_saveDirectory, _profiles.at(index).filename())).c_str(), 0, 0);
|
|
if(profile_source == nullptr) {
|
|
_lastError = zip_strerror(zip);
|
|
zip_source_free(profile_source);
|
|
return false;
|
|
}
|
|
|
|
if(zip_file_add(zip, _profiles.at(index).filename().c_str(), profile_source, ZIP_FL_ENC_UTF_8) == -1) {
|
|
_lastError = zip_strerror(zip);
|
|
zip_source_free(profile_source);
|
|
return false;
|
|
}
|
|
|
|
std::string comment = Utility::String::join({_profiles.at(index).companyName(),
|
|
_profiles.at(index).type() == ProfileType::Demo ? "demo" : "full",
|
|
Utility::formatString("{}-{:.2d}-{:.2d}-{:.2d}-{:.2d}-{:.2d}",
|
|
time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
|
|
time->tm_hour, time->tm_min, time->tm_sec)
|
|
}, '|');
|
|
zip_set_archive_comment(zip, comment.c_str(), comment.length());
|
|
|
|
if(backup_builds) {
|
|
for(std::uint8_t i = 0; i < 32; ++i) {
|
|
std::string build_filename = Utility::formatString("{}Unit{:.2d}{}.sav",
|
|
_profiles.at(index).type() == ProfileType::Demo ? "Demo": "",
|
|
i, _profiles.at(index).steamId());
|
|
|
|
if(!Utility::Directory::exists(Utility::Directory::join(_saveDirectory, build_filename))) {
|
|
continue;
|
|
}
|
|
|
|
zip_source_t* build_source = zip_source_file(zip, Utility::Directory::toNativeSeparators(Utility::Directory::join(_saveDirectory, build_filename)).c_str(), 0, 0);
|
|
if(build_source == nullptr) {
|
|
zip_source_free(build_source);
|
|
continue;
|
|
}
|
|
|
|
if(zip_file_add(zip, build_filename.c_str(), build_source, ZIP_FL_ENC_UTF_8) == -1) {
|
|
zip_source_free(build_source);
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(zip_close(zip) == -1) {
|
|
_lastError = zip_strerror(zip);
|
|
return false;
|
|
}
|
|
|
|
refreshBackups();
|
|
|
|
return true;
|
|
}
|
|
|
|
auto ProfileManager::backups() -> std::vector<Backup> const& {
|
|
return _backups;
|
|
}
|
|
|
|
void ProfileManager::refreshBackups() {
|
|
_backups.clear();
|
|
|
|
using Utility::Directory::Flag;
|
|
std::vector<std::string> files = Utility::Directory::list(_backupsDirectory, Flag::SkipSpecial|Flag::SkipDirectories|Flag::SkipDotAndDotDot);
|
|
|
|
auto predicate = [](const std::string& file)->bool{
|
|
return !Utility::String::endsWith(file, ".mbprofbackup");
|
|
};
|
|
|
|
files.erase(std::remove_if(files.begin(), files.end(), predicate), files.end());
|
|
|
|
int error_code = 0;
|
|
zip_t* zip = nullptr;
|
|
for(const std::string& file : files) {
|
|
Backup backup;
|
|
backup.filename = file;
|
|
|
|
zip = zip_open(Utility::Directory::join(_backupsDirectory, file).c_str(), ZIP_RDONLY, &error_code);
|
|
if(zip == nullptr) {
|
|
continue;
|
|
}
|
|
|
|
Containers::ScopeGuard guard{zip, zip_close};
|
|
|
|
int comment_length;
|
|
const char* comment = zip_get_archive_comment(zip, &comment_length, ZIP_FL_UNCHANGED);
|
|
if(comment == nullptr) {
|
|
continue;
|
|
}
|
|
|
|
auto info = Utility::String::split(comment, '|');
|
|
|
|
if(info.size() != 3) {
|
|
continue;
|
|
}
|
|
|
|
backup.company = info.at(0);
|
|
|
|
if(info.at(1) == "full") {
|
|
backup.type = ProfileType::FullGame;
|
|
}
|
|
else if(info.at(1) == "demo") {
|
|
backup.type = ProfileType::Demo;
|
|
}
|
|
else {
|
|
continue;
|
|
}
|
|
|
|
auto ts = Utility::String::split(info.at(2), '-');
|
|
if(ts.size() != 6) {
|
|
continue;
|
|
}
|
|
|
|
backup.timestamp.year = std::stoi(ts.at(0));
|
|
backup.timestamp.month = std::stoi(ts.at(1));
|
|
backup.timestamp.day = std::stoi(ts.at(2));
|
|
backup.timestamp.hour = std::stoi(ts.at(3));
|
|
backup.timestamp.minute = std::stoi(ts.at(4));
|
|
backup.timestamp.second = std::stoi(ts.at(5));
|
|
|
|
std::int64_t num_entries = zip_get_num_entries(zip, ZIP_FL_UNCHANGED);
|
|
|
|
if(num_entries == 0) {
|
|
continue;
|
|
}
|
|
|
|
backup.includedFiles.reserve(num_entries);
|
|
|
|
for(std::int64_t i = 0; i < num_entries; i++) {
|
|
backup.includedFiles.emplace_back(zip_get_name(zip, i, ZIP_FL_UNCHANGED));
|
|
}
|
|
|
|
_backups.push_back(std::move(backup));
|
|
}
|
|
}
|
|
|
|
auto ProfileManager::deleteBackup(std::size_t index) -> bool {
|
|
if(!Utility::Directory::rm(Utility::Directory::join(_backupsDirectory, _backups.at(index).filename))) {
|
|
_lastError = "Couldn't delete " + _backups.at(index).filename;
|
|
return false;
|
|
}
|
|
|
|
_backups.erase(_backups.begin() + index);
|
|
|
|
return true;
|
|
}
|
|
|
|
auto ProfileManager::restoreBackup(std::size_t index) -> bool {
|
|
const Backup& backup = _backups.at(index);
|
|
|
|
static const char* error_format = "Extraction of file {} failed: {}";
|
|
|
|
int error_code = 0;
|
|
zip_t* zip = nullptr;
|
|
|
|
zip = zip_open(Utility::Directory::join(_backupsDirectory, backup.filename).c_str(), ZIP_RDONLY, &error_code);
|
|
if(zip == nullptr) {
|
|
zip_error_t error;
|
|
zip_error_init_with_code(&error, error_code);
|
|
_lastError = zip_error_strerror(&error);
|
|
return false;
|
|
}
|
|
|
|
Containers::ScopeGuard zip_guard{zip, zip_close};
|
|
|
|
for(const std::string& file : backup.includedFiles) {
|
|
FILE* out = std::fopen(Utility::Directory::join(_saveDirectory, file).c_str(), "wb");
|
|
if(out == nullptr) {
|
|
_lastError = Utility::formatString(error_format, file, std::strerror(errno));
|
|
return false;
|
|
}
|
|
|
|
Containers::ScopeGuard out_guard{out, std::fclose};
|
|
|
|
zip_file_t* zf = zip_fopen(zip, file.c_str(), ZIP_FL_ENC_GUESS);
|
|
if(zf == nullptr) {
|
|
_lastError = Utility::formatString(error_format, file, zip_strerror(zip));
|
|
return false;
|
|
}
|
|
|
|
Containers::ScopeGuard zf_guard{zf, zip_fclose};
|
|
|
|
Containers::StaticArray<8192, char> buf{ValueInit};
|
|
|
|
std::int64_t bytes_read = 0;
|
|
while((bytes_read = zip_fread(zf, buf.data(), buf.size())) > 0) {
|
|
if(std::fwrite(buf.data(), sizeof(char), bytes_read, out) < static_cast<std::size_t>(bytes_read)) {
|
|
_lastError = Utility::formatString(error_format, file, "not enough bytes written.");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if(bytes_read == -1) {
|
|
_lastError = Utility::formatString(error_format, file, "couldn't read bytes from archive.");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|