celestia/src/celscript/legacy/legacyscript.cpp

111 lines
2.6 KiB
C++

// legacyscript.cpp
//
// Copyright (C) 2019, the Celestia Development Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include <fstream>
#include <string>
#include <celcompat/filesystem.h>
#include <celestia/celestiacore.h>
#include <celutil/gettext.h>
#include "legacyscript.h"
#include "cmdparser.h"
#include "execution.h"
using namespace std;
namespace celestia::scripts
{
// Extremely basic implementation of an ExecutionEnvironment for
// running scripts.
class CoreExecutionEnvironment : public ExecutionEnvironment
{
private:
CelestiaCore& core;
public:
CoreExecutionEnvironment(CelestiaCore& _core) : core(_core)
{
}
Simulation* getSimulation() const
{
return core.getSimulation();
}
Renderer* getRenderer() const
{
return core.getRenderer();
}
CelestiaCore* getCelestiaCore() const
{
return &core;
}
void showText(string s, int horig, int vorig, int hoff, int voff,
double duration)
{
core.showText(s, horig, vorig, hoff, voff, duration);
}
};
LegacyScript::LegacyScript(CelestiaCore *core) :
m_appCore(core),
m_execEnv(new CoreExecutionEnvironment(*core))
{
}
bool LegacyScript::load(ifstream &scriptfile, const fs::path &/*path*/, string &errorMsg)
{
CommandParser parser(scriptfile, m_appCore->scriptMaps());
CommandSequence* script = parser.parse();
if (script == nullptr)
{
auto errors = parser.getErrors();
if (!errors.empty())
errorMsg = errors[0];
return false;
}
m_runningScript = unique_ptr<Execution>(new Execution(*script, *m_execEnv));
return true;
}
bool LegacyScript::tick(double dt)
{
return m_runningScript->tick(dt);
}
bool LegacyScriptPlugin::isOurFile(const fs::path &p) const
{
return p.extension() == ".cel";
}
unique_ptr<IScript> LegacyScriptPlugin::loadScript(const fs::path &path)
{
ifstream scriptfile(path);
if (!scriptfile.good())
{
appCore()->fatalError(_("Error opening script file."));
return nullptr;
}
auto script = unique_ptr<LegacyScript>(new LegacyScript(appCore()));
string errorMsg;
if (!script->load(scriptfile, path, errorMsg))
{
if (errorMsg.empty())
errorMsg = _("Unknown error loading script");
appCore()->fatalError(errorMsg);
return nullptr;
}
return script;
}
}