-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate-pde-and-pjs.php
303 lines (269 loc) · 8.49 KB
/
generate-pde-and-pjs.php
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
<?php
/**
* Generate Game/Tetris.pde and Game/Tetris.pjs from all the java source files from Game/src folder.
* Tetris.pde can be run in Processing (applet version of the game).
* Tetris.pjs can be run in Processing.js (javascript version of the game).
*/
ob_start();
// Check ip.
$ip = $_SERVER['REMOTE_ADDR'];
if (!in_array($ip, array('127.0.0.1'))) {
return;
}
// Constants.
include 'define.php';
defined('DS') || define('DS', '\\');
defined('APP_PATH') || define('APP_PATH', realpath(dirname(__FILE__)) . DS);
// Parameters.
$fromDir = APP_PATH . 'Game' . DS . 'src';
$pdeFolder = APP_PATH . 'Game-pde' . DS . 'Tetris' . DS;
$pdeFilename = $pdeFolder . 'Tetris.pde';
$pjsFolder = APP_PATH . 'Game-pjs' . DS;
$pjsFilename = $pjsFolder . 'Tetris.pjs';
// Exclude folder/files:
$excludeFolders = array(
$fromDir . DS . 'Genetic',
);
// Content to remove.
$excludeContentOptions = array(
/***********
* BOTH
**********/
// Delete applet class 'extends PApplet'. Processing and Processing.js don't know about PApplet.
array(
'content' => 'extends PApplet',
),
// Delete unnecessary import from pde file.
array(
'content' => 'import processing.core.PApplet;',
'onlyForFiles' => array(DS . 'Tetris' . DS . 'Tetris.java'),
),
// Delete @Override annotations. Processing.js doesn't allow them. Processing doesn't like them.
array(
'content' => <<<JAVA
@Override
JAVA
),
/***********
* PDE
**********/
// Remove any way the PApplet is accessed, in processing the PApplet functions are recognised anywhere in the code.
array(
'content' => 'this.app.',
'onlyFor' => 'pjs',
),
array(
'content' => 'this.getParentController().getView().getParentView().getApp().',
'onlyFor' => 'pjs',
),
array(
'content' => 'this.getParentController().getView().getApp().',
'onlyFor' => 'pjs'),
array(
'content' => 'this.getView().getParentView().getParentView().getApp().',
'onlyFor' => 'pjs',
),
array(
'content' => 'this.getView().getParentView().getApp().',
'onlyFor' => 'pjs',
),
array(
'content' => 'this.getParentView().getParentView().getApp().',
'onlyFor' => 'pjs',
),
array(
'content' => 'this.getParentView().getApp().',
'onlyFor' => 'pjs',
),
array(
'content' => 'PApplet.',
'onlyFor' => 'pjs',
),
//For some reason a constructor throwing exception gives error
// "Unable to execute pjs sketch: TypeError: Cannot read property '1' of null" on processing.js compilation.
array(
'content' => 'throws Model_Exception',
'onlyFor' => 'pjs',
),
array(
'content' => '@removeLineFromPde',
'onlyFor' => 'pjs',
),
/***********
* PJS
**********/
array(
'content' => '@removeLineFromPjs',
'onlyFor' => 'pde',
),
);
// Lines to remove.
$excludeLineContentOptions = array(
/***********
* BOTH
**********/
// Remove liens containing the @removeLineFromAll text.
array('pattern' => '/@removeLineFromAll/'),
// Delete lines starting with package.
array('pattern' => '/^package/'),
/***********
* PDE
**********/
// Remove lines containing the @removeLineFromPde text.
array('pattern' => '/@removeLineFromPde/',
'onlyFor' => 'pde'),
// Processing doesn't allow packages.
array('pattern' => '/^import Tetris/',
'onlyFor' => 'pde'),
/***********
* PJS
**********/
// Remove lines containing the @removeLineFromPjs text.
array('pattern' => '/@removeLineFromPjs/',
'onlyFor' => 'pjs'),
// Delete lines starting with import. Processing.js ignores these lines.
array('pattern' => '/^import/',
'onlyFor' => 'pjs'),
);
// Get all files from fromDir.
$checkOnlyJava = function($dir, $file) {
return substr($file, -4) == 'java';
};
// Exclude folders.
$checkExcludeFolders = function ($dir, $sub) {
global $excludeFolders;
$ok = !in_array($dir . DS . $sub, $excludeFolders);
if ($ok) {
echo $dir . DS . $sub . "<br>\n";
}
return $ok;
};
// Get files.
$files = dirToArray($fromDir, $checkOnlyJava, $checkExcludeFolders);
// Function to get all files from directory $dir and all its subdirectories.
function dirToArray($dir, $checkFile = null, $checkDir = null) {
$result = array();
$files = scandir($dir);
foreach ($files as $key => $value) {
if (!in_array($value, array(".", ".."))) {
if (is_dir($dir . DS . $value)
) {
if (!$checkDir || $checkDir($dir, $value)) {
$result[$value] = dirToArray($dir . DS . $value, $checkFile, $checkDir);
}
}
else {
if (!$checkFile || $checkFile($dir, $value)) {
$result[] = $value;
}
}
}
}
return $result;
}
// Create Game/Tetris.pde and Game/Tetris.pjs.
$pdeFile = fopen($pdeFilename, 'w');
if ($pdeFile === false) {
die();
}
$pjsFile = fopen($pjsFilename, 'w');
if ($pjsFile === false) {
die();
}
// Go through all files and include them in the pde file.
checkFiles('', $files);
// Function to recursively include all files from array of files $files, which are in directory $dir.
function checkFiles($dir, $files) {
// First check dirs.
foreach ($files as $key => $value) {
if (is_array($value)) {
checkFiles($dir . DS . $key, $value);
}
}
// Then check files.
foreach ($files as $key => $value) {
if (!is_array($value)) {
global $pdeFile, $pjsFile;
checkFile($dir, $value, $pdeFile, 'pde');
checkFile($dir, $value, $pjsFile, 'pjs');
}
}
}
// Function to get the contents of $file from $dir, filter it and include it in the $toFile of type $typeToFile.
function checkFile($dir, $file, $toFile, $typeToFile) {
// Get file content.
global $fromDir;
$content = file_get_contents($fromDir . DS . $dir . DS . $file);
// Exclude content.
global $excludeContentOptions;
foreach ($excludeContentOptions as $excludeContentOption) {
if (optionIsValidForFile($excludeContentOption, $dir . DS . $file, $typeToFile)) {
$content = str_replace($excludeContentOption['content'], '', $content);
}
}
// Make the remained content an array of lines.
$lines = explode("\n", $content);
// Prepare final lines.
$finalLines = array();
// Go through all lines.
global $excludeLineContentOptions;
foreach ($lines as $line) {
$ok = true;
// If it matches on of the excludeLineContentOptions patterns, then ok = false.
foreach ($excludeLineContentOptions as $excludeLineContentOption) {
$ok = !optionIsValidForFile($excludeLineContentOption, $dir . DS . $file, $typeToFile)
|| preg_match($excludeLineContentOption['pattern'], $line) === 0;
if (!$ok) {
break;
}
}
// If ok remained true, then add it in the final lines.
if ($ok) {
$finalLines[] = $line;
}
}
// Prepare final content.
$finalContent = implode("\n", $finalLines);
if ($typeToFile == 'pjs') {
// For pjs , include file in the pjs file.
fwrite($toFile, $finalContent);
} else {
// For pde, create a new file.
if ($file == "StartGame.java") {
$file = "Tetris.pde";
}
global $pdeFolder;
$file = fopen($pdeFolder . $file, 'w');
if ($file === false) {
die();
}
fwrite($file, $finalContent);
fclose($file);
}
}
/**
* Checks if option is valid for the file, which is included in the content of type contentType (pde or pjs).
* @param $option
* @param $file
* @param $contentType
* @return bool
*/
function optionIsValidForFile($option, $file, $contentType) {
// Option is given only for some files.
$onlyForFiles = isset($option['onlyForFiles']) ? $option['onlyForFiles'] : null;
if ($onlyForFiles && !in_array($file, $onlyForFiles)) {
return false;
}
// Option is given only for pde or pjs.
$onlyFor = isset($option['onlyFor']) ? $option['onlyFor'] : null;
if ($onlyFor && $onlyFor != $contentType) {
return false;
}
// It's good.
return true;
}
// Close file.
fclose($pdeFile);
$content = ob_get_clean();
echo "OK<br>\n";
echo $content;