2048: Use LibConfig instead of Core::ConfigFile
This commit is contained in:
parent
1fa5fba432
commit
666397e1a7
Notes:
sideshowbarker
2024-07-18 05:16:37 +09:00
Author: https://github.com/fluxth 🔰 Commit: https://github.com/SerenityOS/serenity/commit/666397e1a78 Pull-request: https://github.com/SerenityOS/serenity/pull/9614
2 changed files with 21 additions and 27 deletions
|
@ -12,4 +12,4 @@ set(SOURCES
|
|||
)
|
||||
|
||||
serenity_app(2048 ICON app-2048)
|
||||
target_link_libraries(2048 LibGUI)
|
||||
target_link_libraries(2048 LibConfig LibGUI)
|
||||
|
|
|
@ -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 <LibCore/ConfigFile.h>
|
||||
#include <LibConfig/Client.h>
|
||||
#include <LibGUI/Action.h>
|
||||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/BoxLayout.h>
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue