-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.php
executable file
·420 lines (331 loc) · 11.9 KB
/
update.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
/// If this environment variable exist, it should be the same as the GET_ key provided
$keyName = 'UNITDB_UPGRADE_SECRET';
if (getenv($keyName) !== false){
if ($_GET['token'] != $_ENV[$keyName]){
header('HTTP/1.1 403 Forbidden');
exit;
}
}
/*
({)(\s*(\s*'(\w|\<|\>|_| )*',*)+),\s*(})
*/
require('res/scripts/luaToPhp.php');
function rrmdir($src) {
if (file_exists($src)){
// echo '<p>--> Found '.$src.' [Exists] </p>'; ////////DEBUG
if (is_dir($src)){
$ls = scandirVisible($src);
foreach($ls as $thisSub){
if ($thisSub != "." && $thisSub != ".."){
$full = $src.'/'.$thisSub;
rrmdir($full);
}
}
}
else {
// echo '<p>--> Not a directory ("'.$src.'"), unlinking </p>'; ////////DEBUG
unlink($src);
}
// echo '<p>--> Removing source "'.$src.'" </p>'; ////////DEBUG
if(file_exists($src)) rmdir($src);
}
}
function scandirVisible($dir){
return array_diff(scandir($dir), array('..', '.'));
}
function prepareForConversion($string_bp){
$string_bp = preg_replace('/--(.*)/', "", $string_bp);
$string_bp = preg_replace('/#(.*)/', "", $string_bp);
$string_bp = str_replace("'", '"', $string_bp);
$string_bp = str_replace('Sound', '', $string_bp);
return $string_bp;
}
function locfileToPhp($locContent){
$exp = explode("\n", $locContent);
$finalLoc = [];
foreach($exp as $line){
$content = explode('=', $line);
if (count($content) <= 1){
continue;
}
$name = $content[0];
$translation = $content[1];
$translation = preg_replace("/(--\[\[(.*)--\]\]+)/", "", $translation);
$finalLoc[$name] = $translation;
}
return $finalLoc;
}
//GET EXTRACTION INFO AND PREPARE FALLBACK
$toExtract = json_decode(file_get_contents('config/datafiles.json'));
$toExtractLoc = json_decode(file_get_contents('config/locfiles.json'));
$debug = false;
if (isset($_GET['debug'])){
$debug = $_GET['debug'];
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo '<div style="color:orange;background-color:#111111;font-family:Consolas;padding:8px;">';
}
file_put_contents("config/UPDATE.TMP", "If this file is present, either the database is updating or the last update failed.");
// STEP 0 : DOWNLOAD data IF NEED
if ($debug) echo '<p>STEP 0 ----- </p>'; ////////DEBUG
if (isset($_GET['version']) && $_GET['version'] != "local"){
$version = $_GET['version'];
$urlVar = "UNITDB_FILES_API_URL_FORMAT";
$apiUrl = "https://api.faforever.com/featuredMods/0/files/%s";
if (getenv($urlVar) !== false){
$apiUrl = $_ENV[$urlVar];
}
$url = sprintf($apiUrl, $version);
if ($debug){
echo "<p>Using url ".$url."</p>";
}
$neededFiles = array(
"units.nx2"=>"data/gamedata/",
"projectiles.nx2"=>"data/gamedata/",
"loc.nx2"=>"data/loc/"
);
$jsonString = file_get_contents($url);
$json = json_decode($jsonString, true);
$files = $json["data"];
foreach($files as $thisFile){
$name = $thisFile["attributes"]["name"];
$md5 = $thisFile["attributes"]["md5"];
$url = $thisFile["attributes"]["url"];
$neededFilesKeys = array_keys($neededFiles);
if (in_array($name, $neededFilesKeys)){
$path = $neededFiles[$name];
if ($debug) echo "Downloading ".$name." from ".$url." to ".$path." [".$md5."]<br>";
if (file_exists($path.$name)) unlink($path.$name);
if (!file_exists($path)) mkdir($path, 0777, true);
file_put_contents($path.$name, fopen($url, 'r'));
$sum = md5_file($path.$name);
if ($sum != $md5){
if ($debug) echo "-> MD5 MISMATCH !<br>";
if ($debug) echo "--> Exiting.<br>";
exit;
}
else{
if ($debug) echo "=> MD5 OK !<br>";
}
}
}
}
//STEP 1 : UNZIP data
if ($debug) echo '<p>STEP 1 ----- </p>'; ////////DEBUG
$failed = 0;
for ($h = 0; $h < sizeOf($toExtract); $h ++){
$zip = new ZipArchive;
if ($zip->open(''.($toExtract[$h]).'') === TRUE) {
if ($debug) echo '<p>-> Opened archive '.$toExtract[$h].' and found '.($zip->numFiles).' files. </p>'; ////////DEBUG
for ($i=0; $i<$zip->numFiles;$i++) {
$name = ($zip->statIndex($i)['name']);
if ($debug) echo '<p>--> Found file '.$name.'</p>'; ////////DEBUG
if (strpos(basename($name), '.bp') !== false){
if ($debug) echo '<p>---> Extracting '.$name.' to data/_temp/'.$toExtract[$h].'/ ...</p>'; ////////DEBUG
$success = $zip->extractTo('data/_temp/'.$toExtract[$h].'/',($name)); //Ex : extracts "units.scd.3599" to /data/gamedata/_temp/units.scd.3599
// Let's assure the unit blueprint has the right casing
$basename = basename('data/_temp/'.$toExtract[$h].'/'.$name);
$path = str_replace($basename, "", 'data/_temp/'.$toExtract[$h].'/'.$name);
if ($debug) echo '<p>---> Renaming '.$path.$basename. " to ". $path.strtoupper($basename).'</p>'; ////////DEBUG
rename($path.$basename, $path.strtoupper($basename));
if (!$success){
if ($debug) echo '<p>----> Extraction FAILED !</p>'; ////////DEBUG
if ($debug) echo '<p>----> Error : '.error_get_last()['message'].'</p>'; ////////DEBUG
}
}
}
$zip->close();
} else {
if ($debug) echo '<p>-> FAILED opening archive '.$toExtract[$h].' </p>'; ////////DEBUG
$failed++;
}
}
if ($failed > 0){
if ($debug) echo '<p> -> '.$failed.' files could not be extracted. </p>'; ////////DEBUG
}
//loc -->
$failed = 0;
if ($debug) echo '<p>-> Opening loc Files... </p>'; ////////DEBUG
foreach($toExtractLoc as $locArch){
$zip = new ZipArchive;
if ($zip->open(''.($locArch).'') === TRUE) {
if ($debug) echo '<p>-> Opened loc archive '.$locArch.' and found '.($zip->numFiles).' files. </p>'; ////////DEBUG
for ($i=0; $i<$zip->numFiles;$i++) {
$name = $zip->statIndex($i)['name'];
if (strpos($name, '.lua') !== false){
$zip->extractTo('data/_temp/'.$locArch.'/', $name);
}
}
$zip->close();
}
else {
if ($debug) echo '<p>-> FAILED opening loc archive '.$locArch.' </p>'; ////////DEBUG
$failed++;
}
}
if ($failed > 0){
if ($debug) echo '<p> ->'.$failed.' loc files could not be extracted. </p>'; ////////DEBUG
}
//endof
//STEP 2 : MERGING FILES
if ($debug) echo '<p>------------ </p>'; ////////DEBUG
if ($debug) echo '<p>STEP 2 ----- </p>'; ////////DEBUG
$idsUnitsList = [];
$finalLangs = [];
$dir = 'data/_temp/';
if (is_dir($dir)){
if ($debug) echo '<p>-> Directory '.$dir.' found </p>'; ////////DEBUG
foreach($toExtract as $fileFolder) { //For every PAK to use, like units.3599.scd or units.nx2
$realPath = $dir.$fileFolder;
if ($debug) echo '<p>-> Working on '.$realPath.'</p>'; ////////DEBUG
$skipping = false;
if (!is_dir($realPath)){
if ($debug) echo '<p>--> No directory, SKIPPING </p>'; ////////DEBUG
continue;
}
$dirs = scandirVisible($realPath);
$thisPakUnitsList = [];
$totalFound = 0;
$notFoundAfterX = 0;
foreach($dirs as $thisDirectory){ //For every subfolder of the PAK, like "/units" or "/projectiles"
$unitList = scandirVisible($realPath.'/'.$thisDirectory);
$thisSubfolderUnitsList = [];
$units = 0;
foreach($unitList as $thisUnit){ // For every unit inside this folder.
$thisUnitDirectory = $realPath.'/'.$thisDirectory.'/'.$thisUnit;
$thisMissileFile = $thisUnitDirectory.'/'.strtoupper($thisUnit).'_PROJ.BP';
$thisUnit = strtoupper($thisUnit);
$thisUnitFile = $thisUnitDirectory.'/'.strtoupper($thisUnit).'_UNIT.BP';
$proj = false;
if (file_exists($thisMissileFile)){
$proj = true;
$file = $thisMissileFile;
}
else{
$file = $thisUnitFile;
}
if ($debug) echo '--> Adding unit '.$thisUnit.' from '.$file.'...<br>';
if (file_exists($file)){
$blueprint = file_get_contents($file);
$blueprint = makePhpArray(prepareForConversion($blueprint));
//var_dump("3");
$blueprint['Id'] = ($thisUnit);
// var_dump("4");
if ($proj){
$blueprint['BlueprintType'] = 'ProjectileBlueprint';
}
else{
$blueprint['BlueprintType'] = 'UnitBlueprint';
}
$thisSubfolderUnitsList[$thisUnit]= $blueprint; //Key is ID
$units++;
}
else{
if ($debug) echo '---> File not found!<br>';
$notFoundAfterX++;
}
}
if ($debug) echo '<p>--> Found '.$units.' units in directory '.$thisDirectory.'</p>'; ////////DEBUG
if ($debug) echo '<p>--> Could not find '.$notFoundAfterX.' units</p>'; ////////DEBUG
$totalFound += $units;
$thisPakUnitsList = array_merge($thisPakUnitsList, $thisSubfolderUnitsList);
}
//$o = $idsUnitsList;
if ($debug) echo '<p>-> Total units found for pak '.$realPath.' : '.$totalFound.' </p>'; ////////DEBUG
$idsUnitsList = array_merge($idsUnitsList, $thisPakUnitsList);
}
//loc
$totalLines = 0;
foreach($toExtractLoc as $locFolder){
$realPath = $dir.$locFolder;
if ($debug) echo '<p>-> Working on loc '.$realPath.'</p>'; ////////DEBUG
if (!is_dir($realPath)){
if ($debug) echo '<p>--> No directory, SKIPPING </p>'; ////////DEBUG
continue;
}
$dirs = scandirVisible($realPath);
$thisPakLangs = [];
foreach($dirs as $thisDirectory){ //For every subfolder of the PAK, like "/units" or "/projectiles"
$langs = scandirVisible($realPath.'/'.$thisDirectory);
$thisSubfolderLocList = [];
$foundLines = 0;
foreach($langs as $thisLang){ // For every LANG inside the folder
$thisLang = strtoupper($thisLang);
$thisLangDirectory = $realPath.'/'.$thisDirectory.'/'.$thisLang;
$file = $thisLangDirectory.'/'.'strings_db.lua';
if (file_exists($file)){
$lines = file_get_contents($file);
$lines = locfileToPhp($lines);
$thisSubfolderLocList[$thisLang]= $lines;
$foundLines++;
//echo '--> Found lang '.$thisLang.'<br>';
}
}
if ($debug) echo '<p>--> Found '.$foundLines.' locfiles in directory '.$thisDirectory.'</p>'; ////////DEBUG
$totalLines += $foundLines;
$thisPakLangs = array_merge($thisPakLangs, $thisSubfolderLocList);
}
if ($debug) echo '<p>-> Total files found for loc '.$realPath.' : '.$totalLines.' </p>'; ////////DEBUG
$finalLangs = array_merge($finalLangs, $thisPakLangs);
}
//ENDOF
}
else{
if ($debug) echo '<p>'.$dir.' not found. EXITING !</p>'; ////////DEBUG
exit;
}
//STEP 3 : MAKING JSON
if ($debug) echo '<p>------------ </p>'; ////////DEBUG
if ($debug) echo '<p>STEP 3 ----- </p>'; ////////DEBUG
$finalUnitList = [];
foreach($idsUnitsList as $thisUnit){
$finalUnitList[]= $thisUnit;
}
file_put_contents('data/blueprints.json', json_encode($finalUnitList));
file_put_contents('data/localization.json', json_encode($finalLangs));
//STEP 4 : CLEANING UP
if ($debug) echo '<p>------------ </p>'; ////////DEBUG
if ($debug) echo '<p>STEP 4 ----- </p>'; ////////DEBUG
if ($debug) echo '<p>-> Beginning '.$dir.' cleanup </p>';
if (is_dir($dir)){
$files = scandirVisible($dir);
foreach($files as $unit) {
if ($debug) echo '<p>-> Removing '.$dir.$unit.' </p>'; ////////DEBUG
rrmdir($dir.$unit);
};
}
unlink("config/UPDATE.TMP");
if ($debug) echo '<p>Unliked UPDATE.TMP - all operations complete.</p>'; ////////DEBUG
if ($debug) echo '</div>'; ////////DEBUG
?>
<script>
function hideUpdateMenu(){
document.getElementById('updateMenu').style.display = "none";
}
</script>
<div style="
z-index:10;
position:fixed;
left:50%;
top:25%;
margin-left:-300px;
color:#EEEEEE;
background-color:#303030;
border:1px solid white;
width:600px;
display:block;" id="updateMenu">
<div style="font-family:Zeroes;text-align:center;width:100%;margin-top:8px;margin-bottom:16px;">
Unit database has been updated.
</div>
<div class="flexRows" style="width:100%;text-align:center;margin-bottom:32px;">
<div>
<button style="
font-family:Zeroes;
color:#303030;
background-color:#EEEEEE;
width:30%;" onClick="hideUpdateMenu()">OK
</button>
</div>
</div>
</div>