From 998d3a98ecf2af77d42959b90c010496d4cd0627 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Thu, 26 Aug 2021 20:28:21 -0400 Subject: Spider: Use LibConfig instead of Core::ConfigFile There was a lot of error handling here previously when writing to a config file failed, but this was removed since we have no way of conveying a `Config::write` failure from the ConfigServer. --- Userland/Games/Spider/CMakeLists.txt | 2 +- Userland/Games/Spider/main.cpp | 51 +++++++++++------------------------- 2 files changed, 17 insertions(+), 36 deletions(-) (limited to 'Userland') diff --git a/Userland/Games/Spider/CMakeLists.txt b/Userland/Games/Spider/CMakeLists.txt index 29a5fd9e81..d7fa9dfcce 100644 --- a/Userland/Games/Spider/CMakeLists.txt +++ b/Userland/Games/Spider/CMakeLists.txt @@ -13,4 +13,4 @@ set(SOURCES ) serenity_app(Spider ICON app-spider) -target_link_libraries(Spider LibCards LibGUI LibGfx LibCore) +target_link_libraries(Spider LibCards LibGUI LibGfx LibCore LibConfig) diff --git a/Userland/Games/Spider/main.cpp b/Userland/Games/Spider/main.cpp index 50e910af39..2cc37f4cac 100644 --- a/Userland/Games/Spider/main.cpp +++ b/Userland/Games/Spider/main.cpp @@ -1,12 +1,13 @@ /* * Copyright (c) 2021, Jamie Mansfield + * Copyright (c) 2021, Mustafa Quraish * * SPDX-License-Identifier: BSD-2-Clause */ #include "Game.h" #include -#include +#include #include #include #include @@ -39,7 +40,8 @@ int main(int argc, char** argv) { auto app = GUI::Application::construct(argc, argv); auto app_icon = GUI::Icon::default_icon("app-spider"); - auto config = Core::ConfigFile::open_for_app("Spider", Core::ConfigFile::AllowWriting::Yes); + + Config::pledge_domains("Spider"); if (pledge("stdio recvfd sendfd rpath wpath cpath", nullptr) < 0) { perror("pledge"); @@ -51,11 +53,6 @@ int main(int argc, char** argv) return 1; } - if (unveil(config->filename().characters(), "crw") < 0) { - perror("unveil"); - return 1; - } - if (unveil(nullptr, nullptr) < 0) { perror("unveil"); return 1; @@ -64,13 +61,11 @@ int main(int argc, char** argv) auto window = GUI::Window::construct(); window->set_title("Spider"); - auto mode = static_cast(config->read_num_entry("Settings", "Mode", static_cast(Spider::Mode::SingleSuit))); + auto mode = static_cast(Config::read_i32("Spider", "Settings", "Mode", static_cast(Spider::Mode::SingleSuit))); auto update_mode = [&](Spider::Mode new_mode) { mode = new_mode; - config->write_num_entry("Settings", "Mode", static_cast(mode)); - if (!config->sync()) - GUI::MessageBox::show(window, "Configuration could not be saved", "Error", GUI::MessageBox::Type::Error); + Config::write_i32("Spider", "Settings", "Mode", static_cast(mode)); }; auto mode_id = [&]() { @@ -84,54 +79,40 @@ int main(int argc, char** argv) } }; - auto statistic_display = static_cast(config->read_num_entry("Settings", "StatisticDisplay", static_cast(StatisticDisplay::HighScore))); + auto statistic_display = static_cast(Config::read_i32("Spider", "Settings", "StatisticDisplay", static_cast(StatisticDisplay::HighScore))); auto update_statistic_display = [&](StatisticDisplay new_statistic_display) { statistic_display = new_statistic_display; - config->write_num_entry("Settings", "StatisticDisplay", static_cast(statistic_display)); - if (!config->sync()) - GUI::MessageBox::show(window, "Configuration could not be saved", "Error", GUI::MessageBox::Type::Error); + Config::write_i32("Spider", "Settings", "StatisticDisplay", static_cast(statistic_display)); }; auto high_score = [&]() { - return static_cast(config->read_num_entry("HighScores", mode_id(), 0)); + return static_cast(Config::read_i32("Spider", "HighScores", mode_id(), 0)); }; auto update_high_score = [&](u32 new_high_score) { - config->write_num_entry("HighScores", mode_id(), static_cast(new_high_score)); - - if (!config->sync()) - GUI::MessageBox::show(window, "Configuration could not be saved", "Error", GUI::MessageBox::Type::Error); + Config::write_i32("Spider", "HighScores", mode_id(), static_cast(new_high_score)); }; auto best_time = [&]() { - return static_cast(config->read_num_entry("BestTimes", mode_id(), 0)); + return static_cast(Config::read_i32("Spider", "BestTimes", mode_id(), 0)); }; auto update_best_time = [&](u32 new_best_time) { - config->write_num_entry("BestTimes", mode_id(), static_cast(new_best_time)); - - if (!config->sync()) - GUI::MessageBox::show(window, "Configuration could not be saved", "Error", GUI::MessageBox::Type::Error); + Config::write_i32("Spider", "BestTimes", mode_id(), static_cast(new_best_time)); }; auto total_wins = [&]() { - return static_cast(config->read_num_entry("TotalWins", mode_id(), 0)); + return static_cast(Config::read_i32("Spider", "TotalWins", mode_id(), 0)); }; auto increment_total_wins = [&]() { - config->write_num_entry("TotalWins", mode_id(), static_cast(total_wins() + 1)); - - if (!config->sync()) - GUI::MessageBox::show(window, "Configuration could not be saved", "Error", GUI::MessageBox::Type::Error); + Config::write_i32("Spider", "TotalWins", mode_id(), static_cast(total_wins() + 1)); }; auto total_losses = [&]() { - return static_cast(config->read_num_entry("TotalLosses", mode_id(), 0)); + return static_cast(Config::read_i32("Spider", "TotalLosses", mode_id(), 0)); }; auto increment_total_losses = [&]() { - config->write_num_entry("TotalLosses", mode_id(), static_cast(total_losses() + 1)); - - if (!config->sync()) - GUI::MessageBox::show(window, "Configuration could not be saved", "Error", GUI::MessageBox::Type::Error); + Config::write_i32("Spider", "TotalLosses", mode_id(), static_cast(total_losses() + 1)); }; if (mode >= Spider::Mode::__Count) -- cgit v1.2.3