StelScriptMgr.cpp   StelScriptMgr.cpp 
skipping to change at line 52 skipping to change at line 52
#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>
#include <cmath> #include <cmath>
Q_DECLARE_METATYPE(Vec3f)
QScriptValue vec3fToScriptValue(QScriptEngine *engine, const Vec3f& c) QScriptValue vec3fToScriptValue(QScriptEngine *engine, const Vec3f& c)
{ {
QScriptValue obj = engine->newObject(); QScriptValue obj = engine->newObject();
obj.setProperty("r", QScriptValue(engine, c[0])); obj.setProperty("r", QScriptValue(engine, c[0]));
obj.setProperty("g", QScriptValue(engine, c[1])); obj.setProperty("g", QScriptValue(engine, c[1]));
obj.setProperty("b", QScriptValue(engine, c[2])); obj.setProperty("b", QScriptValue(engine, c[2]));
return obj; return obj;
} }
void vec3fFromScriptValue(const QScriptValue& obj, Vec3f& c) void vec3fFromScriptValue(const QScriptValue& obj, Vec3f& c)
skipping to change at line 188 skipping to change at line 186
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()) return scriptFileName;
return scriptFileName;
else
return QString();
} }
const QString StelScriptMgr::getHeaderSingleLineCommentText(const QString& s, const QString& id, const QString& notFoundText) QString StelScriptMgr::getHeaderSingleLineCommentText(const QString& s, con st QString& id, const QString& notFoundText) const
{ {
QFile file(StelFileMgr::findFile("scripts/" + s, StelFileMgr::File)) ; QFile file(StelFileMgr::findFile("scripts/" + s, StelFileMgr::File)) ;
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{ {
QString msg = QString("WARNING: script file %1 could not be opened for reading").arg(QDir::toNativeSeparators(s)); QString msg = QString("WARNING: script file %1 could not be opened for reading").arg(QDir::toNativeSeparators(s));
emit(scriptDebug(msg)); emit(scriptDebug(msg));
qWarning() << msg; qWarning() << msg;
return QString(); return QString();
} }
//use a buffered stream instead of QFile::readLine - much faster!
QTextStream textStream(&file);
textStream.setCodec("UTF-8");
QRegExp nameExp("^\\s*//\\s*" + id + ":\\s*(.+)$"); QRegExp nameExp("^\\s*//\\s*" + id + ":\\s*(.+)$");
while (!file.atEnd()) while (!textStream.atEnd())
{ {
QString line = QString::fromUtf8(file.readLine()); QString line = textStream.readLine();
if (nameExp.exactMatch(line)) if (nameExp.exactMatch(line))
{ {
file.close(); file.close();
return nameExp.capturedTexts().at(1); return nameExp.capturedTexts().at(1).trimmed();
} }
} }
file.close(); file.close();
return notFoundText; return notFoundText;
} }
const QString StelScriptMgr::getName(const QString& s) QString StelScriptMgr::getHtmlDescription(const QString &s, bool generateDo
cumentTags) const
{
QString html;
if (generateDocumentTags)
html += "<html><body>";
html += "<h2>" + q_(getName(s).trimmed()) + "</h2>";
QString d = getDescription(s).trimmed();
d.replace("\n", "<br />");
d.replace(QRegExp("\\s{2,}"), " ");
html += "<p>" + q_(d) + "</p>";
html += "<p>";
QString author = getAuthor(s).trimmed();
if (!author.isEmpty())
{
html += "<strong>" + q_("Author") + "</strong>: " + author +
"<br />";
}
QString license = getLicense(s).trimmed();
if (!license.isEmpty())
{
html += "<strong>" + q_("License") + "</strong>: " + license
+ "<br />";
}
QString shortcut = getShortcut(s).trimmed();
if (!shortcut.isEmpty())
{
html += "<strong>" + q_("Shortcut") + "</strong>: " + shortc
ut;
}
html += "</p>";
if (generateDocumentTags)
html += "</body></html>";
return html;
}
QString StelScriptMgr::getName(const QString& s) const
{ {
return getHeaderSingleLineCommentText(s, "Name", s); return getHeaderSingleLineCommentText(s, "Name", s);
} }
const QString StelScriptMgr::getAuthor(const QString& s) QString StelScriptMgr::getAuthor(const QString& s) const
{ {
return getHeaderSingleLineCommentText(s, "Author"); return getHeaderSingleLineCommentText(s, "Author");
} }
const QString StelScriptMgr::getLicense(const QString& s) QString StelScriptMgr::getLicense(const QString& s) const
{ {
return getHeaderSingleLineCommentText(s, "License", ""); return getHeaderSingleLineCommentText(s, "License", "");
} }
const QString StelScriptMgr::getShortcut(const QString& s) QString StelScriptMgr::getShortcut(const QString& s) const
{ {
return getHeaderSingleLineCommentText(s, "Shortcut", "").trimmed(); return getHeaderSingleLineCommentText(s, "Shortcut", "");
} }
const QString StelScriptMgr::getDescription(const QString& s) QString StelScriptMgr::getDescription(const QString& s) const
{ {
QFile file(StelFileMgr::findFile("scripts/" + s, StelFileMgr::File)) ; QFile file(StelFileMgr::findFile("scripts/" + s, StelFileMgr::File)) ;
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{ {
QString msg = QString("WARNING: script file %1 could not be opened for reading").arg(QDir::toNativeSeparators(s)); QString msg = QString("WARNING: script file %1 could not be opened for reading").arg(QDir::toNativeSeparators(s));
emit(scriptDebug(msg)); emit(scriptDebug(msg));
qWarning() << msg; qWarning() << msg;
return QString(); return QString();
} }
//use a buffered stream instead of QFile::readLine - much faster!
QTextStream textStream(&file);
textStream.setCodec("UTF-8");
QString desc = ""; QString desc = "";
bool inDesc = false; bool inDesc = false;
QRegExp descExp("^\\s*//\\s*Description:\\s*([^\\s].+)\\s*$"); QRegExp descExp("^\\s*//\\s*Description:\\s*([^\\s].+)\\s*$");
QRegExp descNewlineExp("^\\s*//\\s*$"); QRegExp descNewlineExp("^\\s*//\\s*$");
QRegExp descContExp("^\\s*//\\s*([^\\s].*)\\s*$"); QRegExp descContExp("^\\s*//\\s*([^\\s].*)\\s*$");
while (!file.atEnd()) while (!textStream.atEnd())
{ {
QString line = QString::fromUtf8(file.readLine()); QString line = textStream.readLine();
if (!inDesc && descExp.exactMatch(line)) if (!inDesc && descExp.exactMatch(line))
{ {
inDesc = true; inDesc = true;
desc = descExp.capturedTexts().at(1) + " "; desc = descExp.capturedTexts().at(1) + " ";
desc.replace("\n",""); desc.replace("\n","");
} }
else if (inDesc) else if (inDesc)
{ {
QString d(""); QString d("");
if (descNewlineExp.exactMatch(line)) if (descNewlineExp.exactMatch(line))
skipping to change at line 283 skipping to change at line 321
} }
else else
{ {
file.close(); file.close();
return desc; return desc;
} }
desc += d; desc += d;
} }
} }
file.close(); file.close();
return desc; return desc.trimmed();
} }
bool StelScriptMgr::runPreprocessedScript(const QString &preprocessedScript ) bool StelScriptMgr::runPreprocessedScript(const QString &preprocessedScript , const QString& scriptId)
{ {
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 until it's over.");
emit(scriptDebug(msg)); emit(scriptDebug(msg));
qWarning() << msg; qWarning() << msg;
return false; return false;
} }
// Seed the PRNG so that script random numbers aren't always the sam
e sequence
qsrand(QDateTime::currentDateTime().toTime_t());
// Make sure that the gui object have been completely initialized (t here used to be problems with startup scripts). // Make sure that the gui objects have been completely initialized ( there used to be problems with startup scripts).
Q_ASSERT(StelApp::getInstance().getGui()); Q_ASSERT(StelApp::getInstance().getGui());
engine.globalObject().setProperty("scriptRateReadOnly", 1.0); engine.globalObject().setProperty("scriptRateReadOnly", 1.0);
scriptFileName = scriptId;
// Notify that the script starts here // Notify that the script starts here
emit(scriptRunning()); emit(scriptRunning());
emit runningScriptIdChanged(scriptId);
// 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 preprocessedScript;
prepareScript(preprocessedScript,fileName,includePath);
return runPreprocessedScript(preprocessedScript,fileName);
}
bool StelScriptMgr::runScriptDirect(const QString &scriptCode, const QStrin
g& includePath)
{
if(includePath.isNull())
return runPreprocessedScript(scriptCode, "<Direct script inp
ut>");
else
{
QString path = includePath;
if(includePath.isEmpty())
path = QStringLiteral("scripts");
QString processed;
bool ok = preprocessScript(scriptCode,processed, path);
if(ok)
return runPreprocessedScript(processed, "<Direct scr
ipt input>");
return false;
}
}
bool StelScriptMgr::prepareScript(QString &script, const QString &fileName,
const QString &includePath)
{
QString absPath; QString absPath;
if (QFileInfo(fileName).isAbsolute()) if (QFileInfo(fileName).isAbsolute())
absPath = fileName; absPath = fileName;
else else
absPath = StelFileMgr::findFile("scripts/" + fileName); absPath = StelFileMgr::findFile("scripts/" + fileName);
if (absPath.isEmpty()) if (absPath.isEmpty())
{ {
QString msg = QString("WARNING: could not find script file % 1").arg(QDir::toNativeSeparators(fileName)); QString msg = QString("WARNING: could not find script file % 1").arg(QDir::toNativeSeparators(fileName));
skipping to change at line 341 skipping to change at line 405
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;
if (!includePath.isEmpty()) if (!includePath.isEmpty())
scriptDir = includePath; scriptDir = includePath;
QString preprocessedScript;
bool ok = false; bool ok = false;
if (fileName.endsWith(".ssc")) if (fileName.endsWith(".ssc"))
ok = preprocessScript(fic, preprocessedScript, scriptDir); ok = preprocessScript(fic, script, scriptDir);
if (!ok) if (!ok)
{ {
return false; return false;
} }
return runPreprocessedScript(preprocessedScript);
return true;
} }
void StelScriptMgr::stopScript() void StelScriptMgr::stopScript()
{ {
if (engine.isEvaluating()) if (engine.isEvaluating())
{ {
GETSTELMODULE(LabelMgr)->deleteAllLabels(); GETSTELMODULE(LabelMgr)->deleteAllLabels();
GETSTELMODULE(ScreenImageMgr)->deleteAllImages(); GETSTELMODULE(ScreenImageMgr)->deleteAllImages();
if (agent->getPauseScript()) { if (agent->getPauseScript()) {
agent->setPauseScript(false); agent->setPauseScript(false);
skipping to change at line 425 skipping to change at line 489
StelScriptOutput::writeLog(msg); StelScriptOutput::writeLog(msg);
emit(scriptOutput(msg)); emit(scriptOutput(msg));
} }
void StelScriptMgr::resetOutput(void) void StelScriptMgr::resetOutput(void)
{ {
StelScriptOutput::reset(); StelScriptOutput::reset();
emit(scriptOutput("")); emit(scriptOutput(""));
} }
void StelScriptMgr::saveOutputAs(const QString &filename)
{
StelScriptOutput::saveOutputAs(filename);
}
void StelScriptMgr::scriptEnded() void StelScriptMgr::scriptEnded()
{ {
if (engine.hasUncaughtException()) if (engine.hasUncaughtException())
{ {
QString msg = QString("script error: \"%1\" @ line %2").arg( engine.uncaughtException().toString()).arg(engine.uncaughtExceptionLineNumb er()); QString msg = QString("script error: \"%1\" @ line %2").arg( engine.uncaughtException().toString()).arg(engine.uncaughtExceptionLineNumb er());
emit(scriptDebug(msg)); emit(scriptDebug(msg));
qWarning() << msg; qWarning() << msg;
} }
GETSTELMODULE(StelMovementMgr)->setMovementSpeedFactor(1.0); GETSTELMODULE(StelMovementMgr)->setMovementSpeedFactor(1.0);
scriptFileName = QString();
emit runningScriptIdChanged(scriptFileName);
emit(scriptStopped()); emit(scriptStopped());
} }
QMap<QString, QString> StelScriptMgr::mappify(const QStringList& args, bool lowerKey) QMap<QString, QString> StelScriptMgr::mappify(const QStringList& args, bool lowerKey)
{ {
QMap<QString, QString> map; QMap<QString, QString> map;
for(int i=0; i+1<args.size(); i++) for(int i=0; i+1<args.size(); i++)
if (lowerKey) if (lowerKey)
map[args.at(i).toLower()] = args.at(i+1); map[args.at(i).toLower()] = args.at(i+1);
else else
skipping to change at line 503 skipping to change at line 574
qWarning() << "WARNING: could not open scrip t include file for reading:" << QDir::toNativeSeparators(path); qWarning() << "WARNING: could not open scrip t include file for reading:" << QDir::toNativeSeparators(path);
return false; return false;
} }
} }
else else
{ {
output += line; output += line;
output += '\n'; output += '\n';
} }
} }
if (qApp->property("verbose")==true)
{
// Debug to find stupid errors. The line usually reported ma
y be off due to the preprocess stage.
QStringList outputList=output.split('\n');
qDebug() << "Script after preprocessing:";
int lineIdx=0;
foreach (const QString& line, outputList)
{
qDebug() << lineIdx << ":" << line;
lineIdx++;
}
}
return true; return true;
} }
bool StelScriptMgr::preprocessScript(QFile &input, QString& output, const Q String& scriptDir) bool StelScriptMgr::preprocessScript(QFile &input, QString& output, const Q String& scriptDir)
{ {
QString s = QString::fromUtf8(input.readAll()); QString s = QString::fromUtf8(input.readAll());
return preprocessScript(s, output, scriptDir); return preprocessScript(s, output, scriptDir);
} }
StelScriptEngineAgent::StelScriptEngineAgent(QScriptEngine *engine) StelScriptEngineAgent::StelScriptEngineAgent(QScriptEngine *engine)
 End of changes. 31 change blocks. 
29 lines changed or deleted 121 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/