diff --git a/Userland/Games/Chess/ChessWidget.cpp b/Userland/Games/Chess/ChessWidget.cpp index 3609e6c60d..e704a48471 100644 --- a/Userland/Games/Chess/ChessWidget.cpp +++ b/Userland/Games/Chess/ChessWidget.cpp @@ -7,9 +7,9 @@ #include "ChessWidget.h" #include "PromotionDialog.h" -#include #include #include +#include #include #include #include @@ -368,7 +368,6 @@ static RefPtr get_piece(StringView set, StringView image) void ChessWidget::set_piece_set(StringView set) { - m_piece_set = set; m_pieces.set({ Chess::Color::White, Chess::Type::Pawn }, get_piece(set, "white-pawn.png"sv)); m_pieces.set({ Chess::Color::Black, Chess::Type::Pawn }, get_piece(set, "black-pawn.png"sv)); m_pieces.set({ Chess::Color::White, Chess::Type::Knight }, get_piece(set, "white-knight.png"sv)); @@ -522,9 +521,9 @@ void ChessWidget::playback_move(PlaybackDirection direction) update(); } -DeprecatedString ChessWidget::get_fen() const +ErrorOr ChessWidget::get_fen() const { - return (m_playback ? m_board_playback.to_fen() : m_board.to_fen()).release_value_but_fixme_should_propagate_errors().to_deprecated_string(); + return TRY(m_playback ? m_board_playback.to_fen() : m_board.to_fen()); } ErrorOr ChessWidget::import_pgn(Core::File& file) @@ -548,13 +547,13 @@ ErrorOr ChessWidget::import_pgn(Core::File& file) bool recursive_annotation = false; bool future_expansion = false; Chess::Color turn = Chess::Color::White; - DeprecatedString movetext; + String movetext; for (size_t j = i; j < lines.size(); j++) - movetext = DeprecatedString::formatted("{}{}", movetext, lines.at(i).to_deprecated_string()); + movetext = TRY(String::formatted("{}{}", movetext, lines.at(i))); - for (auto token : movetext.split(' ')) { - token = token.trim_whitespace(); + for (auto token : TRY(movetext.split(' '))) { + token = TRY(token.trim_ascii_whitespace()); // FIXME: Parse all of these tokens when we start caring about them if (token.ends_with('}')) { @@ -629,16 +628,19 @@ ErrorOr ChessWidget::export_pgn(Core::File& file) const // Tag Pair Section TRY(file.write_until_depleted("[Event \"Casual Game\"]\n"sv.bytes())); TRY(file.write_until_depleted("[Site \"SerenityOS Chess\"]\n"sv.bytes())); - TRY(file.write_until_depleted(DeprecatedString::formatted("[Date \"{}\"]\n", Core::DateTime::now().to_deprecated_string("%Y.%m.%d"sv)).bytes())); + TRY(file.write_formatted("[Date \"{}\"]\n", Core::DateTime::now().to_deprecated_string("%Y.%m.%d"sv))); TRY(file.write_until_depleted("[Round \"1\"]\n"sv.bytes())); - DeprecatedString username(getlogin()); - auto const player1 = (!username.is_empty() ? username.view() : "?"sv.bytes()); - auto const player2 = (!m_engine.is_null() ? "SerenityOS ChessEngine"sv.bytes() : "?"sv.bytes()); - TRY(file.write_until_depleted(DeprecatedString::formatted("[White \"{}\"]\n", m_side == Chess::Color::White ? player1 : player2).bytes())); - TRY(file.write_until_depleted(DeprecatedString::formatted("[Black \"{}\"]\n", m_side == Chess::Color::Black ? player1 : player2).bytes())); + auto current_user = TRY(Core::Account::self(Core::Account::Read::PasswdOnly)); + auto const username = TRY(String::from_deprecated_string(current_user.username())); - TRY(file.write_until_depleted(DeprecatedString::formatted("[Result \"{}\"]\n", Chess::Board::result_to_points_string(m_board.game_result(), m_board.turn())).bytes())); + auto const player1 = (!username.is_empty() ? username : "?"sv); + auto const player2 = (!m_engine.is_null() ? "SerenityOS ChessEngine"sv : "?"sv); + + TRY(file.write_formatted("[White \"{}\"]\n", m_side == Chess::Color::White ? player1 : player2)); + TRY(file.write_formatted("[Black \"{}\"]\n", m_side == Chess::Color::Black ? player1 : player2)); + + TRY(file.write_formatted("[Result \"{}\"]\n", Chess::Board::result_to_points_string(m_board.game_result(), m_board.turn()))); TRY(file.write_until_depleted("[WhiteElo \"?\"]\n"sv.bytes())); TRY(file.write_until_depleted("[BlackElo \"?\"]\n"sv.bytes())); TRY(file.write_until_depleted("[Variant \"Standard\"]\n"sv.bytes())); @@ -648,13 +650,13 @@ ErrorOr ChessWidget::export_pgn(Core::File& file) const // Movetext Section for (size_t i = 0, move_no = 1; i < m_board.moves().size(); i += 2, move_no++) { - const DeprecatedString white = m_board.moves().at(i).to_algebraic().release_value_but_fixme_should_propagate_errors().to_deprecated_string(); + auto const white = TRY(m_board.moves().at(i).to_algebraic()); if (i + 1 < m_board.moves().size()) { - const DeprecatedString black = m_board.moves().at(i + 1).to_algebraic().release_value_but_fixme_should_propagate_errors().to_deprecated_string(); - TRY(file.write_until_depleted(DeprecatedString::formatted("{}. {} {} ", move_no, white, black).bytes())); + auto const black = TRY(m_board.moves().at(i + 1).to_algebraic()); + TRY(file.write_until_depleted(TRY(String::formatted("{}. {} {} ", move_no, white, black)).bytes())); } else { - TRY(file.write_until_depleted(DeprecatedString::formatted("{}. {} ", move_no, white).bytes())); + TRY(file.write_until_depleted(TRY(String::formatted("{}. {} ", move_no, white)).bytes())); } } diff --git a/Userland/Games/Chess/ChessWidget.h b/Userland/Games/Chess/ChessWidget.h index 79a5665a23..ed4242b5d1 100644 --- a/Userland/Games/Chess/ChessWidget.h +++ b/Userland/Games/Chess/ChessWidget.h @@ -43,7 +43,6 @@ public: void set_side(Chess::Color side) { m_side = side; } void set_piece_set(StringView set); - DeprecatedString const& piece_set() const { return m_piece_set; } Optional mouse_to_square(GUI::MouseEvent& event) const; @@ -54,7 +53,7 @@ public: bool show_available_moves() const { return m_show_available_moves; } void set_show_available_moves(bool e) { m_show_available_moves = e; } - DeprecatedString get_fen() const; + ErrorOr get_fen() const; ErrorOr import_pgn(Core::File&); ErrorOr export_pgn(Core::File&) const; @@ -140,7 +139,6 @@ private: Color m_marking_secondary_color { Color::from_argb(0x6655dd55) }; Chess::Color m_side { Chess::Color::White }; HashMap> m_pieces; - DeprecatedString m_piece_set; Chess::Square m_moving_square { 50, 50 }; Gfx::IntPoint m_drag_point; bool m_dragging_piece { false }; diff --git a/Userland/Games/Chess/Engine.cpp b/Userland/Games/Chess/Engine.cpp index d436dd5005..3245f10ded 100644 --- a/Userland/Games/Chess/Engine.cpp +++ b/Userland/Games/Chess/Engine.cpp @@ -18,8 +18,8 @@ Engine::~Engine() quit(); } -Engine::Engine(StringView command) - : m_command(command) +Engine::Engine(String command) + : m_command(move(command)) { connect_to_engine_service(); } @@ -43,13 +43,21 @@ void Engine::connect_to_engine_service() posix_spawn_file_actions_adddup2(&file_actions, wpipefds[0], STDIN_FILENO); posix_spawn_file_actions_adddup2(&file_actions, rpipefds[1], STDOUT_FILENO); - char const* argv[] = { m_command.characters(), nullptr }; + auto command_length = m_command.code_points().length(); + auto command_name = new char[command_length + 1]; + memcpy(command_name, m_command.bytes_as_string_view().characters_without_null_termination(), command_length); + command_name[command_length] = '\0'; + + char const* argv[] = { command_name, nullptr }; + pid_t pid = -1; - if (posix_spawnp(&pid, m_command.characters(), &file_actions, nullptr, const_cast(argv), environ) < 0) { + if (posix_spawnp(&pid, command_name, &file_actions, nullptr, const_cast(argv), environ) < 0) { perror("posix_spawnp"); VERIFY_NOT_REACHED(); } + delete[] command_name; + posix_spawn_file_actions_destroy(&file_actions); close(wpipefds[0]); diff --git a/Userland/Games/Chess/Engine.h b/Userland/Games/Chess/Engine.h index 7b86d50f8a..d5e7f8524b 100644 --- a/Userland/Games/Chess/Engine.h +++ b/Userland/Games/Chess/Engine.h @@ -15,7 +15,7 @@ class Engine : public Chess::UCI::Endpoint { public: virtual ~Engine() override; - Engine(StringView command); + Engine(String command); Engine(Engine const&) = delete; Engine& operator=(Engine const&) = delete; @@ -51,7 +51,7 @@ private: void quit(); void connect_to_engine_service(); - DeprecatedString m_command; + String m_command; Function)> m_bestmove_callback; bool m_connected { false }; }; diff --git a/Userland/Games/Chess/main.cpp b/Userland/Games/Chess/main.cpp index 35ffb221c2..2396d6dda0 100644 --- a/Userland/Games/Chess/main.cpp +++ b/Userland/Games/Chess/main.cpp @@ -69,6 +69,7 @@ ErrorOr serenity_main(Main::Arguments arguments) for (auto const& engine : engines) TRY(Core::System::unveil(engine.path, "x"sv)); + TRY(Core::System::unveil("/etc/passwd", "r")); TRY(Core::System::unveil("/res", "r")); TRY(Core::System::unveil("/bin/GamesSettings", "x")); TRY(Core::System::unveil("/tmp/session/%sid/portal/launch", "rw")); @@ -125,7 +126,7 @@ ErrorOr serenity_main(Main::Arguments arguments) dbgln("Exported PGN file to {}", result.value().filename()); }))); TRY(game_menu->try_add_action(GUI::Action::create("&Copy FEN", { Mod_Ctrl, Key_C }, [&](auto&) { - GUI::Clipboard::the().set_data(widget->get_fen().bytes()); + GUI::Clipboard::the().set_data(widget->get_fen().release_value_but_fixme_should_propagate_errors().bytes()); GUI::MessageBox::show(window, "Board state copied to clipboard as FEN."sv, "Copy FEN"sv, GUI::MessageBox::Type::Information); }))); TRY(game_menu->try_add_separator());