Profile: reimplement the backup feature.

This commit is contained in:
Guillaume Jacquemin 2020-09-30 16:26:34 +02:00
parent fdf72544fb
commit d7fa2abafb
2 changed files with 39 additions and 0 deletions

View file

@ -24,8 +24,12 @@
#include <Corrade/Containers/Array.h> #include <Corrade/Containers/Array.h>
#include <Corrade/Utility/Directory.h> #include <Corrade/Utility/Directory.h>
#include <Corrade/Utility/FormatStl.h>
#include <Corrade/Utility/String.h> #include <Corrade/Utility/String.h>
#include <wx/wfstream.h>
#include <wx/zipstrm.h>
#include "Profile.h" #include "Profile.h"
constexpr char company_name_locator[] = { 'C', 'o', 'm', 'p', 'a', 'n', 'y', 'N', 'a', 'm', 'e', '\0', 0x0C, '\0', '\0', '\0', 'S', 't', 'r', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'y', '\0' }; constexpr char company_name_locator[] = { 'C', 'o', 'm', 'p', 'a', 'n', 'y', 'N', 'a', 'm', 'e', '\0', 0x0C, '\0', '\0', '\0', 'S', 't', 'r', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'y', '\0' };
@ -138,3 +142,36 @@ auto Profile::getCredits() -> std::int32_t {
return _credits; return _credits;
} }
auto Profile::backup(const std::string& filename) -> bool {
if(filename.empty() || (filename.length() < 5 && !Utility::String::endsWith(filename, ".zip"))) {
_lastError = "Invalid filename " + filename + " in Profile::backup()";
return false;
}
if(Utility::Directory::exists(filename)) {
if(!Utility::Directory::rm(filename)) {
_lastError = "Couldn't overwrite " + filename + " in Profile::backup()";
}
}
wxFFileOutputStream out{filename};
wxZipOutputStream zip{out};
{
zip.PutNextEntry(_filename);
wxFFileInputStream profile_stream{Utility::Directory::toNativeSeparators(Utility::Directory::join(_profileDirectory, _filename)), "rb"};
zip.Write(profile_stream);
}
for(int i = 0; i < 32; ++i) {
std::string unit_file = Utility::Directory::join(_profileDirectory, Utility::formatString("{}Unit{:.2d}{}.sav", _type == ProfileType::Demo ? "Demo" : "", i, _steamId));
if(Utility::Directory::exists(unit_file)) {
zip.PutNextEntry(Utility::Directory::filename(unit_file));
wxFFileInputStream unit_stream{Utility::Directory::toNativeSeparators(unit_file)};
zip.Write(unit_stream);
}
}
return true;
}

View file

@ -32,6 +32,8 @@ class Profile {
auto credits() const -> std::int32_t; auto credits() const -> std::int32_t;
auto getCredits() -> std::int32_t; auto getCredits() -> std::int32_t;
auto backup(const std::string& filename) -> bool;
private: private:
std::string _profileDirectory; std::string _profileDirectory;
std::string _filename; std::string _filename;