Profile: adapt to UESaveFile.

Also change ProfileManager to use growable arrays instead of vectors.
This commit is contained in:
Guillaume Jacquemin 2021-09-23 19:01:42 +02:00
parent 1caa472833
commit 50a7b1d7f0
5 changed files with 254 additions and 944 deletions

File diff suppressed because it is too large Load Diff

View File

@ -20,6 +20,8 @@
#include <Magnum/Magnum.h>
#include "../UESaveFile/UESaveFile.h"
using namespace Magnum;
enum class ProfileType : UnsignedByte {
@ -43,144 +45,92 @@ class Profile {
void refreshValues();
auto companyName() const -> std::string const&;
auto getCompanyName() -> std::string const&;
auto companyName() -> std::string const&;
auto renameCompany(const std::string& new_name) -> bool;
auto activeFrameSlot() const -> Int;
auto getActiveFrameSlot() -> Int;
auto activeFrameSlot() -> Int;
auto credits() const -> Int;
auto getCredits() -> Int;
auto credits() -> Int;
auto setCredits(Int credits) -> bool;
auto storyProgress() const -> Int;
auto getStoryProgress() -> Int;
auto storyProgress() -> Int;
auto setStoryProgress(Int progress) -> bool;
auto lastMissionId() const -> Int;
auto getLastMissionId() -> Int;
auto lastMissionId() -> Int;
auto verseSteel() const -> Int;
auto getVerseSteel() -> Int;
auto verseSteel() -> Int;
auto setVerseSteel(Int amount) -> bool;
auto undinium() const -> Int;
auto getUndinium() -> Int;
auto undinium() -> Int;
auto setUndinium(Int amount) -> bool;
auto necriumAlloy() const -> Int;
auto getNecriumAlloy() -> Int;
auto necriumAlloy() -> Int;
auto setNecriumAlloy(Int amount) -> bool;
auto lunarite() const -> Int;
auto getLunarite() -> Int;
auto lunarite() -> Int;
auto setLunarite(Int amount) -> bool;
auto asterite() const -> Int;
auto getAsterite() -> Int;
auto asterite() -> Int;
auto setAsterite(Int amount) -> bool;
auto ednil() const -> Int;
auto getEdnil() -> Int;
auto ednil() -> Int;
auto setEdnil(Int amount) -> bool;
auto nuflalt() const -> Int;
auto getNuflalt() -> Int;
auto nuflalt() -> Int;
auto setNuflalt(Int amount) -> bool;
auto aurelene() const -> Int;
auto getAurelene() -> Int;
auto aurelene() -> Int;
auto setAurelene(Int amount) -> bool;
auto soldus() const -> Int;
auto getSoldus() -> Int;
auto soldus() -> Int;
auto setSoldus(Int amount) -> bool;
auto synthesizedN() const -> Int;
auto getSynthesizedN() -> Int;
auto synthesizedN() -> Int;
auto setSynthesizedN(Int amount) -> bool;
auto alcarbonite() const -> Int;
auto getAlcarbonite() -> Int;
auto alcarbonite() -> Int;
auto setAlcarbonite(Int amount) -> bool;
auto keriphene() const -> Int;
auto getKeriphene() -> Int;
auto keriphene() -> Int;
auto setKeriphene(Int amount) -> bool;
auto nitinolCM() const -> Int;
auto getNitinolCM() -> Int;
auto nitinolCM() -> Int;
auto setNitinolCM(Int amount) -> bool;
auto quarkium() const -> Int;
auto getQuarkium() -> Int;
auto quarkium() -> Int;
auto setQuarkium(Int amount) -> bool;
auto alterene() const -> Int;
auto getAlterene() -> Int;
auto alterene() -> Int;
auto setAlterene(Int amount) -> bool;
auto mixedComposition() const -> Int;
auto getMixedComposition() -> Int;
auto mixedComposition() -> Int;
auto setMixedComposition(Int amount) -> bool;
auto voidResidue() const -> Int;
auto getVoidResidue() -> Int;
auto voidResidue() -> Int;
auto setVoidResidue(Int amount) -> bool;
auto muscularConstruction() const -> Int;
auto getMuscularConstruction() -> Int;
auto muscularConstruction() -> Int;
auto setMuscularConstruction(Int amount) -> bool;
auto mineralExoskeletology() const -> Int;
auto getMineralExoskeletology() -> Int;
auto mineralExoskeletology() -> Int;
auto setMineralExoskeletology(Int amount) -> bool;
auto carbonizedSkin() const -> Int;
auto getCarbonizedSkin() -> Int;
auto carbonizedSkin() -> Int;
auto setCarbonizedSkin(Int amount) -> bool;
private:
auto resource(const char* container, Int id) -> Int;
auto setResource(const char* container, Int id, Int amount) -> bool;
std::string _profileDirectory;
std::string _filename;
ProfileType _type;
UESaveFile _profile;
std::string _steamId;
bool _valid = false;
std::string _lastError;
std::string _companyName;
Int _activeFrameSlot = 0;
Int _credits;
Int _storyProgress;
Int _lastMissionId;
Int _verseSteel;
Int _undinium;
Int _necriumAlloy;
Int _lunarite;
Int _asterite;
Int _ednil;
Int _nuflalt;
Int _aurelene;
Int _soldus;
Int _synthesizedN;
Int _alcarbonite;
Int _keriphene;
Int _nitinolCM;
Int _quarkium;
Int _alterene;
Int _mixedComposition;
Int _voidResidue;
Int _muscularConstruction;
Int _mineralExoskeletology;
Int _carbonizedSkin;
};

View File

@ -31,8 +31,6 @@
#include "ProfileManager.h"
using namespace Corrade;
ProfileManager::ProfileManager(const std::string& save_dir, const std::string& backup_dir):
_saveDirectory{save_dir},
_backupsDirectory{backup_dir}
@ -48,12 +46,12 @@ auto ProfileManager::lastError() -> std::string const& {
return _lastError;
}
auto ProfileManager::profiles() -> std::vector<Profile> const& {
auto ProfileManager::profiles() -> Containers::ArrayView<Profile> {
return _profiles;
}
auto ProfileManager::refreshProfiles() -> bool {
_profiles.clear();
_profiles = Containers::Array<Profile>{};
using Utility::Directory::Flag;
std::vector<std::string> files = Utility::Directory::list(_saveDirectory, Flag::SkipSpecial|Flag::SkipDirectories|Flag::SkipDotAndDotDot);
@ -74,7 +72,7 @@ auto ProfileManager::refreshProfiles() -> bool {
continue;
}
_profiles.push_back(std::move(profile));
arrayAppend(_profiles, std::move(profile));
}
if(_profiles.empty()) {
@ -86,14 +84,14 @@ auto ProfileManager::refreshProfiles() -> bool {
}
auto ProfileManager::getProfile(std::size_t index) -> Profile* {
return &(_profiles.at(index));
return index <= _profiles.size() ? &(_profiles[index]) : nullptr;
}
auto ProfileManager::deleteProfile(std::size_t index, bool delete_builds) -> bool {
if(!Utility::Directory::rm(Utility::Directory::join(_saveDirectory, _profiles.at(index).filename()))) {
if(!Utility::Directory::rm(Utility::Directory::join(_saveDirectory, _profiles[index].filename()))) {
_lastError = Utility::formatString("Couldn't delete {} (filename: {}).",
_profiles.at(index).companyName(),
_profiles.at(index).filename());
_profiles[index].companyName(),
_profiles[index].filename());
refreshProfiles();
return false;
}
@ -101,13 +99,18 @@ auto ProfileManager::deleteProfile(std::size_t index, bool delete_builds) -> boo
if(delete_builds) {
for(UnsignedByte 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());
_profiles[index].type() == ProfileType::Demo ? "Demo": "",
i, _profiles[index].steamId());
Utility::Directory::rm(Utility::Directory::join(_saveDirectory, filename));
}
}
_profiles.erase(_profiles.cbegin() + index);
std::string file = _profiles[index].filename();
auto it = std::remove_if(_profiles.begin(), _profiles.end(), [&file](Profile& profile){return profile.filename() == file;});
if(it != _profiles.end()) {
arrayRemoveSuffix(_profiles, 1);
}
return true;
}
@ -117,7 +120,7 @@ auto ProfileManager::backupProfile(std::size_t index, bool backup_builds) -> boo
std::tm* time = std::localtime(&timestamp);
std::string filename = Utility::formatString("{}_{}{:.2d}{:.2d}_{:.2d}{:.2d}{:.2d}.mbprofbackup",
Utility::String::replaceAll(_profiles.at(index).companyName(), " ", "_"),
Utility::String::replaceAll(_profiles[index].companyName(), " ", "_"),
time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
time->tm_hour, time->tm_min, time->tm_sec);
@ -130,21 +133,21 @@ auto ProfileManager::backupProfile(std::size_t index, bool backup_builds) -> boo
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);
zip_source_t* profile_source = zip_source_file(zip, Utility::Directory::toNativeSeparators(Utility::Directory::join(_saveDirectory, _profiles[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) {
if(zip_file_add(zip, _profiles[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",
std::string comment = Utility::String::join({_profiles[index].companyName(),
_profiles[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)
@ -154,8 +157,8 @@ auto ProfileManager::backupProfile(std::size_t index, bool backup_builds) -> boo
if(backup_builds) {
for(UnsignedByte 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());
_profiles[index].type() == ProfileType::Demo ? "Demo": "",
i, _profiles[index].steamId());
if(!Utility::Directory::exists(Utility::Directory::join(_saveDirectory, build_filename))) {
continue;
@ -184,12 +187,12 @@ auto ProfileManager::backupProfile(std::size_t index, bool backup_builds) -> boo
return true;
}
auto ProfileManager::backups() -> std::vector<Backup> const& {
auto ProfileManager::backups() -> Containers::ArrayView<Backup> {
return _backups;
}
void ProfileManager::refreshBackups() {
_backups.clear();
_backups = Containers::Array<Backup>{};
using Utility::Directory::Flag;
std::vector<std::string> files = Utility::Directory::list(_backupsDirectory, Flag::SkipSpecial|Flag::SkipDirectories|Flag::SkipDotAndDotDot);
@ -255,29 +258,34 @@ void ProfileManager::refreshBackups() {
continue;
}
backup.includedFiles.reserve(num_entries);
arrayReserve(backup.includedFiles, num_entries);
for(Long i = 0; i < num_entries; i++) {
backup.includedFiles.emplace_back(zip_get_name(zip, i, ZIP_FL_UNCHANGED));
arrayAppend(backup.includedFiles, InPlaceInit, zip_get_name(zip, i, ZIP_FL_UNCHANGED));
}
_backups.push_back(std::move(backup));
arrayAppend(_backups, 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;
if(!Utility::Directory::rm(Utility::Directory::join(_backupsDirectory, _backups[index].filename))) {
_lastError = "Couldn't delete " + _backups[index].filename;
return false;
}
_backups.erase(_backups.begin() + index);
std::string file = _backups[index].filename;
auto it = std::remove_if(_backups.begin(), _backups.end(), [&file](Backup& backup){return backup.filename == file;});
if(it != _backups.end()) {
arrayRemoveSuffix(_backups, 1);
}
return true;
}
auto ProfileManager::restoreBackup(std::size_t index) -> bool {
const Backup& backup = _backups.at(index);
const Backup& backup = _backups[index];
static const char* error_format = "Extraction of file {} failed: {}";

View File

@ -17,10 +17,13 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <string>
#include <vector>
#include <Corrade/Containers/Array.h>
#include "../Profile/Profile.h"
using namespace Corrade;
struct Backup {
std::string filename;
std::string company;
@ -33,7 +36,7 @@ struct Backup {
int minute;
int second;
} timestamp;
std::vector<std::string> includedFiles;
Containers::Array<std::string> includedFiles;
};
class ProfileManager {
@ -43,14 +46,14 @@ class ProfileManager {
auto ready() const -> bool;
auto lastError() -> std::string const&;
auto profiles() -> std::vector<Profile> const&;
auto profiles() -> Containers::ArrayView<Profile>;
auto refreshProfiles() -> bool;
auto getProfile(std::size_t index) -> Profile*;
auto deleteProfile(std::size_t index, bool delete_builds) -> bool;
auto backupProfile(std::size_t index, bool backup_builds) -> bool;
auto backups() -> std::vector<Backup> const&;
auto backups() -> Containers::ArrayView<Backup>;
void refreshBackups();
auto deleteBackup(std::size_t index) -> bool;
@ -63,6 +66,6 @@ class ProfileManager {
const std::string& _saveDirectory;
const std::string& _backupsDirectory;
std::vector<Profile> _profiles;
std::vector<Backup> _backups;
Containers::Array<Profile> _profiles;
Containers::Array<Backup> _backups;
};

View File

@ -81,7 +81,7 @@ void SaveTool::drawProfileManager() {
ImGui::TableSetColumnIndex(0);
ImGui::PushID(i);
if(ImGui::Selectable(_profileManager->profiles().at(i).companyName().c_str(), false,
if(ImGui::Selectable(_profileManager->profiles()[i].companyName().c_str(), false,
ImGuiSelectableFlags_SpanAllColumns|ImGuiSelectableFlags_AllowItemOverlap))
{
_currentProfile = _profileManager->getProfile(i);
@ -90,7 +90,7 @@ void SaveTool::drawProfileManager() {
}
ImGui::TableSetColumnIndex(1);
ImGui::TextUnformatted(_profileManager->profiles().at(i).type() == ProfileType::Demo ? "Demo" : "Full");
ImGui::TextUnformatted(_profileManager->profiles()[i].type() == ProfileType::Demo ? "Demo" : "Full");
ImGui::TableSetColumnIndex(2);
if(ImGui::SmallButton(ICON_FA_FILE_ARCHIVE)) {
@ -132,13 +132,13 @@ auto SaveTool::drawBackupListPopup() -> ImGuiID {
{
ImGui::PushTextWrapPos(windowSize().x() * 0.40f);
ImGui::Text("Are you sure you want to restore the %s backup from %.4i-%.2i-%.2i %.2i:%.2i:%.2i ? Any existing data will be overwritten.",
_profileManager->backups().at(backup_index).company.c_str(),
_profileManager->backups().at(backup_index).timestamp.year,
_profileManager->backups().at(backup_index).timestamp.month,
_profileManager->backups().at(backup_index).timestamp.day,
_profileManager->backups().at(backup_index).timestamp.hour,
_profileManager->backups().at(backup_index).timestamp.minute,
_profileManager->backups().at(backup_index).timestamp.second);
_profileManager->backups()[backup_index].company.c_str(),
_profileManager->backups()[backup_index].timestamp.year,
_profileManager->backups()[backup_index].timestamp.month,
_profileManager->backups()[backup_index].timestamp.day,
_profileManager->backups()[backup_index].timestamp.hour,
_profileManager->backups()[backup_index].timestamp.minute,
_profileManager->backups()[backup_index].timestamp.second);
ImGui::PopTextWrapPos();
if(ImGui::BeginTable("##RestoreBackupLayout", 2)) {
@ -172,13 +172,13 @@ auto SaveTool::drawBackupListPopup() -> ImGuiID {
{
ImGui::PushTextWrapPos(windowSize().x() * 0.40f);
ImGui::Text("Are you sure you want to delete the %s backup from %.4i-%.2i-%.2i %.2i:%.2i:%.2i ? This operation is irreversible.",
_profileManager->backups().at(backup_index).company.c_str(),
_profileManager->backups().at(backup_index).timestamp.year,
_profileManager->backups().at(backup_index).timestamp.month,
_profileManager->backups().at(backup_index).timestamp.day,
_profileManager->backups().at(backup_index).timestamp.hour,
_profileManager->backups().at(backup_index).timestamp.minute,
_profileManager->backups().at(backup_index).timestamp.second);
_profileManager->backups()[backup_index].company.c_str(),
_profileManager->backups()[backup_index].timestamp.year,
_profileManager->backups()[backup_index].timestamp.month,
_profileManager->backups()[backup_index].timestamp.day,
_profileManager->backups()[backup_index].timestamp.hour,
_profileManager->backups()[backup_index].timestamp.minute,
_profileManager->backups()[backup_index].timestamp.second);
ImGui::PopTextWrapPos();
if(ImGui::BeginTable("##DeleteBackupLayout", 2)) {
@ -251,10 +251,10 @@ auto SaveTool::drawBackupListPopup() -> ImGuiID {
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::TextUnformatted(_profileManager->backups().at(i).company.c_str());
ImGui::TextUnformatted(_profileManager->backups()[i].company.c_str());
if(ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
for(const auto& file : _profileManager->backups().at(i).includedFiles) {
for(const auto& file : _profileManager->backups()[i].includedFiles) {
ImGui::TextUnformatted(file.c_str());
}
ImGui::EndTooltip();
@ -262,15 +262,15 @@ auto SaveTool::drawBackupListPopup() -> ImGuiID {
ImGui::TableSetColumnIndex(1);
ImGui::Text("%.4i-%.2i-%.2i %.2i:%.2i:%.2i",
_profileManager->backups().at(i).timestamp.year,
_profileManager->backups().at(i).timestamp.month,
_profileManager->backups().at(i).timestamp.day,
_profileManager->backups().at(i).timestamp.hour,
_profileManager->backups().at(i).timestamp.minute,
_profileManager->backups().at(i).timestamp.second);
_profileManager->backups()[i].timestamp.year,
_profileManager->backups()[i].timestamp.month,
_profileManager->backups()[i].timestamp.day,
_profileManager->backups()[i].timestamp.hour,
_profileManager->backups()[i].timestamp.minute,
_profileManager->backups()[i].timestamp.second);
ImGui::TableSetColumnIndex(2);
ImGui::TextUnformatted(_profileManager->backups().at(i).type == ProfileType::Demo ? "Demo" : "Full");
ImGui::TextUnformatted(_profileManager->backups()[i].type == ProfileType::Demo ? "Demo" : "Full");
ImGui::TableSetColumnIndex(3);
ImGui::PushID(i);
@ -363,8 +363,8 @@ auto SaveTool::drawDeleteProfilePopup(std::size_t profile_index) -> ImGuiID {
ImGui::PushTextWrapPos(windowSize().x() * 0.40f);
ImGui::Text("Are you sure you want to delete the %s %s profile ? This operation is irreversible.",
_profileManager->profiles().at(profile_index).companyName().c_str(),
_profileManager->profiles().at(profile_index).type() == ProfileType::Demo ? "demo" : "full game");
_profileManager->profiles()[profile_index].companyName().c_str(),
_profileManager->profiles()[profile_index].type() == ProfileType::Demo ? "demo" : "full game");
ImGui::PopTextWrapPos();
if(ImGui::BeginTable("##DeleteProfileLayout", 2)) {