StelScriptMgr.cpp   StelScriptMgr.cpp 
skipping to change at line 23 skipping to change at line 23
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* 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 "LabelMgr.hpp"
#include "ScreenImageMgr.hpp" #include "ScreenImageMgr.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"
skipping to change at line 117 skipping to change at line 119
//! @param dt the date string to use //! @param dt the date string to use
//! @param spec "local" or "utc" //! @param spec "local" or "utc"
engine.evaluate("function mywaitFor__(dt, spec) {if (!spec) spec=\"u tc\";" engine.evaluate("function mywaitFor__(dt, spec) {if (!spec) spec=\"u tc\";"
" var JD = core.jdFromDateString(dt, spec);" " var JD = core.jdFromDateString(dt, spec);"
" var timeSpeed = core.getTimeRate();" " var timeSpeed = core.getTimeRate();"
" if (timeSpeed == 0.) {core.debug(\"waitFor called with no ti me passing - would be infinite. not waiting!\"); return;}" " if (timeSpeed == 0.) {core.debug(\"waitFor called with no ti me passing - would be infinite. not waiting!\"); return;}"
" if (timeSpeed > 0) {core.wait((JD-core.getJDay())*timeSpeed) ;}" " if (timeSpeed > 0) {core.wait((JD-core.getJDay())*timeSpeed) ;}"
" else {core.wait((core.getJDay()-JD)*timeSpeed);}}"); " else {core.wait((core.getJDay()-JD)*timeSpeed);}}");
engine.evaluate("core['waitFor'] = mywaitFor__;"); engine.evaluate("core['waitFor'] = mywaitFor__;");
// Add all the StelModules into the script engine
StelModuleMgr* mmgr = &StelApp::getInstance().getModuleMgr();
foreach (StelModule* m, mmgr->getAllModules())
{
objectValue = engine.newQObject(m);
engine.globalObject().setProperty(m->objectName(), objectVal
ue);
}
// Add other classes which we want to be directly accessible from sc ripts // Add other classes which we want to be directly accessible from sc ripts
if(StelSkyLayerMgr* smgr = GETSTELMODULE(StelSkyLayerMgr)) if(StelSkyLayerMgr* smgr = GETSTELMODULE(StelSkyLayerMgr))
objectValue = engine.newQObject(smgr); objectValue = engine.newQObject(smgr);
// 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);
} }
StelScriptMgr::~StelScriptMgr() StelScriptMgr::~StelScriptMgr()
{ {
} }
void StelScriptMgr::addModules()
{
// Add all the StelModules into the script engine
StelModuleMgr* mmgr = &StelApp::getInstance().getModuleMgr();
foreach (StelModule* m, mmgr->getAllModules())
{
QScriptValue objectValue = engine.newQObject(m);
engine.globalObject().setProperty(m->objectName(), objectVal
ue);
}
}
QStringList StelScriptMgr::getScriptList() QStringList StelScriptMgr::getScriptList()
{ {
QStringList scriptFiles; QStringList scriptFiles;
try try
{ {
QSet<QString> files = StelFileMgr::listContents("scripts", S telFileMgr::File, true); QSet<QString> files = StelFileMgr::listContents("scripts", S telFileMgr::File, true);
foreach(QString f, files) foreach(QString f, files)
{ {
#ifdef ENABLE_STRATOSCRIPT_COMPAT #ifdef ENABLE_STRATOSCRIPT_COMPAT
QRegExp fileRE("^.*\\.(ssc|sts)$"); QRegExp fileRE("^.*\\.(ssc|sts)$");
skipping to change at line 292 skipping to change at line 297
} }
catch(std::runtime_error& e) catch(std::runtime_error& e)
{ {
QString msg = QString("WARNING: script file %1 could not be found: %2").arg(s).arg(e.what()); QString msg = QString("WARNING: script file %1 could not be found: %2").arg(s).arg(e.what());
emit(scriptDebug(msg)); emit(scriptDebug(msg));
qWarning() << msg; qWarning() << msg;
return QString(); return QString();
} }
} }
// Run the script located at the given location bool StelScriptMgr::runPreprocessedScript(const QString &preprocessedScript
bool StelScriptMgr::runScript(const QString& fileName, const QString& inclu )
dePath)
{ {
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;
} }
// 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).
Q_ASSERT(StelApp::getInstance().getGui());
engine.globalObject().setProperty("scriptRateReadOnly", 1.0);
// Notify that the script starts here
emit(scriptRunning());
// run that script
engine.evaluate(preprocessedScript);
scriptEnded();
return true;
}
// Run the script located at the given location
bool StelScriptMgr::runScript(const QString& fileName, const QString& inclu
dePath)
{
QString absPath; QString absPath;
QString scriptDir; QString scriptDir;
try try
{ {
if (QFileInfo(fileName).isAbsolute()) if (QFileInfo(fileName).isAbsolute())
absPath = fileName; absPath = fileName;
else else
absPath = StelFileMgr::findFile("scripts/" + fileNam e); absPath = StelFileMgr::findFile("scripts/" + fileNam e);
scriptDir = QFileInfo(absPath).dir().path(); scriptDir = QFileInfo(absPath).dir().path();
skipping to change at line 332 skipping to change at line 356
{ {
QString msg = QString("WARNING: cannot open script: %1").arg (fileName); QString msg = QString("WARNING: cannot open script: %1").arg (fileName);
emit(scriptDebug(msg)); emit(scriptDebug(msg));
qWarning() << msg; qWarning() << msg;
return false; return false;
} }
scriptFileName = fileName; scriptFileName = fileName;
if (!includePath.isEmpty()) if (!includePath.isEmpty())
scriptDir = includePath; scriptDir = includePath;
// 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).
Q_ASSERT(StelApp::getInstance().getGui());
engine.globalObject().setProperty("scriptRateReadOnly", 1.0);
// Notify that the script starts here although we still have to prep
rocess it.
emit(scriptRunning());
QString preprocessedScript; QString preprocessedScript;
bool ok=false; bool ok = false;
if (fileName.endsWith(".ssc")) if (fileName.endsWith(".ssc"))
ok = preprocessScript(fic, preprocessedScript, scriptDir); ok = preprocessScript(fic, preprocessedScript, scriptDir);
#ifdef ENABLE_STRATOSCRIPT_COMPAT #ifdef ENABLE_STRATOSCRIPT_COMPAT
else if (fileName.endsWith(".sts")) else if (fileName.endsWith(".sts"))
ok = preprocessStratoScript(fic, preprocessedScript, scriptD ir); ok = preprocessStratoScript(fic, preprocessedScript, scriptD ir);
#endif #endif
if (!ok) if (!ok)
{ {
scriptEnded();
return false; return false;
} }
return runPreprocessedScript(preprocessedScript);
// run that script
engine.evaluate(preprocessedScript);
scriptEnded();
return true;
} }
void StelScriptMgr::stopScript() void StelScriptMgr::stopScript()
{ {
if (engine.isEvaluating()) if (engine.isEvaluating())
{ {
GETSTELMODULE(LabelMgr)->deleteAllLabels();
GETSTELMODULE(ScreenImageMgr)->deleteAllImages();
if (agent->getPauseScript()) { if (agent->getPauseScript()) {
agent->setPauseScript(false); agent->setPauseScript(false);
} }
QString msg = QString("INFO: asking running script to exit") ; QString msg = QString("INFO: asking running script to exit") ;
emit(scriptDebug(msg)); emit(scriptDebug(msg));
//qDebug() << msg; //qDebug() << msg;
engine.abortEvaluation(); engine.abortEvaluation();
} }
scriptEnded(); scriptEnded();
} }
skipping to change at line 424 skipping to change at line 433
void StelScriptMgr::debug(const QString& msg) void StelScriptMgr::debug(const QString& msg)
{ {
emit(scriptDebug(msg)); emit(scriptDebug(msg));
} }
void StelScriptMgr::scriptEnded() void StelScriptMgr::scriptEnded()
{ {
if (engine.hasUncaughtException()) if (engine.hasUncaughtException())
{ {
QString msg = QString("script error: \"%1\" @ line %2").arg( // NOTE: engine.uncaughtExceptionLineNumber() gives incorrec
engine.uncaughtException().toString()).arg(engine.uncaughtExceptionLineNumb t line number (probably Qt bug). Correct line number can be
er()); // calculate as (engine.uncaughtExceptionLineNumber()+1)/2
// More info: https://bugs.launchpad.net/stellarium/+bug/104
6518 and
// http://sourceforge.net/projects/stellarium/forums/forum/2
78769/topic/5591465
QString msg = QString("script error: \"%1\" @ line %2").arg(
engine.uncaughtException().toString()).arg((engine.uncaughtExceptionLineNum
ber() + 1)/2);
emit(scriptDebug(msg)); emit(scriptDebug(msg));
qWarning() << msg; qWarning() << msg;
} }
GETSTELMODULE(StelMovementMgr)->setMovementSpeedFactor(1.0); GETSTELMODULE(StelMovementMgr)->setMovementSpeedFactor(1.0);
emit(scriptStopped()); emit(scriptStopped());
} }
QMap<QString, QString> StelScriptMgr::mappify(const QStringList& args, bool lowerKey) QMap<QString, QString> StelScriptMgr::mappify(const QStringList& args, bool lowerKey)
{ {
skipping to change at line 453 skipping to change at line 466
bool StelScriptMgr::strToBool(const QString& str) bool StelScriptMgr::strToBool(const QString& str)
{ {
if (str.toLower() == "off" || str.toLower() == "no") if (str.toLower() == "off" || str.toLower() == "no")
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(QFile& input, QString& output, const Q String& scriptDir) bool StelScriptMgr::preprocessScript(const QString &input, QString &output, const QString &scriptDir)
{ {
QStringList lines = input.split("\n", QString::SkipEmptyParts);
QRegExp includeRe("^include\\s*\\(\\s*\"([^\"]+)\"\\s*\\)\\s*;\\s*(/ /.*)?$"); QRegExp includeRe("^include\\s*\\(\\s*\"([^\"]+)\"\\s*\\)\\s*;\\s*(/ /.*)?$");
while (!input.atEnd()) foreach (QString line, lines)
{ {
QString line = QString::fromUtf8(input.readLine());
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;
skipping to change at line 504 skipping to change at line 517
} }
else else
{ {
output += line; output += line;
output += '\n'; output += '\n';
} }
} }
return true; return true;
} }
bool StelScriptMgr::preprocessScript(QFile &input, QString& output, const Q
String& scriptDir)
{
QString s = QString::fromUtf8(input.readAll());
return preprocessScript(s, output, scriptDir);
}
StelScriptEngineAgent::StelScriptEngineAgent(QScriptEngine *engine) StelScriptEngineAgent::StelScriptEngineAgent(QScriptEngine *engine)
: QScriptEngineAgent(engine) : QScriptEngineAgent(engine)
{ {
isPaused = false; isPaused = false;
} }
void StelScriptEngineAgent::positionChange(qint64 scriptId, int lineNumber, int columnNumber) void StelScriptEngineAgent::positionChange(qint64 scriptId, int lineNumber, int columnNumber)
{ {
Q_UNUSED(scriptId); Q_UNUSED(scriptId);
Q_UNUSED(lineNumber); Q_UNUSED(lineNumber);
 End of changes. 17 change blocks. 
41 lines changed or deleted 64 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/