|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace VoidEngine; |
|
4
|
|
|
|
|
5
|
|
|
use VoidCore; |
|
6
|
|
|
|
|
7
|
|
|
function dn (...$args): NetObject |
|
8
|
|
|
{ |
|
9
|
|
|
try |
|
10
|
|
|
{ |
|
11
|
|
|
$object = new NetObject (...$args); |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
catch (\WinformsException $e) |
|
15
|
|
|
{ |
|
16
|
|
|
if (VoidCore::callMethod ($e->getNetException (), 'ToString') == 'System.MemberAccessException') |
|
17
|
|
|
throw $e; |
|
18
|
|
|
|
|
19
|
|
|
$object = new NetClass (...$args); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
return $object; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
function thread (callable $callable): NetObject |
|
26
|
|
|
{ |
|
27
|
|
|
return new NetObject (VoidCore::createThread ($callable)); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
function enum (string $baseType, string $value) |
|
31
|
|
|
{ |
|
32
|
|
|
try |
|
33
|
|
|
{ |
|
34
|
|
|
return VoidCore::callMethod (VoidCore::getClass ('System.Enum'), ['parse', VC_OBJECT], VoidCore::typeof ($baseType), $value, true); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
catch (\WinformsException $e) |
|
38
|
|
|
{ |
|
39
|
|
|
return (new NetClass ($baseType))->$value; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
function getNetArray (string $type, array $items = []): NetObject |
|
44
|
|
|
{ |
|
45
|
|
|
$array = (new NetClass ('System.Array')) |
|
46
|
|
|
->createInstance (VoidCore::typeof ($type), $size = sizeof ($items)); |
|
47
|
|
|
|
|
48
|
|
|
for ($i = 0; $i < $size; ++$i) |
|
49
|
|
|
$array[$i] = array_shift ($items); |
|
50
|
|
|
|
|
51
|
|
|
return $array; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
function dir_create (string $path, int $mode = 0777): void |
|
55
|
|
|
{ |
|
56
|
|
|
if (!is_dir ($path)) |
|
57
|
|
|
mkdir ($path, $mode, true); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
function dir_delete (string $path): bool |
|
61
|
|
|
{ |
|
62
|
|
|
if (!is_dir ($path)) |
|
63
|
|
|
return false; |
|
64
|
|
|
|
|
65
|
|
|
foreach (array_slice (scandir ($path), 2) as $file) |
|
|
|
|
|
|
66
|
|
|
if (is_dir ($file = $path .'/'. $file)) |
|
67
|
|
|
{ |
|
68
|
|
|
dir_delete ($file); |
|
69
|
|
|
|
|
70
|
|
|
if (is_dir ($file)) |
|
71
|
|
|
rmdir ($file); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
else unlink ($file); |
|
75
|
|
|
|
|
76
|
|
|
rmdir ($path); |
|
77
|
|
|
|
|
78
|
|
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
function dir_clean (string $path): void |
|
82
|
|
|
{ |
|
83
|
|
|
dir_delete ($path); |
|
84
|
|
|
dir_create ($path); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
function dir_copy (string $from, string $to): bool |
|
88
|
|
|
{ |
|
89
|
|
|
if (!is_dir ($from)) |
|
90
|
|
|
return false; |
|
91
|
|
|
|
|
92
|
|
|
if (!is_dir ($to)) |
|
93
|
|
|
dir_create ($to); |
|
94
|
|
|
|
|
95
|
|
|
foreach (array_slice (scandir ($from), 2) as $file) |
|
|
|
|
|
|
96
|
|
|
if (is_dir ($f = $from .'/'. $file)) |
|
97
|
|
|
dir_copy ($f, $to .'/'. $file); |
|
98
|
|
|
|
|
99
|
|
|
else copy ($f, $to .'/'. $file); |
|
100
|
|
|
|
|
101
|
|
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
function argb (string $color) |
|
105
|
|
|
{ |
|
106
|
|
|
return (new NetClass ('System.Drawing.ColorTranslator'))->fromHtml ($color); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
function replaceSl (string $string): string |
|
110
|
|
|
{ |
|
111
|
|
|
return str_replace ('\\', '/', $string); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
function replaceSr (string $string): string |
|
115
|
|
|
{ |
|
116
|
|
|
return str_replace ('/', '\\', $string); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
function basenameNoExt (string $path): string |
|
120
|
|
|
{ |
|
121
|
|
|
return pathinfo ($path, PATHINFO_FILENAME); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
function file_ext (string $path): string |
|
125
|
|
|
{ |
|
126
|
|
|
return strtolower (pathinfo ($path, PATHINFO_EXTENSION)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
function filepathNoExt (string $path): string |
|
130
|
|
|
{ |
|
131
|
|
|
return dirname ($path) .'/'. basenameNoExt ($path); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
function pre (...$args): void |
|
135
|
|
|
{ |
|
136
|
|
|
message (sizeof ($args) < 2 ? current ($args) : $args); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
function messageBox (string $message, string $caption = '', ...$args): int |
|
140
|
|
|
{ |
|
141
|
|
|
return (new MessageBox)->show ($message, $caption, ...$args); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
function run (string $path, ...$args) |
|
145
|
|
|
{ |
|
146
|
|
|
return (new Process)->start ($path, ...$args); |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
function setTimer (int $interval, callable $function): Timer |
|
150
|
|
|
{ |
|
151
|
|
|
$timer = new Timer; |
|
152
|
|
|
$timer->interval = $interval; |
|
|
|
|
|
|
153
|
|
|
$timer->tickEvent = fn ($self) => $function ($self); |
|
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
$timer->start (); |
|
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
return $timer; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
function setTimeout (int $timeout, callable $function): Timer |
|
161
|
|
|
{ |
|
162
|
|
|
$timer = new Timer; |
|
163
|
|
|
$timer->interval = $timeout; |
|
|
|
|
|
|
164
|
|
|
$timer->tickEvent = function ($self) use ($function) |
|
|
|
|
|
|
165
|
|
|
{ |
|
166
|
|
|
$self->stop (); |
|
167
|
|
|
|
|
168
|
|
|
$function ($self); |
|
169
|
|
|
}; |
|
170
|
|
|
|
|
171
|
|
|
$timer->start (); |
|
172
|
|
|
|
|
173
|
|
|
return $timer; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
set_error_handler (function ($no, $str, $file, $line) |
|
177
|
|
|
{ |
|
178
|
|
|
// Мог ли я здесь сделать более адекватный код с использованием pow/sqrt? Да, мог |
|
179
|
|
|
// Почему не сделал? Скорость важнее |
|
180
|
|
|
static $errarr = [ |
|
181
|
|
|
1 => 'E_ERROR', |
|
182
|
|
|
2 => 'E_WARNING', |
|
183
|
|
|
4 => 'E_PARSE', |
|
184
|
|
|
8 => 'E_NOTICE', |
|
185
|
|
|
16 => 'E_CORE_ERROR', |
|
186
|
|
|
32 => 'E_CORE_WARNING', |
|
187
|
|
|
64 => 'E_COMPILE_ERROR', |
|
188
|
|
|
128 => 'E_COMPILE_WARNING', |
|
189
|
|
|
256 => 'E_USER_ERROR', |
|
190
|
|
|
512 => 'E_USER_WARNING', |
|
191
|
|
|
1024 => 'E_USER_NOTICE', |
|
192
|
|
|
2048 => 'E_STRICT', |
|
193
|
|
|
4096 => 'E_RECOVERABLE_ERROR', |
|
194
|
|
|
8192 => 'E_DEPRECATED', |
|
195
|
|
|
16384 => 'E_USER_DEPRECATED' |
|
196
|
|
|
]; |
|
197
|
|
|
|
|
198
|
|
|
message ([ |
|
199
|
|
|
'type' => $errarr[$no], |
|
200
|
|
|
'text' => $str, |
|
201
|
|
|
'file' => $file, |
|
202
|
|
|
'line' => $line |
|
203
|
|
|
], 'PHP Script Error'); |
|
204
|
|
|
}); |
|
205
|
|
|
|