From c6f648cfb49372508fc4f2c6281d7752074397d7 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Fri, 25 Feb 2022 11:42:46 +0100 Subject: [PATCH] Improve pgn save performance --- src/base_tab/BaseTab.cpp | 13 ++++----- src/base_tab/BaseTab.hpp | 3 ++- src/base_tab/gamebase/PGNGameBase.cpp | 3 ++- src/game_tab/Game.cpp | 6 +++++ src/game_tab/Game.hpp | 1 + src/game_tab/GameTab.cpp | 1 + src/game_tab/HalfMove.cpp | 39 +++++++++++++++++---------- src/game_tab/HalfMove.hpp | 6 +++-- 8 files changed, 48 insertions(+), 24 deletions(-) diff --git a/src/base_tab/BaseTab.cpp b/src/base_tab/BaseTab.cpp index 13530c7..bc1c09f 100644 --- a/src/base_tab/BaseTab.cpp +++ b/src/base_tab/BaseTab.cpp @@ -2,7 +2,7 @@ #include BaseTab::BaseTab(wxFrame *parent, std::string base_file) - : BasePanelBF(parent), TabInfos(TabInfos::BASE), base(NULL) { + : BasePanelBF(parent), base_file(base_file), TabInfos(TabInfos::BASE), base(NULL) { game_list->InsertColumn(0, L"id", wxLIST_FORMAT_LEFT, 50); game_list->InsertColumn(1, L"White", wxLIST_FORMAT_LEFT, 200); @@ -16,7 +16,7 @@ BaseTab::BaseTab(wxFrame *parent, std::string base_file) this->Bind(wxEVT_BUTTON, &BaseTab::OnSave, this, ID_SAVE_BUTTON); this->Bind(wxEVT_LIST_ITEM_ACTIVATED, &BaseTab::OnOpenGame, this, wxID_ANY); current_base->SetLabel(base_file); - LoadFile(base_file); + LoadFile(); } void BaseTab::OnDelete(wxCommandEvent &event) { @@ -39,8 +39,9 @@ void BaseTab::OnSave(wxCommandEvent &event) { std::vector new_games_bases; std::vector new_games; base->Save(deleted, new_games_bases, new_games); - game_list->ClearAll(); + game_list->DeleteAllItems(); deleted.clear(); + LoadFile(); } void BaseTab::OnOpenGame(wxListEvent &event) { @@ -57,11 +58,11 @@ void BaseTab::OnOpenGame(wxListEvent &event) { void BaseTab::ApplyPreferences() {} -void BaseTab::LoadFile(std::string path) { - wxFileName file(path); +void BaseTab::LoadFile() { + wxFileName file(base_file); wxString ext = file.GetExt().Lower(); if (ext == "pgn") { - base = new PGNGameBase(path); + base = new PGNGameBase(base_file); SetLabel(file.GetName() + "(PGN)"); } diff --git a/src/base_tab/BaseTab.hpp b/src/base_tab/BaseTab.hpp index d0e359c..2980b22 100644 --- a/src/base_tab/BaseTab.hpp +++ b/src/base_tab/BaseTab.hpp @@ -11,11 +11,12 @@ wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent); class BaseTab : public BasePanelBF, public TabInfos { GameBase *base; std::vector deleted; + std::string base_file; public: BaseTab(wxFrame *parent, std::string base_file); void ApplyPreferences(); - void LoadFile(std::string path); + void LoadFile(); void OnDelete(wxCommandEvent &event); void OnSave(wxCommandEvent &event); void OnOpenGame(wxListEvent &event); diff --git a/src/base_tab/gamebase/PGNGameBase.cpp b/src/base_tab/gamebase/PGNGameBase.cpp index 69dcc4f..a5a8b02 100644 --- a/src/base_tab/gamebase/PGNGameBase.cpp +++ b/src/base_tab/gamebase/PGNGameBase.cpp @@ -31,7 +31,8 @@ Game *PGNGameBase::GetCurrentGame() { if (pgn->HasTag("FEN")) { fen = pgn->GetTagValue("FEN"); } - HalfMove *m = new HalfMove(pgnp_moves, fen); + HalfMove *m = new HalfMove(pgnp_moves); + m->SetFen(fen); Game *g = new Game(m, fen); for (std::string &s : pgn->GetTagList()) { g->SetTag(s, pgn->GetTagValue(s)); diff --git a/src/game_tab/Game.cpp b/src/game_tab/Game.cpp index 2b2955a..dab67b8 100644 --- a/src/game_tab/Game.cpp +++ b/src/game_tab/Game.cpp @@ -144,3 +144,9 @@ std::string Game::GetPGN() { } void Game::SetResult(std::string result) { this->result = result; } + +void Game::BuildAndVerify() { + if (moves != NULL) { + moves->BuildAndVerify(GetFen()); + } +} diff --git a/src/game_tab/Game.hpp b/src/game_tab/Game.hpp index 348bff9..37b6f77 100644 --- a/src/game_tab/Game.hpp +++ b/src/game_tab/Game.hpp @@ -37,4 +37,5 @@ public: std::vector ListTags(); std::string GetPGN(); void SetResult(std::string result); + void BuildAndVerify(); }; \ No newline at end of file diff --git a/src/game_tab/GameTab.cpp b/src/game_tab/GameTab.cpp index 3e32b4e..5a3e6b8 100644 --- a/src/game_tab/GameTab.cpp +++ b/src/game_tab/GameTab.cpp @@ -10,6 +10,7 @@ GameTab::GameTab(wxFrame *parent, Game *game) splitter->SetMinimumPaneSize(100); // Panels + game->BuildAndVerify(); board_panel = new BoardPanel((wxFrame *)splitter, game); editor_panel = new EditorPanel((wxFrame *)splitter, game); splitter->SplitVertically(board_panel, editor_panel); diff --git a/src/game_tab/HalfMove.cpp b/src/game_tab/HalfMove.cpp index 2b84382..f43b7f2 100644 --- a/src/game_tab/HalfMove.cpp +++ b/src/game_tab/HalfMove.cpp @@ -115,27 +115,16 @@ void HalfMove::SetAsMainline() { HalfMove *HalfMove::GetMainline() { return (mainline); } -HalfMove::HalfMove(pgnp::HalfMove *m, std::string initial_fen) : capture(' ') { - chessarbiter::ChessArbiter arbiter; - arbiter.Setup(initial_fen); - bool work = arbiter.Play(arbiter.ParseSAN(m->move)); - if (!work) { - wxLogDebug("Bug! %s", m->move); - } - char capture = arbiter.GetCapture(); - if (capture != ' ') { - this->capture = capture; - } - this->fen = arbiter.GetFEN(); +HalfMove::HalfMove(pgnp::HalfMove *m) : capture(' ') { this->move = m->move; this->IsBlack = m->isBlack; this->SetComment(m->comment); this->Number = m->count; if (m->MainLine != NULL) { - this->SetMainline(new HalfMove(m->MainLine, arbiter.GetFEN())); + this->SetMainline(new HalfMove(m->MainLine)); } for (pgnp::HalfMove *v : m->variations) { - this->AddVariation(new HalfMove(v, initial_fen)); + this->AddVariation(new HalfMove(v)); } } @@ -216,3 +205,25 @@ std::string HalfMove::GetPGN(bool needDots) { return (part); } + +void HalfMove::BuildAndVerify(HalfMove *m, std::string fen) { + arbiter.Setup(fen); + bool work = arbiter.Play(arbiter.ParseSAN(m->move)); + if (!work) { + wxLogDebug("Bug! %s", m->move); + } + char capture = arbiter.GetCapture(); + if (capture != ' ') { + m->capture = capture; + } + m->fen = arbiter.GetFEN(); + if (m->mainline != NULL) { + BuildAndVerify(m->mainline, arbiter.GetFEN()); + } + for (HalfMove *v : m->variations) { + BuildAndVerify(v,fen); + } +} +void HalfMove::BuildAndVerify(std::string initial_fen) { + BuildAndVerify(this, initial_fen); +} \ No newline at end of file diff --git a/src/game_tab/HalfMove.hpp b/src/game_tab/HalfMove.hpp index c8ef8c0..07be4d0 100644 --- a/src/game_tab/HalfMove.hpp +++ b/src/game_tab/HalfMove.hpp @@ -16,15 +16,17 @@ class HalfMove : public cgeditor::CGEHalfMove { HalfMove *parent = NULL; HalfMove *mainline = NULL; + chessarbiter::ChessArbiter arbiter; std::vector variations; std::string fen; char capture; std::string GetPGN(bool needDots); + void BuildAndVerify(HalfMove *m, std::string fen); public: HalfMove(std::string move); HalfMove(std::string move, std::string fen); - HalfMove(pgnp::HalfMove *m, std::string initial_fen); + HalfMove(pgnp::HalfMove *m); ~HalfMove(); /// @brief Add variation to current move @@ -53,5 +55,5 @@ public: void SetFen(std::string fen); void SetCapture(char c); std::string GetPGN(); + void BuildAndVerify(std::string initial_fen); }; -