2020-01-12 14:54:23 +01:00
|
|
|
// wxMASSManager
|
|
|
|
// Copyright (C) 2020 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/>.
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
#include <Corrade/version.h>
|
2020-01-12 14:54:23 +01:00
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
#if !(CORRADE_VERSION_YEAR * 100 + CORRADE_VERSION_MONTH >= 202006)
|
|
|
|
#error This application requires Corrade 2020.06 or later to build.
|
|
|
|
#endif
|
2020-01-12 14:54:23 +01:00
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
#include <algorithm>
|
2020-01-12 14:54:23 +01:00
|
|
|
|
|
|
|
#include <Corrade/Utility/Directory.h>
|
|
|
|
#include <Corrade/Utility/FormatStl.h>
|
|
|
|
#include <Corrade/Utility/String.h>
|
|
|
|
|
|
|
|
#include "MassManager.h"
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
static const std::string empty_string = "";
|
2020-01-12 14:54:23 +01:00
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
const std::string MassManager::_stagingAreaDirectory =
|
|
|
|
Utility::Directory::join(Utility::Directory::path(Utility::Directory::executableLocation()), "staging");
|
2020-01-12 14:54:23 +01:00
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
MassManager::MassManager(const std::string& save_path, const std::string& steam_id, bool demo) {
|
|
|
|
_saveDirectory = save_path;
|
|
|
|
_steamId = steam_id;
|
|
|
|
_demo = demo;
|
2020-01-12 14:54:23 +01:00
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
Containers::arrayReserve(_hangars, 32);
|
2020-06-21 08:52:26 +02:00
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
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});
|
2020-01-12 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
if(!Utility::Directory::exists(_stagingAreaDirectory)) {
|
|
|
|
Utility::Directory::mkpath(_stagingAreaDirectory);
|
2020-01-12 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
2020-07-22 08:55:12 +02:00
|
|
|
using Utility::Directory::Flag;
|
2020-09-30 16:27:26 +02:00
|
|
|
std::vector<std::string> file_list =
|
|
|
|
Utility::Directory::list(_stagingAreaDirectory, Flag::SkipSpecial|Flag::SkipDirectories|Flag::SkipDotAndDotDot);
|
2020-07-22 08:55:12 +02:00
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
auto iter = std::remove_if(file_list.begin(), file_list.end(), [](std::string& file){
|
|
|
|
return !Utility::String::endsWith(file, ".sav");
|
|
|
|
});
|
2020-07-22 08:55:12 +02:00
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
file_list.erase(iter, file_list.end());
|
2020-07-22 08:55:12 +02:00
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
for(const std::string& file : file_list) {
|
|
|
|
std::string name = Mass::getNameFromFile(Utility::Directory::join(_stagingAreaDirectory, file));
|
2020-07-22 08:55:12 +02:00
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
if(name != "") {
|
|
|
|
_stagedMasses[file] = name;
|
|
|
|
}
|
2020-07-22 08:55:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 14:54:23 +01:00
|
|
|
auto MassManager::saveDirectory() -> std::string const& {
|
|
|
|
return _saveDirectory;
|
|
|
|
}
|
|
|
|
|
2020-01-16 00:35:23 +01:00
|
|
|
auto MassManager::stagingAreaDirectory() -> std::string const& {
|
|
|
|
return _stagingAreaDirectory;
|
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
auto MassManager::lastError() -> std::string const& {
|
|
|
|
return _lastError;
|
2020-01-12 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
auto MassManager::massName(int hangar) -> std::string const& {
|
|
|
|
if(hangar < 0 || hangar >= 32) {
|
|
|
|
return empty_string;
|
2020-01-12 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
return _hangars[hangar].name();
|
2020-01-12 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
auto MassManager::massState(int hangar) -> MassState {
|
|
|
|
if(hangar < 0 || hangar >= 32) {
|
|
|
|
return MassState::Empty;
|
2020-01-12 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
return _hangars[hangar].state();
|
2020-01-12 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
void MassManager::refreshHangar(int hangar) {
|
|
|
|
if(hangar < 0 || hangar >= 32) {
|
|
|
|
return;
|
2020-01-12 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
std::string mass_filename =
|
|
|
|
Utility::Directory::join(_saveDirectory, Utility::formatString("{}Unit{:.2d}{}.sav", _demo ? "Demo" : "", hangar, _steamId));
|
|
|
|
_hangars[hangar] = Mass{mass_filename};
|
2020-01-12 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
2020-01-16 00:35:23 +01:00
|
|
|
auto MassManager::importMass(int staged_index, int hangar) -> bool {
|
2020-09-30 16:27:26 +02:00
|
|
|
if(hangar < 0 || hangar >= 32) {
|
2020-01-16 00:35:23 +01:00
|
|
|
_lastError = "Hangar out of range in MassManager::importMass()";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for(const auto& mass_info : _stagedMasses) {
|
|
|
|
if(i != staged_index) {
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string source = Utility::Directory::join(_stagingAreaDirectory, mass_info.first);
|
|
|
|
|
|
|
|
Utility::Directory::copy(source, source + ".tmp");
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
if(!Mass{source + ".tmp"}.updateSteamId(_steamId))
|
2020-01-16 00:35:23 +01:00
|
|
|
{
|
2020-09-30 16:27:26 +02:00
|
|
|
_lastError = "The M.A.S.S. file at " + source + " seems to be corrupt.";
|
|
|
|
Utility::Directory::rm(source + ".tmp");
|
|
|
|
return false;
|
2020-01-16 00:35:23 +01:00
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
if(Utility::Directory::exists(_hangars[hangar].filename())) {
|
|
|
|
Utility::Directory::rm(_hangars[hangar].filename());
|
2020-01-16 00:35:23 +01:00
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
Utility::Directory::move(source + ".tmp", _hangars[hangar].filename());
|
2020-01-16 00:35:23 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_lastError = "";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto MassManager::exportMass(int hangar) -> bool {
|
2020-09-30 16:27:26 +02:00
|
|
|
if(hangar < 0 || hangar >= 32) {
|
2020-01-16 00:35:23 +01:00
|
|
|
_lastError = "Hangar out of range in MassManager::exportMass()";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
if(_hangars[hangar].state() == MassState::Empty ||
|
|
|
|
_hangars[hangar].state() == MassState::Invalid) {
|
2020-01-16 00:35:23 +01:00
|
|
|
_lastError = Utility::formatString("There is no valid data to export in hangar {:.2d}", hangar);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
auto name = _hangars[hangar].name();
|
|
|
|
|
|
|
|
std::string source = Utility::Directory::join(_saveDirectory, _hangars[hangar].filename());
|
|
|
|
std::string dest = Utility::Directory::join(_stagingAreaDirectory, Utility::formatString("{}_{}.sav", _steamId, _hangars[hangar].name()));
|
2020-01-16 00:35:23 +01:00
|
|
|
|
|
|
|
if(!Utility::Directory::copy(source, dest)) {
|
|
|
|
_lastError = Utility::formatString("Couldn't export data from hangar {:.2d} to {}", hangar, dest);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-12 14:54:23 +01:00
|
|
|
auto MassManager::moveMass(int source, int destination) -> bool {
|
2020-09-30 16:27:26 +02:00
|
|
|
if(source < 0 || source >= 32) {
|
|
|
|
_lastError = "Source hangar out of range.";
|
2020-01-12 14:54:23 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
if(destination < 0 || destination >= 32) {
|
|
|
|
_lastError = "Destination hangar out of range.";
|
2020-01-12 14:54:23 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
std::string source_file = _hangars[source].filename();
|
|
|
|
std::string dest_file = _hangars[destination].filename();
|
|
|
|
MassState dest_state = _hangars[destination].state();
|
2020-01-12 14:54:23 +01:00
|
|
|
|
|
|
|
switch(dest_state) {
|
2020-09-30 16:27:26 +02:00
|
|
|
case MassState::Empty:
|
2020-01-12 14:54:23 +01:00
|
|
|
break;
|
2020-09-30 16:27:26 +02:00
|
|
|
case MassState::Invalid:
|
2020-01-12 14:54:23 +01:00
|
|
|
Utility::Directory::rm(dest_file);
|
|
|
|
break;
|
2020-09-30 16:27:26 +02:00
|
|
|
case MassState::Valid:
|
2020-01-12 14:54:23 +01:00
|
|
|
Utility::Directory::move(dest_file, dest_file + ".tmp");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Utility::Directory::move(source_file, dest_file);
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
if(dest_state == MassState::Valid) {
|
2020-01-12 14:54:23 +01:00
|
|
|
Utility::Directory::move(dest_file + ".tmp", source_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-14 10:39:13 +01:00
|
|
|
auto MassManager::deleteMass(int hangar) -> bool {
|
2020-09-30 16:27:26 +02:00
|
|
|
if(hangar < 0 || hangar >= 32) {
|
|
|
|
_lastError = "Hangar out of bounds";
|
2020-01-12 14:54:23 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
bool result = Utility::Directory::rm(_hangars[hangar].filename());
|
|
|
|
if(!result) {
|
|
|
|
_lastError = "Deletion failed. Maybe the file was already deleted, or it's locked by another application.";
|
2020-01-12 14:54:23 +01:00
|
|
|
}
|
2020-09-30 16:27:26 +02:00
|
|
|
return result;
|
2020-01-12 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
auto MassManager::renameMass(int hangar, const std::string& new_name) -> bool {
|
|
|
|
if(hangar < 0 || hangar >= 32) {
|
|
|
|
_lastError = "Hangar number out of range.";
|
2020-06-13 22:28:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(new_name.length() > 32) {
|
2020-09-30 16:27:26 +02:00
|
|
|
_lastError = "The new name is longer than 32 characters.";
|
2020-06-13 22:28:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
if(!_hangars[hangar].rename(new_name)) {
|
|
|
|
_lastError = _hangars[hangar].lastError();
|
2020-06-13 22:28:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-01-12 14:54:23 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
auto MassManager::stagedMasses() -> std::map<std::string, std::string> const& {
|
|
|
|
return _stagedMasses;
|
2020-01-12 14:54:23 +01:00
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
auto MassManager::stagedMassName(int index) -> std::string {
|
|
|
|
int i = 0;
|
|
|
|
for(const auto& mass_info : _stagedMasses) {
|
|
|
|
if(i != index) {
|
|
|
|
++i;
|
|
|
|
continue;
|
2020-01-16 00:35:23 +01:00
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
return mass_info.second;
|
|
|
|
}
|
2020-01-16 00:35:23 +01:00
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
return std::string{""};
|
2020-01-16 00:35:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto MassManager::updateStagedMass(const std::string& filename) -> int {
|
|
|
|
std::string file = Utility::Directory::join(_stagingAreaDirectory, filename);
|
|
|
|
|
|
|
|
if(!Utility::Directory::exists(file)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
auto name = Mass::getNameFromFile(file);
|
2020-01-16 00:35:23 +01:00
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
if(name == "") {
|
2020-01-16 00:35:23 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-09-30 16:27:26 +02:00
|
|
|
_stagedMasses[filename] = name;
|
2020-01-16 00:35:23 +01:00
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
for(const auto& mass: _stagedMasses) {
|
|
|
|
if(mass.first != filename) {
|
|
|
|
++index;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto MassManager::removeStagedMass(const std::string& filename) -> int {
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
for(auto it = _stagedMasses.begin(); it != _stagedMasses.end(); ++it, ++index) {
|
|
|
|
if(it->first == filename) {
|
|
|
|
_stagedMasses.erase(it);
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-01-16 01:21:47 +01:00
|
|
|
void MassManager::deleteStagedMass(int index) {
|
|
|
|
int i = 0;
|
|
|
|
for(auto it = _stagedMasses.begin(); it != _stagedMasses.end(); ++it, ++i) {
|
|
|
|
if(i == index) {
|
|
|
|
Utility::Directory::rm(Utility::Directory::join(_stagingAreaDirectory, it->first));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|