forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlegacyscript.cpp
More file actions
113 lines (97 loc) · 2.59 KB
/
legacyscript.cpp
File metadata and controls
113 lines (97 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 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
{
namespace 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.string());
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;
}
}
}