mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 02:07:36 +00:00
Chess: Replace usage of DeprecatedString
This commit is contained in:
parent
a94c0eea94
commit
eb85291a18
5 changed files with 38 additions and 29 deletions
|
@ -7,9 +7,9 @@
|
|||
|
||||
#include "ChessWidget.h"
|
||||
#include "PromotionDialog.h"
|
||||
#include <AK/DeprecatedString.h>
|
||||
#include <AK/Random.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibCore/Account.h>
|
||||
#include <LibCore/DateTime.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibGUI/MessageBox.h>
|
||||
|
@ -368,7 +368,6 @@ static RefPtr<Gfx::Bitmap> 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<String> 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<void> ChessWidget::import_pgn(Core::File& file)
|
||||
|
@ -548,13 +547,13 @@ ErrorOr<void> 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<void> 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<void> 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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Chess::Square> 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<String> get_fen() const;
|
||||
ErrorOr<void> import_pgn(Core::File&);
|
||||
ErrorOr<void> 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<Chess::Piece, RefPtr<Gfx::Bitmap const>> m_pieces;
|
||||
DeprecatedString m_piece_set;
|
||||
Chess::Square m_moving_square { 50, 50 };
|
||||
Gfx::IntPoint m_drag_point;
|
||||
bool m_dragging_piece { false };
|
||||
|
|
|
@ -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<char**>(argv), environ) < 0) {
|
||||
if (posix_spawnp(&pid, command_name, &file_actions, nullptr, const_cast<char**>(argv), environ) < 0) {
|
||||
perror("posix_spawnp");
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
delete[] command_name;
|
||||
|
||||
posix_spawn_file_actions_destroy(&file_actions);
|
||||
|
||||
close(wpipefds[0]);
|
||||
|
|
|
@ -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<void(ErrorOr<Chess::Move>)> m_bestmove_callback;
|
||||
bool m_connected { false };
|
||||
};
|
||||
|
|
|
@ -69,6 +69,7 @@ ErrorOr<int> 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<int> 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());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue