cleanup the settings parser code, split into functions

This commit is contained in:
otavepto 2023-12-20 17:30:06 +02:00 committed by otavepto
parent cf41523751
commit e28752420a

View File

@ -271,12 +271,9 @@ static void load_gamecontroller_settings(Settings *settings)
settings->glyphs_directory = path + (PATH_SEPARATOR "glyphs" PATH_SEPARATOR);
}
uint32 create_localstorage_settings(Settings **settings_client_out, Settings **settings_server_out, Local_Storage **local_storage_out)
// steam_appid.txt
uint32 parse_steam_app_id(std::string &program_path)
{
std::string program_path = Local_Storage::get_program_path(), save_path = Local_Storage::get_user_appdata_path();;
PRINT_DEBUG("Current Path %s save_path: %s\n", program_path.c_str(), save_path.c_str());
char array[10] = {};
array[0] = '0';
Local_Storage::get_file_data(Local_Storage::get_game_settings_path() + "steam_appid.txt", array, sizeof(array) - 1);
@ -349,21 +346,25 @@ uint32 create_localstorage_settings(Settings **settings_client_out, Settings **s
}
}
return appid;
}
// local_save.txt
bool parse_local_save(std::string &program_path, std::string &save_path)
{
bool local_save = false;
{
char array[256] = {};
if (Local_Storage::get_file_data(program_path + "local_save.txt", array, sizeof(array) - 1) != -1) {
save_path = program_path + Settings::sanitize(array);
local_save = true;
}
return local_save;
}
PRINT_DEBUG("Set save_path: %s\n", save_path.c_str());
Local_Storage *local_storage = new Local_Storage(save_path);
local_storage->setAppId(appid);
// Listen port
// listen_port.txt
uint16 parse_listen_port(class Local_Storage *local_storage)
{
char array_port[10] = {};
array_port[0] = '0';
local_storage->get_data_settings("listen_port.txt", array_port, sizeof(array_port) - 1);
@ -373,28 +374,23 @@ uint32 create_localstorage_settings(Settings **settings_client_out, Settings **s
snprintf(array_port, sizeof(array_port), "%hu", port);
local_storage->store_data_settings("listen_port.txt", array_port, strlen(array_port));
}
return port;
}
// Custom broadcasts
std::set<IP_PORT> custom_broadcasts;
load_custom_broadcasts(local_storage->get_global_settings_path() + "custom_broadcasts.txt", custom_broadcasts);
load_custom_broadcasts(Local_Storage::get_game_settings_path() + "custom_broadcasts.txt", custom_broadcasts);
// Acount name
char name[32] = {};
// account_name.txt
std::string parse_account_name(class Local_Storage *local_storage)
{
char name[100] = {};
if (local_storage->get_data_settings("account_name.txt", name, sizeof(name) - 1) <= 0) {
strcpy(name, DEFAULT_NAME);
local_storage->store_data_settings("account_name.txt", name, strlen(name));
}
// Language
char language[32] = {};
if (local_storage->get_data_settings("language.txt", language, sizeof(language) - 1) <= 0) {
strcpy(language, DEFAULT_LANGUAGE);
local_storage->store_data_settings("language.txt", language, strlen(language));
return std::string(name);
}
// Steam ID
// user_steam_id.txt
CSteamID parse_user_steam_id(class Local_Storage *local_storage)
{
char array_steam_id[32] = {};
CSteamID user_id;
uint64 steam_id = 0;
@ -429,9 +425,26 @@ uint32 create_localstorage_settings(Settings **settings_client_out, Settings **s
local_storage->store_data_settings("user_steam_id.txt", temp_text, strlen(temp_text));
}
return user_id;
}
// language.txt
std::string parse_current_language(class Local_Storage *local_storage)
{
char language[64] = {};
if (local_storage->get_data_settings("language.txt", language, sizeof(language) - 1) <= 0) {
strcpy(language, DEFAULT_LANGUAGE);
local_storage->store_data_settings("language.txt", language, strlen(language));
}
return std::string(language);
}
// supported_languages.txt
std::set<std::string> parse_supported_languages(class Local_Storage *local_storage, std::string &language)
{
std::set<std::string> supported_languages;
{
std::string lang_config_path = Local_Storage::get_game_settings_path() + "supported_languages.txt";
std::ifstream input( utf8_decode(lang_config_path) );
@ -456,143 +469,19 @@ uint32 create_localstorage_settings(Settings **settings_client_out, Settings **s
}
}
// if the current emu language is not in the supported languages list
if (!supported_languages.count(language)) {
// and the supported languages list wasn't empty
if (first_language.size()) {
memset(language, 0, sizeof(language));
first_language.copy(language, sizeof(language) - 1);
}
language = first_language;
}
}
bool steam_offline_mode = false;
bool steam_deck_mode = false;
bool steamhttp_online_mode = false;
bool disable_networking = false;
bool disable_overlay = false;
bool disable_overlay_achievement_notification = false;
bool disable_overlay_friend_notification = false;
bool disable_overlay_warning = false;
bool disable_lobby_creation = false;
bool disable_source_query = false;
bool disable_account_avatar = false;
bool achievement_bypass = false;
bool is_beta_branch = false;
int build_id = 10;
bool warn_forced = false;
{
std::string steam_settings_path = Local_Storage::get_game_settings_path();
std::vector<std::string> paths = Local_Storage::get_filenames_path(steam_settings_path);
for (auto & p: paths) {
PRINT_DEBUG("steam settings path %s\n", p.c_str());
if (p == "offline.txt") {
steam_offline_mode = true;
} else if (p == "steam_deck.txt") {
steam_deck_mode = true;
} else if (p == "http_online.txt") {
steamhttp_online_mode = true;
} else if (p == "disable_networking.txt") {
disable_networking = true;
} else if (p == "disable_overlay.txt") {
disable_overlay = true;
} else if (p == "disable_overlay_achievement_notification.txt") {
disable_overlay_achievement_notification = true;
} else if (p == "disable_overlay_friend_notification.txt") {
disable_overlay_friend_notification = true;
} else if (p == "disable_overlay_warning.txt") {
disable_overlay_warning = true;
} else if (p == "disable_lobby_creation.txt") {
disable_lobby_creation = true;
} else if (p == "disable_source_query.txt") {
disable_source_query = true;
} else if (p == "disable_account_avatar.txt") {
disable_account_avatar = true;
} else if (p == "achievements_bypass.txt") {
achievement_bypass = true;
} else if (p == "is_beta_branch.txt") {
is_beta_branch = true;
} else if (p == "force_language.txt") {
int len = Local_Storage::get_file_data(steam_settings_path + "force_language.txt", language, sizeof(language) - 1);
if (len > 0) {
language[len] = 0;
warn_forced = true;
}
} else if (p == "force_steamid.txt") {
char steam_id_text[32] = {};
if (Local_Storage::get_file_data(steam_settings_path + "force_steamid.txt", steam_id_text, sizeof(steam_id_text) - 1) > 0) {
CSteamID temp_id = CSteamID((uint64)std::atoll(steam_id_text));
if (temp_id.IsValid()) {
user_id = temp_id;
warn_forced = true;
}
}
} else if (p == "force_account_name.txt") {
int len = Local_Storage::get_file_data(steam_settings_path + "force_account_name.txt", name, sizeof(name) - 1);
if (len > 0) {
name[len] = 0;
warn_forced = true;
}
} else if (p == "force_listen_port.txt") {
char array_port[10] = {};
int len = Local_Storage::get_file_data(steam_settings_path + "force_listen_port.txt", array_port, sizeof(array_port) - 1);
if (len > 0) {
port = std::stoi(array_port);
warn_forced = true;
}
} else if (p == "build_id.txt") {
char array_id[10] = {};
int len = Local_Storage::get_file_data(steam_settings_path + "build_id.txt", array_id, sizeof(array_id) - 1);
if (len > 0) build_id = std::stoi(array_id);
}
}
}
Settings *settings_client = new Settings(user_id, CGameID(appid), name, language, steam_offline_mode);
Settings *settings_server = new Settings(generate_steam_id_server(), CGameID(appid), name, language, steam_offline_mode);
settings_client->set_port(port);
settings_server->set_port(port);
settings_client->custom_broadcasts = custom_broadcasts;
settings_server->custom_broadcasts = custom_broadcasts;
settings_client->disable_networking = disable_networking;
settings_server->disable_networking = disable_networking;
settings_client->disable_overlay = disable_overlay;
settings_server->disable_overlay = disable_overlay;
settings_client->disable_overlay_achievement_notification = disable_overlay_achievement_notification;
settings_server->disable_overlay_achievement_notification = disable_overlay_achievement_notification;
settings_client->disable_overlay_friend_notification = disable_overlay_friend_notification;
settings_server->disable_overlay_friend_notification = disable_overlay_friend_notification;
settings_client->disable_overlay_warning = disable_overlay_warning;
settings_server->disable_overlay_warning = disable_overlay_warning;
settings_client->disable_lobby_creation = disable_lobby_creation;
settings_server->disable_lobby_creation = disable_lobby_creation;
settings_client->disable_source_query = disable_source_query;
settings_server->disable_source_query = disable_source_query;
settings_client->disable_account_avatar = disable_account_avatar;
settings_server->disable_account_avatar = disable_account_avatar;
settings_client->build_id = build_id;
settings_server->build_id = build_id;
settings_client->warn_forced = warn_forced;
settings_server->warn_forced = warn_forced;
settings_client->warn_local_save = local_save;
settings_server->warn_local_save = local_save;
settings_client->supported_languages = supported_languages;
settings_server->supported_languages = supported_languages;
settings_client->steam_deck = steam_deck_mode;
settings_server->steam_deck = steam_deck_mode;
settings_client->http_online = steamhttp_online_mode;
settings_server->http_online = steamhttp_online_mode;
settings_client->achievement_bypass = achievement_bypass;
settings_server->achievement_bypass = achievement_bypass;
settings_client->is_beta_branch = is_beta_branch;
settings_server->is_beta_branch = is_beta_branch;
if (local_save) {
settings_client->local_save = save_path;
settings_server->local_save = save_path;
return supported_languages;
}
// DLC.txt
static void parse_dlc(class Settings *settings_client, Settings *settings_server)
{
std::string dlc_config_path = Local_Storage::get_game_settings_path() + "DLC.txt";
std::ifstream input( utf8_decode(dlc_config_path) );
@ -635,6 +524,8 @@ uint32 create_localstorage_settings(Settings **settings_client_out, Settings **s
}
}
// app_paths.txt
static void parse_app_paths(class Settings *settings_client, Settings *settings_server, std::string &program_path)
{
std::string dlc_config_path = Local_Storage::get_game_settings_path() + "app_paths.txt";
std::ifstream input( utf8_decode(dlc_config_path) );
@ -668,8 +559,11 @@ uint32 create_localstorage_settings(Settings **settings_client_out, Settings **s
}
}
}
}
// leaderboards.txt
static void parse_leaderboards(class Settings *settings_client, Settings *settings_server)
{
std::string dlc_config_path = Local_Storage::get_game_settings_path() + "leaderboards.txt";
std::ifstream input( utf8_decode(dlc_config_path) );
@ -710,8 +604,11 @@ uint32 create_localstorage_settings(Settings **settings_client_out, Settings **s
}
}
}
}
// stats.txt
static void parse_stats(class Settings *settings_client, Settings *settings_server)
{
std::string stats_config_path = Local_Storage::get_game_settings_path() + "stats.txt";
std::ifstream input( utf8_decode(stats_config_path) );
@ -775,8 +672,11 @@ uint32 create_localstorage_settings(Settings **settings_client_out, Settings **s
}
}
}
}
// depots.txt
static void parse_depots(class Settings *settings_client, Settings *settings_server)
{
std::string depots_config_path = Local_Storage::get_game_settings_path() + "depots.txt";
std::ifstream input( utf8_decode(depots_config_path) );
@ -799,8 +699,11 @@ uint32 create_localstorage_settings(Settings **settings_client_out, Settings **s
} catch (...) {}
}
}
}
// subscribed_groups.txt
static void parse_subscribed_groups(class Settings *settings_client, Settings *settings_server)
{
std::string depots_config_path = Local_Storage::get_game_settings_path() + "subscribed_groups.txt";
std::ifstream input( utf8_decode(depots_config_path) );
@ -823,8 +726,11 @@ uint32 create_localstorage_settings(Settings **settings_client_out, Settings **s
} catch (...) {}
}
}
}
// installed_app_ids.txt
static void parse_installed_app_Ids(class Settings *settings_client, Settings *settings_server)
{
std::string installed_apps_list_path = Local_Storage::get_game_settings_path() + "installed_app_ids.txt";
std::ifstream input( utf8_decode(installed_apps_list_path) );
@ -854,8 +760,11 @@ uint32 create_localstorage_settings(Settings **settings_client_out, Settings **s
settings_server->assume_any_app_installed = true;
PRINT_DEBUG("Assuming any app is installed\n");
}
}
// force_branch_name.txt
static void parse_force_branch_name(class Settings *settings_client, Settings *settings_server)
{
std::string installed_apps_list_path = Local_Storage::get_game_settings_path() + "force_branch_name.txt";
std::ifstream input( utf8_decode(installed_apps_list_path) );
@ -879,12 +788,8 @@ uint32 create_localstorage_settings(Settings **settings_client_out, Settings **s
}
}
load_subscribed_groups_clans(local_storage->get_global_settings_path() + "subscribed_groups_clans.txt", settings_client, settings_server);
load_subscribed_groups_clans(Local_Storage::get_game_settings_path() + "subscribed_groups_clans.txt", settings_client, settings_server);
load_overlay_appearance(local_storage->get_global_settings_path() + "overlay_appearance.txt", settings_client, settings_server);
load_overlay_appearance(Local_Storage::get_game_settings_path() + "overlay_appearance.txt", settings_client, settings_server);
// steam_settings/mods
static void parse_mods_folder(class Settings *settings_client, Settings *settings_server, class Local_Storage *local_storage)
{
std::string mod_path = Local_Storage::get_game_settings_path() + "mods";
nlohmann::json mod_items = nlohmann::json::object();
@ -963,8 +868,250 @@ uint32 create_localstorage_settings(Settings **settings_client_out, Settings **s
} catch (...) {}
}
}
}
// force_language.txt
static bool parse_force_language(std::string &language, std::string &steam_settings_path)
{
bool warn_forced = false;
char forced_language[64] = {};
int len = Local_Storage::get_file_data(steam_settings_path + "force_language.txt", forced_language, sizeof(forced_language) - 1);
if (len > 0) {
forced_language[len] = 0;
warn_forced = true;
}
language = std::string(forced_language);
return warn_forced;
}
// force_steamid.txt
static bool parse_force_user_steam_id(CSteamID &user_id, std::string &steam_settings_path)
{
bool warn_forced = false;
char steam_id_text[32] = {};
if (Local_Storage::get_file_data(steam_settings_path + "force_steamid.txt", steam_id_text, sizeof(steam_id_text) - 1) > 0) {
CSteamID temp_id = CSteamID((uint64)std::atoll(steam_id_text));
if (temp_id.IsValid()) {
user_id = temp_id;
warn_forced = true;
}
}
return warn_forced;
}
// force_account_name.txt
static bool parse_force_account_name(std::string &name, std::string &steam_settings_path)
{
bool warn_forced = false;
char forced_name[100] = {};
int len = Local_Storage::get_file_data(steam_settings_path + "force_account_name.txt", forced_name, sizeof(forced_name) - 1);
if (len > 0) {
forced_name[len] = 0;
warn_forced = true;
}
name = std::string(forced_name);
return warn_forced;
}
// force_listen_port.txt
static bool parse_force_listen_port(uint16 &port, std::string &steam_settings_path)
{
bool warn_forced = false;
char array_port[10] = {};
int len = Local_Storage::get_file_data(steam_settings_path + "force_listen_port.txt", array_port, sizeof(array_port) - 1);
if (len > 0) {
port = std::stoi(array_port);
warn_forced = true;
}
return warn_forced;
}
// build_id.txt
static void parse_build_id(int &build_id, std::string &steam_settings_path)
{
char array_id[10] = {};
int len = Local_Storage::get_file_data(steam_settings_path + "build_id.txt", array_id, sizeof(array_id) - 1);
if (len > 0) {
build_id = std::stoi(array_id);
}
}
uint32 create_localstorage_settings(Settings **settings_client_out, Settings **settings_server_out, Local_Storage **local_storage_out)
{
std::string program_path = Local_Storage::get_program_path();
std::string save_path = Local_Storage::get_user_appdata_path();
PRINT_DEBUG("Current Path %s save_path: %s\n", program_path.c_str(), save_path.c_str());
uint32 appid = parse_steam_app_id(program_path);
bool local_save = parse_local_save(program_path, save_path);
PRINT_DEBUG("Set save_path: %s\n", save_path.c_str());
Local_Storage *local_storage = new Local_Storage(save_path);
local_storage->setAppId(appid);
// Listen port
uint16 port = parse_listen_port(local_storage);
// Custom broadcasts
std::set<IP_PORT> custom_broadcasts;
load_custom_broadcasts(local_storage->get_global_settings_path() + "custom_broadcasts.txt", custom_broadcasts);
load_custom_broadcasts(Local_Storage::get_game_settings_path() + "custom_broadcasts.txt", custom_broadcasts);
// Acount name
std::string name = parse_account_name(local_storage);
// Steam ID
CSteamID user_id = parse_user_steam_id(local_storage);
// Language
std::string language = parse_current_language(local_storage);
// Supported languages, this will change the current language if needed
std::set<std::string> supported_languages = parse_supported_languages(local_storage, language);
bool steam_offline_mode = false;
bool steam_deck_mode = false;
bool steamhttp_online_mode = false;
bool disable_networking = false;
bool disable_overlay = false;
bool disable_overlay_achievement_notification = false;
bool disable_overlay_friend_notification = false;
bool disable_overlay_warning = false;
bool disable_lobby_creation = false;
bool disable_source_query = false;
bool disable_account_avatar = false;
bool achievement_bypass = false;
bool is_beta_branch = false;
int build_id = 10;
bool warn_forced = false;
// boolean flags and forced configurations
{
std::string steam_settings_path = Local_Storage::get_game_settings_path();
std::vector<std::string> paths = Local_Storage::get_filenames_path(steam_settings_path);
for (auto & p: paths) {
PRINT_DEBUG("steam settings path %s\n", p.c_str());
if (p == "offline.txt") {
steam_offline_mode = true;
} else if (p == "steam_deck.txt") {
steam_deck_mode = true;
} else if (p == "http_online.txt") {
steamhttp_online_mode = true;
} else if (p == "disable_networking.txt") {
disable_networking = true;
} else if (p == "disable_overlay.txt") {
disable_overlay = true;
} else if (p == "disable_overlay_achievement_notification.txt") {
disable_overlay_achievement_notification = true;
} else if (p == "disable_overlay_friend_notification.txt") {
disable_overlay_friend_notification = true;
} else if (p == "disable_overlay_warning.txt") {
disable_overlay_warning = true;
} else if (p == "disable_lobby_creation.txt") {
disable_lobby_creation = true;
} else if (p == "disable_source_query.txt") {
disable_source_query = true;
} else if (p == "disable_account_avatar.txt") {
disable_account_avatar = true;
} else if (p == "achievements_bypass.txt") {
achievement_bypass = true;
} else if (p == "is_beta_branch.txt") {
is_beta_branch = true;
} else if (p == "force_language.txt") {
warn_forced = parse_force_language(language, steam_settings_path);
} else if (p == "force_steamid.txt") {
warn_forced = parse_force_user_steam_id(user_id, steam_settings_path);
} else if (p == "force_account_name.txt") {
warn_forced = parse_force_account_name(name, steam_settings_path);
} else if (p == "force_listen_port.txt") {
warn_forced = parse_force_listen_port(port, steam_settings_path);
} else if (p == "build_id.txt") {
parse_build_id(build_id, steam_settings_path);
}
}
}
Settings *settings_client = new Settings(user_id, CGameID(appid), name, language, steam_offline_mode);
Settings *settings_server = new Settings(generate_steam_id_server(), CGameID(appid), name, language, steam_offline_mode);
settings_client->set_port(port);
settings_server->set_port(port);
settings_client->custom_broadcasts = custom_broadcasts;
settings_server->custom_broadcasts = custom_broadcasts;
settings_client->disable_networking = disable_networking;
settings_server->disable_networking = disable_networking;
settings_client->disable_overlay = disable_overlay;
settings_server->disable_overlay = disable_overlay;
settings_client->disable_overlay_achievement_notification = disable_overlay_achievement_notification;
settings_server->disable_overlay_achievement_notification = disable_overlay_achievement_notification;
settings_client->disable_overlay_friend_notification = disable_overlay_friend_notification;
settings_server->disable_overlay_friend_notification = disable_overlay_friend_notification;
settings_client->disable_overlay_warning = disable_overlay_warning;
settings_server->disable_overlay_warning = disable_overlay_warning;
settings_client->disable_lobby_creation = disable_lobby_creation;
settings_server->disable_lobby_creation = disable_lobby_creation;
settings_client->disable_source_query = disable_source_query;
settings_server->disable_source_query = disable_source_query;
settings_client->disable_account_avatar = disable_account_avatar;
settings_server->disable_account_avatar = disable_account_avatar;
settings_client->build_id = build_id;
settings_server->build_id = build_id;
settings_client->warn_forced = warn_forced;
settings_server->warn_forced = warn_forced;
settings_client->warn_local_save = local_save;
settings_server->warn_local_save = local_save;
settings_client->supported_languages = supported_languages;
settings_server->supported_languages = supported_languages;
settings_client->steam_deck = steam_deck_mode;
settings_server->steam_deck = steam_deck_mode;
settings_client->http_online = steamhttp_online_mode;
settings_server->http_online = steamhttp_online_mode;
settings_client->achievement_bypass = achievement_bypass;
settings_server->achievement_bypass = achievement_bypass;
settings_client->is_beta_branch = is_beta_branch;
settings_server->is_beta_branch = is_beta_branch;
if (local_save) {
settings_client->local_save = save_path;
settings_server->local_save = save_path;
}
parse_dlc(settings_client, settings_server);
parse_app_paths(settings_client, settings_server, program_path);
parse_leaderboards(settings_client, settings_server);
parse_stats(settings_client, settings_server);
parse_depots(settings_client, settings_server);
parse_subscribed_groups(settings_client, settings_server);
parse_installed_app_Ids(settings_client, settings_server);
parse_force_branch_name(settings_client, settings_server);
load_subscribed_groups_clans(local_storage->get_global_settings_path() + "subscribed_groups_clans.txt", settings_client, settings_server);
load_subscribed_groups_clans(Local_Storage::get_game_settings_path() + "subscribed_groups_clans.txt", settings_client, settings_server);
load_overlay_appearance(local_storage->get_global_settings_path() + "overlay_appearance.txt", settings_client, settings_server);
load_overlay_appearance(Local_Storage::get_game_settings_path() + "overlay_appearance.txt", settings_client, settings_server);
parse_mods_folder(settings_client, settings_server, local_storage);
load_gamecontroller_settings(settings_client);
*settings_client_out = settings_client;