Skip to content

Commit

Permalink
Merge pull request dolphin-emu#8672 from JosJuice/wii-setting-backup
Browse files Browse the repository at this point in the history
Back up Wii setting.txt and SYSCONF while emulating
  • Loading branch information
leoetlino authored Mar 16, 2020
2 parents d408538 + 1b84406 commit 0461170
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 14 deletions.
7 changes: 7 additions & 0 deletions Source/Core/Common/StringUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,13 @@ bool SplitPath(std::string_view full_path, std::string* path, std::string* filen
return true;
}

std::string PathToFileName(std::string_view path)
{
std::string file_name, extension;
SplitPath(path, nullptr, &file_name, &extension);
return file_name + extension;
}

void BuildCompleteFilename(std::string& complete_filename, std::string_view path,
std::string_view filename)
{
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/Common/StringUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ std::string JoinStrings(const std::vector<std::string>& strings, const std::stri
bool SplitPath(std::string_view full_path, std::string* path, std::string* filename,
std::string* extension);

std::string PathToFileName(std::string_view path);

void BuildCompleteFilename(std::string& complete_filename, std::string_view path,
std::string_view filename);

Expand Down
5 changes: 5 additions & 0 deletions Source/Core/Core/BootManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "Core/Movie.h"
#include "Core/NetPlayProto.h"
#include "Core/PowerPC/PowerPC.h"
#include "Core/WiiRoot.h"

#include "DiscIO/Enums.h"

Expand Down Expand Up @@ -439,7 +440,10 @@ bool BootCore(std::unique_ptr<BootParameters> boot, const WindowSystemInfo& wsi)

// Ensure any new settings are written to the SYSCONF
if (StartUp.bWii)
{
Core::BackupWiiSettings();
ConfigLoaders::SaveToSYSCONF(Config::LayerType::Meta);
}

const bool load_ipl = !StartUp.bWii && !StartUp.bHLE_BS2 &&
std::holds_alternative<BootParameters::Disc>(boot->parameters);
Expand Down Expand Up @@ -487,6 +491,7 @@ static void RestoreSYSCONF()

void RestoreConfig()
{
Core::RestoreWiiSettings(Core::RestoreReason::EmulationEnd);
RestoreSYSCONF();
Config::ClearCurrentRunLayer();
Config::RemoveLayer(Config::LayerType::Movie);
Expand Down
57 changes: 57 additions & 0 deletions Source/Core/Core/WiiRoot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "Common/FileUtil.h"
#include "Common/Logging/Log.h"
#include "Common/NandPaths.h"
#include "Common/StringUtil.h"
#include "Core/CommonTitles.h"
#include "Core/ConfigManager.h"
#include "Core/HW/WiiSave.h"
#include "Core/IOS/ES/ES.h"
Expand All @@ -32,6 +34,38 @@ namespace FS = IOS::HLE::FS;

static std::string s_temp_wii_root;

static bool CopyBackupFile(const std::string& path_from, const std::string& path_to)
{
if (!File::Exists(path_from))
return false;

return File::Copy(path_from, path_to);
}

static void DeleteBackupFile(const std::string& file_name)
{
File::Delete(File::GetUserPath(D_BACKUP_IDX) + DIR_SEP + file_name);
}

static void BackupFile(const std::string& path_in_nand)
{
const std::string file_name = PathToFileName(path_in_nand);
const std::string original_path = File::GetUserPath(D_WIIROOT_IDX) + DIR_SEP + path_in_nand;
const std::string backup_path = File::GetUserPath(D_BACKUP_IDX) + DIR_SEP + file_name;

CopyBackupFile(original_path, backup_path);
}

static void RestoreFile(const std::string& path_in_nand)
{
const std::string file_name = PathToFileName(path_in_nand);
const std::string original_path = File::GetUserPath(D_WIIROOT_IDX) + DIR_SEP + path_in_nand;
const std::string backup_path = File::GetUserPath(D_BACKUP_IDX) + DIR_SEP + file_name;

if (CopyBackupFile(backup_path, original_path))
DeleteBackupFile(file_name);
}

static void CopySave(FS::FileSystem* source, FS::FileSystem* dest, const u64 title_id)
{
dest->CreateFullPath(IOS::PID_KERNEL, IOS::PID_KERNEL, Common::GetTitleDataPath(title_id) + '/',
Expand Down Expand Up @@ -173,6 +207,29 @@ void ShutdownWiiRoot()
}
}

void BackupWiiSettings()
{
// Back up files which Dolphin can modify at boot, so that we can preserve the original contents.
// For SYSCONF, the backup is only needed in case Dolphin crashes or otherwise exists unexpectedly
// during emulation, since the config system will restore the SYSCONF settings at emulation end.
// For setting.txt, there is no other code that restores the original values for us.

BackupFile(Common::GetTitleDataPath(Titles::SYSTEM_MENU) + "/" WII_SETTING);
BackupFile("/shared2/sys/SYSCONF");
}

void RestoreWiiSettings(RestoreReason reason)
{
RestoreFile(Common::GetTitleDataPath(Titles::SYSTEM_MENU) + "/" WII_SETTING);

// We must not restore the SYSCONF backup when ending emulation cleanly, since the user may have
// edited the SYSCONF file in the NAND using the emulated software (e.g. the Wii Menu settings).
if (reason == RestoreReason::CrashRecovery)
RestoreFile("/shared2/sys/SYSCONF");
else
DeleteBackupFile("SYSCONF");
}

/// Copy a directory from host_source_path (on the host FS) to nand_target_path on the NAND.
///
/// Both paths should not have trailing slashes. To specify the NAND root, use "".
Expand Down
9 changes: 9 additions & 0 deletions Source/Core/Core/WiiRoot.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@

namespace Core
{
enum class RestoreReason
{
EmulationEnd,
CrashRecovery,
};

void InitializeWiiRoot(bool use_temporary);
void ShutdownWiiRoot();

void BackupWiiSettings();
void RestoreWiiSettings(RestoreReason reason);

// Initialize or clean up the filesystem contents.
void InitializeWiiFileSystemContents();
void CleanUpWiiFileSystemContents();
Expand Down
17 changes: 7 additions & 10 deletions Source/Core/DolphinQt/Settings/InterfacePane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,9 @@ void InterfacePane::CreateUI()
// List avalable themes
auto theme_search_results =
Common::DoFileSearch({File::GetUserPath(D_THEMES_IDX), File::GetSysDirectory() + THEMES_DIR});
for (const std::string& filename : theme_search_results)
for (const std::string& path : theme_search_results)
{
std::string name, ext;
SplitPath(filename, nullptr, &name, &ext);
name += ext;
QString qt_name = QString::fromStdString(name);
const QString qt_name = QString::fromStdString(PathToFileName(path));
m_combobox_theme->addItem(qt_name);
}

Expand All @@ -137,12 +134,12 @@ void InterfacePane::CreateUI()

m_combobox_userstyle->addItem(tr("(None)"), QString{});

for (const std::string& filename : userstyle_search_results)
for (const std::string& path : userstyle_search_results)
{
std::string name, ext;
SplitPath(filename, nullptr, &name, &ext);
QString qt_name = QString::fromStdString(name);
m_combobox_userstyle->addItem(qt_name, QString::fromStdString(filename));
std::string name;
SplitPath(path, nullptr, &name, nullptr);
const QString qt_name = QString::fromStdString(name);
m_combobox_userstyle->addItem(qt_name, QString::fromStdString(path));
}

// Checkboxes
Expand Down
6 changes: 2 additions & 4 deletions Source/Core/UICommon/GameFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,9 @@ GameFile::GameFile() = default;

GameFile::GameFile(std::string path) : m_file_path(std::move(path))
{
{
std::string name, extension;
SplitPath(m_file_path, nullptr, &name, &extension);
m_file_name = name + extension;
m_file_name = PathToFileName(m_file_path);

{
std::unique_ptr<DiscIO::Volume> volume(DiscIO::CreateVolume(m_file_path));
if (volume != nullptr)
{
Expand Down
3 changes: 3 additions & 0 deletions Source/Core/UICommon/UICommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "Core/HW/Wiimote.h"
#include "Core/IOS/IOS.h"
#include "Core/IOS/STM/STM.h"
#include "Core/WiiRoot.h"

#include "InputCommon/GCAdapter.h"

Expand Down Expand Up @@ -88,6 +89,8 @@ static void InitCustomPaths()

void Init()
{
Core::RestoreWiiSettings(Core::RestoreReason::CrashRecovery);

Config::Init();
Config::AddConfigChangedCallback(InitCustomPaths);
Config::AddLayer(ConfigLoaders::GenerateBaseConfigLoader());
Expand Down

0 comments on commit 0461170

Please sign in to comment.