-
Notifications
You must be signed in to change notification settings - Fork 716
/
EventsCodeGenerator.h
294 lines (260 loc) · 9.51 KB
/
EventsCodeGenerator.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
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
/*
* GDevelop JS Platform
* Copyright 2008-2016 Florian Rival (Florian.Rival@gmail.com). All rights
* reserved. This project is released under the MIT License.
*/
#ifndef EVENTSCODEGENERATOR_H
#define EVENTSCODEGENERATOR_H
#include <set>
#include <string>
#include <vector>
#include "GDCore/Events/CodeGeneration/EventsCodeGenerator.h"
#include "GDCore/Events/Event.h"
#include "GDCore/Events/InstructionsList.h"
namespace gd {
class ObjectsContainer;
class EventsFunction;
class ObjectMetadata;
class BehaviorMetadata;
class InstructionMetadata;
class ExpressionCodeGenerationInformation;
class EventsCodeGenerationContext;
} // namespace gd
namespace gdjs {
/**
* \brief The class being responsible for generating Javascript code from
* events.
*
* See also gd::EventsCodeGenerator.
*/
class EventsCodeGenerator : public gd::EventsCodeGenerator {
public:
/**
* Generate complete JS file for executing events of a scene
*
* \param project Project used
* \param scene Scene used
* \param events events of the scene
* \param includeFiles A reference to a set of strings where needed
* includes files will be stored.
* \param compilationForRuntime Set this to true if the code is generated for
* runtime.
*
* \return JavaScript code
*/
static gd::String GenerateSceneEventsCompleteCode(
gd::Project& project,
const gd::Layout& scene,
const gd::EventsList& events,
std::set<gd::String>& includeFiles,
bool compilationForRuntime = false);
/**
* Generate JavaScript for executing events in a function
*
* \param project Project used
* \param parameters The parameters of the function (objects will be deduced
* from these). \param events events of the scene \param compilationForRuntime
* Set this to true if the code is generated for runtime.
*
* \return JavaScript code
*/
static gd::String GenerateEventsFunctionCode(
gd::Project& project,
const gd::EventsFunction& eventsFunction,
const gd::String& codeNamespace,
std::set<gd::String>& includeFiles,
bool compilationForRuntime = false);
/**
* \brief Generate code for executing an event list
* \note To reduce the stress on JS engines, the code is generated inside
* a separate JS function (see
* gd::EventsCodeGenerator::AddCustomCodeOutsideMain). This method will return
* the code to call this separate function.
*
* \param events std::vector of events
* \param context Context used for generation
* \return Code
*/
virtual gd::String GenerateEventsListCode(
gd::EventsList& events, const gd::EventsCodeGenerationContext& context);
/**
* Generate code for executing a condition list
*
* \param game Game used
* \param scene Scene used
* \param conditions std::vector of conditions
* \param context Context used for generation
* \return JS code.
*/
virtual gd::String GenerateConditionsListCode(
gd::InstructionsList& conditions,
gd::EventsCodeGenerationContext& context);
/**
* \brief Generate the full name for accessing to a boolean variable used for
* conditions.
*/
virtual gd::String GenerateBooleanFullName(
const gd::String& boolName,
const gd::EventsCodeGenerationContext& context);
/**
* \brief Set a boolean to false.
*/
virtual gd::String GenerateBooleanInitializationToFalse(
const gd::String& boolName,
const gd::EventsCodeGenerationContext& context);
/**
* \brief Get the full name for accessing to a list of objects
*/
virtual gd::String GetObjectListName(
const gd::String& name, const gd::EventsCodeGenerationContext& context);
/**
* \brief Get the namespace to be used to store code generated
* objects/values/functions, with the extra "dot" at the end to be used to
* access to a property/member.
*
* Example: "gdjs.something."
*/
virtual gd::String GetCodeNamespaceAccessor() {
return GetCodeNamespace() + ".";
};
/**
* \brief Get the namespace to be used to store code generated
* objects/values/functions.
*
* Example: "gdjs.something"
*/
virtual gd::String GetCodeNamespace();
/**
* \brief Specify the code namespace to use, useful for functions as it is not
* autogenerated.
*
* Example: "gdjs.something"
*/
void SetCodeNamespace(const gd::String& codeNamespace_) {
codeNamespace = codeNamespace_;
};
protected:
virtual gd::String GenerateParameterCodes(
const gd::String& parameter,
const gd::ParameterMetadata& metadata,
gd::EventsCodeGenerationContext& context,
const gd::String& previousParameter,
std::vector<std::pair<gd::String, gd::String> >*
supplementaryParametersTypes);
virtual gd::String GenerateObjectFunctionCall(
gd::String objectListName,
const gd::ObjectMetadata& objMetadata,
const gd::ExpressionCodeGenerationInformation& codeInfo,
gd::String parametersStr,
gd::String defaultOutput,
gd::EventsCodeGenerationContext& context);
virtual gd::String GenerateObjectBehaviorFunctionCall(
gd::String objectListName,
gd::String behaviorName,
const gd::BehaviorMetadata& autoInfo,
const gd::ExpressionCodeGenerationInformation& codeInfo,
gd::String parametersStr,
gd::String defaultOutput,
gd::EventsCodeGenerationContext& context);
virtual gd::String GenerateFreeCondition(
const std::vector<gd::String>& arguments,
const gd::InstructionMetadata& instrInfos,
const gd::String& returnBoolean,
bool conditionInverted,
gd::EventsCodeGenerationContext& context);
virtual gd::String GenerateObjectCondition(
const gd::String& objectName,
const gd::ObjectMetadata& objInfo,
const std::vector<gd::String>& arguments,
const gd::InstructionMetadata& instrInfos,
const gd::String& returnBoolean,
bool conditionInverted,
gd::EventsCodeGenerationContext& context);
virtual gd::String GenerateBehaviorCondition(
const gd::String& objectName,
const gd::String& behaviorName,
const gd::BehaviorMetadata& autoInfo,
const std::vector<gd::String>& arguments,
const gd::InstructionMetadata& instrInfos,
const gd::String& returnBoolean,
bool conditionInverted,
gd::EventsCodeGenerationContext& context);
virtual gd::String GenerateObjectAction(
const gd::String& objectName,
const gd::ObjectMetadata& objInfo,
const std::vector<gd::String>& arguments,
const gd::InstructionMetadata& instrInfos,
gd::EventsCodeGenerationContext& context);
virtual gd::String GenerateBehaviorAction(
const gd::String& objectName,
const gd::String& behaviorName,
const gd::BehaviorMetadata& autoInfo,
const std::vector<gd::String>& arguments,
const gd::InstructionMetadata& instrInfos,
gd::EventsCodeGenerationContext& context);
virtual gd::String GenerateNegatedPredicat(const gd::String& predicat) const {
return "!(" + predicat + ")";
};
virtual gd::String GenerateReferenceToUpperScopeBoolean(
const gd::String& referenceName,
const gd::String& referencedBoolean,
gd::EventsCodeGenerationContext& context);
virtual gd::String GenerateObjectsDeclarationCode(
gd::EventsCodeGenerationContext& context);
virtual gd::String GenerateAllInstancesGetter(gd::String& objectName);
virtual gd::String GenerateProfilerSectionBegin(const gd::String& section);
virtual gd::String GenerateProfilerSectionEnd(const gd::String& section);
private:
/**
* \brief Generate the declarations of all the booleans required to run
* events.
*
* This should be called after generating events list code, so that the code
* generator knows all the booleans to be declared.
*/
gd::String GenerateAllConditionsBooleanDeclarations();
/**
* \brief Generate the declarations of all the objects list arrays.
*
* This should be called after generating events list code, with the maximum
* depth reached by events.
*/
std::pair<gd::String, gd::String> GenerateAllObjectsDeclarationsAndResets(
unsigned int maxDepthLevelReached);
/**
* \brief Add to include files all the files required by the object and their
* behaviors.
*/
void AddAllObjectsIncludeFiles();
/**
* \brief Generate the list of parameters of a function.
*
* \note runtimeScene is always added as the first parameter, and
* parentEventsFunctionContext as the last parameter.
*/
gd::String GenerateEventsFunctionParameterDeclarationsList(
const std::vector<gd::ParameterMetadata>& parameters);
/**
* \brief Generate the "eventsFunctionContext" object that allow a function
* to provides access objects, object creation and access to arguments from
* the rest of the events.
*/
gd::String GenerateEventsFunctionContext(
const std::vector<gd::ParameterMetadata>& parameters);
gd::String GenerateEventsFunctionReturn(
const gd::EventsFunction & eventFunction);
/**
* \brief Construct a code generator for the specified project and layout.
*/
EventsCodeGenerator(gd::Project& project, const gd::Layout& layout);
/**
* \brief Construct a code generator for the specified objects and groups.
*/
EventsCodeGenerator(gd::ObjectsContainer& globalObjectsAndGroups,
const gd::ObjectsContainer& objectsAndGroups);
virtual ~EventsCodeGenerator();
gd::String codeNamespace; ///< Optional namespace for the generated code,
///< used when generating events function.
};
} // namespace gdjs
#endif // EVENTSCODEGENERATOR_H