- fixed xml loading via rapidxml for some scenarios that had embeddex xml comments in lua

- added automation abilities for automated testing with automated saved games
This commit is contained in:
Mark Vejvoda 2012-03-17 08:20:17 +00:00
parent 617344c97d
commit 1ac9aa6d3f
18 changed files with 204 additions and 35 deletions

View File

@ -15,7 +15,9 @@
#include "main_menu.h"
#include "menu_state_new_game.h"
#include "menu_state_scenario.h"
#include "menu_state_custom_game.h"
#include "game.h"
#include "core_data.h"
#include "config.h"
#include "leak_dumper.h"
@ -28,33 +30,52 @@ namespace Glest{ namespace Game{
// =====================================================
const time_t AutoTest::invalidTime = -1;
const time_t AutoTest::gameTime = 60*20;
time_t AutoTest::gameTime = 60 * 20;
bool AutoTest::wantExitGame = false;
GameSettings AutoTest::gameSettings;
string AutoTest::loadGameSettingsFile = "";
// ===================== PUBLIC ========================
AutoTest::AutoTest(){
AutoTest::AutoTest() {
exitGame = false;
gameStartTime = invalidTime;
random.init(time(NULL));
}
AutoTest & AutoTest::getInstance(){
AutoTest & AutoTest::getInstance() {
static AutoTest autoTest;
return autoTest;
}
void AutoTest::updateIntro(Program *program){
void AutoTest::updateIntro(Program *program) {
program->setState(new MainMenu(program));
}
void AutoTest::updateRoot(Program *program, MainMenu *mainMenu){
void AutoTest::updateRoot(Program *program, MainMenu *mainMenu) {
mainMenu->setState(new MenuStateNewGame(program, mainMenu));
}
void AutoTest::updateNewGame(Program *program, MainMenu *mainMenu){
mainMenu->setState(new MenuStateScenario(program, mainMenu, Config::getInstance().getPathListForType(ptScenarios)));
void AutoTest::updateNewGame(Program *program, MainMenu *mainMenu) {
if(loadGameSettingsFile != "") {
gameStartTime = invalidTime;
bool fileFound = CoreData::getInstance().loadGameSettingsFromFile(
loadGameSettingsFile, &gameSettings);
if(fileFound == false) {
throw runtime_error("Specified game settings file [" + loadGameSettingsFile + "] was NOT found!");
}
//printf("Got settings:\n%s",gameSettings.toString().c_str());
mainMenu->setState(new MenuStateCustomGame(program, mainMenu, false, pNewGame, true, &gameSettings));
}
else {
mainMenu->setState(new MenuStateScenario(program, mainMenu,
Config::getInstance().getPathListForType(ptScenarios)));
}
}
void AutoTest::updateScenario(MenuStateScenario *menuStateScenario){
void AutoTest::updateScenario(MenuStateScenario *menuStateScenario) {
gameStartTime = invalidTime;
int scenarioIndex = random.randRange(0, menuStateScenario->getScenarioCount()-1);
@ -63,23 +84,27 @@ void AutoTest::updateScenario(MenuStateScenario *menuStateScenario){
menuStateScenario->launchGame();
}
void AutoTest::updateGame(Game *game){
bool AutoTest::updateGame(Game *game) {
// record start time
if(gameStartTime==invalidTime)
{
if(gameStartTime == invalidTime) {
gameStartTime = time(NULL);
}
// quit if we've espend enough time in the game
if(time(NULL)-gameStartTime>gameTime){
if(difftime(time(NULL),gameStartTime) > gameTime) {
Program *program = game->getProgram();
Stats endStats = game->quitGame();
if(AutoTest::wantExitGame == true) {
exitGame = true;
}
Game::exitGameState(program, endStats);
return true;
}
return false;
}
void AutoTest::updateBattleEnd(Program *program){
void AutoTest::updateBattleEnd(Program *program) {
program->setState(new MainMenu(program));
}

View File

@ -13,10 +13,12 @@
#define _SHARED_UTIL_AUTO_TEST_H_
#include <ctime>
#include "randomgen.h"
#include <string>
#include "game_settings.h"
#include "leak_dumper.h"
using namespace std;
using Shared::Util::RandomGen;
namespace Glest{ namespace Game{
@ -36,20 +38,31 @@ class AutoTest{
private:
int gameStartTime;
RandomGen random;
bool exitGame;
static bool wantExitGame;
static GameSettings gameSettings;
static string loadGameSettingsFile;
private:
static const time_t invalidTime;
static const time_t gameTime;
static time_t gameTime;
public:
static AutoTest & getInstance();
AutoTest();
static void setMaxGameTime(time_t value) { gameTime = value; }
static void setWantExitGameWhenDone(bool value) { wantExitGame = value; }
static string getLoadGameSettingsFile() { return loadGameSettingsFile; }
static void setLoadGameSettingsFile(string filename) { loadGameSettingsFile = filename; }
bool mustExitGame() const { return exitGame; }
void updateIntro(Program *program);
void updateRoot(Program *program, MainMenu *mainMenu);
void updateNewGame(Program *program, MainMenu *mainMenu);
void updateScenario(MenuStateScenario *menuStateScenario);
void updateGame(Game *game);
bool updateGame(Game *game);
void updateBattleEnd(Program *program);
};

View File

@ -1274,6 +1274,7 @@ void Game::update() {
//update auto test
if(Config::getInstance().getBool("AutoTest")){
AutoTest::getInstance().updateGame(this);
return;
}
if(world.getQueuedScenario() != "") {
@ -2651,7 +2652,9 @@ Stats Game::quitGame() {
}
//printf("Check savegame\n");
//printf("Saving...\n");
this->saveGame(GameConstants::saveGameFileDefault);
if(Config::getInstance().getBool("AutoTest")){
this->saveGame(GameConstants::saveGameFileAutoTestDefault);
}
//Stats stats = *(world.getStats());
Stats endStats;
@ -2689,7 +2692,8 @@ void Game::exitGameState(Program *program, Stats &endStats) {
game->endGame();
}
if(game->isMasterserverMode() == true) {
if(game->isMasterserverMode() == true ||
Config::getInstance().getBool("AutoTest") == true) {
printf("Game ending with stats:\n");
printf("-----------------------\n");
@ -3464,6 +3468,16 @@ string Game::saveGame(string name) {
sprintf(szBuf,name.c_str(),szBuf2);
name = szBuf;
}
else if(name == GameConstants::saveGameFileAutoTestDefault) {
time_t curTime = time(NULL);
struct tm *loctime = localtime (&curTime);
char szBuf2[100]="";
strftime(szBuf2,100,"%Y%m%d_%H%M%S",loctime);
char szBuf[8096]="";
sprintf(szBuf,name.c_str(),szBuf2);
name = szBuf;
}
//XmlTree xmlTree(XML_XERCES_ENGINE);
XmlTree xmlTree;
xmlTree.init("megaglest-saved-game");

View File

@ -126,6 +126,7 @@ public:
static const char *application_name;
static const char *saveGameFileDefault;
static const char *saveGameFileAutoTestDefault;
static const char *saveGameFilePattern;
// VC++ Chokes on init of non integral static types

View File

@ -62,6 +62,7 @@ const char *GameConstants::path_ini_CacheLookupKey = "ini";
const char *GameConstants::path_logs_CacheLookupKey = "logs";
const char *GameConstants::saveGameFileDefault = "megaglest-saved.xml";
const char *GameConstants::saveGameFileAutoTestDefault = "megaglest-auto-saved_%s.xml";
const char *GameConstants::saveGameFilePattern = "megaglest-saved_%s.xml";
const char *Config::glest_ini_filename = "glest.ini";

View File

@ -687,22 +687,27 @@ void CoreData::saveGameSettingsToFile(std::string fileName, GameSettings *gameSe
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
}
void CoreData::loadGameSettingsFromFile(std::string fileName, GameSettings *gameSettings) {
bool CoreData::loadGameSettingsFromFile(std::string fileName, GameSettings *gameSettings) {
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
bool fileWasFound = false;
Config &config = Config::getInstance();
string userData = config.getString("UserData_Root","");
if(userData != "") {
endPathWithSlash(userData);
}
fileName = userData + fileName;
if(fileExists(userData + fileName) == true) {
fileName = userData + fileName;
fileWasFound = true;
}
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] fileName = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.c_str());
if(fileExists(fileName) == false) {
return;
return false;
}
fileWasFound = true;
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] fileName = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.c_str());
Properties properties;
@ -757,6 +762,8 @@ void CoreData::loadGameSettingsFromFile(std::string fileName, GameSettings *game
}
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
return fileWasFound;
}
// ================== PRIVATE ========================

View File

@ -142,7 +142,7 @@ public:
Font3D *getConsoleFont3D() const {return consoleFont3D;}
void saveGameSettingsToFile(std::string fileName, GameSettings *gameSettings,int advancedIndex=0);
void loadGameSettingsFromFile(std::string fileName, GameSettings *gameSettings);
bool loadGameSettingsFromFile(std::string fileName, GameSettings *gameSettings);
private:
CoreData();

View File

@ -93,6 +93,7 @@ BattleEnd::~BattleEnd() {
void BattleEnd::update() {
if(Config::getInstance().getBool("AutoTest")){
AutoTest::getInstance().updateBattleEnd(program);
return;
}
mouse2d= (mouse2d+1) % Renderer::maxMouse2dAnim;

View File

@ -518,6 +518,7 @@ void Intro::update() {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
AutoTest::getInstance().updateIntro(program);
return;
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}

View File

@ -39,12 +39,9 @@
#include <iterator>
#include "core_data.h"
#include "font_text.h"
//#include "FileReader.h"
//#include "JPGReader.h"
//#include "sound.h"
//#include "unicode/uclean.h"
#include <locale.h>
#include "string_utils.h"
#include "auto_test.h"
// For gcc backtrace on crash!
#if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__FreeBSD__) && !defined(BSD)
@ -3068,6 +3065,52 @@ int glestMain(int argc, char** argv) {
}
}
if(hasCommandArgument(argc, argv,string(GAME_ARGS[GAME_ARG_AUTO_TEST])) == true ||
Config::getInstance().getBool("AutoTest","false") == true) {
printf("Running in auto test mode\n");
}
if(hasCommandArgument(argc, argv,string(GAME_ARGS[GAME_ARG_AUTO_TEST])) == true) {
Config::getInstance().setBool("AutoTest","true");
int foundParamIndIndex = -1;
hasCommandArgument(argc, argv,string(GAME_ARGS[GAME_ARG_AUTO_TEST]) + string("="),&foundParamIndIndex);
if(foundParamIndIndex < 0) {
hasCommandArgument(argc, argv,string(GAME_ARGS[GAME_ARG_AUTO_TEST]),&foundParamIndIndex);
}
string paramValue = argv[foundParamIndIndex];
vector<string> paramPartTokens;
Tokenize(paramValue,paramPartTokens,"=");
if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) {
vector<string> paramPartTokens2;
Tokenize(paramPartTokens[1],paramPartTokens2,",");
if(paramPartTokens2.size() >= 1 && paramPartTokens2[0].length() > 0) {
string newMaxSeconds = paramPartTokens2[0];
time_t newTimeMaxSeconds = strToInt(newMaxSeconds);
AutoTest::setMaxGameTime(newTimeMaxSeconds);
//printf("#1 Forcing font [%s] paramPartTokens.size() = %d, paramValue [%s]\n",newfont.c_str(),paramPartTokens.size(),paramValue.c_str());
printf("Forcing maximum game time to [%ld] seconds (%.2f minutes)\n",newTimeMaxSeconds,((double)newTimeMaxSeconds / 60.0));
}
if(paramPartTokens2.size() >= 3 && paramPartTokens2[2].length() > 0) {
string autoTestCmd = paramPartTokens2[2];
if(autoTestCmd == "exit") {
printf("Detected auto test command [%s], will exit after game.\n",autoTestCmd.c_str());
AutoTest::setWantExitGameWhenDone(true);
}
else {
printf("WARNING: Detected and UNKNOWN auto test command [%s].\n",autoTestCmd.c_str());
}
}
if(paramPartTokens2.size() >= 2 && paramPartTokens2[1].length() > 0) {
string newGameSettingsFileToLoad = paramPartTokens2[1];
printf("About to auto test using game settings file [%s]\n",newGameSettingsFileToLoad.c_str());
AutoTest::setLoadGameSettingsFile(newGameSettingsFileToLoad);
}
}
}
Renderer &renderer= Renderer::getInstance();
lang.loadStrings(language,false, true);

View File

@ -394,6 +394,7 @@ void MenuStateLoadGame::render(){
void MenuStateLoadGame::update(){
if(Config::getInstance().getBool("AutoTest")){
AutoTest::getInstance().updateNewGame(program, mainMenu);
return;
}
slotsScrollBar.arrangeComponents(slotsGB);
console.update();

View File

@ -142,6 +142,7 @@ void MenuStateNewGame::render(){
void MenuStateNewGame::update(){
if(Config::getInstance().getBool("AutoTest")){
AutoTest::getInstance().updateNewGame(program, mainMenu);
return;
}
console.update();
}

View File

@ -293,9 +293,15 @@ void MenuStateRoot::render() {
if(program != NULL) program->renderProgramMsgBox();
}
void MenuStateRoot::update(){
if(Config::getInstance().getBool("AutoTest")){
AutoTest::getInstance().updateRoot(program, mainMenu);
void MenuStateRoot::update() {
if(Config::getInstance().getBool("AutoTest")) {
if(AutoTest::getInstance().mustExitGame() == false) {
AutoTest::getInstance().updateRoot(program, mainMenu);
}
else {
program->exit();
}
return;
}
console.update();
}

View File

@ -228,6 +228,7 @@ void MenuStateScenario::render(){
void MenuStateScenario::update() {
if(Config::getInstance().getBool("AutoTest")) {
AutoTest::getInstance().updateScenario(this);
return;
}
if(this->autoloadScenarioName != "") {
listBoxScenario.setSelectedItem(formatString(this->autoloadScenarioName),false);

View File

@ -214,6 +214,8 @@ void updatePathClimbingParts(string &path);
string formatPath(string path);
string replaceAll(string& context, const string& from, const string& to);
vector<char> replaceAllBetweenTokens(vector<char>& context, const string startToken, const string endToken, const string newText, bool removeTokens=true);
string replaceAllBetweenTokens(string& context, const string startToken, const string endToken, const string newText, bool removeTokens=true);
bool removeFile(string file);
bool renameFile(string oldFile, string newFile);
void removeFolder(const string path);

View File

@ -27,6 +27,7 @@ const char *GAME_ARGS[] = {
"--autostart-lastgame",
"--load-saved-game",
"--auto-test",
"--connecthost",
"--starthost",
"--headless-server-mode",
@ -79,6 +80,7 @@ enum GAME_ARG_TYPE {
GAME_ARG_AUTOSTART_LASTGAME,
GAME_ARG_AUTOSTART_LAST_SAVED_GAME,
GAME_ARG_AUTO_TEST,
GAME_ARG_CLIENT,
GAME_ARG_SERVER,
GAME_ARG_MASTERSERVER_MODE,
@ -147,6 +149,14 @@ void printParameterHelp(const char *argv0, bool foundInvalidArgs) {
printf("\n \t\tWhere x is an optional name of the saved game file to load.");
printf("\n \t\tIf x is not specified we load the last game that was saved.");
printf("\n%s=x,y,z\t\t\tRun in auto test mode.",GAME_ARGS[GAME_ARG_AUTO_TEST]);
printf("\n \t\tWhere x is an optional maximum # seconds to play.");
printf("\n \t\tIf x is not specified the default is 1200 seconds (20 minutes).");
printf("\n \t\tWhere y is an optional game settings file to play.");
printf("\n \t\tIf y is not specified (or is empty) then auto test cycles through playing scenarios.");
printf("\n \t\tWhere z is the word exit indicating the game should exit after the game is finished or the time runs out.");
printf("\n \t\tIf z is not specified (or is empty) then auto test continues to cycle.");
printf("\n%s=x\t\t\tAuto connect to host server at IP or hostname x",GAME_ARGS[GAME_ARG_CLIENT]);
printf("\n%s\t\t\tAuto create a host server.",GAME_ARGS[GAME_ARG_SERVER]);

View File

@ -1743,6 +1743,48 @@ string replaceAll(string& context, const string& from, const string& to) {
return context;
}
vector<char> replaceAllBetweenTokens(vector<char>& context,
const string startToken, const string endToken, const string newText,
bool removeTokens) {
string newValue(context.begin(),context.end());
replaceAllBetweenTokens(newValue,startToken,endToken,newText,removeTokens);
context = vector<char>(newValue.begin(),newValue.end());
return context;
}
string replaceAllBetweenTokens(string& context, const string startToken,
const string endToken, const string newText, bool removeTokens) {
size_t lookHere = 0;
size_t foundHere = 0;
size_t foundHereEnd = 0;
if((foundHere = context.find(startToken, lookHere)) != string::npos) {
//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Replacing context [%s] from [%s] to [%s]\n",context.c_str(),from.c_str(),to.c_str());
while((foundHere = context.find(startToken, lookHere)) != string::npos) {
size_t foundHereEnd = context.find(endToken, foundHere+1);
if(foundHereEnd == string::npos) {
break;
}
if(removeTokens == true) {
foundHereEnd += endToken.size();
context.replace(foundHere, foundHereEnd-foundHere+1, newText);
lookHere = foundHere + newText.size();
}
else {
foundHere += startToken.size();
foundHereEnd -= 1;
context.replace(foundHere, foundHereEnd-foundHere+1, newText);
lookHere = foundHere + newText.size();
}
}
//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("New context [%s]\n",context.c_str());
}
return context;
}
string getFullFileArchiveExtractCommand(string fileArchiveExtractCommand,
string fileArchiveExtractCommandParameters, string outputpath, string archivename) {
string parsedOutputpath = outputpath;

View File

@ -294,9 +294,9 @@ XmlNode *XmlIoRapid::load(const string &path, std::map<string,string> mapTagRepl
xmlFile.read(&buffer.front(), static_cast<streamsize>(size));
buffer[size] = 0;
//doc->parse<parse_no_utf8 | parse_validate_closing_tags>(&buffer[0]);
//doc->parse<parse_full>(&buffer.front());
//doc->parse<parse_declaration_node | parse_doctype_node | parse_pi_nodes | parse_validate_closing_tags>(&buffer[0]);
// This is required because rapidxml seems to choke when we load lua
// scenarios that have lua + xml style comments
replaceAllBetweenTokens(buffer, "<!--","-->", "", true);
doc->parse<parse_no_data_nodes>(&buffer.front());
rootNode= new XmlNode(doc->first_node(),mapTagReplacementValues);
@ -518,7 +518,7 @@ XmlNode::XmlNode(xml_node<> *node, std::map<string,string> mapTagReplacementValu
name="document";
}
//printf("Found XML Node [%s]\n",name.c_str());
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Found XML Node\nName [%s]\nValue [%s]\n",name.c_str(),node->value());
//check children
for(xml_node<> *currentNode = node->first_node();