-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.cpp
More file actions
363 lines (307 loc) · 10.2 KB
/
plugin.cpp
File metadata and controls
363 lines (307 loc) · 10.2 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "config.h"
#include "plugin.h"
#include "SIM.h"
#include "UI.h"
#include <simPlusPlus-2/Plugin.h>
#include "common.h"
#include "stubs.h"
#include <QtCore>
#include <QHostInfo>
class Plugin : public sim::Plugin
{
public:
void onInit()
{
if(sim::getIntProperty(sim_handle_app, "headlessMode"))
throw std::runtime_error("cannot load in headless mode");
if(!registerScriptStuff())
throw std::runtime_error("failed to register script stuff");
setExtVersion("Code Editor Plugin");
setBuildDate(BUILD_DATE);
// XXX: parameter doesn't exist when called later?
EditorOptions::resourcesPath = QString::fromStdString(sim::getStringProperty(sim_handle_app, "resourcePath"));
auto p = sim::getBoolProperty(sim_handle_app, "customData.simCmd.verboseErrors", {});
if(p)
verboseErrors = *p;
simThread();
sim = new SIM();
// development builds don't look up online docs:
int rev = sim::getIntProperty(sim_handle_app, "productVersionNb");
if((rev % 2) == 0)
{
QHostInfo::lookupHost("www.coppeliarobotics.com",
[=] (const QHostInfo &info)
{
if(info.error() == QHostInfo::NoError)
online = true;
}
);
}
}
void onCleanup()
{
delete sim; //sim->deleteLater(); crashes on quit
SIM_THREAD = NULL;
}
void onUIInit()
{
uiThread();
ui = new UI(sim);
}
void onUICleanup()
{
ui->deleteLater();
UI_THREAD = NULL;
}
void onSimulationAboutToStart()
{
sim->simulationRunning(true);
}
void onSimulationEnded()
{
sim->simulationRunning(false);
}
char * codeEditor_openModal(const char *initText, const char *properties, int *positionAndSize)
{ // special: blocking until dlg closed
ASSERT_THREAD(!UI);
sim::addLog(sim_verbosity_debug, "codeEditor_openModal: initText=%s, properties=%s", initText, properties);
QString text;
if(QThread::currentThreadId() == UI_THREAD)
{
ui->openModal(QString(initText), QString(properties),text, positionAndSize);
}
else
{
if(sim)
sim->openModal(QString(initText), QString(properties), text, positionAndSize);
}
char* retVal = stringBufferCopy(text);
sim::addLog(sim_verbosity_debug, "codeEditor_openModal: done");
return retVal;
}
int codeEditor_open(const char *initText, const char *properties)
{
sim::addLog(sim_verbosity_debug, "codeEditor_open: initText=%s, properties=%s", initText, properties);
int handle = -1;
if(QThread::currentThreadId() == UI_THREAD)
ui->open(QString(initText), QString(properties), &handle);
else
{
if(sim)
sim->open(QString(initText), QString(properties), &handle);
}
sim::addLog(sim_verbosity_debug, "codeEditor_open: done");
return handle;
}
int codeEditor_setText(int handle, const char *text, int insertMode)
{
sim::addLog(sim_verbosity_debug, "codeEditor_setText: handle=%d, text=%s, insertMode=%d", handle, text, insertMode);
if(QThread::currentThreadId() == UI_THREAD)
ui->setText(handle, QString(text), insertMode);
else
{
if(sim)
sim->setText(handle, QString(text), insertMode);
}
sim::addLog(sim_verbosity_debug, "codeEditor_setText: done");
return -1;
}
char * codeEditor_getText(int handle, int* posAndSize)
{
sim::addLog(sim_verbosity_debug, "codeEditor_getText: handle=%d", handle);
QString text;
if(QThread::currentThreadId() == UI_THREAD)
ui->getText(handle, &text, posAndSize);
else
{
if(sim)
sim->getText(handle, &text, posAndSize);
}
sim::addLog(sim_verbosity_debug, "codeEditor_getText: done");
return stringBufferCopy(text);
}
int codeEditor_show(int handle, int showState)
{
sim::addLog(sim_verbosity_debug, "codeEditor_getText: handle=%d, showState=%d", handle, showState);
if(QThread::currentThreadId() == UI_THREAD)
ui->show(handle, showState);
else
{
if(sim)
sim->show(handle, showState);
}
sim::addLog(sim_verbosity_debug, "codeEditor_show: done");
return -1;
}
int codeEditor_close(int handle, int *positionAndSize)
{
sim::addLog(sim_verbosity_debug, "codeEditor_close: handle=%d", handle);
if(QThread::currentThreadId() == UI_THREAD)
ui->close(handle, positionAndSize);
else
{
if(sim)
sim->close(handle, positionAndSize);
}
sim::addLog(sim_verbosity_debug, "codeEditor_close: done");
return -1;
}
QUrl apiReferenceForSymbol(const QString &sym)
{
// split symbol (e.g.: "sim.getObject" -> "sim", "getObject")
QString mod;
QString func(sym);
int dotPos = sym.indexOf('.');
if(dotPos >= 0)
{
mod = sym.left(dotPos);
func = sym.mid(dotPos + 1);
}
// locate local "manual" dir
QDir manual(QCoreApplication::applicationDirPath());
#ifdef MAC_SIM
#if SIM_PROGRAM_FULL_VERSION_NB < 4010000
if(!manual.cd("../../.."))
{
if(verboseErrors)
sim::addLog(sim_verbosity_errors, "Bad directory layout (<4.1.0)");
return {};
}
#else
// since 4.1.0, we have app bundle layout:
if(!manual.cd("../Resources"))
{
if(verboseErrors)
sim::addLog(sim_verbosity_errors, "Bad directory layout (can't locate \"Resources\" dir)");
return {};
}
#endif // SIM_PROGRAM_FULL_VERSION_NB
#endif // MAC_SIM
if(!manual.cd("manual"))
{
if(verboseErrors)
sim::addLog(sim_verbosity_errors, "Bad directory layout (can't locate \"manual\" dir)");
return {};
}
// read index/<mod>.json file
QDir idxDir(manual);
if(!idxDir.cd("index"))
{
if(verboseErrors)
sim::addLog(sim_verbosity_errors, "Bad directory layout (missing \"index\" dir inside \"manual\" dir)");
return {};
}
QString idx = (mod.isEmpty() ? "__global__" : mod) + ".json";
if(!idxDir.exists(idx))
{
if(verboseErrors)
sim::addLog(sim_verbosity_errors, "File %s not found", idx.toStdString());
return {};
}
QFile idxFile(idxDir.filePath(idx));
if(!idxFile.open(QIODevice::ReadOnly))
{
if(verboseErrors)
sim::addLog(sim_verbosity_errors, "Could not open index file %s for reading", idx.toStdString());
return {};
}
QByteArray idxData = idxFile.readAll();
QJsonDocument idxDoc(QJsonDocument::fromJson(idxData));
QJsonObject idxObj(idxDoc.object());
QJsonObject::const_iterator i;
if(!mod.isEmpty())
{
// look for a <mod> key, with an object value
i = idxObj.constFind(mod);
if(i == idxObj.constEnd())
{
if(verboseErrors)
sim::addLog(sim_verbosity_errors, "Bad index file %s (missing \"%s\" key)", idx.toStdString(), mod.toStdString());
return {};
}
idxObj = i.value().toObject();
}
// then for a <func> key, with a string value
i = idxObj.constFind(func);
if(i == idxObj.constEnd())
{
if(verboseErrors)
sim::addLog(sim_verbosity_errors, "Key \"%s\" not found in index file %s", func.toStdString(), idx.toStdString());
return {};
}
QString fileName = i.value().toString();
if(fileName.isNull())
{
if(verboseErrors)
sim::addLog(sim_verbosity_errors, "Bad key \"%s\" in index file %s (not a string)", func.toStdString(), idx.toStdString());
return {};
}
// split fragment if any
int anchorPos = fileName.lastIndexOf("#");
QString anchor;
if(anchorPos >= 0)
{
anchor = fileName.mid(anchorPos + 1);
fileName = fileName.left(anchorPos);
}
// compose online/offline URL
QUrl url;
if(isOnline())
{
url.setScheme("https");
url.setHost("manual.coppeliarobotics.com");
url.setPath("/en/" + fileName);
}
else
{
url.setScheme("file");
url.setPath(manual.absolutePath() + "/en/" + fileName);
}
if(!anchor.isEmpty())
url.setFragment(anchor);
return url;
}
bool isOnline() const
{
return online;
}
private:
UI *ui;
SIM *sim;
bool online = false;
bool verboseErrors = false;
};
SIM_UI_PLUGIN(Plugin)
QUrl apiReferenceForSymbol(const QString &sym)
{
return sim::plugin->apiReferenceForSymbol(sym);
}
bool isOnline()
{
return sim::plugin->isOnline();
}
// plugin entrypoints:
SIM_DLLEXPORT char * codeEditor_openModal(const char *initText, const char *properties, int *positionAndSize)
{
return sim::plugin->codeEditor_openModal(initText, properties, positionAndSize);
}
SIM_DLLEXPORT int codeEditor_open(const char *initText, const char *properties)
{
return sim::plugin->codeEditor_open(initText, properties);
}
SIM_DLLEXPORT int codeEditor_setText(int handle, const char *text, int insertMode)
{
return sim::plugin->codeEditor_setText(handle, text, insertMode);
}
SIM_DLLEXPORT char * codeEditor_getText(int handle, int *positionAndSize)
{
return sim::plugin->codeEditor_getText(handle,positionAndSize);
}
SIM_DLLEXPORT int codeEditor_show(int handle, int showState)
{
return sim::plugin->codeEditor_show(handle, showState);
}
SIM_DLLEXPORT int codeEditor_close(int handle, int *positionAndSize)
{
return sim::plugin->codeEditor_close(handle, positionAndSize);
}