/
scriptmanager.h
123 lines (92 loc) · 2.99 KB
/
scriptmanager.h
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
/*
* scriptmanager.h
* Copyright 2018, Thorbjørn Lindeijer <bjorn@lindeijer.nl>
*
* This file is part of Tiled.
*
* 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.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "filesystemwatcher.h"
#include <QJSValue>
#include <QObject>
#include <QStringList>
class QJSEngine;
namespace Tiled {
class ScriptModule;
/**
* Singleton for managing the script engine and module.
*
* Dependencies: ProjectManager, DocumentManager (optional)
*/
class ScriptManager : public QObject
{
Q_OBJECT
Q_PROPERTY(bool projectExtensionsSuppressed READ projectExtensionsSuppressed NOTIFY projectExtensionsSuppressedChanged)
public:
static ScriptManager &instance();
static void deleteInstance();
void ensureInitialized();
const QString &extensionsPath() const;
ScriptModule *module() const;
QJSEngine *engine() const;
QJSValue evaluate(const QString &program,
const QString &fileName = QString(), int lineNumber = 1);
QJSValue evaluateFile(const QString &fileName);
/**
* Create a new global identifier ($0, $1, $2, ...) for the value. Returns
* the name of the identifier.
*/
QString createTempValue(const QJSValue &value);
bool checkError(QJSValue value, const QString &program = QString());
void throwError(const QString &message);
void throwNullArgError(int argNumber);
void refreshExtensionsPaths();
bool projectExtensionsSuppressed() const;
signals:
void projectExtensionsSuppressedChanged(bool);
private:
explicit ScriptManager(QObject *parent = nullptr);
~ScriptManager() = default;
void reset();
void initialize();
void scriptFilesChanged(const QStringList &scriptFiles);
void loadExtensions();
void loadExtension(const QString &path);
QJSEngine *mEngine = nullptr;
ScriptModule *mModule = nullptr;
FileSystemWatcher mWatcher;
QString mExtensionsPath;
QStringList mExtensionsPaths;
int mTempCount = 0;
bool mProjectExtensionsSuppressed = false;
static ScriptManager *mInstance;
};
inline const QString &ScriptManager::extensionsPath() const
{
return mExtensionsPath;
}
inline ScriptModule *ScriptManager::module() const
{
return mModule;
}
inline QJSEngine *ScriptManager::engine() const
{
return mEngine;
}
inline bool ScriptManager::projectExtensionsSuppressed() const
{
return mProjectExtensionsSuppressed;
}
} // namespace Tiled