1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:07:45 +00:00

2048: Use LibConfig instead of Core::ConfigFile

This commit is contained in:
Thitat Auareesuksakul 2021-08-26 19:43:42 +07:00 committed by Andreas Kling
parent 1fa5fba432
commit 666397e1a7
2 changed files with 21 additions and 27 deletions

View file

@ -12,4 +12,4 @@ set(SOURCES
) )
serenity_app(2048 ICON app-2048) serenity_app(2048 ICON app-2048)
target_link_libraries(2048 LibGUI) target_link_libraries(2048 LibConfig LibGUI)

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2020, the SerenityOS developers. * Copyright (c) 2020-2021, the SerenityOS developers.
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -7,7 +7,7 @@
#include "BoardView.h" #include "BoardView.h"
#include "Game.h" #include "Game.h"
#include "GameSizeDialog.h" #include "GameSizeDialog.h"
#include <LibCore/ConfigFile.h> #include <LibConfig/Client.h>
#include <LibGUI/Action.h> #include <LibGUI/Action.h>
#include <LibGUI/Application.h> #include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h> #include <LibGUI/BoxLayout.h>
@ -37,22 +37,7 @@ int main(int argc, char** argv)
auto window = GUI::Window::construct(); auto window = GUI::Window::construct();
auto config = Core::ConfigFile::open_for_app("2048", Core::ConfigFile::AllowWriting::Yes); Config::pledge_domains("2048");
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();
if (pledge("stdio rpath recvfd sendfd wpath cpath", nullptr) < 0) { if (pledge("stdio rpath recvfd sendfd wpath cpath", nullptr) < 0) {
perror("pledge"); perror("pledge");
@ -64,7 +49,7 @@ int main(int argc, char** argv)
return 1; return 1;
} }
if (unveil(config->filename().characters(), "crw") < 0) { if (unveil("/tmp/portal/config", "rw") < 0) {
perror("unveil"); perror("unveil");
return 1; return 1;
} }
@ -74,6 +59,19 @@ int main(int argc, char** argv)
return 1; 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_double_buffering_enabled(false);
window->set_title("2048"); window->set_title("2048");
window->resize(315, 336); window->resize(315, 336);
@ -121,14 +119,10 @@ int main(int argc, char** argv)
if (!size_dialog->temporary()) { if (!size_dialog->temporary()) {
config->write_num_entry("", "board_size", board_size); Config::write_i32("2048", "", "board_size", board_size);
config->write_num_entry("", "target_tile", target_tile); Config::write_i32("2048", "", "target_tile", target_tile);
config->write_bool_entry("", "evil_ai", evil_ai); 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); 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; return;
} }