diff --git a/Userland/Games/2048/CMakeLists.txt b/Userland/Games/2048/CMakeLists.txt index d8f9bb2e7b..e31191c05a 100644 --- a/Userland/Games/2048/CMakeLists.txt +++ b/Userland/Games/2048/CMakeLists.txt @@ -12,4 +12,4 @@ set(SOURCES ) serenity_app(2048 ICON app-2048) -target_link_libraries(2048 LibGUI) +target_link_libraries(2048 LibConfig LibGUI) diff --git a/Userland/Games/2048/main.cpp b/Userland/Games/2048/main.cpp index 1182b97223..a8a4c0a604 100644 --- a/Userland/Games/2048/main.cpp +++ b/Userland/Games/2048/main.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, the SerenityOS developers. + * Copyright (c) 2020-2021, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ @@ -7,7 +7,7 @@ #include "BoardView.h" #include "Game.h" #include "GameSizeDialog.h" -#include +#include #include #include #include @@ -37,22 +37,7 @@ int main(int argc, char** argv) auto window = GUI::Window::construct(); - auto config = Core::ConfigFile::open_for_app("2048", Core::ConfigFile::AllowWriting::Yes); - - size_t board_size = config->read_num_entry("", "board_size", 4); - u32 target_tile = config->read_num_entry("", "target_tile", 2048); - bool evil_ai = config->read_bool_entry("", "evil_ai", false); - - if ((target_tile & (target_tile - 1)) != 0) { - // If the target tile is not a power of 2, reset to its default value. - target_tile = 2048; - } - - config->write_num_entry("", "board_size", board_size); - config->write_num_entry("", "target_tile", target_tile); - config->write_bool_entry("", "evil_ai", evil_ai); - - config->sync(); + Config::pledge_domains("2048"); if (pledge("stdio rpath recvfd sendfd wpath cpath", nullptr) < 0) { perror("pledge"); @@ -64,7 +49,7 @@ int main(int argc, char** argv) return 1; } - if (unveil(config->filename().characters(), "crw") < 0) { + if (unveil("/tmp/portal/config", "rw") < 0) { perror("unveil"); return 1; } @@ -74,6 +59,19 @@ int main(int argc, char** argv) return 1; } + size_t board_size = Config::read_i32("2048", "", "board_size", 4); + u32 target_tile = Config::read_i32("2048", "", "target_tile", 2048); + bool evil_ai = Config::read_bool("2048", "", "evil_ai", false); + + if ((target_tile & (target_tile - 1)) != 0) { + // If the target tile is not a power of 2, reset to its default value. + target_tile = 2048; + } + + Config::write_i32("2048", "", "board_size", board_size); + Config::write_i32("2048", "", "target_tile", target_tile); + Config::write_bool("2048", "", "evil_ai", evil_ai); + window->set_double_buffering_enabled(false); window->set_title("2048"); window->resize(315, 336); @@ -121,14 +119,10 @@ int main(int argc, char** argv) if (!size_dialog->temporary()) { - config->write_num_entry("", "board_size", board_size); - config->write_num_entry("", "target_tile", target_tile); - config->write_bool_entry("", "evil_ai", evil_ai); + Config::write_i32("2048", "", "board_size", board_size); + Config::write_i32("2048", "", "target_tile", target_tile); + Config::write_bool("2048", "", "evil_ai", evil_ai); - if (!config->sync()) { - GUI::MessageBox::show(window, "Configuration could not be synced", "Error", GUI::MessageBox::Type::Error); - return; - } GUI::MessageBox::show(window, "New settings have been saved and will be applied on a new game", "Settings Changed Successfully", GUI::MessageBox::Type::Information); return; }