Refactor switch Settings as singleton

This commit is contained in:
h0neybadger 2020-12-25 18:43:20 +01:00 committed by Florian Märkl
commit 05b22e15f1
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
5 changed files with 562 additions and 527 deletions

View file

@ -5,19 +5,24 @@
#include <regex>
#include "host.h"
#include <chiaki/log.h>
#include "host.h"
// mutual host and settings
class Host;
class Settings
{
private:
ChiakiLog * log;
const char * filename = "chiaki.conf";
protected:
// keep constructor private (sigleton class)
Settings();
static Settings * instance;
private:
const char * filename = "chiaki.conf";
ChiakiLog log;
std::map<std::string, Host> hosts;
std::map<std::string, Host> *hosts;
// global_settings from psedo INI file
ChiakiVideoResolutionPreset global_video_resolution = CHIAKI_VIDEO_RESOLUTION_PRESET_720p;
ChiakiVideoFPSPreset global_video_fps = CHIAKI_VIDEO_FPS_PRESET_60;
@ -43,53 +48,71 @@ class Settings
// the aim is not to have bulletproof parser
// the goal is to read/write inernal flat configuration file
const std::map<Settings::ConfigurationItem, std::regex> re_map = {
{ HOST_NAME, std::regex("^\\[\\s*(.+)\\s*\\]") },
{ HOST_ADDR, std::regex("^\\s*host_(?:ip|addr)\\s*=\\s*\"?((\\d+\\.\\d+\\.\\d+\\.\\d+)|([A-Za-z0-9-]{1,255}))\"?") },
{ PSN_ONLINE_ID, std::regex("^\\s*psn_online_id\\s*=\\s*\"?(\\w+)\"?") },
{ PSN_ACCOUNT_ID, std::regex("^\\s*psn_account_id\\s*=\\s*\"?([\\w/=+]+)\"?") },
{ RP_KEY, std::regex("^\\s*rp_key\\s*=\\s*\"?([\\w/=+]+)\"?") },
{ RP_KEY_TYPE, std::regex("^\\s*rp_key_type\\s*=\\s*\"?(\\d)\"?") },
{ RP_REGIST_KEY, std::regex("^\\s*rp_regist_key\\s*=\\s*\"?([\\w/=+]+)\"?") },
{ VIDEO_RESOLUTION, std::regex("^\\s*video_resolution\\s*=\\s*\"?(1080p|720p|540p|360p)\"?") },
{ VIDEO_FPS, std::regex("^\\s*video_fps\\s*=\\s*\"?(60|30)\"?") },
{ TARGET, std::regex("^\\s*target\\s*=\\s*\"?(\\d+)\"?") },
{HOST_NAME, std::regex("^\\[\\s*(.+)\\s*\\]")},
{HOST_ADDR, std::regex("^\\s*host_(?:ip|addr)\\s*=\\s*\"?((\\d+\\.\\d+\\.\\d+\\.\\d+)|([A-Za-z0-9-]{1,255}))\"?")},
{PSN_ONLINE_ID, std::regex("^\\s*psn_online_id\\s*=\\s*\"?(\\w+)\"?")},
{PSN_ACCOUNT_ID, std::regex("^\\s*psn_account_id\\s*=\\s*\"?([\\w/=+]+)\"?")},
{RP_KEY, std::regex("^\\s*rp_key\\s*=\\s*\"?([\\w/=+]+)\"?")},
{RP_KEY_TYPE, std::regex("^\\s*rp_key_type\\s*=\\s*\"?(\\d)\"?")},
{RP_REGIST_KEY, std::regex("^\\s*rp_regist_key\\s*=\\s*\"?([\\w/=+]+)\"?")},
{VIDEO_RESOLUTION, std::regex("^\\s*video_resolution\\s*=\\s*\"?(1080p|720p|540p|360p)\"?")},
{VIDEO_FPS, std::regex("^\\s*video_fps\\s*=\\s*\"?(60|30)\"?")},
{TARGET, std::regex("^\\s*target\\s*=\\s*\"?(\\d+)\"?")},
};
ConfigurationItem ParseLine(std::string * line, std::string * value);
size_t GetB64encodeSize(size_t);
public:
Settings(ChiakiLog * log, std::map<std::string, Host> * hosts): log(log), hosts(hosts){};
// singleton configuration
Settings(const Settings&) = delete;
void operator=(const Settings&) = delete;
static Settings * GetInstance();
ChiakiLog * GetLogger();
std::map<std::string, Host> * GetHostsMap();
Host * GetOrCreateHost(std::string * host_name);
ChiakiLog* GetLogger();
std::string GetPSNOnlineID(Host * host);
std::string GetPSNAccountID(Host * host);
void SetPSNOnlineID(Host * host, std::string psn_online_id);
void SetPSNAccountID(Host * host, std::string psn_account_id);
ChiakiVideoResolutionPreset GetVideoResolution(Host * host);
ChiakiVideoFPSPreset GetVideoFPS(Host * host);
std::string ResolutionPresetToString(ChiakiVideoResolutionPreset resolution);
std::string FPSPresetToString(ChiakiVideoFPSPreset fps);
ChiakiVideoResolutionPreset StringToResolutionPreset(std::string value);
ChiakiVideoFPSPreset StringToFPSPreset(std::string value);
int ResolutionPresetToInt(ChiakiVideoResolutionPreset resolution);
int FPSPresetToInt(ChiakiVideoFPSPreset fps);
void SetVideoResolution(Host * host, ChiakiVideoResolutionPreset value);
void SetVideoFPS(Host * host, ChiakiVideoFPSPreset value);
void SetVideoResolution(Host * host, std::string value);
void SetVideoFPS(Host * host, std::string value);
bool SetChiakiTarget(Host * host, ChiakiTarget target);
bool SetChiakiTarget(Host * host, std::string value);
std::string GetHostAddr(Host * host);
std::string GetHostName(Host * host);
bool SetHostRPKeyType(Host * host, std::string value);
int GetHostRPKeyType(Host * host);
std::string GetHostRPKey(Host * host);
std::string GetHostRPRegistKey(Host * host);
bool SetHostRPKey(Host * host, std::string rp_key_b64);
bool SetHostRPRegistKey(Host * host, std::string rp_regist_key_b64);
ChiakiTarget GetChiakiTarget(Host * host);
void ParseFile();
int WriteFile();
std::string ResolutionPresetToString(ChiakiVideoResolutionPreset resolution);
int ResolutionPresetToInt(ChiakiVideoResolutionPreset resolution);
ChiakiVideoResolutionPreset StringToResolutionPreset(std::string value);
std::string FPSPresetToString(ChiakiVideoFPSPreset fps);
int FPSPresetToInt(ChiakiVideoFPSPreset fps);
ChiakiVideoFPSPreset StringToFPSPreset(std::string value);
std::string GetHostName(Host * host);
std::string GetHostAddr(Host * host);
std::string GetPSNOnlineID(Host * host);
void SetPSNOnlineID(Host * host, std::string psn_online_id);
std::string GetPSNAccountID(Host * host);
void SetPSNAccountID(Host * host, std::string psn_account_id);
ChiakiVideoResolutionPreset GetVideoResolution(Host * host);
void SetVideoResolution(Host * host, ChiakiVideoResolutionPreset value);
void SetVideoResolution(Host * host, std::string value);
ChiakiVideoFPSPreset GetVideoFPS(Host * host);
void SetVideoFPS(Host * host, ChiakiVideoFPSPreset value);
void SetVideoFPS(Host * host, std::string value);
ChiakiTarget GetChiakiTarget(Host * host);
bool SetChiakiTarget(Host * host, ChiakiTarget target);
bool SetChiakiTarget(Host * host, std::string value);
std::string GetHostRPKey(Host * host);
bool SetHostRPKey(Host * host, std::string rp_key_b64);
std::string GetHostRPRegistKey(Host * host);
bool SetHostRPRegistKey(Host * host, std::string rp_regist_key_b64);
int GetHostRPKeyType(Host * host);
bool SetHostRPKeyType(Host * host, std::string value);
};
#endif // CHIAKI_SETTINGS_H