From 332be6c771f6afc9580f9ed7a30fff909e3a93b3 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Mon, 1 Apr 2024 20:57:04 +1000 Subject: [PATCH] SettingsInterface: Add Error to Save() --- common/MemorySettingsInterface.cpp | 10 ++++++---- common/MemorySettingsInterface.h | 4 ++-- common/SettingsInterface.h | 7 +++++-- pcsx2/INISettingsInterface.cpp | 16 ++++++++++++---- pcsx2/INISettingsInterface.h | 4 ++-- pcsx2/LayeredSettingsInterface.cpp | 5 +++-- pcsx2/LayeredSettingsInterface.h | 6 ++++-- 7 files changed, 34 insertions(+), 18 deletions(-) diff --git a/common/MemorySettingsInterface.cpp b/common/MemorySettingsInterface.cpp index 36f00ad356..4ab6bc0d0f 100644 --- a/common/MemorySettingsInterface.cpp +++ b/common/MemorySettingsInterface.cpp @@ -1,15 +1,17 @@ -// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team +// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team // SPDX-License-Identifier: LGPL-3.0+ -#include "common/MemorySettingsInterface.h" -#include "common/StringUtil.h" +#include "MemorySettingsInterface.h" +#include "Error.h" +#include "StringUtil.h" MemorySettingsInterface::MemorySettingsInterface() = default; MemorySettingsInterface::~MemorySettingsInterface() = default; -bool MemorySettingsInterface::Save() +bool MemorySettingsInterface::Save(Error* error) { + Error::SetStringView(error, "Memory settings cannot be saved."); return false; } diff --git a/common/MemorySettingsInterface.h b/common/MemorySettingsInterface.h index f8cba7222a..6c926aaa4e 100644 --- a/common/MemorySettingsInterface.h +++ b/common/MemorySettingsInterface.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team +// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team // SPDX-License-Identifier: LGPL-3.0+ #pragma once @@ -12,7 +12,7 @@ public: MemorySettingsInterface(); ~MemorySettingsInterface(); - bool Save() override; + bool Save(Error* error = nullptr) override; void Clear() override; diff --git a/common/SettingsInterface.h b/common/SettingsInterface.h index 1544326279..460b254c2b 100644 --- a/common/SettingsInterface.h +++ b/common/SettingsInterface.h @@ -1,19 +1,22 @@ -// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team +// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team // SPDX-License-Identifier: LGPL-3.0+ #pragma once #include "Pcsx2Defs.h" + #include #include #include +class Error; + class SettingsInterface { public: virtual ~SettingsInterface() = default; - virtual bool Save() = 0; + virtual bool Save(Error* error = nullptr) = 0; virtual void Clear() = 0; virtual bool GetIntValue(const char* section, const char* key, int* value) const = 0; diff --git a/pcsx2/INISettingsInterface.cpp b/pcsx2/INISettingsInterface.cpp index 17e95cc4fd..341e79f06b 100644 --- a/pcsx2/INISettingsInterface.cpp +++ b/pcsx2/INISettingsInterface.cpp @@ -1,10 +1,13 @@ -// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team +// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team // SPDX-License-Identifier: LGPL-3.0+ #include "INISettingsInterface.h" + +#include "common/Error.h" #include "common/FileSystem.h" #include "common/Console.h" #include "common/StringUtil.h" + #include #include #include @@ -66,15 +69,18 @@ bool INISettingsInterface::Load() return (err == SI_OK); } -bool INISettingsInterface::Save() +bool INISettingsInterface::Save(Error* error) { if (m_filename.empty()) + { + Error::SetStringView(error, "Filename is not set."); return false; + } std::unique_lock lock(s_ini_load_save_mutex); std::string temp_filename(GetTemporaryFileName(m_filename)); SI_Error err = SI_FAIL; - std::FILE* fp = FileSystem::OpenCFile(temp_filename.c_str(), "wb"); + std::FILE* fp = FileSystem::OpenCFile(temp_filename.c_str(), "wb", error); if (fp) { err = m_ini.SaveFile(fp, false); @@ -82,10 +88,12 @@ bool INISettingsInterface::Save() if (err != SI_OK) { + Error::SetStringFmt(error, "INI SaveFile() failed: {}", static_cast(err)); + // remove temporary file FileSystem::DeleteFilePath(temp_filename.c_str()); } - else if (!FileSystem::RenamePath(temp_filename.c_str(), m_filename.c_str())) + else if (!FileSystem::RenamePath(temp_filename.c_str(), m_filename.c_str(), error)) { Console.Error("Failed to rename '%s' to '%s'", temp_filename.c_str(), m_filename.c_str()); FileSystem::DeleteFilePath(temp_filename.c_str()); diff --git a/pcsx2/INISettingsInterface.h b/pcsx2/INISettingsInterface.h index 82cc795d5a..0d5083738d 100644 --- a/pcsx2/INISettingsInterface.h +++ b/pcsx2/INISettingsInterface.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team +// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team // SPDX-License-Identifier: LGPL-3.0+ #pragma once @@ -20,7 +20,7 @@ public: bool IsDirty() const { return m_dirty; } bool Load(); - bool Save() override; + bool Save(Error* error = nullptr) override; void Clear() override; diff --git a/pcsx2/LayeredSettingsInterface.cpp b/pcsx2/LayeredSettingsInterface.cpp index 82b5b0b4d5..ce12962f6d 100644 --- a/pcsx2/LayeredSettingsInterface.cpp +++ b/pcsx2/LayeredSettingsInterface.cpp @@ -1,7 +1,8 @@ -// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team +// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team // SPDX-License-Identifier: LGPL-3.0+ #include "LayeredSettingsInterface.h" + #include "common/Assertions.h" #include @@ -10,7 +11,7 @@ LayeredSettingsInterface::LayeredSettingsInterface() = default; LayeredSettingsInterface::~LayeredSettingsInterface() = default; -bool LayeredSettingsInterface::Save() +bool LayeredSettingsInterface::Save(Error* error) { pxFailRel("Attempting to save layered settings interface"); return false; diff --git a/pcsx2/LayeredSettingsInterface.h b/pcsx2/LayeredSettingsInterface.h index 9d60f8a58b..e37903480f 100644 --- a/pcsx2/LayeredSettingsInterface.h +++ b/pcsx2/LayeredSettingsInterface.h @@ -1,8 +1,10 @@ -// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team +// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team // SPDX-License-Identifier: LGPL-3.0+ #pragma once + #include "common/SettingsInterface.h" + #include class LayeredSettingsInterface final : public SettingsInterface @@ -23,7 +25,7 @@ public: SettingsInterface* GetLayer(Layer layer) const { return m_layers[layer]; } void SetLayer(Layer layer, SettingsInterface* sif) { m_layers[layer] = sif; } - bool Save() override; + bool Save(Error* error = nullptr) override; void Clear() override;