From 3c5f33d1a18a97630fd42a05e52c69d160221c65 Mon Sep 17 00:00:00 2001 From: pavanvo Date: Sun, 7 Aug 2022 20:43:38 +0400 Subject: [PATCH 1/8] feat: add class CallBack --- source/shared_lib/include/util/callback.h | 71 +++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 source/shared_lib/include/util/callback.h diff --git a/source/shared_lib/include/util/callback.h b/source/shared_lib/include/util/callback.h new file mode 100644 index 000000000..d0b5e6eba --- /dev/null +++ b/source/shared_lib/include/util/callback.h @@ -0,0 +1,71 @@ +#pragma once + +#include +#include +#include + +using std::function; +using std::thread; + +namespace Shared{ namespace Util{ + +template +class CallBack { + + +private: + function _onThen = [](){}; + function _mainFunc = [](){}; + T _result; + +public: + CallBack(function func){ + _mainFunc = func; + }; + + ~CallBack() { + //printf("CallBack::~CallBack p [%p]\n",this); + }; + + void then(function onThen){ + _onThen = onThen; + }; + + T getResult(){ + return _result; + } + + void run(){ + _result = _mainFunc(); + _onThen(); + }; +}; + +template<> +class CallBack { + +private: + function _onThen = [](){}; + function _mainFunc = [](){}; + +public: + CallBack(function func){ + _mainFunc = func; + }; + + ~CallBack() { + //printf("CallBack::~CallBack p [%p]\n",this); + }; + + void then(function onThen){ + _onThen = onThen; + }; + + void run(){ + _mainFunc(); + _onThen(); + }; + +}; + +}} From 632c670f98761bdbceaecd9832e20d849fda7bb8 Mon Sep 17 00:00:00 2001 From: pavanvo Date: Sun, 7 Aug 2022 20:48:10 +0400 Subject: [PATCH 2/8] feat: add loadAsync method to XmlTree class --- source/shared_lib/include/xml/xml_parser.h | 3 +++ source/shared_lib/sources/xml/xml_parser.cpp | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/source/shared_lib/include/xml/xml_parser.h b/source/shared_lib/include/xml/xml_parser.h index d7fd4e832..d52832643 100644 --- a/source/shared_lib/include/xml/xml_parser.h +++ b/source/shared_lib/include/xml/xml_parser.h @@ -23,10 +23,12 @@ #endif #include "rapidxml/rapidxml.hpp" +#include "callback.h" #include "data_types.h" #include "leak_dumper.h" using namespace rapidxml; +using namespace Shared::Util; using namespace std; #if defined(WANT_XERCES) @@ -151,6 +153,7 @@ class XmlTree{ void setSkipUpdatePathClimbingParts(bool value); void init(const string &name); void load(const string &path, const std::map &mapTagReplacementValues, bool noValidation=false,bool skipStackCheck=false,bool skipStackTrace=false); + std::shared_ptr> loadAsync(const string &path, const std::map &mapTagReplacementValues, bool noValidation=false,bool skipStackCheck=false,bool skipStackTrace=false); void save(const string &path); XmlNode *getRootNode() const {return rootNode;} diff --git a/source/shared_lib/sources/xml/xml_parser.cpp b/source/shared_lib/sources/xml/xml_parser.cpp index 4832fac19..6230db865 100644 --- a/source/shared_lib/sources/xml/xml_parser.cpp +++ b/source/shared_lib/sources/xml/xml_parser.cpp @@ -585,6 +585,16 @@ void XmlTree::load(const string &path, const std::map &mapTagRepl if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] about to load [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,path.c_str()); } +std::shared_ptr> XmlTree::loadAsync(const string &path, const std::map &mapTagReplacementValues, bool noValidation,bool skipStackCheck,bool skipStackTrace) { + auto load = [this, path, mapTagReplacementValues, noValidation, skipStackCheck, skipStackTrace]() { + this->load(path, mapTagReplacementValues, noValidation, skipStackCheck, skipStackTrace); + }; + auto cb = std::make_shared>(load); + + std::thread([cb]() { cb->run(); }).detach(); + return cb; +} + void XmlTree::save(const string &path) { #if defined(WANT_XERCES) From edbee7ced74caf7d0783021ca60c79530587c86b Mon Sep 17 00:00:00 2001 From: pavanvo Date: Sun, 7 Aug 2022 20:50:57 +0400 Subject: [PATCH 3/8] feat: load game non-bloking --- source/glest_game/menu/menu_state_load_game.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/source/glest_game/menu/menu_state_load_game.cpp b/source/glest_game/menu/menu_state_load_game.cpp index 876c65e16..39557863f 100644 --- a/source/glest_game/menu/menu_state_load_game.cpp +++ b/source/glest_game/menu/menu_state_load_game.cpp @@ -317,16 +317,17 @@ void MenuStateLoadGame::mouseClick(int x, int y, MouseButton mouseButton){ #endif - XmlTree xmlTree(engine_type); - + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Before load of XML\n"); std::map mapExtraTagReplacementValues; try { - xmlTree.load(filename, Properties::getTagReplacementValues(&mapExtraTagReplacementValues),true,false,true); - + auto xmlTree = std::make_shared(engine_type); + xmlTree->loadAsync(filename, Properties::getTagReplacementValues(&mapExtraTagReplacementValues),true,false,true) + ->then([this, xmlTree, &lang, filename](){ + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("After load of XML\n"); - const XmlNode *rootNode= xmlTree.getRootNode(); + const XmlNode *rootNode= xmlTree->getRootNode(); if(rootNode != NULL && rootNode->hasChild("megaglest-saved-game") == true) { rootNode = rootNode->getChild("megaglest-saved-game"); } @@ -363,6 +364,7 @@ void MenuStateLoadGame::mouseClick(int x, int y, MouseButton mouseButton){ newGameSettings.getThisFactionIndex() < newGameSettings.getFactionCount() ? newGameSettings.getFactionTypeName(newGameSettings.getThisFactionIndex()).c_str() : "")); infoTextLabel.setText(szBuf); + }); } catch(const megaglest_runtime_error &ex) { char szBuf[8096]=""; From f891ad9336d27a2d931d97ea08e49f681bc3d821 Mon Sep 17 00:00:00 2001 From: pavanvo Date: Sun, 7 Aug 2022 21:33:48 +0400 Subject: [PATCH 4/8] feat: loading indicator --- source/glest_game/menu/menu_state_load_game.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/glest_game/menu/menu_state_load_game.cpp b/source/glest_game/menu/menu_state_load_game.cpp index 39557863f..328b20f9e 100644 --- a/source/glest_game/menu/menu_state_load_game.cpp +++ b/source/glest_game/menu/menu_state_load_game.cpp @@ -317,8 +317,9 @@ void MenuStateLoadGame::mouseClick(int x, int y, MouseButton mouseButton){ #endif - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Before load of XML\n"); + versionWarningLabel.setText(""); + infoTextLabel.setText("Loading..."); std::map mapExtraTagReplacementValues; try { auto xmlTree = std::make_shared(engine_type); From 73814e3ad8270088b310f24d59769142d4b4fa0b Mon Sep 17 00:00:00 2001 From: pavanvo Date: Sun, 7 Aug 2022 22:04:28 +0400 Subject: [PATCH 5/8] feat: disable game slots buttons while game info is loading --- source/glest_game/menu/menu_state_load_game.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/glest_game/menu/menu_state_load_game.cpp b/source/glest_game/menu/menu_state_load_game.cpp index 328b20f9e..ce8b2c8a2 100644 --- a/source/glest_game/menu/menu_state_load_game.cpp +++ b/source/glest_game/menu/menu_state_load_game.cpp @@ -281,6 +281,7 @@ void MenuStateLoadGame::mouseClick(int x, int y, MouseButton mouseButton){ Lang &lang= Lang::getInstance(); cleanupTexture(&previewTexture); selectedButton = slots[i]; + for(auto slot : slots) slot->setEnabled(false); string filename = saveGameDir + selectedButton->getText()+".xml"; string screenShotFilename = filename + ".jpg"; if(fileExists(screenShotFilename) == true) { @@ -365,6 +366,7 @@ void MenuStateLoadGame::mouseClick(int x, int y, MouseButton mouseButton){ newGameSettings.getThisFactionIndex() < newGameSettings.getFactionCount() ? newGameSettings.getFactionTypeName(newGameSettings.getThisFactionIndex()).c_str() : "")); infoTextLabel.setText(szBuf); + for(auto slot : slots) slot->setEnabled(true); }); } catch(const megaglest_runtime_error &ex) { @@ -373,6 +375,7 @@ void MenuStateLoadGame::mouseClick(int x, int y, MouseButton mouseButton){ SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); showMessageBox(ex.what(), lang.getString("Notice"), false); + for(auto slot : slots) slot->setEnabled(true); } } else { From 4f86eac474ce7c1726997f8ce65a1bd60fdce9da Mon Sep 17 00:00:00 2001 From: pavanvo Date: Sun, 14 Aug 2022 17:41:17 +0400 Subject: [PATCH 6/8] style: tabs insted of spaces --- .../glest_game/menu/menu_state_load_game.cpp | 34 ++++----- source/shared_lib/include/util/callback.h | 69 +++++++++---------- source/shared_lib/include/xml/xml_parser.h | 2 +- source/shared_lib/sources/xml/xml_parser.cpp | 12 ++-- 4 files changed, 58 insertions(+), 59 deletions(-) diff --git a/source/glest_game/menu/menu_state_load_game.cpp b/source/glest_game/menu/menu_state_load_game.cpp index ce8b2c8a2..c9e8802a2 100644 --- a/source/glest_game/menu/menu_state_load_game.cpp +++ b/source/glest_game/menu/menu_state_load_game.cpp @@ -281,7 +281,7 @@ void MenuStateLoadGame::mouseClick(int x, int y, MouseButton mouseButton){ Lang &lang= Lang::getInstance(); cleanupTexture(&previewTexture); selectedButton = slots[i]; - for(auto slot : slots) slot->setEnabled(false); + for(auto slot : slots) slot->setEnabled(false); string filename = saveGameDir + selectedButton->getText()+".xml"; string screenShotFilename = filename + ".jpg"; if(fileExists(screenShotFilename) == true) { @@ -319,13 +319,13 @@ void MenuStateLoadGame::mouseClick(int x, int y, MouseButton mouseButton){ #endif if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Before load of XML\n"); - versionWarningLabel.setText(""); - infoTextLabel.setText("Loading..."); + versionWarningLabel.setText(""); + infoTextLabel.setText("Loading..."); std::map mapExtraTagReplacementValues; try { - auto xmlTree = std::make_shared(engine_type); + auto xmlTree = std::make_shared(engine_type); xmlTree->loadAsync(filename, Properties::getTagReplacementValues(&mapExtraTagReplacementValues),true,false,true) - ->then([this, xmlTree, &lang, filename](){ + ->then([this, xmlTree, &lang, filename](){ if(SystemFlags::VERBOSE_MODE_ENABLED) printf("After load of XML\n"); @@ -366,8 +366,8 @@ void MenuStateLoadGame::mouseClick(int x, int y, MouseButton mouseButton){ newGameSettings.getThisFactionIndex() < newGameSettings.getFactionCount() ? newGameSettings.getFactionTypeName(newGameSettings.getThisFactionIndex()).c_str() : "")); infoTextLabel.setText(szBuf); - for(auto slot : slots) slot->setEnabled(true); - }); + for(auto slot : slots) slot->setEnabled(true); + }); } catch(const megaglest_runtime_error &ex) { char szBuf[8096]=""; @@ -375,7 +375,7 @@ void MenuStateLoadGame::mouseClick(int x, int y, MouseButton mouseButton){ SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); showMessageBox(ex.what(), lang.getString("Notice"), false); - for(auto slot : slots) slot->setEnabled(true); + for(auto slot : slots) slot->setEnabled(true); } } else { @@ -384,21 +384,21 @@ void MenuStateLoadGame::mouseClick(int x, int y, MouseButton mouseButton){ break; } - } - } - } + } + } + } } void MenuStateLoadGame::mouseUp(int x, int y, const MouseButton mouseButton) { - if (mouseButton == mbLeft) { - slotsScrollBar.mouseUp(x, y); - } + if (mouseButton == mbLeft) { + slotsScrollBar.mouseUp(x, y); + } } void MenuStateLoadGame::mouseMove(int x, int y, const MouseState *ms) { - abortButton.mouseMove(x, y); - deleteButton.mouseMove(x, y); - loadButton.mouseMove(x, y); + abortButton.mouseMove(x, y); + deleteButton.mouseMove(x, y); + loadButton.mouseMove(x, y); if(slotsScrollBar.getElementCount()!=0){ for(int i = slotsScrollBar.getVisibleStart(); i <= slotsScrollBar.getVisibleEnd(); ++i) { slots[i]->mouseMove(x, y); diff --git a/source/shared_lib/include/util/callback.h b/source/shared_lib/include/util/callback.h index d0b5e6eba..374685d7f 100644 --- a/source/shared_lib/include/util/callback.h +++ b/source/shared_lib/include/util/callback.h @@ -14,58 +14,57 @@ class CallBack { private: - function _onThen = [](){}; - function _mainFunc = [](){}; - T _result; + function _onThen = [](){}; + function _mainFunc = [](){}; + T _result; public: - CallBack(function func){ - _mainFunc = func; - }; + CallBack(function func){ + _mainFunc = func; + }; - ~CallBack() { - //printf("CallBack::~CallBack p [%p]\n",this); - }; + ~CallBack() { + //printf("CallBack::~CallBack p [%p]\n",this); + }; - void then(function onThen){ - _onThen = onThen; - }; + void then(function onThen){ + _onThen = onThen; + }; - T getResult(){ - return _result; - } + T getResult(){ + return _result; + } - void run(){ - _result = _mainFunc(); - _onThen(); - }; + void run(){ + _result = _mainFunc(); + _onThen(); + }; }; template<> class CallBack { private: - function _onThen = [](){}; - function _mainFunc = [](){}; + function _onThen = [](){}; + function _mainFunc = [](){}; public: - CallBack(function func){ - _mainFunc = func; - }; + CallBack(function func){ + _mainFunc = func; + }; - ~CallBack() { - //printf("CallBack::~CallBack p [%p]\n",this); - }; + ~CallBack() { + //printf("CallBack::~CallBack p [%p]\n",this); + }; - void then(function onThen){ - _onThen = onThen; - }; + void then(function onThen){ + _onThen = onThen; + }; - void run(){ - _mainFunc(); - _onThen(); - }; - + void run(){ + _mainFunc(); + _onThen(); + }; }; }} diff --git a/source/shared_lib/include/xml/xml_parser.h b/source/shared_lib/include/xml/xml_parser.h index d52832643..a216b145c 100644 --- a/source/shared_lib/include/xml/xml_parser.h +++ b/source/shared_lib/include/xml/xml_parser.h @@ -153,7 +153,7 @@ class XmlTree{ void setSkipUpdatePathClimbingParts(bool value); void init(const string &name); void load(const string &path, const std::map &mapTagReplacementValues, bool noValidation=false,bool skipStackCheck=false,bool skipStackTrace=false); - std::shared_ptr> loadAsync(const string &path, const std::map &mapTagReplacementValues, bool noValidation=false,bool skipStackCheck=false,bool skipStackTrace=false); + std::shared_ptr> loadAsync(const string &path, const std::map &mapTagReplacementValues, bool noValidation=false,bool skipStackCheck=false,bool skipStackTrace=false); void save(const string &path); XmlNode *getRootNode() const {return rootNode;} diff --git a/source/shared_lib/sources/xml/xml_parser.cpp b/source/shared_lib/sources/xml/xml_parser.cpp index 6230db865..8ce696891 100644 --- a/source/shared_lib/sources/xml/xml_parser.cpp +++ b/source/shared_lib/sources/xml/xml_parser.cpp @@ -586,13 +586,13 @@ void XmlTree::load(const string &path, const std::map &mapTagRepl } std::shared_ptr> XmlTree::loadAsync(const string &path, const std::map &mapTagReplacementValues, bool noValidation,bool skipStackCheck,bool skipStackTrace) { - auto load = [this, path, mapTagReplacementValues, noValidation, skipStackCheck, skipStackTrace]() { - this->load(path, mapTagReplacementValues, noValidation, skipStackCheck, skipStackTrace); - }; - auto cb = std::make_shared>(load); + auto load = [this, path, mapTagReplacementValues, noValidation, skipStackCheck, skipStackTrace]() { + this->load(path, mapTagReplacementValues, noValidation, skipStackCheck, skipStackTrace); + }; + auto cb = std::make_shared>(load); - std::thread([cb]() { cb->run(); }).detach(); - return cb; + std::thread([cb]() { cb->run(); }).detach(); + return cb; } void XmlTree::save(const string &path) { From e7610e4e27d07dd5649c1ff928b2ccc3420f75c5 Mon Sep 17 00:00:00 2001 From: pavanvo Date: Sun, 14 Aug 2022 18:54:22 +0400 Subject: [PATCH 7/8] fix: #include for modern compilers --- source/shared_lib/include/xml/xml_parser.h | 1 + 1 file changed, 1 insertion(+) diff --git a/source/shared_lib/include/xml/xml_parser.h b/source/shared_lib/include/xml/xml_parser.h index a216b145c..6f25dbceb 100644 --- a/source/shared_lib/include/xml/xml_parser.h +++ b/source/shared_lib/include/xml/xml_parser.h @@ -15,6 +15,7 @@ #include #include #include +#include #if defined(WANT_XERCES) From 99c55292b03a9fc3aa4e0fb1ce1feb06e5e8c738 Mon Sep 17 00:00:00 2001 From: pavanvo Date: Mon, 15 Aug 2022 01:34:11 +0400 Subject: [PATCH 8/8] fix: disable 'load button' while loading game xml --- source/glest_game/menu/menu_state_load_game.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/glest_game/menu/menu_state_load_game.cpp b/source/glest_game/menu/menu_state_load_game.cpp index c9e8802a2..9fe19af1a 100644 --- a/source/glest_game/menu/menu_state_load_game.cpp +++ b/source/glest_game/menu/menu_state_load_game.cpp @@ -276,6 +276,7 @@ void MenuStateLoadGame::mouseClick(int x, int y, MouseButton mouseButton){ if(slotsScrollBar.getElementCount()!=0){ for(int i = slotsScrollBar.getVisibleStart(); i <= slotsScrollBar.getVisibleEnd(); ++i) { if(slots[i]->mouseClick(x, y) && selectedButton != slots[i]) { + loadButton.setEnabled(false); soundRenderer.playFx(coreData.getClickSoundB()); Lang &lang= Lang::getInstance(); @@ -367,6 +368,7 @@ void MenuStateLoadGame::mouseClick(int x, int y, MouseButton mouseButton){ newGameSettings.getFactionTypeName(newGameSettings.getThisFactionIndex()).c_str() : "")); infoTextLabel.setText(szBuf); for(auto slot : slots) slot->setEnabled(true); + loadButton.setEnabled(true); }); } catch(const megaglest_runtime_error &ex) { @@ -376,6 +378,7 @@ void MenuStateLoadGame::mouseClick(int x, int y, MouseButton mouseButton){ showMessageBox(ex.what(), lang.getString("Notice"), false); for(auto slot : slots) slot->setEnabled(true); + loadButton.setEnabled(true); } } else {