mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2026-04-02 19:08:03 -06:00
draft user_settings
This commit is contained in:
parent
296cab2129
commit
47318e60b9
@ -872,6 +872,8 @@ set(CORE src/core/aerolib/stubs.cpp
|
||||
src/core/user_manager.h
|
||||
src/core/emulator_state.cpp
|
||||
src/core/emulator_state.h
|
||||
src/core/user_settings.cpp
|
||||
src/core/user_settings.h
|
||||
)
|
||||
|
||||
if (ARCHITECTURE STREQUAL "x86_64")
|
||||
|
||||
77
src/core/user_settings.cpp
Normal file
77
src/core/user_settings.cpp
Normal file
@ -0,0 +1,77 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2025-2026 shadPS4 Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <map>
|
||||
#include <common/path_util.h>
|
||||
#include <common/scm_rev.h>
|
||||
#include "common/logging/log.h"
|
||||
#include "user_settings.h"
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
// Singleton storage
|
||||
std::shared_ptr<UserSettingsImpl> UserSettingsImpl::s_instance = nullptr;
|
||||
std::mutex UserSettingsImpl::s_mutex;
|
||||
|
||||
// Singleton
|
||||
UserSettingsImpl::UserSettingsImpl() = default;
|
||||
|
||||
UserSettingsImpl::~UserSettingsImpl() {
|
||||
Save();
|
||||
}
|
||||
|
||||
bool UserSettingsImpl::Save() const {
|
||||
const auto path = Common::FS::GetUserPath(Common::FS::PathType::UserDir) / "users.json";
|
||||
try {
|
||||
json j;
|
||||
j["Users"] = m_userManager.GetUsers();
|
||||
|
||||
std::ofstream out(path);
|
||||
if (!out) {
|
||||
LOG_ERROR(EmuSettings, "Failed to open user settings for writing: {}", path.string());
|
||||
return false;
|
||||
}
|
||||
out << std::setw(4) << j;
|
||||
return !out.fail();
|
||||
} catch (const std::exception& e) {
|
||||
LOG_ERROR(EmuSettings, "Error saving user settings: {}", e.what());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool UserSettingsImpl::Load() {
|
||||
const auto path = Common::FS::GetUserPath(Common::FS::PathType::UserDir) / "users.json";
|
||||
try {
|
||||
if (!std::filesystem::exists(path)) {
|
||||
LOG_DEBUG(EmuSettings, "User settings file not found: {}", path.string());
|
||||
// Create default user if no file exists
|
||||
if (m_userManager.GetUsers().user.empty())
|
||||
m_userManager.GetUsers().user = m_userManager.CreateDefaultUser();
|
||||
Save();
|
||||
return false;
|
||||
}
|
||||
|
||||
std::ifstream in(path);
|
||||
if (!in) {
|
||||
LOG_ERROR(EmuSettings, "Failed to open user settings: {}", path.string());
|
||||
return false;
|
||||
}
|
||||
|
||||
json j;
|
||||
in >> j;
|
||||
|
||||
if (j.contains("Users")) {
|
||||
m_userManager.GetUsers() = j.at("Users").get<Users>();
|
||||
LOG_DEBUG(EmuSettings, "User settings loaded successfully");
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (const std::exception& e) {
|
||||
LOG_ERROR(EmuSettings, "Error loading user settings: {}", e.what());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
43
src/core/user_settings.h
Normal file
43
src/core/user_settings.h
Normal file
@ -0,0 +1,43 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2025-2026 shadPS4 Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "common/logging/log.h"
|
||||
#include "common/types.h"
|
||||
#include "core/user_manager.h"
|
||||
|
||||
#define UserSettings (*UserSettingsImpl::GetInstance())
|
||||
|
||||
// -------------------------------
|
||||
// User settings
|
||||
// -------------------------------
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(User, user_id, user_color, user_name, controller_port)
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Users, default_user_id, user)
|
||||
|
||||
class UserSettingsImpl {
|
||||
public:
|
||||
UserSettingsImpl();
|
||||
~UserSettingsImpl();
|
||||
|
||||
UserManager& GetUserManager() {
|
||||
return m_userManager;
|
||||
}
|
||||
bool Save() const;
|
||||
bool Load();
|
||||
|
||||
private:
|
||||
UserManager m_userManager;
|
||||
|
||||
static std::shared_ptr<UserSettingsImpl> s_instance;
|
||||
static std::mutex s_mutex;
|
||||
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user