StelScriptMgr.cpp   StelScriptMgr.cpp 
skipping to change at line 26 skipping to change at line 26
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA. * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*/ */
#include "StelScriptMgr.hpp" #include "StelScriptMgr.hpp"
#include "StelMainScriptAPI.hpp" #include "StelMainScriptAPI.hpp"
#include "StelModuleMgr.hpp" #include "StelModuleMgr.hpp"
#include "LabelMgr.hpp" #include "LabelMgr.hpp"
#include "ScreenImageMgr.hpp" #include "ScreenImageMgr.hpp"
#include "StelActionMgr.hpp"
#include "StelTranslator.hpp"
#include "StelApp.hpp" #include "StelApp.hpp"
#include "StelCore.hpp" #include "StelCore.hpp"
#include "StelFileMgr.hpp" #include "StelFileMgr.hpp"
#include "StelModuleMgr.hpp" #include "StelModuleMgr.hpp"
#include "StelMovementMgr.hpp" #include "StelMovementMgr.hpp"
#include "StelSkyDrawer.hpp" #include "StelSkyDrawer.hpp"
#include "StelSkyLayerMgr.hpp" #include "StelSkyLayerMgr.hpp"
#include <QAction>
#include <QDateTime> #include <QDateTime>
#include <QDebug> #include <QDebug>
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
#include <QFileInfo> #include <QFileInfo>
#include <QRegExp> #include <QRegExp>
#include <QSet> #include <QSet>
#include <QStringList> #include <QStringList>
#include <QTemporaryFile> #include <QTemporaryFile>
#include <QVariant> #include <QVariant>
skipping to change at line 79 skipping to change at line 80
{ {
Vec3f c; Vec3f c;
c[0] = context->argument(0).toNumber(); c[0] = context->argument(0).toNumber();
c[1] = context->argument(1).toNumber(); c[1] = context->argument(1).toNumber();
c[2] = context->argument(2).toNumber(); c[2] = context->argument(2).toNumber();
return vec3fToScriptValue(engine, c); return vec3fToScriptValue(engine, c);
} }
StelScriptMgr::StelScriptMgr(QObject *parent): QObject(parent) StelScriptMgr::StelScriptMgr(QObject *parent): QObject(parent)
{ {
connect(&StelApp::getInstance(), SIGNAL(aboutToQuit()), this, SLOT(s topScript()), Qt::DirectConnection);
// Scripting images // Scripting images
ScreenImageMgr* scriptImages = new ScreenImageMgr(); ScreenImageMgr* scriptImages = new ScreenImageMgr();
scriptImages->init(); scriptImages->init();
StelApp::getInstance().getModuleMgr().registerModule(scriptImages); StelApp::getInstance().getModuleMgr().registerModule(scriptImages);
// Allow Vec3f managment in scripts // Allow Vec3f managment in scripts
qScriptRegisterMetaType(&engine, vec3fToScriptValue, vec3fFromScript Value); qScriptRegisterMetaType(&engine, vec3fToScriptValue, vec3fFromScript Value);
// Constructor // Constructor
QScriptValue ctor = engine.newFunction(createVec3f); QScriptValue ctor = engine.newFunction(createVec3f);
engine.globalObject().setProperty("Vec3f", ctor); engine.globalObject().setProperty("Vec3f", ctor);
skipping to change at line 133 skipping to change at line 135
// For accessing star scale, twinkle etc. // For accessing star scale, twinkle etc.
objectValue = engine.newQObject(StelApp::getInstance().getCore()->ge tSkyDrawer()); objectValue = engine.newQObject(StelApp::getInstance().getCore()->ge tSkyDrawer());
engine.globalObject().setProperty("StelSkyDrawer", objectValue); engine.globalObject().setProperty("StelSkyDrawer", objectValue);
setScriptRate(1.0); setScriptRate(1.0);
engine.setProcessEventsInterval(10); engine.setProcessEventsInterval(10);
agent = new StelScriptEngineAgent(&engine); agent = new StelScriptEngineAgent(&engine);
engine.setAgent(agent); engine.setAgent(agent);
initActions();
}
void StelScriptMgr::initActions()
{
StelActionMgr* actionMgr = StelApp::getInstance().getStelActionManag
er();
QSignalMapper* mapper = new QSignalMapper(this);
foreach(const QString script, getScriptList())
{
QString shortcut = getShortcut(script);
QString actionId = "actionScript/" + script;
StelAction* action = actionMgr->addAction(
actionId, N_("Scripts"), q_(getName(script).trimmed()),
mapper, "map()", shortcut);
mapper->setMapping(action, script);
}
connect(mapper, SIGNAL(mapped(QString)), this, SLOT(runScript(QStrin
g)));
} }
StelScriptMgr::~StelScriptMgr() StelScriptMgr::~StelScriptMgr()
{ {
} }
void StelScriptMgr::addModules() void StelScriptMgr::addModules()
{ {
// Add all the StelModules into the script engine // Add all the StelModules into the script engine
StelModuleMgr* mmgr = &StelApp::getInstance().getModuleMgr(); StelModuleMgr* mmgr = &StelApp::getInstance().getModuleMgr();
skipping to change at line 154 skipping to change at line 173
{ {
QScriptValue objectValue = engine.newQObject(m); QScriptValue objectValue = engine.newQObject(m);
engine.globalObject().setProperty(m->objectName(), objectVal ue); engine.globalObject().setProperty(m->objectName(), objectVal ue);
} }
} }
QStringList StelScriptMgr::getScriptList() QStringList StelScriptMgr::getScriptList()
{ {
QStringList scriptFiles; QStringList scriptFiles;
try
{ QSet<QString> files = StelFileMgr::listContents("scripts", StelFileM
QSet<QString> files = StelFileMgr::listContents("scripts", S gr::File, true);
telFileMgr::File, true);
foreach(QString f, files)
{
#ifdef ENABLE_STRATOSCRIPT_COMPAT #ifdef ENABLE_STRATOSCRIPT_COMPAT
QRegExp fileRE("^.*\\.(ssc|sts)$"); QRegExp fileRE("^.*\\.(ssc|sts)$");
#else // ENABLE_STRATOSCRIPT_COMPAT #else // ENABLE_STRATOSCRIPT_COMPAT
QRegExp fileRE("^.*\\.ssc$"); QRegExp fileRE("^.*\\.ssc$");
#endif // ENABLE_STRATOSCRIPT_COMPAT #endif // ENABLE_STRATOSCRIPT_COMPAT
if (fileRE.exactMatch(f)) foreach(const QString& f, files)
scriptFiles << f;
}
}
catch (std::runtime_error& e)
{ {
QString msg = QString("WARNING: could not list scripts: %1") if (fileRE.exactMatch(f))
.arg(e.what()); scriptFiles << f;
qWarning() << msg;
emit(scriptDebug(msg));
} }
return scriptFiles; return scriptFiles;
} }
bool StelScriptMgr::scriptIsRunning() bool StelScriptMgr::scriptIsRunning()
{ {
return engine.isEvaluating(); return engine.isEvaluating();
} }
QString StelScriptMgr::runningScriptId() QString StelScriptMgr::runningScriptId()
{ {
if (engine.isEvaluating()) if (engine.isEvaluating())
return scriptFileName; return scriptFileName;
else else
return QString(); return QString();
} }
const QString StelScriptMgr::getHeaderSingleLineCommentText(const QString& s, const QString& id, const QString& notFoundText) const QString StelScriptMgr::getHeaderSingleLineCommentText(const QString& s, const QString& id, const QString& notFoundText)
{ {
try QFile file(StelFileMgr::findFile("scripts/" + s, StelFileMgr::File))
;
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{ {
QFile file(StelFileMgr::findFile("scripts/" + s, StelFileMgr QString msg = QString("WARNING: script file %1 could not be
::File)); opened for reading").arg(QDir::toNativeSeparators(s));
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QString msg = QString("WARNING: script file %1 could
not be opened for reading").arg(QDir::toNativeSeparators(s));
emit(scriptDebug(msg));
qWarning() << msg;
return QString();
}
QRegExp nameExp("^\\s*//\\s*" + id + ":\\s*(.+)$");
while (!file.atEnd())
{
QString line = QString::fromUtf8(file.readLine());
if (nameExp.exactMatch(line))
{
file.close();
return nameExp.capturedTexts().at(1);
}
}
file.close();
return notFoundText;
}
catch(std::runtime_error& e)
{
QString msg = QString("WARNING: script file %1 could not be
found: %2").arg(QDir::toNativeSeparators(s)).arg(e.what());
emit(scriptDebug(msg)); emit(scriptDebug(msg));
qWarning() << msg; qWarning() << msg;
return QString(); return QString();
} }
QRegExp nameExp("^\\s*//\\s*" + id + ":\\s*(.+)$");
while (!file.atEnd())
{
QString line = QString::fromUtf8(file.readLine());
if (nameExp.exactMatch(line))
{
file.close();
return nameExp.capturedTexts().at(1);
}
}
file.close();
return notFoundText;
} }
const QString StelScriptMgr::getName(const QString& s) const QString StelScriptMgr::getName(const QString& s)
{ {
return getHeaderSingleLineCommentText(s, "Name", s); return getHeaderSingleLineCommentText(s, "Name", s);
} }
const QString StelScriptMgr::getAuthor(const QString& s) const QString StelScriptMgr::getAuthor(const QString& s)
{ {
return getHeaderSingleLineCommentText(s, "Author"); return getHeaderSingleLineCommentText(s, "Author");
} }
const QString StelScriptMgr::getLicense(const QString& s) const QString StelScriptMgr::getLicense(const QString& s)
{ {
return getHeaderSingleLineCommentText(s, "License", ""); return getHeaderSingleLineCommentText(s, "License", "");
} }
const QString StelScriptMgr::getShortcut(const QString& s)
{
return getHeaderSingleLineCommentText(s, "Shortcut", "").trimmed();
}
const QString StelScriptMgr::getDescription(const QString& s) const QString StelScriptMgr::getDescription(const QString& s)
{ {
try QFile file(StelFileMgr::findFile("scripts/" + s, StelFileMgr::File))
;
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QString msg = QString("WARNING: script file %1 could not be
opened for reading").arg(QDir::toNativeSeparators(s));
emit(scriptDebug(msg));
qWarning() << msg;
return QString();
}
QString desc = "";
bool inDesc = false;
QRegExp descExp("^\\s*//\\s*Description:\\s*([^\\s].+)\\s*$");
QRegExp descNewlineExp("^\\s*//\\s*$");
QRegExp descContExp("^\\s*//\\s*([^\\s].*)\\s*$");
while (!file.atEnd())
{ {
QFile file(StelFileMgr::findFile("scripts/" + s, StelFileMgr QString line = QString::fromUtf8(file.readLine());
::File)); if (!inDesc && descExp.exactMatch(line))
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{ {
QString msg = QString("WARNING: script file %1 could inDesc = true;
not be opened for reading").arg(QDir::toNativeSeparators(s)); desc = descExp.capturedTexts().at(1) + " ";
emit(scriptDebug(msg)); desc.replace("\n","");
qWarning() << msg;
return QString();
} }
else if (inDesc)
QString desc = "";
bool inDesc = false;
QRegExp descExp("^\\s*//\\s*Description:\\s*([^\\s].+)\\s*$"
);
QRegExp descNewlineExp("^\\s*//\\s*$");
QRegExp descContExp("^\\s*//\\s*([^\\s].*)\\s*$");
while (!file.atEnd())
{ {
QString line = QString::fromUtf8(file.readLine()); QString d("");
if (!inDesc && descExp.exactMatch(line)) if (descNewlineExp.exactMatch(line))
d = "\n";
else if (descContExp.exactMatch(line))
{ {
inDesc = true; d = descContExp.capturedTexts().at(1) + " ";
desc = descExp.capturedTexts().at(1) + " "; d.replace("\n","");
desc.replace("\n","");
} }
else if (inDesc) else
{ {
QString d(""); file.close();
if (descNewlineExp.exactMatch(line)) return desc;
d = "\n";
else if (descContExp.exactMatch(line))
{
d = descContExp.capturedTexts().at(1
) + " ";
d.replace("\n","");
}
else
{
file.close();
return desc;
}
desc += d;
} }
desc += d;
} }
file.close();
return desc;
}
catch(std::runtime_error& e)
{
QString msg = QString("WARNING: script file %1 could not be
found: %2").arg(QDir::toNativeSeparators(s)).arg(e.what());
emit(scriptDebug(msg));
qWarning() << msg;
return QString();
} }
file.close();
return desc;
} }
bool StelScriptMgr::runPreprocessedScript(const QString &preprocessedScript ) bool StelScriptMgr::runPreprocessedScript(const QString &preprocessedScript )
{ {
if (engine.isEvaluating()) if (engine.isEvaluating())
{ {
QString msg = QString("ERROR: there is already a script runn ing, please wait that it's over."); QString msg = QString("ERROR: there is already a script runn ing, please wait that it's over.");
emit(scriptDebug(msg)); emit(scriptDebug(msg));
qWarning() << msg; qWarning() << msg;
return false; return false;
skipping to change at line 327 skipping to change at line 323
// run that script // run that script
engine.evaluate(preprocessedScript); engine.evaluate(preprocessedScript);
scriptEnded(); scriptEnded();
return true; return true;
} }
// Run the script located at the given location // Run the script located at the given location
bool StelScriptMgr::runScript(const QString& fileName, const QString& inclu dePath) bool StelScriptMgr::runScript(const QString& fileName, const QString& inclu dePath)
{ {
QString absPath; QString absPath;
QString scriptDir;
try
{
if (QFileInfo(fileName).isAbsolute())
absPath = fileName;
else
absPath = StelFileMgr::findFile("scripts/" + fileNam
e);
scriptDir = QFileInfo(absPath).dir().path(); if (QFileInfo(fileName).isAbsolute())
} absPath = fileName;
catch (std::runtime_error& e) else
absPath = StelFileMgr::findFile("scripts/" + fileName);
if (absPath.isEmpty())
{ {
QString msg = QString("WARNING: could not find script file % 1: %2").arg(QDir::toNativeSeparators(fileName)).arg(e.what()); QString msg = QString("WARNING: could not find script file % 1").arg(QDir::toNativeSeparators(fileName));
emit(scriptDebug(msg)); emit(scriptDebug(msg));
qWarning() << msg; qWarning() << msg;
return false; return false;
} }
QString scriptDir = QFileInfo(absPath).dir().path();
QFile fic(absPath); QFile fic(absPath);
if (!fic.open(QIODevice::ReadOnly)) if (!fic.open(QIODevice::ReadOnly))
{ {
QString msg = QString("WARNING: cannot open script: %1").arg (QDir::toNativeSeparators(fileName)); QString msg = QString("WARNING: cannot open script: %1").arg (QDir::toNativeSeparators(fileName));
emit(scriptDebug(msg)); emit(scriptDebug(msg));
qWarning() << msg; qWarning() << msg;
return false; return false;
} }
scriptFileName = fileName; scriptFileName = fileName;
skipping to change at line 466 skipping to change at line 461
return false; return false;
else if (str.toLower() == "on" || str.toLower() == "yes") else if (str.toLower() == "on" || str.toLower() == "yes")
return true; return true;
return QVariant(str).toBool(); return QVariant(str).toBool();
} }
bool StelScriptMgr::preprocessScript(const QString &input, QString &output, const QString &scriptDir) bool StelScriptMgr::preprocessScript(const QString &input, QString &output, const QString &scriptDir)
{ {
QStringList lines = input.split("\n", QString::SkipEmptyParts); QStringList lines = input.split("\n", QString::SkipEmptyParts);
QRegExp includeRe("^include\\s*\\(\\s*\"([^\"]+)\"\\s*\\)\\s*;\\s*(/ /.*)?$"); QRegExp includeRe("^include\\s*\\(\\s*\"([^\"]+)\"\\s*\\)\\s*;\\s*(/ /.*)?$");
foreach (QString line, lines) foreach (const QString& line, lines)
{ {
if (includeRe.exactMatch(line)) if (includeRe.exactMatch(line))
{ {
QString fileName = includeRe.capturedTexts().at(1); QString fileName = includeRe.capturedTexts().at(1);
QString path; QString path;
// Search for the include file. Rules are: // Search for the include file. Rules are:
// 1. If path is absolute, just use that // 1. If path is absolute, just use that
// 2. If path is relative, look in scriptDir + inclu ded filename // 2. If path is relative, look in scriptDir + inclu ded filename
if (QFileInfo(fileName).isAbsolute()) if (QFileInfo(fileName).isAbsolute())
path = fileName; path = fileName;
else else
{ {
try path = StelFileMgr::findFile(scriptDir + "/"
{ + fileName);
path = StelFileMgr::findFile(scriptD if (path.isEmpty())
ir + "/" + fileName);
}
catch(std::runtime_error& e)
{ {
qWarning() << "WARNING: script inclu de:" << QDir::toNativeSeparators(fileName) << e.what(); qWarning() << "WARNING: script inclu de:" << QDir::toNativeSeparators(fileName);
return false; return false;
} }
} }
QFile fic(path); QFile fic(path);
bool ok = fic.open(QIODevice::ReadOnly); bool ok = fic.open(QIODevice::ReadOnly);
if (ok) if (ok)
{ {
qDebug() << "script include: " << QDir::toNa tiveSeparators(path); qDebug() << "script include: " << QDir::toNa tiveSeparators(path);
preprocessScript(fic, output, scriptDir); preprocessScript(fic, output, scriptDir);
 End of changes. 31 change blocks. 
115 lines changed or deleted 104 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/