MassBuilderSaveTool/src/MassManager/MassManager.cpp

215 lines
6.8 KiB
C++

// MassBuilderSaveTool
// Copyright (C) 2021-2022 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 <algorithm>
#include <Corrade/Utility/Directory.h>
#include <Corrade/Utility/FormatStl.h>
#include <Corrade/Utility/String.h>
#include "MassManager.h"
static const std::string empty_string = "";
MassManager::MassManager(const std::string& save_path, const std::string& steam_id, bool demo, const std::string& staging_dir):
_saveDirectory{save_path},
_steamId{steam_id},
_demo{demo},
_stagingAreaDirectory{staging_dir}
{
Containers::arrayReserve(_hangars, 32);
std::string mass_filename = "";
for(int i = 0; i < 32; i++) {
mass_filename = Utility::Directory::join(_saveDirectory, Utility::formatString("{}Unit{:.2d}{}.sav", demo ? "Demo" : "", i, _steamId));
Containers::arrayAppend(_hangars, Mass{mass_filename});
}
if(!Utility::Directory::exists(_stagingAreaDirectory)) {
Utility::Directory::mkpath(_stagingAreaDirectory);
}
refreshStagedMasses();
}
auto MassManager::lastError() -> std::string const& {
return _lastError;
}
auto MassManager::hangar(Int hangar) -> Mass& {
return _hangars[hangar];
}
void MassManager::refreshHangar(Int hangar) {
if(hangar < 0 || hangar >= 32) {
return;
}
std::string mass_filename =
Utility::Directory::join(_saveDirectory, Utility::formatString("{}Unit{:.2d}{}.sav", _demo ? "Demo" : "", hangar, _steamId));
_hangars[hangar] = Mass{mass_filename};
}
auto MassManager::importMass(const std::string& staged_fn, Int hangar) -> bool {
if(hangar < 0 || hangar >= 32) {
_lastError = "Hangar out of range in MassManager::importMass()";
return false;
}
auto it = _stagedMasses.find(staged_fn);
if(it == _stagedMasses.end()) {
_lastError = "Couldn't find " + staged_fn + " in the staged M.A.S.S.es.";
return false;
}
std::string source = Utility::Directory::join(_stagingAreaDirectory, staged_fn);
Utility::Directory::copy(source, source + ".tmp");
{
Mass mass{source + ".tmp"};
if(!mass.updateAccount(_steamId)) {
_lastError = mass.lastError();
Utility::Directory::rm(source + ".tmp");
return false;
}
}
std::string dest = Utility::Directory::join(_saveDirectory, _hangars[hangar].filename());
if(Utility::Directory::exists(dest)) {
Utility::Directory::rm(dest);
}
if(!Utility::Directory::move(source + ".tmp", dest)) {
_lastError = Utility::formatString("Couldn't move {} to hangar {:.2d}", staged_fn, hangar + 1);
return false;
}
return true;
}
auto MassManager::exportMass(Int hangar) -> bool {
if(hangar < 0 || hangar >= 32) {
_lastError = "Hangar out of range in MassManager::exportMass()";
return false;
}
if(_hangars[hangar].state() != Mass::State::Valid) {
_lastError = Utility::formatString("There is no valid data to export in hangar {:.2d}", hangar + 1);
return false;
}
std::string source = Utility::Directory::join(_saveDirectory, _hangars[hangar].filename());
std::string dest = Utility::Directory::join(_stagingAreaDirectory,
Utility::formatString("{}_{}.sav", *_hangars[hangar].name(), _steamId));
if(!Utility::Directory::copy(source, dest)) {
_lastError = Utility::formatString("Couldn't export data from hangar {:.2d} to {}", hangar, dest);
return false;
}
return true;
}
auto MassManager::moveMass(Int source, Int destination) -> bool {
if(source < 0 || source >= 32) {
_lastError = "Source hangar out of range.";
return false;
}
if(destination < 0 || destination >= 32) {
_lastError = "Destination hangar out of range.";
return false;
}
std::string source_file = Utility::Directory::join(_saveDirectory, _hangars[source].filename());
std::string dest_file = Utility::Directory::join(_saveDirectory, _hangars[destination].filename());
Mass::State dest_state = _hangars[destination].state();
switch(dest_state) {
case Mass::State::Empty:
break;
case Mass::State::Invalid:
Utility::Directory::rm(dest_file);
break;
case Mass::State::Valid:
Utility::Directory::move(dest_file, dest_file + ".tmp");
break;
}
Utility::Directory::move(source_file, dest_file);
if(dest_state == Mass::State::Valid) {
Utility::Directory::move(dest_file + ".tmp", source_file);
}
return true;
}
auto MassManager::deleteMass(Int hangar) -> bool {
if(hangar < 0 || hangar >= 32) {
_lastError = "Hangar out of range.";
return false;
}
if(!Utility::Directory::rm(Utility::Directory::join(_saveDirectory, _hangars[hangar].filename()))) {
_lastError = Utility::formatString("Deletion failed: {}", std::strerror(errno));
return false;
}
return true;
}
auto MassManager::stagedMasses() -> std::map<std::string, std::string> const& {
return _stagedMasses;
}
void MassManager::refreshStagedMasses() {
_stagedMasses.clear();
using Utility::Directory::Flag;
std::vector<std::string> file_list = Utility::Directory::list(_stagingAreaDirectory, Flag::SkipSpecial|Flag::SkipDirectories|Flag::SkipDotAndDotDot);
auto iter = std::remove_if(file_list.begin(), file_list.end(), [](std::string& file){
return !Utility::String::endsWith(file, ".sav");
});
file_list.erase(iter, file_list.end());
for(const std::string& file : file_list) {
std::string name = *Mass::getNameFromFile(Utility::Directory::join(_stagingAreaDirectory, file));
if(!name.empty()) {
_stagedMasses[file] = name;
}
}
}
auto MassManager::deleteStagedMass(const std::string& filename) -> bool {
if(_stagedMasses.find(filename) == _stagedMasses.cend()) {
_lastError = "The file " + filename + " couldn't be found in the list of staged M.A.S.S.es.";
return false;
}
if(!Utility::Directory::rm(Utility::Directory::join(_stagingAreaDirectory, filename))) {
_lastError = Utility::formatString("{} couldn't be deleted: {}", filename, std::strerror(errno));
return false;
}
return true;
}