Managers: simplify MassManager.

This commit is contained in:
Guillaume Jacquemin 2024-04-09 00:39:35 +02:00
parent 661f6acd12
commit e10e457ad8
Signed by: williamjcm
SSH key fingerprint: SHA256:AYLOg+iTV0ElElnlu4vqM4edFazVdRiuQB0Y5LoKc4A
3 changed files with 22 additions and 24 deletions

View file

@ -125,8 +125,7 @@ Application::initialiseManager() {
void
Application::initialiseMassManager() {
LOG_INFO("Initialising the M.A.S.S. manager.");
_massManager.emplace(conf().directories().gameSaves, _currentProfile->account(), _currentProfile->isDemo(),
conf().directories().staging);
_massManager.emplace(_currentProfile->account(), _currentProfile->isDemo());
}
void

View file

@ -19,6 +19,7 @@
#include <Corrade/Utility/Format.h>
#include <Corrade/Utility/Path.h>
#include "../Configuration/Configuration.h"
#include "../Logger/Logger.h"
#include "MassManager.h"
@ -27,13 +28,12 @@ using namespace Containers::Literals;
namespace mbst { namespace Managers {
MassManager::MassManager(Containers::StringView save_path, Containers::StringView account, bool demo,
Containers::StringView staging_dir):
_saveDirectory{save_path}, _account{account}, _demo{demo}, _stagingAreaDirectory{staging_dir}
MassManager::MassManager(Containers::StringView account, bool demo):
_account{account}, _demo{demo}
{
Containers::String mass_filename = "";
for(std::uint32_t i = 0; i < _hangars.size(); i++) {
mass_filename = Utility::Path::join(_saveDirectory,
mass_filename = Utility::Path::join(conf().directories().gameSaves,
Utility::format("{}Unit{:.2d}{}.sav", demo ? "Demo"_s : ""_s, i, _account));
new(&_hangars[i]) GameObjects::Mass{mass_filename};
}
@ -60,7 +60,7 @@ MassManager::refreshHangar(std::int32_t hangar) {
}
Containers::String mass_filename =
Utility::Path::join(_saveDirectory,
Utility::Path::join(conf().directories().gameSaves,
Utility::format("{}Unit{:.2d}{}.sav", _demo ? "Demo" : "", hangar, _account));
_hangars[hangar] = GameObjects::Mass{mass_filename};
}
@ -81,7 +81,7 @@ MassManager::importMass(Containers::StringView staged_fn, std::int32_t hangar) {
return false;
}
Containers::String source = Utility::Path::join(_stagingAreaDirectory, staged_fn);
Containers::String source = Utility::Path::join(conf().directories().staging, staged_fn);
Utility::Path::copy(source, source + ".tmp"_s);
{
@ -93,7 +93,7 @@ MassManager::importMass(Containers::StringView staged_fn, std::int32_t hangar) {
}
}
Containers::String dest = Utility::Path::join(_saveDirectory, _hangars[hangar].filename());
Containers::String dest = Utility::Path::join(conf().directories().gameSaves, _hangars[hangar].filename());
if(Utility::Path::exists(dest)) {
Utility::Path::remove(dest);
@ -122,8 +122,8 @@ MassManager::exportMass(std::int32_t hangar) {
return false;
}
Containers::String source = Utility::Path::join(_saveDirectory, _hangars[hangar].filename());
Containers::String dest = Utility::Path::join(_stagingAreaDirectory,
Containers::String source = Utility::Path::join(conf().directories().gameSaves, _hangars[hangar].filename());
Containers::String dest = Utility::Path::join(conf().directories().staging,
Utility::format("{}_{}.sav", _hangars[hangar].name(), _account));
if(!Utility::Path::copy(source, dest)) {
@ -149,8 +149,10 @@ MassManager::moveMass(std::int32_t source, std::int32_t destination) {
return false;
}
Containers::String source_file = Utility::Path::join(_saveDirectory, _hangars[source].filename());
Containers::String dest_file = Utility::Path::join(_saveDirectory, _hangars[destination].filename());
Containers::String source_file = Utility::Path::join(conf().directories().gameSaves,
_hangars[source].filename());
Containers::String dest_file = Utility::Path::join(conf().directories().gameSaves,
_hangars[destination].filename());
GameObjects::Mass::State dest_state = _hangars[destination].state();
switch(dest_state) {
@ -181,7 +183,7 @@ MassManager::deleteMass(std::int32_t hangar) {
return false;
}
if(!Utility::Path::remove(Utility::Path::join(_saveDirectory, _hangars[hangar].filename()))) {
if(!Utility::Path::remove(Utility::Path::join(conf().directories().gameSaves, _hangars[hangar].filename()))) {
_lastError = Utility::format("Deletion failed: {}", std::strerror(errno));
LOG_ERROR(_lastError);
return false;
@ -200,11 +202,11 @@ MassManager::refreshStagedMasses() {
_stagedMasses.clear();
using Utility::Path::ListFlag;
auto file_list = Utility::Path::list(_stagingAreaDirectory,
auto file_list = Utility::Path::list(conf().directories().staging,
ListFlag::SkipSpecial|ListFlag::SkipDirectories|ListFlag::SkipDotAndDotDot);
if(!file_list) {
LOG_ERROR_FORMAT("{} couldn't be opened.", _stagingAreaDirectory);
LOG_ERROR_FORMAT("{} couldn't be opened.", conf().directories().staging);
return;
}
@ -216,7 +218,7 @@ MassManager::refreshStagedMasses() {
LOG_INFO("Scanning for staged M.A.S.S.es...");
for(Containers::StringView file : list_view) {
auto name = GameObjects::Mass::getNameFromFile(Utility::Path::join(_stagingAreaDirectory, file));
auto name = GameObjects::Mass::getNameFromFile(Utility::Path::join(conf().directories().staging, file));
if(name) {
LOG_INFO_FORMAT("Found staged M.A.S.S.: {}", *name);
@ -232,11 +234,11 @@ void
MassManager::refreshStagedMass(Containers::StringView filename) {
LOG_INFO_FORMAT("Refreshing staged unit with filename {}.", filename);
bool file_exists = Utility::Path::exists(Utility::Path::join(_stagingAreaDirectory, filename));
bool file_exists = Utility::Path::exists(Utility::Path::join(conf().directories().staging, filename));
auto it = _stagedMasses.find(filename);
if(file_exists) {
auto name = GameObjects::Mass::getNameFromFile(Utility::Path::join(_stagingAreaDirectory, filename));
auto name = GameObjects::Mass::getNameFromFile(Utility::Path::join(conf().directories().staging, filename));
if(name) {
_stagedMasses[filename] = *name;
}
@ -257,7 +259,7 @@ MassManager::deleteStagedMass(Containers::StringView filename) {
return false;
}
if(!Utility::Path::remove(Utility::Path::join(_stagingAreaDirectory, filename))) {
if(!Utility::Path::remove(Utility::Path::join(conf().directories().staging, filename))) {
_lastError = filename + " couldn't be deleted: " + std::strerror(errno);
LOG_ERROR(_lastError);
return false;

View file

@ -30,7 +30,7 @@ namespace mbst { namespace Managers {
class MassManager {
public:
MassManager(Containers::StringView save_path, Containers::StringView account, bool demo, Containers::StringView staging_dir);
MassManager(Containers::StringView account, bool demo);
auto lastError() -> Containers::StringView;
@ -50,7 +50,6 @@ class MassManager {
bool deleteStagedMass(Containers::StringView filename);
private:
Containers::StringView _saveDirectory;
Containers::StringView _account;
bool _demo;
@ -58,8 +57,6 @@ class MassManager {
Containers::StaticArray<32, GameObjects::Mass> _hangars{NoInit};
Containers::StringView _stagingAreaDirectory;
std::map<Containers::String, Containers::String> _stagedMasses;
};