Refactor switch Settings as singleton

This commit is contained in:
h0neybadger 2020-12-25 18:43:20 +01:00 committed by Florian Märkl
parent 943e661af4
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

@ -44,9 +44,8 @@ class HostInterface : public brls::List
class MainApplication
{
private:
ChiakiLog * log;
std::map<std::string, Host> * hosts;
Settings * settings;
ChiakiLog * log;
DiscoveryManager * discoverymanager;
IO * io;
brls::TabFrame * rootFrame;
@ -59,9 +58,7 @@ class MainApplication
bool BuildConfigurationMenu(brls::List *, Host * host = nullptr);
void BuildAddHostConfigurationMenu(brls::List *);
public:
MainApplication(std::map<std::string, Host> * hosts,
Settings * settings, DiscoveryManager * discoverymanager,
IO * io, ChiakiLog * log);
MainApplication(DiscoveryManager * discoverymanager, IO * io);
~MainApplication();
bool Load();

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

View file

@ -246,12 +246,11 @@ bool HostInterface::CloseStream(ChiakiQuitEvent * quit)
return false;
}
MainApplication::MainApplication(std::map<std::string, Host> * hosts,
Settings * settings, DiscoveryManager * discoverymanager,
IO * io, ChiakiLog * log)
: hosts(hosts), settings(settings), discoverymanager(discoverymanager),
io(io), log(log)
MainApplication::MainApplication(DiscoveryManager * discoverymanager, IO * io)
: discoverymanager(discoverymanager), io(io)
{
this->settings = Settings::GetInstance();
this->log = this->settings->GetLogger();
}
MainApplication::~MainApplication()
@ -305,9 +304,11 @@ bool MainApplication::Load()
// Add the root view to the stack
brls::Application::pushView(this->rootFrame);
std::map<std::string, Host> * hosts = this->settings->GetHostsMap();
while(brls::Application::mainLoop())
{
for(auto it = this->hosts->begin(); it != this->hosts->end(); it++)
for(auto it = hosts->begin(); it != hosts->end(); it++)
{
// add host to the gui only if the host is registered or discovered
if(this->host_menuitems.find(&it->second) == this->host_menuitems.end()

View file

@ -107,49 +107,39 @@ extern "C" void userAppExit()
int main(int argc, char* argv[])
{
// init chiaki lib
ChiakiLog log;
#if defined(__SWITCH__) && !defined(CHIAKI_ENABLE_SWITCH_NXLINK)
// null log for switch version
chiaki_log_init(&log, 0, chiaki_log_cb_print, NULL);
#else
chiaki_log_init(&log, CHIAKI_LOG_ALL ^ CHIAKI_LOG_VERBOSE, chiaki_log_cb_print, NULL);
#endif
// load chiaki lib
CHIAKI_LOGI(&log, "Loading chaki lib");
Settings * settings = Settings::GetInstance();
ChiakiLog * log = settings->GetLogger();
CHIAKI_LOGI(log, "Loading chaki lib");
ChiakiErrorCode err = chiaki_lib_init();
if(err != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGE(&log, "Chiaki lib init failed: %s\n", chiaki_error_string(err));
CHIAKI_LOGE(log, "Chiaki lib init failed: %s\n", chiaki_error_string(err));
return 1;
}
CHIAKI_LOGI(&log, "Loading SDL audio / joystick");
CHIAKI_LOGI(log, "Loading SDL audio / joystick");
if(SDL_Init( SDL_INIT_AUDIO | SDL_INIT_JOYSTICK ))
{
CHIAKI_LOGE(&log, "SDL initialization failed: %s", SDL_GetError());
CHIAKI_LOGE(log, "SDL initialization failed: %s", SDL_GetError());
return 1;
}
// build sdl OpenGl and AV decoders graphical interface
IO io = IO(&log); // open Input Output class
IO io = IO(log); // open Input Output class
// manage ps4 setting discovery wakeup and registration
std::map<std::string, Host> hosts;
// create host objects form config file
Settings settings = Settings(&log, &hosts);
CHIAKI_LOGI(&log, "Read chiaki settings file");
CHIAKI_LOGI(log, "Read chiaki settings file");
// FIXME use GUI for config
settings.ParseFile();
Host * host = nullptr;
DiscoveryManager discoverymanager = DiscoveryManager(&settings);
MainApplication app = MainApplication(&hosts, &settings, &discoverymanager, &io, &log);
DiscoveryManager discoverymanager = DiscoveryManager(settings);
MainApplication app = MainApplication(&discoverymanager, &io);
app.Load();
CHIAKI_LOGI(&log, "Quit applet");
CHIAKI_LOGI(log, "Quit applet");
SDL_Quit();
return 0;
}

View file

@ -4,33 +4,19 @@
#include <chiaki/base64.h>
#include "settings.h"
Host * Settings::GetOrCreateHost(std::string *host_name)
Settings::Settings()
{
bool created = false;
// update of create Host instance
if(this->hosts->find(*host_name) == hosts->end())
{
// create host if udefined
Host h = Host(this->log, this, *host_name);
this->hosts->emplace( *host_name, h);
created = true;
}
Host *host = &(this->hosts->at(*host_name));
if(created)
{
// copy default settings
// to the newly created host
this->SetPSNOnlineID(host, this->global_psn_online_id);
this->SetPSNAccountID(host, this->global_psn_account_id);
this->SetVideoResolution(host, this->global_video_resolution);
this->SetVideoFPS(host, this->global_video_fps);
}
return host;
#if defined(__SWITCH__) && !defined(CHIAKI_ENABLE_SWITCH_NXLINK)
// null log for switch version
chiaki_log_init(&this->log, 0, chiaki_log_cb_print, NULL);
#else
chiaki_log_init(&this->log, CHIAKI_LOG_ALL ^ CHIAKI_LOG_VERBOSE, chiaki_log_cb_print, NULL);
#endif
CHIAKI_LOGI(&this->log, "Read chiaki settings file %s", this->filename);
this->ParseFile();
}
Settings::ConfigurationItem Settings::ParseLine(std::string *line, std::string *value)
Settings::ConfigurationItem Settings::ParseLine(std::string * line, std::string * value)
{
Settings::ConfigurationItem ci;
std::smatch m;
@ -46,33 +32,334 @@ Settings::ConfigurationItem Settings::ParseLine(std::string *line, std::string *
return UNKNOWN;
}
ChiakiLog* Settings::GetLogger()
{
return this->log;
}
size_t Settings::GetB64encodeSize(size_t in)
{
// calculate base64 buffer size after encode
return ((4 * in / 3) + 3) & ~3;
}
Settings * Settings::instance = nullptr;
Settings * Settings::GetInstance()
{
if(instance == nullptr)
instance = new Settings;
return instance;
}
ChiakiLog * Settings::GetLogger()
{
return &this->log;
}
std::map<std::string, Host> * Settings::GetHostsMap()
{
return &this->hosts;
}
Host * Settings::GetOrCreateHost(std::string * host_name)
{
bool created = false;
// update of create Host instance
if(this->hosts.find(*host_name) == hosts.end())
{
// create host if udefined
Host h = Host(&this->log, this, *host_name);
this->hosts.emplace(*host_name, h);
created = true;
}
Host * host = &(this->hosts.at(*host_name));
if(created)
{
// copy default settings
// to the newly created host
this->SetPSNOnlineID(host, this->global_psn_online_id);
this->SetPSNAccountID(host, this->global_psn_account_id);
this->SetVideoResolution(host, this->global_video_resolution);
this->SetVideoFPS(host, this->global_video_fps);
}
return host;
}
void Settings::ParseFile()
{
CHIAKI_LOGI(&this->log, "Parse config file %s", this->filename);
std::fstream config_file;
config_file.open(this->filename, std::fstream::in);
std::string line;
std::string value;
bool rp_key_b = false, rp_regist_key_b = false, rp_key_type_b = false;
Host * current_host = nullptr;
if(config_file.is_open())
{
CHIAKI_LOGV(&this->log, "Config file opened");
Settings::ConfigurationItem ci;
while(getline(config_file, line))
{
CHIAKI_LOGV(&this->log, "Parse config line `%s`", line.c_str());
// for each line loop over config regex
ci = this->ParseLine(&line, &value);
switch(ci)
{
// got to next line
case UNKNOWN:
CHIAKI_LOGV(&this->log, "UNKNOWN config");
break;
case HOST_NAME:
CHIAKI_LOGV(&this->log, "HOST_NAME %s", value.c_str());
// current host is in context
current_host = this->GetOrCreateHost(&value);
// all following case will edit the current_host config
rp_key_b = false;
rp_regist_key_b = false;
rp_key_type_b = false;
break;
case HOST_ADDR:
CHIAKI_LOGV(&this->log, "HOST_ADDR %s", value.c_str());
if(current_host != nullptr)
current_host->host_addr = value;
break;
case PSN_ONLINE_ID:
CHIAKI_LOGV(&this->log, "PSN_ONLINE_ID %s", value.c_str());
// current_host == nullptr
// means we are in global ini section
// update default setting
this->SetPSNOnlineID(current_host, value);
break;
case PSN_ACCOUNT_ID:
CHIAKI_LOGV(&this->log, "PSN_ACCOUNT_ID %s", value.c_str());
this->SetPSNAccountID(current_host, value);
break;
case RP_KEY:
CHIAKI_LOGV(&this->log, "RP_KEY %s", value.c_str());
if(current_host != nullptr)
rp_key_b = this->SetHostRPKey(current_host, value);
break;
case RP_KEY_TYPE:
CHIAKI_LOGV(&this->log, "RP_KEY_TYPE %s", value.c_str());
if(current_host != nullptr)
// TODO Check possible rp_type values
rp_key_type_b = this->SetHostRPKeyType(current_host, value);
break;
case RP_REGIST_KEY:
CHIAKI_LOGV(&this->log, "RP_REGIST_KEY %s", value.c_str());
if(current_host != nullptr)
rp_regist_key_b = this->SetHostRPRegistKey(current_host, value);
break;
case VIDEO_RESOLUTION:
this->SetVideoResolution(current_host, value);
break;
case VIDEO_FPS:
this->SetVideoFPS(current_host, value);
break;
case TARGET:
CHIAKI_LOGV(&this->log, "TARGET %s", value.c_str());
if(current_host != nullptr)
this->SetChiakiTarget(current_host, value);
break;
} // ci switch
if(rp_key_b && rp_regist_key_b && rp_key_type_b)
// the current host contains rp key data
current_host->rp_key_data = true;
} // is_open
config_file.close();
}
return;
}
int Settings::WriteFile()
{
std::fstream config_file;
CHIAKI_LOGI(&this->log, "Write config file %s", this->filename);
// flush file (trunc)
// the config file is completely overwritten
config_file.open(this->filename, std::fstream::out | std::ofstream::trunc);
std::string line;
std::string value;
if(config_file.is_open())
{
// save global settings
CHIAKI_LOGD(&this->log, "Write Global config file %s", this->filename);
if(this->global_video_resolution)
config_file << "video_resolution = \""
<< this->ResolutionPresetToString(this->GetVideoResolution(nullptr))
<< "\"\n";
if(this->global_video_fps)
config_file << "video_fps = "
<< this->FPSPresetToString(this->GetVideoFPS(nullptr))
<< "\n";
if(this->global_psn_online_id.length())
config_file << "psn_online_id = \"" << this->global_psn_online_id << "\"\n";
if(this->global_psn_account_id.length())
config_file << "psn_account_id = \"" << this->global_psn_account_id << "\"\n";
// write host config in file
// loop over all configured
for(auto it = this->hosts.begin(); it != this->hosts.end(); it++)
{
// first is std::string
// second is Host
CHIAKI_LOGD(&this->log, "Write Host config file %s", it->first.c_str());
config_file << "[" << it->first << "]\n"
<< "host_addr = \"" << it->second.GetHostAddr() << "\"\n"
<< "target = " << it->second.GetChiakiTarget() << "\"\n";
config_file << "target = \"" << it->second.psn_account_id << "\"\n";
if(it->second.video_resolution)
config_file << "video_resolution = \""
<< this->ResolutionPresetToString(this->GetVideoResolution(&it->second))
<< "\"\n";
if(it->second.video_fps)
config_file << "video_fps = "
<< this->FPSPresetToString(this->GetVideoFPS(&it->second))
<< "\n";
if(it->second.psn_online_id.length())
config_file << "psn_online_id = \"" << it->second.psn_online_id << "\"\n";
if(it->second.psn_account_id.length())
config_file << "psn_account_id = \"" << it->second.psn_account_id << "\"\n";
if(it->second.rp_key_data || it->second.registered)
{
char rp_key_type[33] = {0};
snprintf(rp_key_type, sizeof(rp_key_type), "%d", it->second.rp_key_type);
// save registered rp key for auto login
config_file << "rp_key = \"" << this->GetHostRPKey(&it->second) << "\"\n"
<< "rp_regist_key = \"" << this->GetHostRPRegistKey(&it->second) << "\"\n"
<< "rp_key_type = " << rp_key_type << "\n";
}
config_file << "\n";
} // for host
} // is_open
config_file.close();
return 0;
}
std::string Settings::ResolutionPresetToString(ChiakiVideoResolutionPreset resolution)
{
switch(resolution)
{
case CHIAKI_VIDEO_RESOLUTION_PRESET_360p:
return "360p";
case CHIAKI_VIDEO_RESOLUTION_PRESET_540p:
return "540p";
case CHIAKI_VIDEO_RESOLUTION_PRESET_720p:
return "720p";
case CHIAKI_VIDEO_RESOLUTION_PRESET_1080p:
return "1080p";
}
return "UNKNOWN";
}
int Settings::ResolutionPresetToInt(ChiakiVideoResolutionPreset resolution)
{
switch(resolution)
{
case CHIAKI_VIDEO_RESOLUTION_PRESET_360p:
return 360;
case CHIAKI_VIDEO_RESOLUTION_PRESET_540p:
return 540;
case CHIAKI_VIDEO_RESOLUTION_PRESET_720p:
return 720;
case CHIAKI_VIDEO_RESOLUTION_PRESET_1080p:
return 1080;
}
return 0;
}
ChiakiVideoResolutionPreset Settings::StringToResolutionPreset(std::string value)
{
if(value.compare("1080p") == 0)
return CHIAKI_VIDEO_RESOLUTION_PRESET_1080p;
else if(value.compare("720p") == 0)
return CHIAKI_VIDEO_RESOLUTION_PRESET_720p;
else if(value.compare("540p") == 0)
return CHIAKI_VIDEO_RESOLUTION_PRESET_540p;
else if(value.compare("360p") == 0)
return CHIAKI_VIDEO_RESOLUTION_PRESET_360p;
// default
CHIAKI_LOGE(&this->log, "Unable to parse String resolution: %s",
value.c_str());
return CHIAKI_VIDEO_RESOLUTION_PRESET_720p;
}
std::string Settings::FPSPresetToString(ChiakiVideoFPSPreset fps)
{
switch(fps)
{
case CHIAKI_VIDEO_FPS_PRESET_30:
return "30";
case CHIAKI_VIDEO_FPS_PRESET_60:
return "60";
}
return "UNKNOWN";
}
int Settings::FPSPresetToInt(ChiakiVideoFPSPreset fps)
{
switch(fps)
{
case CHIAKI_VIDEO_FPS_PRESET_30:
return 30;
case CHIAKI_VIDEO_FPS_PRESET_60:
return 60;
}
return 0;
}
ChiakiVideoFPSPreset Settings::StringToFPSPreset(std::string value)
{
if(value.compare("60") == 0)
return CHIAKI_VIDEO_FPS_PRESET_60;
else if(value.compare("30") == 0)
return CHIAKI_VIDEO_FPS_PRESET_30;
// default
CHIAKI_LOGE(&this->log, "Unable to parse String fps: %s",
value.c_str());
return CHIAKI_VIDEO_FPS_PRESET_30;
}
std::string Settings::GetHostName(Host * host)
{
if(host != nullptr)
return host->GetHostName();
else
CHIAKI_LOGE(&this->log, "Cannot GetHostName from nullptr host");
return "";
}
std::string Settings::GetHostAddr(Host * host)
{
if(host != nullptr)
return host->GetHostAddr();
else
CHIAKI_LOGE(&this->log, "Cannot GetHostAddr from nullptr host");
return "";
}
std::string Settings::GetPSNOnlineID(Host * host)
{
if(host == nullptr || host->psn_online_id.length() == 0 )
if(host == nullptr || host->psn_online_id.length() == 0)
return this->global_psn_online_id;
else
return host->psn_online_id;
}
std::string Settings::GetPSNAccountID(Host * host)
{
if(host == nullptr || host->psn_account_id.length() == 0 )
return this->global_psn_account_id;
else
return host->psn_account_id;
}
void Settings::SetPSNOnlineID(Host * host, std::string psn_online_id)
{
if(host == nullptr)
@ -81,6 +368,14 @@ void Settings::SetPSNOnlineID(Host * host, std::string psn_online_id)
host->psn_online_id = psn_online_id;
}
std::string Settings::GetPSNAccountID(Host * host)
{
if(host == nullptr || host->psn_account_id.length() == 0)
return this->global_psn_account_id;
else
return host->psn_account_id;
}
void Settings::SetPSNAccountID(Host * host, std::string psn_account_id)
{
if(host == nullptr)
@ -89,94 +384,6 @@ void Settings::SetPSNAccountID(Host * host, std::string psn_account_id)
host->psn_account_id = psn_account_id;
}
std::string Settings::ResolutionPresetToString(ChiakiVideoResolutionPreset resolution)
{
switch(resolution)
{
case CHIAKI_VIDEO_RESOLUTION_PRESET_360p:
return "360p";
case CHIAKI_VIDEO_RESOLUTION_PRESET_540p:
return "540p";
case CHIAKI_VIDEO_RESOLUTION_PRESET_720p:
return "720p";
case CHIAKI_VIDEO_RESOLUTION_PRESET_1080p:
return "1080p";
}
return "UNKNOWN";
}
std::string Settings::FPSPresetToString(ChiakiVideoFPSPreset fps)
{
switch(fps)
{
case CHIAKI_VIDEO_FPS_PRESET_30:
return "30";
case CHIAKI_VIDEO_FPS_PRESET_60:
return "60";
}
return "UNKNOWN";
}
ChiakiVideoResolutionPreset Settings::StringToResolutionPreset(std::string value)
{
if (value.compare("1080p") == 0)
return CHIAKI_VIDEO_RESOLUTION_PRESET_1080p;
else if (value.compare("720p") == 0)
return CHIAKI_VIDEO_RESOLUTION_PRESET_720p;
else if (value.compare("540p") == 0)
return CHIAKI_VIDEO_RESOLUTION_PRESET_540p;
else if (value.compare("360p") == 0)
return CHIAKI_VIDEO_RESOLUTION_PRESET_360p;
// default
CHIAKI_LOGE(this->log, "Unable to parse String resolution: %s",
value.c_str());
return CHIAKI_VIDEO_RESOLUTION_PRESET_720p;
}
ChiakiVideoFPSPreset Settings::StringToFPSPreset(std::string value)
{
if (value.compare("60") == 0)
return CHIAKI_VIDEO_FPS_PRESET_60;
else if (value.compare("30") == 0)
return CHIAKI_VIDEO_FPS_PRESET_30;
// default
CHIAKI_LOGE(this->log, "Unable to parse String fps: %s",
value.c_str());
return CHIAKI_VIDEO_FPS_PRESET_30;
}
int Settings::FPSPresetToInt(ChiakiVideoFPSPreset fps)
{
switch(fps)
{
case CHIAKI_VIDEO_FPS_PRESET_30:
return 30;
case CHIAKI_VIDEO_FPS_PRESET_60:
return 60;
}
return 0;
}
int Settings::ResolutionPresetToInt(ChiakiVideoResolutionPreset resolution)
{
switch(resolution)
{
case CHIAKI_VIDEO_RESOLUTION_PRESET_360p:
return 360;
case CHIAKI_VIDEO_RESOLUTION_PRESET_540p:
return 540;
case CHIAKI_VIDEO_RESOLUTION_PRESET_720p:
return 720;
case CHIAKI_VIDEO_RESOLUTION_PRESET_1080p:
return 1080;
}
return 0;
}
ChiakiVideoResolutionPreset Settings::GetVideoResolution(Host * host)
{
if(host == nullptr)
@ -185,14 +392,6 @@ ChiakiVideoResolutionPreset Settings::GetVideoResolution(Host * host)
return host->video_resolution;
}
ChiakiVideoFPSPreset Settings::GetVideoFPS(Host * host)
{
if(host == nullptr)
return this->global_video_fps;
else
return host->video_fps;
}
void Settings::SetVideoResolution(Host * host, ChiakiVideoResolutionPreset value)
{
if(host == nullptr)
@ -207,6 +406,14 @@ void Settings::SetVideoResolution(Host * host, std::string value)
this->SetVideoResolution(host, p);
}
ChiakiVideoFPSPreset Settings::GetVideoFPS(Host * host)
{
if(host == nullptr)
return this->global_video_fps;
else
return host->video_fps;
}
void Settings::SetVideoFPS(Host * host, ChiakiVideoFPSPreset value)
{
if(host == nullptr)
@ -221,6 +428,139 @@ void Settings::SetVideoFPS(Host * host, std::string value)
this->SetVideoFPS(host, p);
}
ChiakiTarget Settings::GetChiakiTarget(Host * host)
{
return host->GetChiakiTarget();
}
bool Settings::SetChiakiTarget(Host * host, ChiakiTarget target)
{
if(host != nullptr)
{
host->SetChiakiTarget(target);
return true;
}
else
{
CHIAKI_LOGE(&this->log, "Cannot SetChiakiTarget from nullptr host");
return false;
}
}
bool Settings::SetChiakiTarget(Host * host, std::string value)
{
// TODO Check possible target values
return this->SetChiakiTarget(host, static_cast<ChiakiTarget>(std::atoi(value.c_str())));
}
std::string Settings::GetHostRPKey(Host * host)
{
if(host != nullptr)
{
if(host->rp_key_data || host->registered)
{
size_t rp_key_b64_sz = this->GetB64encodeSize(0x10);
char rp_key_b64[rp_key_b64_sz + 1] = {0};
ChiakiErrorCode err;
err = chiaki_base64_encode(
host->rp_key, 0x10,
rp_key_b64, sizeof(rp_key_b64));
if(CHIAKI_ERR_SUCCESS == err)
return rp_key_b64;
else
CHIAKI_LOGE(&this->log, "Failed to encode rp_key to base64");
}
}
else
CHIAKI_LOGE(&this->log, "Cannot GetHostRPKey from nullptr host");
return "";
}
bool Settings::SetHostRPKey(Host * host, std::string rp_key_b64)
{
if(host != nullptr)
{
size_t rp_key_sz = sizeof(host->rp_key);
ChiakiErrorCode err = chiaki_base64_decode(
rp_key_b64.c_str(), rp_key_b64.length(),
host->rp_key, &rp_key_sz);
if(CHIAKI_ERR_SUCCESS != err)
CHIAKI_LOGE(&this->log, "Failed to parse RP_KEY %s (it must be a base64 encoded)", rp_key_b64.c_str());
else
return true;
}
else
CHIAKI_LOGE(&this->log, "Cannot SetHostRPKey from nullptr host");
return false;
}
std::string Settings::GetHostRPRegistKey(Host * host)
{
if(host != nullptr)
{
if(host->rp_key_data || host->registered)
{
size_t rp_regist_key_b64_sz = this->GetB64encodeSize(CHIAKI_SESSION_AUTH_SIZE);
char rp_regist_key_b64[rp_regist_key_b64_sz + 1] = {0};
ChiakiErrorCode err;
err = chiaki_base64_encode(
(uint8_t *)host->rp_regist_key, CHIAKI_SESSION_AUTH_SIZE,
rp_regist_key_b64, sizeof(rp_regist_key_b64));
if(CHIAKI_ERR_SUCCESS == err)
return rp_regist_key_b64;
else
CHIAKI_LOGE(&this->log, "Failed to encode rp_regist_key to base64");
}
}
else
CHIAKI_LOGE(&this->log, "Cannot GetHostRPRegistKey from nullptr host");
return "";
}
bool Settings::SetHostRPRegistKey(Host * host, std::string rp_regist_key_b64)
{
if(host != nullptr)
{
size_t rp_regist_key_sz = sizeof(host->rp_regist_key);
ChiakiErrorCode err = chiaki_base64_decode(
rp_regist_key_b64.c_str(), rp_regist_key_b64.length(),
(uint8_t *)host->rp_regist_key, &rp_regist_key_sz);
if(CHIAKI_ERR_SUCCESS != err)
CHIAKI_LOGE(&this->log, "Failed to parse RP_REGIST_KEY %s (it must be a base64 encoded)", rp_regist_key_b64.c_str());
else
return true;
}
else
CHIAKI_LOGE(&this->log, "Cannot SetHostRPKey from nullptr host");
return false;
}
int Settings::GetHostRPKeyType(Host * host)
{
if(host != nullptr)
return host->rp_key_type;
CHIAKI_LOGE(&this->log, "Cannot GetHostRPKeyType from nullptr host");
return 0;
}
bool Settings::SetHostRPKeyType(Host * host, std::string value)
{
if(host != nullptr)
{
// TODO Check possible rp_type values
host->rp_key_type = std::atoi(value.c_str());
return true;
}
return false;
}
#ifdef CHIAKI_ENABLE_SWITCH_OVERCLOCK
int Settings::GetCPUOverclock(Host * host)
{
@ -259,320 +599,4 @@ void Settings::SetCPUOverclock(Host * host, std::string value)
}
#endif
std::string Settings::GetHostAddr(Host * host)
{
if(host != nullptr)
return host->GetHostAddr();
else
CHIAKI_LOGE(this->log, "Cannot GetHostAddr from nullptr host");
return "";
}
std::string Settings::GetHostName(Host * host)
{
if(host != nullptr)
return host->GetHostName();
else
CHIAKI_LOGE(this->log, "Cannot GetHostName from nullptr host");
return "";
}
int Settings::GetHostRPKeyType(Host * host)
{
if(host != nullptr)
return host->rp_key_type;
CHIAKI_LOGE(this->log, "Cannot GetHostRPKeyType from nullptr host");
return 0;
}
bool Settings::SetHostRPKeyType(Host * host, std::string value)
{
if(host != nullptr)
{
// TODO Check possible rp_type values
host->rp_key_type = std::atoi(value.c_str());
return true;
}
return false;
}
std::string Settings::GetHostRPKey(Host * host)
{
if(host != nullptr)
{
if(host->rp_key_data || host->registered)
{
size_t rp_key_b64_sz = this->GetB64encodeSize(0x10);
char rp_key_b64[rp_key_b64_sz + 1] = {0};
ChiakiErrorCode err;
err = chiaki_base64_encode(
host->rp_key, 0x10,
rp_key_b64, sizeof(rp_key_b64));
if(CHIAKI_ERR_SUCCESS == err)
return rp_key_b64;
else
CHIAKI_LOGE(this->log, "Failed to encode rp_key to base64");
}
}
else
CHIAKI_LOGE(this->log, "Cannot GetHostRPKey from nullptr host");
return "";
}
std::string Settings::GetHostRPRegistKey(Host * host)
{
if(host != nullptr)
{
if(host->rp_key_data || host->registered)
{
size_t rp_regist_key_b64_sz = this->GetB64encodeSize(CHIAKI_SESSION_AUTH_SIZE);
char rp_regist_key_b64[rp_regist_key_b64_sz + 1] = {0};
ChiakiErrorCode err;
err = chiaki_base64_encode(
(uint8_t *) host->rp_regist_key, CHIAKI_SESSION_AUTH_SIZE,
rp_regist_key_b64, sizeof(rp_regist_key_b64));
if(CHIAKI_ERR_SUCCESS == err)
return rp_regist_key_b64;
else
CHIAKI_LOGE(this->log, "Failed to encode rp_regist_key to base64");
}
}
else
CHIAKI_LOGE(this->log, "Cannot GetHostRPRegistKey from nullptr host");
return "";
}
bool Settings::SetHostRPKey(Host * host, std::string rp_key_b64)
{
if(host != nullptr)
{
size_t rp_key_sz = sizeof(host->rp_key);
ChiakiErrorCode err = chiaki_base64_decode(
rp_key_b64.c_str(), rp_key_b64.length(),
host->rp_key, &rp_key_sz);
if(CHIAKI_ERR_SUCCESS != err)
CHIAKI_LOGE(this->log, "Failed to parse RP_KEY %s (it must be a base64 encoded)", rp_key_b64.c_str());
else
return true;
}
else
CHIAKI_LOGE(this->log, "Cannot SetHostRPKey from nullptr host");
return false;
}
bool Settings::SetHostRPRegistKey(Host * host, std::string rp_regist_key_b64)
{
if(host != nullptr)
{
size_t rp_regist_key_sz = sizeof(host->rp_regist_key);
ChiakiErrorCode err = chiaki_base64_decode(
rp_regist_key_b64.c_str(), rp_regist_key_b64.length(),
(uint8_t*) host->rp_regist_key, &rp_regist_key_sz);
if(CHIAKI_ERR_SUCCESS != err)
CHIAKI_LOGE(this->log, "Failed to parse RP_REGIST_KEY %s (it must be a base64 encoded)", rp_regist_key_b64.c_str());
else
return true;
}
else
CHIAKI_LOGE(this->log, "Cannot SetHostRPKey from nullptr host");
return false;
}
ChiakiTarget Settings::GetChiakiTarget(Host * host)
{
return host->GetChiakiTarget();
}
bool Settings::SetChiakiTarget(Host * host, ChiakiTarget target)
{
if(host != nullptr)
{
host->SetChiakiTarget(target);
return true;
}
else
{
CHIAKI_LOGE(this->log, "Cannot SetChiakiTarget from nullptr host");
return false;
}
}
bool Settings::SetChiakiTarget(Host * host, std::string value)
{
// TODO Check possible target values
return this->SetChiakiTarget(host, static_cast<ChiakiTarget>(std::atoi(value.c_str())));
}
void Settings::ParseFile()
{
CHIAKI_LOGI(this->log, "Parse config file %s", this->filename);
std::fstream config_file;
config_file.open(this->filename, std::fstream::in);
std::string line;
std::string value;
bool rp_key_b = false, rp_regist_key_b = false, rp_key_type_b = false;
Host *current_host = nullptr;
if(config_file.is_open())
{
CHIAKI_LOGV(this->log, "Config file opened");
Settings::ConfigurationItem ci;
while(getline(config_file, line))
{
CHIAKI_LOGV(this->log, "Parse config line `%s`", line.c_str());
// for each line loop over config regex
ci = this->ParseLine(&line, &value);
switch(ci)
{
// got to next line
case UNKNOWN: CHIAKI_LOGV(this->log, "UNKNOWN config"); break;
case HOST_NAME:
CHIAKI_LOGV(this->log, "HOST_NAME %s", value.c_str());
// current host is in context
current_host = this->GetOrCreateHost(&value);
// all following case will edit the current_host config
rp_key_b=false;
rp_regist_key_b=false;
rp_key_type_b=false;
break;
case HOST_ADDR:
CHIAKI_LOGV(this->log, "HOST_ADDR %s", value.c_str());
if(current_host != nullptr)
current_host->host_addr = value;
break;
case PSN_ONLINE_ID:
CHIAKI_LOGV(this->log, "PSN_ONLINE_ID %s", value.c_str());
// current_host == nullptr
// means we are in global ini section
// update default setting
this->SetPSNOnlineID(current_host, value);
break;
case PSN_ACCOUNT_ID:
CHIAKI_LOGV(this->log, "PSN_ACCOUNT_ID %s", value.c_str());
this->SetPSNAccountID(current_host, value);
break;
case RP_KEY:
CHIAKI_LOGV(this->log, "RP_KEY %s", value.c_str());
if(current_host != nullptr)
rp_key_b = this->SetHostRPKey(current_host, value);
break;
case RP_KEY_TYPE:
CHIAKI_LOGV(this->log, "RP_KEY_TYPE %s", value.c_str());
if(current_host != nullptr)
// TODO Check possible rp_type values
rp_key_type_b = this->SetHostRPKeyType(current_host, value);
break;
case RP_REGIST_KEY:
CHIAKI_LOGV(this->log, "RP_REGIST_KEY %s", value.c_str());
if(current_host != nullptr)
rp_regist_key_b = this->SetHostRPRegistKey(current_host, value);
break;
case VIDEO_RESOLUTION:
this->SetVideoResolution(current_host, value);
break;
case VIDEO_FPS:
this->SetVideoFPS(current_host, value);
break;
case TARGET:
CHIAKI_LOGV(this->log, "TARGET %s", value.c_str());
if(current_host != nullptr)
this->SetChiakiTarget(current_host, value);
break;
} // ci switch
if(rp_key_b && rp_regist_key_b && rp_key_type_b)
// the current host contains rp key data
current_host->rp_key_data = true;
} // is_open
config_file.close();
}
return;
}
int Settings::WriteFile()
{
std::fstream config_file;
CHIAKI_LOGI(this->log, "Write config file %s", this->filename);
// flush file (trunc)
// the config file is completely overwritten
config_file.open(this->filename, std::fstream::out | std::ofstream::trunc);
std::string line;
std::string value;
if(this->hosts == nullptr)
return -1;
if(config_file.is_open())
{
// save global settings
CHIAKI_LOGD(this->log, "Write Global config file %s", this->filename);
if(this->global_video_resolution)
config_file << "video_resolution = \""
<< this->ResolutionPresetToString(this->GetVideoResolution(nullptr))
<< "\"\n";
if(this->global_video_fps)
config_file << "video_fps = "
<< this->FPSPresetToString(this->GetVideoFPS(nullptr))
<< "\n";
if(this->global_psn_online_id.length())
config_file << "psn_online_id = \"" << this->global_psn_online_id << "\"\n";
if(this->global_psn_account_id.length())
config_file << "psn_account_id = \"" << this->global_psn_account_id << "\"\n";
// write host config in file
// loop over all configured
for(auto it = this->hosts->begin(); it != this->hosts->end(); it++ )
{
// first is std::string
// second is Host
CHIAKI_LOGD(this->log, "Write Host config file %s", it->first.c_str());
config_file << "[" << it->first << "]\n"
<< "host_addr = \"" << it->second.GetHostAddr() << "\"\n"
<< "target = " << it->second.GetChiakiTarget() << "\"\n";
config_file << "target = \"" << it->second.psn_account_id << "\"\n";
if(it->second.video_resolution)
config_file << "video_resolution = \""
<< this->ResolutionPresetToString(this->GetVideoResolution(&it->second))
<< "\"\n";
if(it->second.video_fps)
config_file << "video_fps = "
<< this->FPSPresetToString(this->GetVideoFPS(&it->second))
<< "\n";
if(it->second.psn_online_id.length())
config_file << "psn_online_id = \"" << it->second.psn_online_id << "\"\n";
if(it->second.psn_account_id.length())
config_file << "psn_account_id = \"" << it->second.psn_account_id << "\"\n";
if(it->second.rp_key_data || it->second.registered)
{
char rp_key_type[33] = { 0 };
snprintf(rp_key_type, sizeof(rp_key_type), "%d", it->second.rp_key_type);
// save registered rp key for auto login
config_file << "rp_key = \"" << this->GetHostRPKey(&it->second) << "\"\n"
<< "rp_regist_key = \"" << this->GetHostRPRegistKey(&it->second) << "\"\n"
<< "rp_key_type = " << rp_key_type << "\n";
}
config_file << "\n";
} // for host
} // is_open
config_file.close();
return 0;
}