324 lines
10 KiB
C++
324 lines
10 KiB
C++
// MassBuilderSaveTool
|
|
// Copyright (C) 2021-2024 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 <ctime>
|
|
|
|
#include <algorithm>
|
|
#include <chrono>
|
|
|
|
#include <Corrade/Containers/GrowableArray.h>
|
|
#include <Corrade/Containers/ScopeGuard.h>
|
|
#include <Corrade/Utility/Path.h>
|
|
#include <Corrade/Utility/String.h>
|
|
|
|
#include <zip.h>
|
|
|
|
#include "../Configuration/Configuration.h"
|
|
#include "../Logger/Logger.h"
|
|
#include "../Utilities/Temp.h"
|
|
|
|
#include "BackupManager.h"
|
|
|
|
namespace mbst { namespace Managers {
|
|
|
|
BackupManager::BackupManager() {
|
|
refresh();
|
|
}
|
|
|
|
Containers::StringView
|
|
BackupManager::lastError() {
|
|
return _lastError;
|
|
}
|
|
|
|
void
|
|
BackupManager::refresh() {
|
|
_backups = Containers::Array<Backup>{};
|
|
|
|
scanSubdir(""_s);
|
|
}
|
|
|
|
Containers::ArrayView<const Backup>
|
|
BackupManager::backups() const {
|
|
return _backups;
|
|
}
|
|
|
|
bool
|
|
BackupManager::create(const GameObjects::Profile& profile) {
|
|
if(!profile.valid()) {
|
|
LOG_ERROR(_lastError = "Profile is not valid.");
|
|
return false;
|
|
}
|
|
|
|
const auto timestamp = []{
|
|
std::time_t timestamp = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
|
|
return *std::localtime(×tamp);
|
|
}();
|
|
|
|
auto filename = Utility::format("{}_{}{:.2d}{:.2d}_{:.2d}{:.2d}{:.2d}.backup.mbst",
|
|
Utility::String::replaceAll(profile.companyName(), ' ', '_').data(),
|
|
timestamp.tm_year + 1900, timestamp.tm_mon + 1, timestamp.tm_mday,
|
|
timestamp.tm_hour, timestamp.tm_min, timestamp.tm_sec);
|
|
auto temp_path = Utilities::getTempPath(filename);
|
|
|
|
int error_code = 0;
|
|
auto zip = zip_open(temp_path.data(), ZIP_CREATE|ZIP_TRUNCATE, &error_code);
|
|
if(zip == nullptr) {
|
|
zip_error_t error;
|
|
zip_error_init_with_code(&error, error_code);
|
|
LOG_ERROR(_lastError = zip_error_strerror(&error));
|
|
zip_error_fini(&error);
|
|
return false;
|
|
}
|
|
|
|
Containers::ScopeGuard guard{&filename, [](Containers::String* str){
|
|
Utilities::deleteTempFile(*str);
|
|
}};
|
|
|
|
Containers::StringView save_dir = conf().directories().gameSaves;
|
|
|
|
auto profile_source = zip_source_file(zip, Utility::Path::join(save_dir, profile.filename()).data(), 0, 0);
|
|
if(!profile_source) {
|
|
LOG_ERROR(_lastError = zip_strerror(zip));
|
|
zip_source_free(profile_source);
|
|
return false;
|
|
}
|
|
|
|
if(zip_file_add(zip, profile.filename().data(), profile_source, ZIP_FL_ENC_UTF_8) == -1) {
|
|
LOG_ERROR(_lastError = zip_strerror(zip));
|
|
zip_source_free(profile_source);
|
|
return false;
|
|
}
|
|
|
|
auto comment = Utility::format("{}|{}|{}-{:.2d}-{:.2d}-{:.2d}-{:.2d}-{:.2d}",
|
|
profile.companyName(), profile.isDemo() ? "demo"_s : "full"_s,
|
|
timestamp.tm_year + 1900, timestamp.tm_mon + 1, timestamp.tm_mday,
|
|
timestamp.tm_hour, timestamp.tm_min, timestamp.tm_sec);
|
|
zip_set_archive_comment(zip, comment.data(), comment.size());
|
|
|
|
for(std::uint8_t i = 0; i < 32; ++i) {
|
|
auto build_filename = Utility::format("{}Unit{:.2d}{}.sav",
|
|
profile.isDemo() ? "Demo"_s : ""_s, i,
|
|
profile.account());
|
|
|
|
if(!Utility::Path::exists(Utility::Path::join(save_dir, build_filename))) {
|
|
continue;
|
|
}
|
|
|
|
auto build_source = zip_source_file(zip, Utility::Path::join(save_dir, build_filename).data(), 0, 0);
|
|
if(!build_source) {
|
|
LOG_ERROR(_lastError = zip_strerror(zip));
|
|
zip_source_free(build_source);
|
|
return false;
|
|
}
|
|
|
|
if(zip_file_add(zip, build_filename.data(), build_source, ZIP_FL_ENC_UTF_8) == -1) {
|
|
LOG_ERROR(_lastError = zip_strerror(zip));
|
|
zip_source_free(build_source);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if(zip_close(zip) == -1) {
|
|
LOG_ERROR(_lastError = zip_strerror(zip));
|
|
return false;
|
|
}
|
|
|
|
if(!Utilities::moveFromTemp(filename, conf().directories().backups)) {
|
|
_lastError = Utility::format("Couldn't move {} to {}.", filename, conf().directories().backups);
|
|
return false;
|
|
}
|
|
|
|
guard.release();
|
|
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
BackupManager::remove(std::size_t index) {
|
|
CORRADE_INTERNAL_ASSERT(index < _backups.size());
|
|
|
|
if(!Utility::Path::remove(Utility::Path::join(conf().directories().backups, _backups[index].filename))) {
|
|
LOG_ERROR(_lastError = "Couldn't delete " + _backups[index].filename);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
BackupManager::restore(std::size_t index) {
|
|
CORRADE_INTERNAL_ASSERT(index < _backups.size());
|
|
|
|
const auto& backup = _backups[index];
|
|
|
|
int error_code = 0;
|
|
auto zip = zip_open(Utility::Path::join(conf().directories().backups, backup.filename).data(), ZIP_RDONLY,
|
|
&error_code);
|
|
if(zip == nullptr) {
|
|
zip_error_t error;
|
|
zip_error_init_with_code(&error, error_code);
|
|
LOG_ERROR(_lastError = zip_error_strerror(&error));
|
|
zip_error_fini(&error);
|
|
return false;
|
|
}
|
|
|
|
Containers::ScopeGuard zip_guard{zip, zip_close};
|
|
|
|
auto error_format = "Extraction of file {} failed: {}"_s;
|
|
|
|
for(Containers::StringView file : backup.includedFiles) {
|
|
auto temp_file = Utilities::getTempPath(file);
|
|
auto out = std::fopen(temp_file.cbegin(), "wb");
|
|
if(out == nullptr) {
|
|
LOG_ERROR(_lastError = Utility::format(error_format.data(), file, std::strerror(errno)));
|
|
return false;
|
|
}
|
|
|
|
Containers::ScopeGuard out_guard{out, std::fclose};
|
|
|
|
auto zf = zip_fopen(zip, file.data(), ZIP_FL_ENC_UTF_8);
|
|
if(zf == nullptr) {
|
|
LOG_ERROR(_lastError = Utility::format(error_format.data(), file, zip_strerror(zip)));
|
|
return false;
|
|
}
|
|
|
|
Containers::ScopeGuard zf_guard{zf, zip_fclose};
|
|
|
|
Containers::StaticArray<8192, char> buf{ValueInit};
|
|
|
|
std::int64_t bytes_read;
|
|
while((bytes_read = zip_fread(zf, buf.data(), buf.size())) > 0ll) {
|
|
if(std::fwrite(buf.data(), sizeof(char), bytes_read, out) < static_cast<std::size_t>(bytes_read)) {
|
|
LOG_ERROR(_lastError = Utility::format(error_format.data(), file, "not enough bytes written."));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if(bytes_read == -1) {
|
|
LOG_ERROR(_lastError = Utility::format(error_format.data(), file, "couldn't read bytes from archive."));
|
|
return false;
|
|
}
|
|
|
|
if(!Utilities::moveFromTemp(file, conf().directories().gameSaves)) {
|
|
_lastError = Utility::format("Couldn't move {} to {}.", file, conf().directories().gameSaves);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void
|
|
BackupManager::scanSubdir(Containers::StringView subdir) {
|
|
static std::uint8_t depth = 0;
|
|
|
|
using Flag = Utility::Path::ListFlag;
|
|
auto files = Utility::Path::list(conf().directories().backups, Flag::SkipDirectories|Flag::SkipSpecial);
|
|
if(!files) {
|
|
LOG_ERROR_FORMAT("Couldn't list contents of {}.", conf().directories().backups);
|
|
}
|
|
|
|
auto predicate = [](Containers::StringView file)->bool{
|
|
return !(file.hasSuffix(".mbprofbackup"_s) || file.hasSuffix(".backup.mbst"_s));
|
|
};
|
|
|
|
auto files_view = files->exceptSuffix(files->end() - std::remove_if(files->begin(), files->end(), predicate));
|
|
|
|
int error_code = 0;
|
|
zip_t* zip;
|
|
for(Containers::StringView file : files_view) {
|
|
Backup backup;
|
|
backup.filename = Utility::Path::join(subdir, file);
|
|
|
|
zip = zip_open(Utility::Path::join(conf().directories().backups, file).data(), ZIP_RDONLY, &error_code);
|
|
if(zip == nullptr) {
|
|
continue;
|
|
}
|
|
|
|
Containers::ScopeGuard guard{zip, zip_close};
|
|
|
|
auto num_entries = zip_get_num_entries(zip, ZIP_FL_UNCHANGED);
|
|
|
|
if(num_entries == 0) {
|
|
continue;
|
|
}
|
|
|
|
int comment_length;
|
|
Containers::StringView comment = zip_get_archive_comment(zip, &comment_length, ZIP_FL_UNCHANGED);
|
|
if(comment == nullptr) {
|
|
continue;
|
|
}
|
|
|
|
auto info = comment.split('|');
|
|
|
|
if(info.size() != 3) {
|
|
continue;
|
|
}
|
|
|
|
backup.company = info[0];
|
|
|
|
if(info[1].hasPrefix("full")) {
|
|
backup.demo = false;
|
|
}
|
|
else if(info[1].hasPrefix("demo")) {
|
|
backup.demo = true;
|
|
}
|
|
else {
|
|
continue;
|
|
}
|
|
|
|
auto ts = info[2].split('-');
|
|
if(ts.size() != 6) {
|
|
continue;
|
|
}
|
|
|
|
backup.timestamp.year = std::strtol(ts[0].data(), nullptr, 10);
|
|
backup.timestamp.month = std::strtol(ts[1].data(), nullptr, 10);
|
|
backup.timestamp.day = std::strtol(ts[2].data(), nullptr, 10);
|
|
backup.timestamp.hour = std::strtol(ts[3].data(), nullptr, 10);
|
|
backup.timestamp.minute = std::strtol(ts[4].data(), nullptr, 10);
|
|
backup.timestamp.second = std::strtol(ts[5].data(), nullptr, 10);
|
|
|
|
arrayReserve(backup.includedFiles, num_entries);
|
|
|
|
for(auto i = 0; i < num_entries; i++) {
|
|
arrayAppend(backup.includedFiles, InPlaceInit, zip_get_name(zip, i, ZIP_FL_UNCHANGED));
|
|
}
|
|
|
|
arrayAppend(_backups, Utility::move(backup));
|
|
}
|
|
|
|
auto subdirs = Utility::Path::list(conf().directories().backups,
|
|
Flag::SkipFiles|Flag::SkipSpecial|Flag::SkipDotAndDotDot);
|
|
if(!subdirs) {
|
|
LOG_ERROR_FORMAT("Couldn't list contents of {}.", conf().directories().backups);
|
|
}
|
|
|
|
if(depth == 5) {
|
|
return;
|
|
}
|
|
|
|
depth++;
|
|
|
|
for(auto& dir : *subdirs) {
|
|
scanSubdir(Utility::Path::join(subdir, dir));
|
|
}
|
|
|
|
depth--;
|
|
}
|
|
|
|
}}
|