style: adjust clang-format rules (#2186)

Co-authored-by: Vithorio Polten <reach@vithor.io>
This commit is contained in:
ReenigneArcher
2025-01-19 22:34:47 -05:00
committed by GitHub
parent f57aee9025
commit c2420427b1
158 changed files with 8754 additions and 9994 deletions

View File

@@ -2,8 +2,10 @@
* @file src/platform/windows/nvprefs/driver_settings.cpp
* @brief Definitions for nvidia driver settings.
*/
// local includes
// this include
#include "driver_settings.h"
// local includes
#include "nvprefs_common.h"
namespace {
@@ -11,15 +13,13 @@ namespace {
const auto sunshine_application_profile_name = L"SunshineStream";
const auto sunshine_application_path = L"sunshine.exe";
void
nvapi_error_message(NvAPI_Status status) {
void nvapi_error_message(NvAPI_Status status) {
NvAPI_ShortString message = {};
NvAPI_GetErrorMessage(status, message);
nvprefs::error_message(std::string("NvAPI error: ") + message);
}
void
fill_nvapi_string(NvAPI_UnicodeString &dest, const wchar_t *src) {
void fill_nvapi_string(NvAPI_UnicodeString &dest, const wchar_t *src) {
static_assert(sizeof(NvU16) == sizeof(wchar_t));
memcpy_s(dest, NVAPI_UNICODE_STRING_MAX * sizeof(NvU16), src, (wcslen(src) + 1) * sizeof(wchar_t));
}
@@ -34,9 +34,10 @@ namespace nvprefs {
}
}
bool
driver_settings_t::init() {
if (session_handle) return true;
bool driver_settings_t::init() {
if (session_handle) {
return true;
}
NvAPI_Status status;
@@ -56,8 +57,7 @@ namespace nvprefs {
return load_settings();
}
void
driver_settings_t::destroy() {
void driver_settings_t::destroy() {
if (session_handle) {
NvAPI_DRS_DestroySession(session_handle);
session_handle = 0;
@@ -65,9 +65,10 @@ namespace nvprefs {
NvAPI_Unload();
}
bool
driver_settings_t::load_settings() {
if (!session_handle) return false;
bool driver_settings_t::load_settings() {
if (!session_handle) {
return false;
}
NvAPI_Status status = NvAPI_DRS_LoadSettings(session_handle);
if (status != NVAPI_OK) {
@@ -80,9 +81,10 @@ namespace nvprefs {
return true;
}
bool
driver_settings_t::save_settings() {
if (!session_handle) return false;
bool driver_settings_t::save_settings() {
if (!session_handle) {
return false;
}
NvAPI_Status status = NvAPI_DRS_SaveSettings(session_handle);
if (status != NVAPI_OK) {
@@ -94,9 +96,10 @@ namespace nvprefs {
return true;
}
bool
driver_settings_t::restore_global_profile_to_undo(const undo_data_t &undo_data) {
if (!session_handle) return false;
bool driver_settings_t::restore_global_profile_to_undo(const undo_data_t &undo_data) {
if (!session_handle) {
return false;
}
const auto &swapchain_data = undo_data.get_opengl_swapchain();
if (swapchain_data) {
@@ -130,8 +133,7 @@ namespace nvprefs {
error_message("NvAPI_DRS_SetSetting() OGL_CPL_PREFER_DXPRESENT failed");
return false;
}
}
else {
} else {
status = NvAPI_DRS_DeleteProfileSetting(session_handle, profile_handle, OGL_CPL_PREFER_DXPRESENT_ID);
if (status != NVAPI_OK && status != NVAPI_SETTING_NOT_FOUND) {
@@ -142,11 +144,9 @@ namespace nvprefs {
}
info_message("Restored OGL_CPL_PREFER_DXPRESENT for base profile");
}
else if (status == NVAPI_OK || status == NVAPI_SETTING_NOT_FOUND) {
} else if (status == NVAPI_OK || status == NVAPI_SETTING_NOT_FOUND) {
info_message("OGL_CPL_PREFER_DXPRESENT has been changed from our value in base profile, not restoring");
}
else {
} else {
error_message("NvAPI_DRS_GetSetting() OGL_CPL_PREFER_DXPRESENT failed");
return false;
}
@@ -155,9 +155,10 @@ namespace nvprefs {
return true;
}
bool
driver_settings_t::check_and_modify_global_profile(std::optional<undo_data_t> &undo_data) {
if (!session_handle) return false;
bool driver_settings_t::check_and_modify_global_profile(std::optional<undo_data_t> &undo_data) {
if (!session_handle) {
return false;
}
undo_data.reset();
NvAPI_Status status;
@@ -184,8 +185,7 @@ namespace nvprefs {
undo_data = undo_data_t();
if (status == NVAPI_OK) {
undo_data->set_opengl_swapchain(OGL_CPL_PREFER_DXPRESENT_PREFER_ENABLED, setting.u32CurrentValue);
}
else {
} else {
undo_data->set_opengl_swapchain(OGL_CPL_PREFER_DXPRESENT_PREFER_ENABLED, std::nullopt);
}
@@ -204,8 +204,7 @@ namespace nvprefs {
}
info_message("Changed OGL_CPL_PREFER_DXPRESENT to OGL_CPL_PREFER_DXPRESENT_PREFER_ENABLED for base profile");
}
else if (status != NVAPI_OK) {
} else if (status != NVAPI_OK) {
nvapi_error_message(status);
error_message("NvAPI_DRS_GetSetting() OGL_CPL_PREFER_DXPRESENT failed");
return false;
@@ -214,9 +213,10 @@ namespace nvprefs {
return true;
}
bool
driver_settings_t::check_and_modify_application_profile(bool &modified) {
if (!session_handle) return false;
bool driver_settings_t::check_and_modify_application_profile(bool &modified) {
if (!session_handle) {
return false;
}
modified = false;
NvAPI_Status status;
@@ -285,10 +285,9 @@ namespace nvprefs {
info_message(std::wstring(L"Removed PREFERRED_PSTATE for ") + sunshine_application_path);
}
}
else if (status != NVAPI_OK ||
setting.settingLocation != NVDRS_CURRENT_PROFILE_LOCATION ||
setting.u32CurrentValue != PREFERRED_PSTATE_PREFER_MAX) {
} else if (status != NVAPI_OK ||
setting.settingLocation != NVDRS_CURRENT_PROFILE_LOCATION ||
setting.u32CurrentValue != PREFERRED_PSTATE_PREFER_MAX) {
// Set power setting if needed
setting = {};
setting.version = NVDRS_SETTING_VER1;

View File

@@ -21,26 +21,19 @@ namespace nvprefs {
public:
~driver_settings_t();
bool
init();
bool init();
void
destroy();
void destroy();
bool
load_settings();
bool load_settings();
bool
save_settings();
bool save_settings();
bool
restore_global_profile_to_undo(const undo_data_t &undo_data);
bool restore_global_profile_to_undo(const undo_data_t &undo_data);
bool
check_and_modify_global_profile(std::optional<undo_data_t> &undo_data);
bool check_and_modify_global_profile(std::optional<undo_data_t> &undo_data);
bool
check_and_modify_application_profile(bool &modified);
bool check_and_modify_application_profile(bool &modified);
private:
NvDRSSessionHandle session_handle = 0;

View File

@@ -2,7 +2,7 @@
* @file src/platform/windows/nvprefs/nvapi_opensource_wrapper.cpp
* @brief Definitions for the NVAPI wrapper.
*/
// standard library headers
// standard includes
#include <map>
// local includes
@@ -17,9 +17,8 @@ namespace {
std::map<const char *, void *> interfaces;
HMODULE dll = NULL;
template <typename Func, typename... Args>
NvAPI_Status
call_interface(const char *name, Args... args) {
template<typename Func, typename... Args>
NvAPI_Status call_interface(const char *name, Args... args) {
auto func = (Func *) interfaces[name];
if (!func) {
@@ -38,7 +37,9 @@ extern void *__cdecl nvapi_QueryInterface(NvU32 id);
NVAPI_INTERFACE
NvAPI_Initialize() {
if (dll) return NVAPI_OK;
if (dll) {
return NVAPI_OK;
}
#ifdef _WIN64
auto dll_name = "nvapi64.dll";
@@ -59,8 +60,7 @@ NvAPI_Initialize() {
return NVAPI_LIBRARY_NOT_FOUND;
}
NVAPI_INTERFACE
NvAPI_Unload() {
NVAPI_INTERFACE NvAPI_Unload() {
if (dll) {
interfaces.clear();
FreeLibrary(dll);
@@ -69,69 +69,56 @@ NvAPI_Unload() {
return NVAPI_OK;
}
NVAPI_INTERFACE
NvAPI_GetErrorMessage(NvAPI_Status nr, NvAPI_ShortString szDesc) {
NVAPI_INTERFACE NvAPI_GetErrorMessage(NvAPI_Status nr, NvAPI_ShortString szDesc) {
return call_interface<decltype(NvAPI_GetErrorMessage)>("NvAPI_GetErrorMessage", nr, szDesc);
}
// This is only a subset of NvAPI_DRS_* functions, more can be added if needed
NVAPI_INTERFACE
NvAPI_DRS_CreateSession(NvDRSSessionHandle *phSession) {
NVAPI_INTERFACE NvAPI_DRS_CreateSession(NvDRSSessionHandle *phSession) {
return call_interface<decltype(NvAPI_DRS_CreateSession)>("NvAPI_DRS_CreateSession", phSession);
}
NVAPI_INTERFACE
NvAPI_DRS_DestroySession(NvDRSSessionHandle hSession) {
NVAPI_INTERFACE NvAPI_DRS_DestroySession(NvDRSSessionHandle hSession) {
return call_interface<decltype(NvAPI_DRS_DestroySession)>("NvAPI_DRS_DestroySession", hSession);
}
NVAPI_INTERFACE
NvAPI_DRS_LoadSettings(NvDRSSessionHandle hSession) {
NVAPI_INTERFACE NvAPI_DRS_LoadSettings(NvDRSSessionHandle hSession) {
return call_interface<decltype(NvAPI_DRS_LoadSettings)>("NvAPI_DRS_LoadSettings", hSession);
}
NVAPI_INTERFACE
NvAPI_DRS_SaveSettings(NvDRSSessionHandle hSession) {
NVAPI_INTERFACE NvAPI_DRS_SaveSettings(NvDRSSessionHandle hSession) {
return call_interface<decltype(NvAPI_DRS_SaveSettings)>("NvAPI_DRS_SaveSettings", hSession);
}
NVAPI_INTERFACE
NvAPI_DRS_CreateProfile(NvDRSSessionHandle hSession, NVDRS_PROFILE *pProfileInfo, NvDRSProfileHandle *phProfile) {
NVAPI_INTERFACE NvAPI_DRS_CreateProfile(NvDRSSessionHandle hSession, NVDRS_PROFILE *pProfileInfo, NvDRSProfileHandle *phProfile) {
return call_interface<decltype(NvAPI_DRS_CreateProfile)>("NvAPI_DRS_CreateProfile", hSession, pProfileInfo, phProfile);
}
NVAPI_INTERFACE
NvAPI_DRS_FindProfileByName(NvDRSSessionHandle hSession, NvAPI_UnicodeString profileName, NvDRSProfileHandle *phProfile) {
NVAPI_INTERFACE NvAPI_DRS_FindProfileByName(NvDRSSessionHandle hSession, NvAPI_UnicodeString profileName, NvDRSProfileHandle *phProfile) {
return call_interface<decltype(NvAPI_DRS_FindProfileByName)>("NvAPI_DRS_FindProfileByName", hSession, profileName, phProfile);
}
NVAPI_INTERFACE
NvAPI_DRS_CreateApplication(NvDRSSessionHandle hSession, NvDRSProfileHandle hProfile, NVDRS_APPLICATION *pApplication) {
NVAPI_INTERFACE NvAPI_DRS_CreateApplication(NvDRSSessionHandle hSession, NvDRSProfileHandle hProfile, NVDRS_APPLICATION *pApplication) {
return call_interface<decltype(NvAPI_DRS_CreateApplication)>("NvAPI_DRS_CreateApplication", hSession, hProfile, pApplication);
}
NVAPI_INTERFACE
NvAPI_DRS_GetApplicationInfo(NvDRSSessionHandle hSession, NvDRSProfileHandle hProfile, NvAPI_UnicodeString appName, NVDRS_APPLICATION *pApplication) {
NVAPI_INTERFACE NvAPI_DRS_GetApplicationInfo(NvDRSSessionHandle hSession, NvDRSProfileHandle hProfile, NvAPI_UnicodeString appName, NVDRS_APPLICATION *pApplication) {
return call_interface<decltype(NvAPI_DRS_GetApplicationInfo)>("NvAPI_DRS_GetApplicationInfo", hSession, hProfile, appName, pApplication);
}
NVAPI_INTERFACE
NvAPI_DRS_SetSetting(NvDRSSessionHandle hSession, NvDRSProfileHandle hProfile, NVDRS_SETTING *pSetting) {
NVAPI_INTERFACE NvAPI_DRS_SetSetting(NvDRSSessionHandle hSession, NvDRSProfileHandle hProfile, NVDRS_SETTING *pSetting) {
return call_interface<decltype(NvAPI_DRS_SetSetting)>("NvAPI_DRS_SetSetting", hSession, hProfile, pSetting);
}
NVAPI_INTERFACE
NvAPI_DRS_GetSetting(NvDRSSessionHandle hSession, NvDRSProfileHandle hProfile, NvU32 settingId, NVDRS_SETTING *pSetting) {
NVAPI_INTERFACE NvAPI_DRS_GetSetting(NvDRSSessionHandle hSession, NvDRSProfileHandle hProfile, NvU32 settingId, NVDRS_SETTING *pSetting) {
return call_interface<decltype(NvAPI_DRS_GetSetting)>("NvAPI_DRS_GetSetting", hSession, hProfile, settingId, pSetting);
}
NVAPI_INTERFACE
NvAPI_DRS_DeleteProfileSetting(NvDRSSessionHandle hSession, NvDRSProfileHandle hProfile, NvU32 settingId) {
NVAPI_INTERFACE NvAPI_DRS_DeleteProfileSetting(NvDRSSessionHandle hSession, NvDRSProfileHandle hProfile, NvU32 settingId) {
return call_interface<decltype(NvAPI_DRS_DeleteProfileSetting)>("NvAPI_DRS_DeleteProfileSetting", hSession, hProfile, settingId);
}
NVAPI_INTERFACE
NvAPI_DRS_GetBaseProfile(NvDRSSessionHandle hSession, NvDRSProfileHandle *phProfile) {
NVAPI_INTERFACE NvAPI_DRS_GetBaseProfile(NvDRSSessionHandle hSession, NvDRSProfileHandle *phProfile) {
return call_interface<decltype(NvAPI_DRS_GetBaseProfile)>("NvAPI_DRS_GetBaseProfile", hSession, phProfile);
}

View File

@@ -2,37 +2,32 @@
* @file src/platform/windows/nvprefs/nvprefs_common.cpp
* @brief Definitions for common nvidia preferences.
*/
// local includes
// this include
#include "nvprefs_common.h"
#include "src/logging.h"
// read user override preferences from global sunshine config
// local includes
#include "src/config.h"
#include "src/logging.h"
namespace nvprefs {
void
info_message(const std::wstring &message) {
void info_message(const std::wstring &message) {
BOOST_LOG(info) << "nvprefs: " << message;
}
void
info_message(const std::string &message) {
void info_message(const std::string &message) {
BOOST_LOG(info) << "nvprefs: " << message;
}
void
error_message(const std::wstring &message) {
void error_message(const std::wstring &message) {
BOOST_LOG(error) << "nvprefs: " << message;
}
void
error_message(const std::string &message) {
void error_message(const std::string &message) {
BOOST_LOG(error) << "nvprefs: " << message;
}
nvprefs_options
get_nvprefs_options() {
nvprefs_options get_nvprefs_options() {
nvprefs_options options;
options.opengl_vulkan_on_dxgi = config::video.nv_opengl_vulkan_on_dxgi;
options.sunshine_high_power_mode = config::video.nv_sunshine_high_power_mode;

View File

@@ -4,57 +4,51 @@
*/
#pragma once
// sunshine utility header for generic smart pointers
#include "src/utility.h"
// winapi headers
// platform includes
// disable clang-format header reordering
// clang-format off
#include <windows.h>
#include <aclapi.h>
// clang-format on
// local includes
#include "src/utility.h"
namespace nvprefs {
struct safe_handle: public util::safe_ptr_v2<void, BOOL, CloseHandle> {
using util::safe_ptr_v2<void, BOOL, CloseHandle>::safe_ptr_v2;
explicit
operator bool() const {
explicit operator bool() const {
auto handle = get();
return handle != NULL && handle != INVALID_HANDLE_VALUE;
}
};
struct safe_hlocal_deleter {
void
operator()(void *p) {
void operator()(void *p) {
LocalFree(p);
}
};
template <typename T>
template<typename T>
using safe_hlocal = util::uniq_ptr<std::remove_pointer_t<T>, safe_hlocal_deleter>;
using safe_sid = util::safe_ptr_v2<void, PVOID, FreeSid>;
void
info_message(const std::wstring &message);
void info_message(const std::wstring &message);
void
info_message(const std::string &message);
void info_message(const std::string &message);
void
error_message(const std::wstring &message);
void error_message(const std::wstring &message);
void
error_message(const std::string &message);
void error_message(const std::string &message);
struct nvprefs_options {
bool opengl_vulkan_on_dxgi = true;
bool sunshine_high_power_mode = true;
};
nvprefs_options
get_nvprefs_options();
nvprefs_options get_nvprefs_options();
} // namespace nvprefs

View File

@@ -39,8 +39,7 @@ namespace nvprefs {
unload();
}
bool
nvprefs_interface::load() {
bool nvprefs_interface::load() {
if (!pimpl->loaded) {
// Check %ProgramData% variable, need it for storing undo file
wchar_t program_data_env[MAX_PATH];
@@ -61,8 +60,7 @@ namespace nvprefs {
return pimpl->loaded;
}
void
nvprefs_interface::unload() {
void nvprefs_interface::unload() {
if (pimpl->loaded) {
// Unload dynamically loaded nvapi library
pimpl->driver_settings.destroy();
@@ -70,9 +68,10 @@ namespace nvprefs {
}
}
bool
nvprefs_interface::restore_from_and_delete_undo_file_if_exists() {
if (!pimpl->loaded) return false;
bool nvprefs_interface::restore_from_and_delete_undo_file_if_exists() {
if (!pimpl->loaded) {
return false;
}
// Check for undo file from previous improper termination
bool access_denied = false;
@@ -82,12 +81,10 @@ namespace nvprefs {
if (auto undo_data = undo_file->read_undo_data()) {
if (pimpl->driver_settings.restore_global_profile_to_undo(*undo_data) && pimpl->driver_settings.save_settings()) {
info_message("Restored global profile settings from undo file - deleting the file");
}
else {
} else {
error_message("Failed to restore global profile settings from undo file, deleting the file anyway");
}
}
else {
} else {
error_message("Coulnd't read undo file, deleting the file anyway");
}
@@ -95,8 +92,7 @@ namespace nvprefs {
error_message("Couldn't delete undo file");
return false;
}
}
else if (access_denied) {
} else if (access_denied) {
error_message("Couldn't open undo file from previous improper termination, or confirm that there's no such file");
return false;
}
@@ -104,43 +100,41 @@ namespace nvprefs {
return true;
}
bool
nvprefs_interface::modify_application_profile() {
if (!pimpl->loaded) return false;
bool nvprefs_interface::modify_application_profile() {
if (!pimpl->loaded) {
return false;
}
// Modify and save sunshine.exe application profile settings, if needed
bool modified = false;
if (!pimpl->driver_settings.check_and_modify_application_profile(modified)) {
error_message("Failed to modify application profile settings");
return false;
}
else if (modified) {
} else if (modified) {
if (pimpl->driver_settings.save_settings()) {
info_message("Modified application profile settings");
}
else {
} else {
error_message("Couldn't save application profile settings");
return false;
}
}
else {
} else {
info_message("No need to modify application profile settings");
}
return true;
}
bool
nvprefs_interface::modify_global_profile() {
if (!pimpl->loaded) return false;
bool nvprefs_interface::modify_global_profile() {
if (!pimpl->loaded) {
return false;
}
// Modify but not save global profile settings, if needed
std::optional<undo_data_t> undo_data;
if (!pimpl->driver_settings.check_and_modify_global_profile(undo_data)) {
error_message("Couldn't modify global profile settings");
return false;
}
else if (!undo_data) {
} else if (!undo_data) {
info_message("No need to modify global profile settings");
return true;
}
@@ -166,8 +160,7 @@ namespace nvprefs {
if (pimpl->undo_data) {
// Merge undo data if settings has been modified externally since our last modification
pimpl->undo_data->merge(*undo_data);
}
else {
} else {
pimpl->undo_data = undo_data;
}
@@ -198,14 +191,14 @@ namespace nvprefs {
return true;
}
bool
nvprefs_interface::owning_undo_file() {
bool nvprefs_interface::owning_undo_file() {
return pimpl->undo_file.has_value();
}
bool
nvprefs_interface::restore_global_profile() {
if (!pimpl->loaded || !pimpl->undo_data || !pimpl->undo_file) return false;
bool nvprefs_interface::restore_global_profile() {
if (!pimpl->loaded || !pimpl->undo_data || !pimpl->undo_file) {
return false;
}
// Restore global profile settings with undo data
if (pimpl->driver_settings.restore_global_profile_to_undo(*pimpl->undo_data) &&
@@ -217,8 +210,7 @@ namespace nvprefs {
}
pimpl->undo_data = std::nullopt;
pimpl->undo_file = std::nullopt;
}
else {
} else {
error_message("Couldn't restore global profile settings");
return false;
}

View File

@@ -4,7 +4,7 @@
*/
#pragma once
// standard library headers
// standard includes
#include <memory>
namespace nvprefs {
@@ -14,26 +14,19 @@ namespace nvprefs {
nvprefs_interface();
~nvprefs_interface();
bool
load();
bool load();
void
unload();
void unload();
bool
restore_from_and_delete_undo_file_if_exists();
bool restore_from_and_delete_undo_file_if_exists();
bool
modify_application_profile();
bool modify_application_profile();
bool
modify_global_profile();
bool modify_global_profile();
bool
owning_undo_file();
bool owning_undo_file();
bool
restore_global_profile();
bool restore_global_profile();
private:
struct impl;

View File

@@ -2,7 +2,7 @@
* @file src/platform/windows/nvprefs/undo_data.cpp
* @brief Definitions for undoing changes to nvidia preferences.
*/
// external includes
// lib includes
#include <nlohmann/json.hpp>
// local includes
@@ -17,54 +17,46 @@ namespace nlohmann {
using data_t = nvprefs::undo_data_t::data_t;
using opengl_swapchain_t = data_t::opengl_swapchain_t;
template <typename T>
template<typename T>
struct adl_serializer<std::optional<T>> {
static void
to_json(json &j, const std::optional<T> &opt) {
static void to_json(json &j, const std::optional<T> &opt) {
if (opt == std::nullopt) {
j = nullptr;
}
else {
} else {
j = *opt;
}
}
static void
from_json(const json &j, std::optional<T> &opt) {
static void from_json(const json &j, std::optional<T> &opt) {
if (j.is_null()) {
opt = std::nullopt;
}
else {
} else {
opt = j.template get<T>();
}
}
};
template <>
template<>
struct adl_serializer<data_t> {
static void
to_json(json &j, const data_t &data) {
j = json { { "opengl_swapchain", data.opengl_swapchain } };
static void to_json(json &j, const data_t &data) {
j = json {{"opengl_swapchain", data.opengl_swapchain}};
}
static void
from_json(const json &j, data_t &data) {
static void from_json(const json &j, data_t &data) {
j.at("opengl_swapchain").get_to(data.opengl_swapchain);
}
};
template <>
template<>
struct adl_serializer<opengl_swapchain_t> {
static void
to_json(json &j, const opengl_swapchain_t &opengl_swapchain) {
static void to_json(json &j, const opengl_swapchain_t &opengl_swapchain) {
j = json {
{ "our_value", opengl_swapchain.our_value },
{ "undo_value", opengl_swapchain.undo_value }
{"our_value", opengl_swapchain.our_value},
{"undo_value", opengl_swapchain.undo_value}
};
}
static void
from_json(const json &j, opengl_swapchain_t &opengl_swapchain) {
static void from_json(const json &j, opengl_swapchain_t &opengl_swapchain) {
j.at("our_value").get_to(opengl_swapchain.our_value);
j.at("undo_value").get_to(opengl_swapchain.undo_value);
}
@@ -73,46 +65,39 @@ namespace nlohmann {
namespace nvprefs {
void
undo_data_t::set_opengl_swapchain(uint32_t our_value, std::optional<uint32_t> undo_value) {
void undo_data_t::set_opengl_swapchain(uint32_t our_value, std::optional<uint32_t> undo_value) {
data.opengl_swapchain = data_t::opengl_swapchain_t {
our_value,
undo_value
};
}
std::optional<undo_data_t::data_t::opengl_swapchain_t>
undo_data_t::get_opengl_swapchain() const {
std::optional<undo_data_t::data_t::opengl_swapchain_t> undo_data_t::get_opengl_swapchain() const {
return data.opengl_swapchain;
}
std::string
undo_data_t::write() const {
std::string undo_data_t::write() const {
try {
// Keep this assignment otherwise data will be treated as an array due to
// initializer list shenanigangs.
const json json_data = data;
return json_data.dump();
}
catch (const std::exception &err) {
error_message(std::string { "failed to serialize json data" });
} catch (const std::exception &err) {
error_message(std::string {"failed to serialize json data"});
return {};
}
}
void
undo_data_t::read(const std::vector<char> &buffer) {
void undo_data_t::read(const std::vector<char> &buffer) {
try {
data = json::parse(std::begin(buffer), std::end(buffer));
}
catch (const std::exception &err) {
error_message(std::string { "failed to parse json data: " } + err.what());
} catch (const std::exception &err) {
error_message(std::string {"failed to parse json data: "} + err.what());
data = {};
}
}
void
undo_data_t::merge(const undo_data_t &newer_data) {
void undo_data_t::merge(const undo_data_t &newer_data) {
const auto &swapchain_data = newer_data.get_opengl_swapchain();
if (swapchain_data) {
set_opengl_swapchain(swapchain_data->our_value, swapchain_data->undo_value);

View File

@@ -4,7 +4,7 @@
*/
#pragma once
// standard library headers
// standard includes
#include <cstdint>
#include <optional>
#include <string>
@@ -23,20 +23,15 @@ namespace nvprefs {
std::optional<opengl_swapchain_t> opengl_swapchain;
};
void
set_opengl_swapchain(uint32_t our_value, std::optional<uint32_t> undo_value);
void set_opengl_swapchain(uint32_t our_value, std::optional<uint32_t> undo_value);
std::optional<data_t::opengl_swapchain_t>
get_opengl_swapchain() const;
std::optional<data_t::opengl_swapchain_t> get_opengl_swapchain() const;
std::string
write() const;
std::string write() const;
void
read(const std::vector<char> &buffer);
void read(const std::vector<char> &buffer);
void
merge(const undo_data_t &newer_data);
void merge(const undo_data_t &newer_data);
private:
data_t data;

View File

@@ -9,13 +9,14 @@ namespace {
using namespace nvprefs;
DWORD
relax_permissions(HANDLE file_handle) {
DWORD relax_permissions(HANDLE file_handle) {
PACL old_dacl = nullptr;
safe_hlocal<PSECURITY_DESCRIPTOR> sd;
DWORD status = GetSecurityInfo(file_handle, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, nullptr, nullptr, &old_dacl, nullptr, &sd);
if (status != ERROR_SUCCESS) return status;
if (status != ERROR_SUCCESS) {
return status;
}
safe_sid users_sid;
SID_IDENTIFIER_AUTHORITY nt_authorithy = SECURITY_NT_AUTHORITY;
@@ -32,10 +33,14 @@ namespace {
safe_hlocal<PACL> new_dacl;
status = SetEntriesInAcl(1, &ea, old_dacl, &new_dacl);
if (status != ERROR_SUCCESS) return status;
if (status != ERROR_SUCCESS) {
return status;
}
status = SetSecurityInfo(file_handle, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, nullptr, nullptr, new_dacl.get(), nullptr);
if (status != ERROR_SUCCESS) return status;
if (status != ERROR_SUCCESS) {
return status;
}
return 0;
}
@@ -44,23 +49,20 @@ namespace {
namespace nvprefs {
std::optional<undo_file_t>
undo_file_t::open_existing_file(std::filesystem::path file_path, bool &access_denied) {
std::optional<undo_file_t> undo_file_t::open_existing_file(std::filesystem::path file_path, bool &access_denied) {
undo_file_t file;
file.file_handle.reset(CreateFileW(file_path.c_str(), GENERIC_READ | DELETE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL));
if (file.file_handle) {
access_denied = false;
return file;
}
else {
} else {
auto last_error = GetLastError();
access_denied = (last_error != ERROR_FILE_NOT_FOUND && last_error != ERROR_PATH_NOT_FOUND);
return std::nullopt;
}
}
std::optional<undo_file_t>
undo_file_t::create_new_file(std::filesystem::path file_path) {
std::optional<undo_file_t> undo_file_t::create_new_file(std::filesystem::path file_path) {
undo_file_t file;
file.file_handle.reset(CreateFileW(file_path.c_str(), GENERIC_WRITE | STANDARD_RIGHTS_ALL, 0, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL));
@@ -70,35 +72,34 @@ namespace nvprefs {
error_message("Failed to relax permissions on undo file");
}
return file;
}
else {
} else {
return std::nullopt;
}
}
bool
undo_file_t::delete_file() {
if (!file_handle) return false;
bool undo_file_t::delete_file() {
if (!file_handle) {
return false;
}
FILE_DISPOSITION_INFO delete_file_info = { TRUE };
FILE_DISPOSITION_INFO delete_file_info = {TRUE};
if (SetFileInformationByHandle(file_handle.get(), FileDispositionInfo, &delete_file_info, sizeof(delete_file_info))) {
file_handle.reset();
return true;
}
else {
} else {
return false;
}
}
bool
undo_file_t::write_undo_data(const undo_data_t &undo_data) {
if (!file_handle) return false;
bool undo_file_t::write_undo_data(const undo_data_t &undo_data) {
if (!file_handle) {
return false;
}
std::string buffer;
try {
buffer = undo_data.write();
}
catch (...) {
} catch (...) {
error_message("Couldn't serialize undo data");
return false;
}
@@ -121,9 +122,10 @@ namespace nvprefs {
return true;
}
std::optional<undo_data_t>
undo_file_t::read_undo_data() {
if (!file_handle) return std::nullopt;
std::optional<undo_data_t> undo_file_t::read_undo_data() {
if (!file_handle) {
return std::nullopt;
}
LARGE_INTEGER file_size;
if (!GetFileSizeEx(file_handle.get(), &file_size)) {
@@ -146,8 +148,7 @@ namespace nvprefs {
undo_data_t undo_data;
try {
undo_data.read(buffer);
}
catch (...) {
} catch (...) {
error_message("Couldn't parse undo file");
return std::nullopt;
}

View File

@@ -4,7 +4,7 @@
*/
#pragma once
// standard library headers
// standard includes
#include <filesystem>
// local includes
@@ -15,20 +15,15 @@ namespace nvprefs {
class undo_file_t {
public:
static std::optional<undo_file_t>
open_existing_file(std::filesystem::path file_path, bool &access_denied);
static std::optional<undo_file_t> open_existing_file(std::filesystem::path file_path, bool &access_denied);
static std::optional<undo_file_t>
create_new_file(std::filesystem::path file_path);
static std::optional<undo_file_t> create_new_file(std::filesystem::path file_path);
bool
delete_file();
bool delete_file();
bool
write_undo_data(const undo_data_t &undo_data);
bool write_undo_data(const undo_data_t &undo_data);
std::optional<undo_data_t>
read_undo_data();
std::optional<undo_data_t> read_undo_data();
private:
undo_file_t() = default;