mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:07:45 +00:00
Chess: Allow stockfish to be used if available
The stockfish chess engine can now be selected from the engine menu if the port has been installed.
This commit is contained in:
parent
56dde3df54
commit
79a204a56a
2 changed files with 51 additions and 19 deletions
|
@ -13,4 +13,4 @@ set(SOURCES
|
||||||
)
|
)
|
||||||
|
|
||||||
serenity_app(Chess ICON app-chess)
|
serenity_app(Chess ICON app-chess)
|
||||||
target_link_libraries(Chess PRIVATE LibChess LibConfig LibFileSystemAccessClient LibGfx LibGUI LibCore LibMain LibDesktop)
|
target_link_libraries(Chess PRIVATE LibChess LibConfig LibFileSystem LibFileSystemAccessClient LibGfx LibGUI LibCore LibMain LibDesktop)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, the SerenityOS developers.
|
* Copyright (c) 2020, the SerenityOS developers.
|
||||||
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
||||||
|
* Copyright (c) 2023, Tim Ledbetter <timledbetter@gmail.com>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -9,6 +10,7 @@
|
||||||
#include <LibConfig/Client.h>
|
#include <LibConfig/Client.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
#include <LibDesktop/Launcher.h>
|
#include <LibDesktop/Launcher.h>
|
||||||
|
#include <LibFileSystem/FileSystem.h>
|
||||||
#include <LibFileSystemAccessClient/Client.h>
|
#include <LibFileSystemAccessClient/Client.h>
|
||||||
#include <LibGUI/ActionGroup.h>
|
#include <LibGUI/ActionGroup.h>
|
||||||
#include <LibGUI/Application.h>
|
#include <LibGUI/Application.h>
|
||||||
|
@ -21,6 +23,32 @@
|
||||||
#include <LibGUI/Window.h>
|
#include <LibGUI/Window.h>
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
|
|
||||||
|
struct EngineDetails {
|
||||||
|
StringView command;
|
||||||
|
StringView name { command };
|
||||||
|
String path {};
|
||||||
|
};
|
||||||
|
|
||||||
|
static Vector<EngineDetails> s_all_engines {
|
||||||
|
{ "ChessEngine"sv },
|
||||||
|
{ "stockfish"sv, "Stockfish"sv },
|
||||||
|
};
|
||||||
|
|
||||||
|
static ErrorOr<Vector<EngineDetails>> available_engines()
|
||||||
|
{
|
||||||
|
Vector<EngineDetails> available_engines;
|
||||||
|
for (auto& engine : s_all_engines) {
|
||||||
|
auto path_or_error = FileSystem::resolve_executable_from_environment(engine.command);
|
||||||
|
if (path_or_error.is_error())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
engine.path = path_or_error.release_value();
|
||||||
|
TRY(available_engines.try_append(engine));
|
||||||
|
}
|
||||||
|
|
||||||
|
return available_engines;
|
||||||
|
}
|
||||||
|
|
||||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
TRY(Core::System::pledge("stdio rpath recvfd sendfd thread proc exec unix"));
|
TRY(Core::System::pledge("stdio rpath recvfd sendfd thread proc exec unix"));
|
||||||
|
@ -38,8 +66,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
auto window = TRY(GUI::Window::try_create());
|
auto window = TRY(GUI::Window::try_create());
|
||||||
auto widget = TRY(window->set_main_widget<ChessWidget>());
|
auto widget = TRY(window->set_main_widget<ChessWidget>());
|
||||||
|
|
||||||
|
auto engines = TRY(available_engines());
|
||||||
|
for (auto const& engine : engines)
|
||||||
|
TRY(Core::System::unveil(engine.path, "x"sv));
|
||||||
|
|
||||||
TRY(Core::System::unveil("/res", "r"));
|
TRY(Core::System::unveil("/res", "r"));
|
||||||
TRY(Core::System::unveil("/bin/ChessEngine", "x"));
|
|
||||||
TRY(Core::System::unveil("/bin/GamesSettings", "x"));
|
TRY(Core::System::unveil("/bin/GamesSettings", "x"));
|
||||||
TRY(Core::System::unveil("/tmp/session/%sid/portal/launch", "rw"));
|
TRY(Core::System::unveil("/tmp/session/%sid/portal/launch", "rw"));
|
||||||
TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
|
TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
|
||||||
|
@ -135,8 +166,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
engines_action_group.add_action(human_engine_checkbox);
|
engines_action_group.add_action(human_engine_checkbox);
|
||||||
TRY(engine_submenu->try_add_action(human_engine_checkbox));
|
TRY(engine_submenu->try_add_action(human_engine_checkbox));
|
||||||
|
|
||||||
auto action = GUI::Action::create_checkable("ChessEngine", [&](auto& action) {
|
for (auto const& engine : engines) {
|
||||||
auto new_engine = Engine::construct(action.text());
|
auto action = GUI::Action::create_checkable(engine.name, [&](auto&) {
|
||||||
|
auto new_engine = Engine::construct(engine.path);
|
||||||
new_engine->on_connection_lost = [&]() {
|
new_engine->on_connection_lost = [&]() {
|
||||||
if (!widget->want_engine_move())
|
if (!widget->want_engine_move())
|
||||||
return;
|
return;
|
||||||
|
@ -151,8 +183,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
widget->input_engine_move();
|
widget->input_engine_move();
|
||||||
});
|
});
|
||||||
engines_action_group.add_action(*action);
|
engines_action_group.add_action(*action);
|
||||||
|
|
||||||
TRY(engine_submenu->try_add_action(*action));
|
TRY(engine_submenu->try_add_action(*action));
|
||||||
|
}
|
||||||
|
|
||||||
auto help_menu = TRY(window->try_add_menu("&Help"_short_string));
|
auto help_menu = TRY(window->try_add_menu("&Help"_short_string));
|
||||||
TRY(help_menu->try_add_action(GUI::CommonActions::make_command_palette_action(window)));
|
TRY(help_menu->try_add_action(GUI::CommonActions::make_command_palette_action(window)));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue