1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace VoidBuilder; |
4
|
|
|
|
5
|
|
|
use VoidEngine\VoidEngine; |
6
|
|
|
|
7
|
|
|
class Builder |
8
|
|
|
{ |
9
|
|
|
public $appDir; |
10
|
|
|
|
11
|
|
|
public function __construct (string $appDir) |
12
|
|
|
{ |
13
|
|
|
if (!is_dir ($appDir)) |
14
|
|
|
throw new \Exception ('Wrong $appDir param'); |
15
|
|
|
|
16
|
|
|
$this->appDir = $appDir; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function build (string $outputDir, string $iconPath = null, bool $union = true): array |
20
|
|
|
{ |
21
|
|
|
\VoidEngine\dir_clean ($outputDir .'/build'); |
22
|
|
|
\VoidEngine\dir_copy (\VoidEngine\CORE_DIR, $outputDir .'/build'); |
23
|
|
|
|
24
|
|
|
unlink ($outputDir .'/build/script.php'); |
25
|
|
|
unlink ($outputDir .'/build/VoidCore.exe'); |
26
|
|
|
|
27
|
|
|
return VoidEngine::compile ($outputDir .'/build/app.exe', \VoidEngine\text ($iconPath ?? dirname (__DIR__) .'/system/Icon.ico'), \VoidEngine\str_replace_assoc (file_get_contents (dirname (__DIR__) .'/system/preset.php'), [ |
|
|
|
|
28
|
|
|
'%VoidEngine%' => self::generateCode (self::getReferences (\VoidEngine\ENGINE_DIR .'/VoidEngine.php')), |
29
|
|
|
'%APP%' => base64_encode (gzdeflate (serialize ($union ? array_merge ( |
30
|
|
|
self::getFiles ($this->appDir), |
31
|
|
|
self::getFiles (dirname ($this->appDir) .'/qero-packages', 'qero-packages/KRypt0nn/VoidFramework') |
32
|
|
|
) : []), 9)) |
33
|
|
|
]), null, null, null, null, null, '', ''); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function generateCode (array $references, bool $removeNamespaces = true): string |
37
|
|
|
{ |
38
|
|
|
$code = "/*\n\n\t". join ("\n\t", explode ("\n", file_get_contents (dirname (\VoidEngine\ENGINE_DIR) .'/license.txt'))) ."\n\n*/\n\n"; |
39
|
|
|
|
40
|
|
|
foreach ($references as $path) |
41
|
|
|
$code .= join (array_slice (array_map (function ($line) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
return substr ($line, 0, 7) != 'require' ? $line : ''; |
44
|
|
|
}, file ($path)), 1)); |
|
|
|
|
45
|
|
|
|
46
|
|
|
return $removeNamespaces ? |
47
|
|
|
preg_replace ('/'. "\n" .'namespace [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*;'. "\n" .'/', "\n\n", $code) : $code; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function getReferences (string $file, bool $parseExtensions = true): array |
51
|
|
|
{ |
52
|
|
|
$references = []; |
53
|
|
|
|
54
|
|
|
foreach (file ($file) as $id => $line) |
55
|
|
|
if (substr ($line, 0, 7) == 'require') |
56
|
|
|
try |
57
|
|
|
{ |
58
|
|
|
$begin = strpos ($line, "'"); |
59
|
|
|
$end = strrpos ($line, "'") - $begin + 1; |
60
|
|
|
|
61
|
|
|
$references = array_merge ($references, self::getReferences (dirname ($file) .'/'. eval ('namespace VoidEngine; return '. substr ($line, $begin, $end) .';'), false)); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
catch (\Throwable $e) |
65
|
|
|
{ |
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($parseExtensions) |
70
|
|
|
if (is_dir (\VoidEngine\ENGINE_DIR .'/extensions') && is_array ($exts = scandir (\VoidEngine\ENGINE_DIR .'/extensions'))) |
71
|
|
|
foreach ($exts as $id => $ext) |
72
|
|
|
if (is_dir (\VoidEngine\ENGINE_DIR .'/extensions/'. $ext) && file_exists ($ext = \VoidEngine\ENGINE_DIR .'/extensions/'. $ext .'/main.php')) |
73
|
|
|
$references = array_merge ($references, self::getReferences ($ext, false)); |
74
|
|
|
|
75
|
|
|
$references[] = $file; |
76
|
|
|
|
77
|
|
|
return $references; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function getFiles (string $path, string $prefixBlacklist = null, array $files = [], int $originalPathLength = -1): array |
81
|
|
|
{ |
82
|
|
|
if ($originalPathLength == -1) |
83
|
|
|
$originalPathLength = strlen (dirname ($path)) + 1; |
84
|
|
|
|
85
|
|
|
$len = strlen ($prefixBlacklist); |
86
|
|
|
|
87
|
|
|
foreach (array_slice (scandir ($path), 2) as $name) |
|
|
|
|
88
|
|
|
if ($prefixBlacklist === null || substr ($path .'/'. $name, $originalPathLength, $len) != $prefixBlacklist) |
89
|
|
|
{ |
90
|
|
|
if (is_dir ($file = $path .'/'. $name)) |
91
|
|
|
$files = self::getFiles ($file, $prefixBlacklist, $files, $originalPathLength); |
92
|
|
|
|
93
|
|
|
else $files[substr ($file, $originalPathLength)] = file_get_contents ($file); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $files; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|