Skip to content

Commit

Permalink
add setting for configurable number of backups + squash commits
Browse files Browse the repository at this point in the history
reuse

Cleanup

Additional folder check

Update translation files
  • Loading branch information
rainmakerv3 committed Dec 30, 2024
1 parent c55e65d commit 7d09362
Show file tree
Hide file tree
Showing 32 changed files with 294 additions and 76 deletions.
14 changes: 14 additions & 0 deletions src/common/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace Config {
static bool isNeo = false;
static bool isBackupSaveEnabled = false;
static int BackupFrequency = 10;
static int BackupNumber = 2;
static bool isFullscreen = false;
static bool playBGM = false;
static bool isTrophyPopupDisabled = false;
Expand Down Expand Up @@ -106,6 +107,10 @@ int getBackupFrequency() {
return BackupFrequency;
}

int getBackupNumber() {
return BackupNumber;
}

bool isFullscreenMode() {
return isFullscreen;
}
Expand Down Expand Up @@ -354,6 +359,10 @@ void setBackupFrequency(int frequency) {
BackupFrequency = frequency;
}

void setBackupNumber(int number) {
BackupNumber = number;
}

void setLogType(const std::string& type) {
logType = type;
}
Expand Down Expand Up @@ -585,6 +594,7 @@ void load(const std::filesystem::path& path) {
isNeo = toml::find_or<bool>(general, "isPS4Pro", false);
isBackupSaveEnabled = toml::find_or<bool>(general, "isBackupSaveEnabled", false);
BackupFrequency = toml::find_or<int>(general, "BackupFrequency", 10);
BackupNumber = toml::find_or<int>(general, "BackupNumber", 2);
isFullscreen = toml::find_or<bool>(general, "Fullscreen", false);
playBGM = toml::find_or<bool>(general, "playBGM", false);
isTrophyPopupDisabled = toml::find_or<bool>(general, "isTrophyPopupDisabled", false);
Expand Down Expand Up @@ -713,6 +723,7 @@ void save(const std::filesystem::path& path) {
data["General"]["isPS4Pro"] = isNeo;
data["General"]["isBackupSaveEnabled"] = isBackupSaveEnabled;
data["General"]["BackupFrequency"] = BackupFrequency;
data["General"]["BackupNumber"] = BackupNumber;
data["General"]["Fullscreen"] = isFullscreen;
data["General"]["isTrophyPopupDisabled"] = isTrophyPopupDisabled;
data["General"]["playBGM"] = playBGM;
Expand Down Expand Up @@ -851,6 +862,9 @@ void setDefaultValues() {
compatibilityData = false;
checkCompatibilityOnStartup = false;
audioBackend = "cubeb";
isBackupSaveEnabled = false;
BackupFrequency = 10;
BackupNumber = 2;
}

} // namespace Config
2 changes: 2 additions & 0 deletions src/common/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void saveMainWindow(const std::filesystem::path& path);
bool isNeoMode();
bool getBackupSaveEnabled();
int getBackupFrequency();
int getBackupNumber();
bool isFullscreenMode();
bool getPlayBGM();
int getBGMvolume();
Expand Down Expand Up @@ -74,6 +75,7 @@ void setLanguage(u32 language);
void setNeoMode(bool enable);
void setBackupSaveEnabled(bool enable);
void setBackupFrequency(int frequency);
void setBackupNumber(int number);
void setUserName(const std::string& type);
void setUpdateChannel(const std::string& type);
void setSeparateUpdateEnabled(bool use);
Expand Down
50 changes: 40 additions & 10 deletions src/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,24 +410,54 @@ void Emulator::UpdatePlayTime(const std::string& serial) {

void Emulator::StartAutosave(std::string game_serial) {
const int SaveInterval = Config::getBackupFrequency();
const auto backup_dir =
Common::FS::GetUserPath(Common::FS::PathType::SaveDataDir) / "1" / "BACKUPS";
const auto save_dir =
Common::FS::GetUserPath(Common::FS::PathType::SaveDataDir) / "1" / game_serial;

if (!std ::filesystem::exists(backup_dir)) {
std::filesystem::create_directory(backup_dir);
const int BackupNumber = Config::getBackupNumber();

for (int i = 1; i < BackupNumber + 1; i++) {
std::string backupstring = "BACKUP" + std::to_string(i);
auto backup_dircreate =
Common::FS::GetUserPath(Common::FS::PathType::SaveDataDir) / "1" / backupstring;
if (!std ::filesystem::exists(backup_dircreate)) {
std::filesystem::create_directory(backup_dircreate);
std::filesystem::create_directory(backup_dircreate / game_serial);
} else if (!std ::filesystem::exists(backup_dircreate / game_serial)) {
std::filesystem::create_directory(backup_dircreate / game_serial);
}
}

while (true) {
std::this_thread::sleep_for(std::chrono::minutes(SaveInterval));

if (BackupNumber > 1) {
const std::string lastDirstring = "BACKUP" + std::to_string(BackupNumber);
const auto lastdir = Common::FS::GetUserPath(Common::FS::PathType::SaveDataDir) / "1" /
lastDirstring / game_serial;
std::filesystem::remove_all(lastdir);
for (int i = BackupNumber - 1; i > 0; i--) {
std::string sourceString = "BACKUP" + std::to_string(i);
std::string destString = "BACKUP" + std::to_string(i + 1);
const auto sourceDir = Common::FS::GetUserPath(Common::FS::PathType::SaveDataDir) /
"1" / sourceString / game_serial;
const auto destDir = Common::FS::GetUserPath(Common::FS::PathType::SaveDataDir) /
"1" / destString / game_serial;
try {
std::filesystem::rename(sourceDir, destDir);
} catch (std::exception& ex) {
LOG_INFO(Frontend, "Error moving backup folders. Exception: {}\n", ex.what());
}
}
}

const auto save_dir =
Common::FS::GetUserPath(Common::FS::PathType::SaveDataDir) / "1" / game_serial;
const auto backup_dir = Common::FS::GetUserPath(Common::FS::PathType::SaveDataDir) / "1" /
"BACKUP1" / game_serial;
try {
std::filesystem::copy(save_dir, backup_dir / game_serial,
std::filesystem::copy(save_dir, backup_dir,
std::filesystem::copy_options::overwrite_existing |
std::filesystem::copy_options::recursive);
LOG_INFO(Frontend, "Backup saves created");
LOG_INFO(Frontend, "New backup saves created");
} catch (std::exception& ex) {
fmt::print("Error creating backup saves. Exception: {}\n", ex.what());
LOG_INFO(Frontend, "Error creating backup saves. Exception: {}\n", ex.what());
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/qt_gui/settings_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ SettingsDialog::SettingsDialog(std::span<const QString> physical_devices,
const QStringList BackupFreqList = {"5", "10", "15", "20", "25", "30"};
ui->BackupFreqComboBox->addItems(BackupFreqList);

const QStringList BackupNumList = {"1", "2", "3", "4", "5"};
ui->BackupNumComboBox->addItems(BackupNumList);

InitializeEmulatorLanguages();
LoadValuesFromConfig();

Expand Down Expand Up @@ -318,6 +321,8 @@ void SettingsDialog::LoadValuesFromConfig() {
toml::find_or<bool>(data, "General", "isBackupSaveEnabled", false));
ui->BackupFreqComboBox->setCurrentText(
QString::number(toml::find_or<int>(data, "General", "BackupFrequency", 10)));
ui->BackupNumComboBox->setCurrentText(
QString::number(toml::find_or<int>(data, "General", "BackupNumber", 2)));

#ifdef ENABLE_UPDATER
ui->updateCheckBox->setChecked(toml::find_or<bool>(data, "General", "autoUpdate", false));
Expand Down Expand Up @@ -401,9 +406,13 @@ void SettingsDialog::OnBackupStateChanged() {
if (ui->BackupCheckBox->isChecked()) {
ui->BackupFreqLabel->show();
ui->BackupFreqComboBox->show();
ui->BackupNumLabel->show();
ui->BackupNumComboBox->show();
} else {
ui->BackupFreqLabel->hide();
ui->BackupFreqComboBox->hide();
ui->BackupNumLabel->hide();
ui->BackupNumComboBox->hide();
}
}

Expand Down Expand Up @@ -569,6 +578,7 @@ void SettingsDialog::UpdateSettings() {
Config::setAudioBackend(ui->audioBackendComboBox->currentText().toStdString());
Config::setBackupSaveEnabled(ui->BackupCheckBox->isChecked());
Config::setBackupFrequency(ui->BackupFreqComboBox->currentText().toInt());
Config::setBackupNumber(ui->BackupNumComboBox->currentText().toInt());

#ifdef ENABLE_DISCORD_RPC
auto* rpc = Common::Singleton<DiscordRPCHandler::RPC>::Instance();
Expand Down
51 changes: 39 additions & 12 deletions src/qt_gui/settings_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -203,20 +203,47 @@
</widget>
</item>
<item>
<widget class="QLabel" name="BackupFreqLabel">
<property name="font">
<font>
<pointsize>11</pointsize>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>Backup Frequency (minutes)</string>
</property>
</widget>
<layout class="QHBoxLayout" name="BackupLabels">
<item>
<widget class="QLabel" name="BackupFreqLabel">
<property name="font">
<font>
<pointsize>8</pointsize>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>Interval (mins)</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="BackupNumLabel">
<property name="font">
<font>
<pointsize>8</pointsize>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>Number of backups</string>
</property>
<property name="alignment">
<set>Qt::AlignmentFlag::AlignLeading|Qt::AlignmentFlag::AlignLeft|Qt::AlignmentFlag::AlignRight|Qt::AlignmentFlag::AlignTrailing|Qt::AlignmentFlag::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QComboBox" name="BackupFreqComboBox"/>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QComboBox" name="BackupFreqComboBox"/>
</item>
<item>
<widget class="QComboBox" name="BackupNumComboBox"/>
</item>
</layout>
</item>
</layout>
</widget>
Expand Down
9 changes: 7 additions & 2 deletions src/qt_gui/translations/ar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,13 @@
</message>
<message>
<location filename="../settings_dialog.ui" line="214"/>
<source>Backup Frequency (minutes)</source>
<translation>Backup Frequency (minutes)</translation>
<source>Interval (mins)</source>
<translation>Interval (mins)</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="221"/>
<source>Number of backups</source>
<translation>Number of backups</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="114"/>
Expand Down
9 changes: 7 additions & 2 deletions src/qt_gui/translations/da_DK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,13 @@
</message>
<message>
<location filename="../settings_dialog.ui" line="214"/>
<source>Backup Frequency (minutes)</source>
<translation>Backup Frequency (minutes)</translation>
<source>Interval (mins)</source>
<translation>Interval (mins)</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="221"/>
<source>Number of backups</source>
<translation>Number of backups</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="114"/>
Expand Down
9 changes: 7 additions & 2 deletions src/qt_gui/translations/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,13 @@
</message>
<message>
<location filename="../settings_dialog.ui" line="214"/>
<source>Backup Frequency (minutes)</source>
<translation>Backup Frequency (minutes)</translation>
<source>Interval (mins)</source>
<translation>Interval (mins)</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="221"/>
<source>Number of backups</source>
<translation>Number of backups</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="114"/>
Expand Down
9 changes: 7 additions & 2 deletions src/qt_gui/translations/el.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,13 @@
</message>
<message>
<location filename="../settings_dialog.ui" line="214"/>
<source>Backup Frequency (minutes)</source>
<translation>Backup Frequency (minutes)</translation>
<source>Interval (mins)</source>
<translation>Interval (mins)</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="221"/>
<source>Number of backups</source>
<translation>Number of backups</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="114"/>
Expand Down
9 changes: 7 additions & 2 deletions src/qt_gui/translations/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,13 @@
</message>
<message>
<location filename="../settings_dialog.ui" line="214"/>
<source>Backup Frequency (minutes)</source>
<translation>Backup Frequency (minutes)</translation>
<source>Interval (mins)</source>
<translation>Interval (mins)</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="221"/>
<source>Number of backups</source>
<translation>Number of backups</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="114"/>
Expand Down
9 changes: 7 additions & 2 deletions src/qt_gui/translations/es_ES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,13 @@
</message>
<message>
<location filename="../settings_dialog.ui" line="214"/>
<source>Backup Frequency (minutes)</source>
<translation>Backup Frequency (minutes)</translation>
<source>Interval (mins)</source>
<translation>Interval (mins)</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="221"/>
<source>Number of backups</source>
<translation>Number of backups</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="114"/>
Expand Down
9 changes: 7 additions & 2 deletions src/qt_gui/translations/fa_IR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,13 @@
</message>
<message>
<location filename="../settings_dialog.ui" line="214"/>
<source>Backup Frequency (minutes)</source>
<translation>Backup Frequency (minutes)</translation>
<source>Interval (mins)</source>
<translation>Interval (mins)</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="221"/>
<source>Number of backups</source>
<translation>Number of backups</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="114"/>
Expand Down
9 changes: 7 additions & 2 deletions src/qt_gui/translations/fi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,13 @@
</message>
<message>
<location filename="../settings_dialog.ui" line="214"/>
<source>Backup Frequency (minutes)</source>
<translation>Backup Frequency (minutes)</translation>
<source>Interval (mins)</source>
<translation>Interval (mins)</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="221"/>
<source>Number of backups</source>
<translation>Number of backups</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="114"/>
Expand Down
9 changes: 7 additions & 2 deletions src/qt_gui/translations/fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,13 @@
</message>
<message>
<location filename="../settings_dialog.ui" line="214"/>
<source>Backup Frequency (minutes)</source>
<translation>Backup Frequency (minutes)</translation>
<source>Interval (mins)</source>
<translation>Interval (mins)</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="221"/>
<source>Number of backups</source>
<translation>Number of backups</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="114"/>
Expand Down
9 changes: 7 additions & 2 deletions src/qt_gui/translations/hu_HU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,13 @@
</message>
<message>
<location filename="../settings_dialog.ui" line="214"/>
<source>Backup Frequency (minutes)</source>
<translation>Backup Frequency (minutes)</translation>
<source>Interval (mins)</source>
<translation>Interval (mins)</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="221"/>
<source>Number of backups</source>
<translation>Number of backups</translation>
</message>
<message>
<location filename="../settings_dialog.ui" line="114"/>
Expand Down
Loading

0 comments on commit 7d09362

Please sign in to comment.