Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/non blocking for loading game info #240

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions source/glest_game/menu/menu_state_load_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -317,16 +318,18 @@ 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");
versionWarningLabel.setText("");
infoTextLabel.setText("Loading...");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaces vs tabs, this codebase uses mostly tabs, let's try to keep it consistent :)

std::map<string,string> mapExtraTagReplacementValues;
try {
xmlTree.load(filename, Properties::getTagReplacementValues(&mapExtraTagReplacementValues),true,false,true);

auto xmlTree = std::make_shared<XmlTree>(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");
}
Expand Down Expand Up @@ -363,13 +366,16 @@ 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) {
char szBuf[8096]="";
snprintf(szBuf,8096,"In [%s::%s Line: %d]\nError [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what());
SystemFlags::OutputDebug(SystemFlags::debugError,szBuf);

showMessageBox(ex.what(), lang.getString("Notice"), false);
for(auto slot : slots) slot->setEnabled(true);
}
}
else {
Expand Down
71 changes: 71 additions & 0 deletions source/shared_lib/include/util/callback.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done in other places of the code with https://github.com/MegaGlest/megaglest-source/blob/develop/source/shared_lib/include/platform/common/simple_threads.h#L71
Although honestly your way seems cleaner to me.


#include <functional>
#include <thread>
#include <utility>

using std::function;
using std::thread;

namespace Shared{ namespace Util{

template<typename T>
class CallBack {


private:
function<void()> _onThen = [](){};
function<T()> _mainFunc = [](){};
T _result;

public:
CallBack(function<T()> func){
_mainFunc = func;
};

~CallBack() {
//printf("CallBack::~CallBack p [%p]\n",this);
};

void then(function<void()> onThen){
_onThen = onThen;
};

T getResult(){
return _result;
}

void run(){
_result = _mainFunc();
_onThen();
};
};

template<>
class CallBack<void> {

private:
function<void()> _onThen = [](){};
function<void()> _mainFunc = [](){};

public:
CallBack(function<void()> func){
_mainFunc = func;
};

~CallBack() {
//printf("CallBack::~CallBack p [%p]\n",this);
};

void then(function<void()> onThen){
_onThen = onThen;
};

void run(){
_mainFunc();
_onThen();
};

};

}}
3 changes: 3 additions & 0 deletions source/shared_lib/include/xml/xml_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
#endif

#include "rapidxml/rapidxml.hpp"
#include "callback.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you forgot to include memory

#include "data_types.h"
#include "leak_dumper.h"

using namespace rapidxml;
using namespace Shared::Util;
using namespace std;

#if defined(WANT_XERCES)
Expand Down Expand Up @@ -151,6 +153,7 @@ class XmlTree{
void setSkipUpdatePathClimbingParts(bool value);
void init(const string &name);
void load(const string &path, const std::map<string,string> &mapTagReplacementValues, bool noValidation=false,bool skipStackCheck=false,bool skipStackTrace=false);
std::shared_ptr<CallBack<void>> loadAsync(const string &path, const std::map<string,string> &mapTagReplacementValues, bool noValidation=false,bool skipStackCheck=false,bool skipStackTrace=false);
void save(const string &path);

XmlNode *getRootNode() const {return rootNode;}
Expand Down
10 changes: 10 additions & 0 deletions source/shared_lib/sources/xml/xml_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,16 @@ void XmlTree::load(const string &path, const std::map<string,string> &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<CallBack<void>> XmlTree::loadAsync(const string &path, const std::map<string,string> &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<CallBack<void>>(load);

std::thread([cb]() { cb->run(); }).detach();
return cb;
}

void XmlTree::save(const string &path) {

#if defined(WANT_XERCES)
Expand Down