From 3e6d643056dae8bcf37d83dfd12448db88a2c52b Mon Sep 17 00:00:00 2001 From: sfb Date: Thu, 2 Jun 2011 13:47:31 -0500 Subject: [PATCH] Changed: Implemented mission validation. --- .../mission_compiler_main_window.cpp | 114 +++++++++++++++ .../mission_compiler_main_window.h | 14 ++ .../mission_compiler_plugin.cpp | 4 - .../mission_compiler_plugin.h | 4 - .../mission_compiler/validation_file.cpp | 133 ++++++++++++++++++ .../mission_compiler/validation_file.h | 51 +++++++ 6 files changed, 312 insertions(+), 8 deletions(-) create mode 100644 code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/validation_file.cpp create mode 100644 code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/validation_file.h diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/mission_compiler_main_window.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/mission_compiler_main_window.cpp index b233ce922..0cbb1e708 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/mission_compiler_main_window.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/mission_compiler_main_window.cpp @@ -1,17 +1,24 @@ #include "mission_compiler_main_window.h" #include "ui_mission_compiler_main_window.h" +#include "validation_file.h" #include #include #include #include #include +#include #include "../core/icore.h" #include "../core/imenu_manager.h" #include "../core/core_constants.h" +#include + #include +#include +#include +#include MissionCompilerMainWindow::MissionCompilerMainWindow(QWidget *parent) : QMainWindow(parent), @@ -19,6 +26,9 @@ MissionCompilerMainWindow::MissionCompilerMainWindow(QWidget *parent) : { ui->setupUi(this); + m_compileLog = ""; + updateCompileLog(); + // Load the settings. loadConfig(); @@ -51,7 +61,11 @@ MissionCompilerMainWindow::MissionCompilerMainWindow(QWidget *parent) : ui->selectedPrimitivesList->setModel(m_selectedPrimitivesModel); connect(ui->filterEdit, SIGNAL(textEdited(const QString&)), this, SLOT(handleFilterChanged(const QString&))); + connect(ui->actionValidate, SIGNAL(triggered()), this, SLOT(handleValidation())); + NLLIGO::Register(); + m_ligoConfig.readPrimitiveClass(NLMISC::CPath::lookup("world_editor_classes.xml").c_str(), false); + NLLIGO::CPrimitiveContext::instance().CurrentLigoConfig = &m_ligoConfig; } void MissionCompilerMainWindow::handleFilterChanged(const QString &text) @@ -60,6 +74,106 @@ void MissionCompilerMainWindow::handleFilterChanged(const QString &text) m_filteredProxyModel->setFilterRegExp(*m_regexpFilter); } +void MissionCompilerMainWindow::handleValidation() +{ + // First switch toolbox pages to show the compilation output. + ui->toolBox->setCurrentIndex(2); + + m_compileLog.append("Begin mission validation.\n"); + updateCompileLog(); + + // Load existing validation + CValidationFile validation; + validation.loadMissionValidationFile("mission_validation.cfg"); + + // Go through each file. + QStringList list = m_selectedPrimitivesModel->stringList(); + QStringListIterator itr(list); + while(itr.hasNext()) + { + QString filename = itr.next(); + //QString filePath = NLMISC::CPath::lookup(filename.toAscii().data(), false).c_str(); + m_compileLog.append("Parsing '"+filename+"'...\n"); + updateCompileLog(); + + TMissionContainer missions; + NLLIGO::CPrimitives primDoc; + NLLIGO::CPrimitiveContext::instance().CurrentPrimitive = &primDoc; + NLLIGO::loadXmlPrimitiveFile(primDoc, NLMISC::CPath::lookup(filename.toAscii().data(), false), m_ligoConfig); + parsePrimForMissions(primDoc.RootNode, missions); + + // Parse missions to check modification + std::map::iterator itMission, itMissionEnd = missions.end(); + for (itMission=missions.begin(); itMission!=itMissionEnd; ++itMission) + { + CValidationFile::TMissionStateContainer::iterator itMissionValidation = validation._MissionStates.find(itMission->first); + if (itMissionValidation!=validation._MissionStates.end()) + { + // Mission already registered, check hash key + if (itMissionValidation->second.hashKey!=itMission->second.hashKey) + { + itMissionValidation->second.hashKey = itMission->second.hashKey; + itMissionValidation->second.state = validation.defaultState(); + } + } + else + { + // New mission + validation.insertMission(itMission->first, itMission->second.hashKey); + } + m_compileLog.append("Mission: '"+QString(itMission->first.c_str())+"->"+QString(itMission->second.hashKey.c_str())+"\n"); + updateCompileLog(); + } + } + validation.saveMissionValidationFile("mission_validation.cfg"); + + m_compileLog.append("Validation finished"); + updateCompileLog(); +} + +bool MissionCompilerMainWindow::parsePrimForMissions(NLLIGO::IPrimitive const *prim, TMissionContainer &missions) +{ + std::string value; + // if the node is a mission parse it + if (prim->getPropertyByName("class",value) && !stricmp(value.c_str(),"mission") ) + { + std::string name; + prim->getPropertyByName("name",name); + + m_compileLog.append(" ** Parsing mission '"+QString(name.c_str())+"'\n"); + updateCompileLog(); + + // parse the mission and put it in our manager + CMission mission(value, ""); + if (!mission.parsePrim(prim) ) + { + m_compileLog.append(" ** Previous errors in mission '"+QString(name.c_str())+"'"); + updateCompileLog(); + return false; + } + missions.insert(make_pair(name, mission)); + return true; + } + else + { + //this is not a mission node, so lookup recursively in the children + bool ok = true; + for (uint i=0;igetNumChildren();++i) + { + const NLLIGO::IPrimitive *child; + if ( !prim->getChild(child,i) || !parsePrimForMissions(child, missions) ) + ok = false; + } + return ok; + } +} + +void MissionCompilerMainWindow::updateCompileLog() +{ + ui->compileOutputText->setPlainText(m_compileLog); + QCoreApplication::processEvents(); +} + void MissionCompilerMainWindow::loadConfig() { QSettings *settings = Core::ICore::instance()->settings(); settings->beginGroup("MissionCompiler"); diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/mission_compiler_main_window.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/mission_compiler_main_window.h index 388b5c57d..de8328dee 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/mission_compiler_main_window.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/mission_compiler_main_window.h @@ -10,10 +10,15 @@ #include #include +#include +#include + namespace Ui { class MissionCompilerMainWindow; } +struct CMission; + class MissionCompilerMainWindow : public QMainWindow { Q_OBJECT @@ -26,18 +31,27 @@ public: void saveConfig(); QUndoStack *getUndoStack() { return m_undoStack; } + typedef std::map TMissionContainer; + public Q_SLOTS: void handleFilterChanged(const QString &text); + void handleValidation(); private: Ui::MissionCompilerMainWindow *ui; + void updateCompileLog(); + bool parsePrimForMissions(NLLIGO::IPrimitive const *prim, TMissionContainer &missions); + QMenu *_toolModeMenu; QUndoStack *m_undoStack; QStringListModel *m_allPrimitivesModel; QStringListModel *m_selectedPrimitivesModel; QSortFilterProxyModel *m_filteredProxyModel; QRegExp *m_regexpFilter; + QString m_compileLog; + + NLLIGO::CLigoConfig m_ligoConfig; }; #endif // MISSION_COMPILER_MAIN_WINDOW_H diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/mission_compiler_plugin.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/mission_compiler_plugin.cpp index 005140391..7bd06de91 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/mission_compiler_plugin.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/mission_compiler_plugin.cpp @@ -62,10 +62,6 @@ void MissionCompilerPlugin::extensionsInitialized() //settings->beginGroup(Core::Constants::DATA_PATH_SECTION); //QString ligoConfigFile = settings->value(Core::Constants::DATA_PATH_SECTION).toString(); //settings->beginGroup(Core::Constants::DATA_PATH_SECTION); - - NLLIGO::Register(); - LigoConfig.readPrimitiveClass(NLMISC::CPath::lookup("world_editor_classes.xml").c_str(), false); - NLLIGO::CPrimitiveContext::instance().CurrentLigoConfig = &LigoConfig; } void MissionCompilerPlugin::setNelContext(NLMISC::INelContext *nelContext) diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/mission_compiler_plugin.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/mission_compiler_plugin.h index a9d34bd13..773c984ea 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/mission_compiler_plugin.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/mission_compiler_plugin.h @@ -9,8 +9,6 @@ // NeL includes #include #include -#include -#include // Qt includes #include @@ -53,8 +51,6 @@ public: QObject *objectByName(const QString &name) const; ExtensionSystem::IPluginSpec *pluginByName(const QString &name) const; - NLLIGO::CLigoConfig LigoConfig; - protected: NLMISC::CLibraryContext *_LibContext; diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/validation_file.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/validation_file.cpp new file mode 100644 index 000000000..3e166da6b --- /dev/null +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/validation_file.cpp @@ -0,0 +1,133 @@ +#include "validation_file.h" + +#include +#include + +void CValidationFile::loadMissionValidationFile(std::string filename) +{ + // load the configuration file + NLMISC::CConfigFile cf; + std::string pathName = NLMISC::CPath::lookup(filename, false); + + if (pathName.empty()) + { + nlwarning("Can't find index file '%s' in search path, no mission will be valid", filename.c_str()); + return; + } + cf.load(pathName); + + // get the variable + NLMISC::CConfigFile::CVar* var = cf.getVarPtr("AuthorizedStates"); + if (var) + { + for (uint i=0; isize(); ++i) + _AuthorizedStates.push_back(var->asString(i)); + } + int missionStatesFields = 3; + var = cf.getVarPtr("MissionStatesFields"); + if (var) + missionStatesFields = var->asInt(); + else + nlwarning("Mission validation file does not contain MissionStatesFields variable. Parsing may fail and corrupt data."); + + var = cf.getVarPtr("MissionStates"); + if (var) + { + for (uint i=0; isize()/missionStatesFields; ++i) + { + std::string mission = var->asString(i*missionStatesFields); + std::string stateName = var->asString(i*missionStatesFields+1); + std::string hashKey = var->asString(i*missionStatesFields+2); + _MissionStates.insert(std::make_pair(mission, CMissionState(mission, stateName, hashKey))); + } + } +} + +void CValidationFile::saveMissionValidationFile(std::string filename) +{ + // load the configuration file + std::string pathName = NLMISC::CPath::lookup(filename, false); + + if (pathName.empty()) + { + nlwarning("Can't find index file '%s' in search path, no mission will be valid", filename.c_str()); + return; + } + FILE* file = fopen(pathName.c_str(), "w"); + nlassert(file!=NULL); + + // AuthorizedStates + fprintf(file, "%s", + "// AuthorizedStates contains the list of authorized states. EGS mission\n" + "// manager can accept any number of states. Default state is the first one.\n" + "AuthorizedStates = {\n"); + std::deque::iterator itAuth, itAuthEnd = _AuthorizedStates.end(); + for (itAuth=_AuthorizedStates.begin(); itAuth!=itAuthEnd; ++itAuth) + fprintf(file, "\t\"%s\",\n", itAuth->c_str()); + fprintf(file, "%s", "};\n\n"); + + // MissionStatesFields + fprintf(file, "%s", + "// MissionStatesFields contains the number of fields in MissionStates, for\n" + "// future compatibility purpose.\n" + "MissionStatesFields = "); + fprintf(file, "%d", 3); // 3 fields: name, state, hash key + fprintf(file, "%s", ";\n\n"); + + // MissionStates + fprintf(file, "%s", + "// MissionStates contains a list of mission with for each the state of the\n" + "// mission and its hash key. The tool will add new missions with the default\n" + "// state. It will flag missions with a modified hash key with default state to\n" + "// prevent untested modified missions to be published.\n" + "// :NOTE: You can add a field to this structure without the need to modify EGS\n" + "// code. Simply update MissionStatesFields.\n" + "MissionStates = {\n"); + TMissionStateContainer::iterator itMission, itMissionEnd = _MissionStates.end(); + for (itMission=_MissionStates.begin(); itMission!=itMissionEnd; ++itMission) + fprintf(file, "\t%-42s %-12s \"%s\",\n", ("\""+itMission->second.name+"\",").c_str(), ("\""+itMission->second.state+"\",").c_str(), itMission->second.hashKey.c_str()); + fprintf(file, "};\n\n"); + + fclose(file); +} + +// :NOTE: This function exists in mission_template.cpp. If you change it here modify the other file. +std::string buildHashKey(std::string const& content) +{ + uint32 sum = 0; + size_t size = content.length()/4; + for (size_t i=0; i>1 | 0x80000000; + else + sum = sum>>1; + } + return NLMISC::toString("0x%08X", sum); +} + +bool CMission::parsePrim(NLLIGO::IPrimitive const* prim) +{ + // init default values + std::vector* params; + // get the mission script + if (!prim->getPropertyByName("script", params) || !params) + { + nlwarning("ERROR : cant find mission script!!!!!!"); + return false; + } + + // parse them + std::string content; + std::vector::iterator itParam, itParamEnd = params->end(); + for (itParam=params->begin(); itParam!=itParamEnd; ++itParam) + { + content += *itParam + "\n"; + } + hashKey = buildHashKey(content); + return true; +} \ No newline at end of file diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/validation_file.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/validation_file.h new file mode 100644 index 000000000..73d661a26 --- /dev/null +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/mission_compiler/validation_file.h @@ -0,0 +1,51 @@ +#ifndef VALIDATION_FILE_H +#define VALIDATION_FILE_H + +#include +#include +#include + +#include + +struct CMissionState +{ + std::string name; + std::string state; + std::string hashKey; + CMissionState(std::string _name, std::string _state, std::string _hashKey) + : name(_name), state(_state), hashKey(_hashKey) { } +}; + +struct CMission +{ + std::string name; + std::string hashKey; + CMission(std::string _name, std::string _hashKey) + : name(_name), hashKey(_hashKey) { } + bool parsePrim(NLLIGO::IPrimitive const* prim); +}; + +class CValidationFile +{ +public: + typedef std::map TMissionStateContainer; + std::deque _AuthorizedStates; + TMissionStateContainer _MissionStates; +public: + // CValidationFile() { } + void loadMissionValidationFile(std::string filename); + void saveMissionValidationFile(std::string filename); + void insertMission(std::string const& mission, std::string const& hashKey) + { + _MissionStates.insert(std::make_pair(mission, CMissionState(mission, defaultState(), hashKey))); + } + std::string defaultState() + { + if (!_AuthorizedStates.empty()) + return _AuthorizedStates.front(); + else + return ""; + } +}; + +#endif // VALIDATION_FILE_H \ No newline at end of file