diff --git a/Profile/Profile.cpp b/Profile/Profile.cpp index c83189c..c410078 100644 --- a/Profile/Profile.cpp +++ b/Profile/Profile.cpp @@ -24,8 +24,12 @@ #include #include +#include #include +#include +#include + #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' }; @@ -138,3 +142,36 @@ auto Profile::getCredits() -> std::int32_t { 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; +} diff --git a/Profile/Profile.h b/Profile/Profile.h index 3497907..0387fcf 100644 --- a/Profile/Profile.h +++ b/Profile/Profile.h @@ -32,6 +32,8 @@ class Profile { auto credits() const -> std::int32_t; auto getCredits() -> std::int32_t; + auto backup(const std::string& filename) -> bool; + private: std::string _profileDirectory; std::string _filename;