forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluascript.cpp
More file actions
285 lines (243 loc) · 7.4 KB
/
luascript.cpp
File metadata and controls
285 lines (243 loc) · 7.4 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// luascript.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 <set>
#include <string>
#include <fstream>
#include <fmt/printf.h>
#include <celcompat/filesystem.h>
#include <celephem/scriptobject.h>
#include <celestia/configfile.h>
#include <celestia/celestiacore.h>
#include <celutil/gettext.h>
#include "celx_internal.h"
#include "luascript.h"
using namespace std;
namespace celestia
{
namespace scripts
{
LuaScript::LuaScript(CelestiaCore *appcore) :
m_appCore(appcore),
m_celxScript(new LuaState)
{
m_celxScript->init(m_appCore);
}
LuaScript::~LuaScript()
{
m_celxScript->cleanup();
}
bool LuaScript::load(ifstream &scriptfile, const fs::path &path, string &errorMsg)
{
if (m_celxScript->loadScript(scriptfile, path) != 0)
{
errorMsg = m_celxScript->getErrorMessage();
return false;
}
return true;
}
bool LuaScript::handleMouseButtonEvent(float x, float y, int button, bool down)
{
return m_celxScript->handleMouseButtonEvent(x, y, button, down);
}
bool LuaScript::charEntered(const char* c)
{
return m_celxScript->charEntered(c);
}
bool LuaScript::handleKeyEvent(const char* key)
{
return m_celxScript->handleKeyEvent(key);
}
bool LuaScript::handleTickEvent(double dt)
{
return m_celxScript->handleTickEvent(dt);
}
bool LuaScript::tick(double dt)
{
return m_celxScript->tick(dt);
}
bool LuaScriptPlugin::isOurFile(const fs::path &p) const
{
auto ext = p.extension();
return ext == ".celx" || ext == ".clx";
}
unique_ptr<IScript> LuaScriptPlugin::loadScript(const fs::path &path)
{
ifstream scriptfile(path.string());
if (!scriptfile.good())
{
appCore()->fatalError(fmt::sprintf(_("Error opening script '%s'"), path));
return nullptr;
}
auto script = unique_ptr<LuaScript>(new LuaScript(appCore()));
string errMsg;
if (!script->load(scriptfile, path, errMsg))
{
if (errMsg.empty())
errMsg = _("Unknown error loading script");
appCore()->fatalError(errMsg);
return nullptr;
}
// Coroutine execution; control may be transferred between the
// script and Celestia's event loop
if (!script->m_celxScript->createThread())
{
appCore()->fatalError(_("Script coroutine initialization failed"));
return nullptr;
}
return script;
}
bool LuaHook::call(const char *method) const
{
return m_state->callLuaHook(appCore(), method);
}
bool LuaHook::call(const char *method, const char *keyName) const
{
return m_state->callLuaHook(appCore(), method, keyName);
}
bool LuaHook::call(const char *method, float x, float y) const
{
return m_state->callLuaHook(appCore(), method, x, y);
}
bool LuaHook::call(const char *method, float x, float y, int b) const
{
return m_state->callLuaHook(appCore(), method, x, y, b);
}
bool LuaHook::call(const char *method, double dt) const
{
return m_state->callLuaHook(appCore(), method, dt);
}
class LuaPathFinder
{
set<fs::path> dirs;
public:
const string getLuaPath() const
{
string out;
for (const auto& dir : dirs)
out += (dir / "?.lua;").string();
return out;
}
void process(const fs::path& p)
{
auto dir = p.parent_path();
if (p.extension() == ".lua" && dirs.count(dir) == 0)
dirs.insert(dir);
}
};
static string lua_path(const CelestiaConfig *config)
{
string LuaPath = "?.lua;celxx/?.lua;";
// Find the path for lua files in the extras directories
for (const auto& dir : config->extrasDirs)
{
if (dir.empty())
continue;
std::error_code ec;
if (!fs::is_directory(dir, ec))
{
fmt::fprintf(cerr, "Path %s doesn't exist or isn't a directory", dir);
continue;
}
LuaPathFinder loader;
auto iter = fs::recursive_directory_iterator(dir, ec);
for (; iter != end(iter); iter.increment(ec))
{
if (ec)
continue;
loader.process(*iter);
}
LuaPath += loader.getLuaPath();
}
return LuaPath;
}
// Initialize the Lua hook table as well as the Lua state for scripted
// objects. The Lua hook operates in a different Lua state than user loaded
// scripts. It always has file system access via the IO package. If the script
// system access policy is "allow", then scripted objects will run in the same
// Lua context as the Lua hook. Sharing state between scripted objects and the
// hook can be very useful, but it gives system access to scripted objects,
// and therefore must be restricted based on the system access policy.
bool CreateLuaEnvironment(CelestiaCore *appCore, const CelestiaConfig *config, ProgressNotifier *progressNotifier)
{
auto LuaPath = lua_path(config);
LuaState *luaHook = new LuaState();
luaHook->init(appCore);
// Always grant access for the Lua hook
luaHook->allowSystemAccess();
luaHook->setLuaPath(LuaPath);
int status = 0;
// Execute the Lua hook initialization script
if (!config->luaHook.empty())
{
ifstream scriptfile(config->luaHook.string());
if (!scriptfile.good())
appCore->fatalError(fmt::sprintf(_("Error opening LuaHook '%s'"), config->luaHook));
if (progressNotifier != nullptr)
progressNotifier->update(config->luaHook.string());
status = luaHook->loadScript(scriptfile, config->luaHook);
}
else
{
status = luaHook->loadScript("");
}
if (status != 0)
{
cerr << "lua hook load failed\n";
string errMsg = luaHook->getErrorMessage();
if (errMsg.empty())
errMsg = _("Unknown error loading hook script");
appCore->fatalError(errMsg);
luaHook = nullptr;
}
else
{
// Coroutine execution; control may be transferred between the
// script and Celestia's event loop
if (!luaHook->createThread())
{
cerr << "hook thread failed\n";
appCore->fatalError(_("Script coroutine initialization failed"));
luaHook = nullptr;
}
if (luaHook != nullptr)
{
auto lh = unique_ptr<LuaHook>(new LuaHook(appCore));
lh->m_state = unique_ptr<LuaState>(luaHook);
appCore->setScriptHook(std::move(lh));
while (!luaHook->tick(0.1)) ;
}
}
// Set up the script context; if the system access policy is allow,
// it will share the same context as the Lua hook. Otherwise, we
// create a private context.
if (config->scriptSystemAccessPolicy == "allow")
{
if (luaHook != nullptr)
SetScriptedObjectContext(luaHook->getState());
}
else
{
auto luaSandbox = new LuaState();
luaSandbox->init(appCore);
// Allow access to functions in package because we need 'require'
// But, loadlib is prohibited.
luaSandbox->allowLuaPackageAccess();
luaSandbox->setLuaPath(LuaPath);
status = luaSandbox->loadScript("");
if (status != 0)
{
luaSandbox = nullptr;
return false;
}
SetScriptedObjectContext(luaSandbox->getState());
}
return true;
}
}
}