1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace VoidEngine; |
4
|
|
|
|
5
|
|
|
function err_status (bool $status = null): bool |
6
|
|
|
{ |
7
|
|
|
$oldStatus = $GLOBALS['__debug']['error_status']; |
8
|
|
|
|
9
|
|
|
if ($status !== null) |
10
|
|
|
$GLOBALS['__debug']['error_status'] = $status; |
11
|
|
|
|
12
|
|
|
return $oldStatus; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
function err_no (): bool |
16
|
|
|
{ |
17
|
|
|
return err_status (false); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
function err_yes (): bool |
21
|
|
|
{ |
22
|
|
|
return err_status (true); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
function run (string $path, ...$args) |
26
|
|
|
{ |
27
|
|
|
return (new Process)->start ($path, ...$args); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
function vbs_exec (string $code) |
31
|
|
|
{ |
32
|
|
|
file_put_contents ($path = getenv ('temp') .'/'. crc32 ($code) .'.vbs', $code); |
33
|
|
|
|
34
|
|
|
(new \COM ('WScript.Shell'))->Run ($path, 0, true); |
35
|
|
|
|
36
|
|
|
unlink ($path); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
function php_errors_check (string $code): ?array |
40
|
|
|
{ |
41
|
|
|
try |
42
|
|
|
{ |
43
|
|
|
eval ('return; '. $code); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
catch (\Throwable $e) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
'text' => $e->getMessage (), |
50
|
|
|
'line' => $e->getLine () |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
function enum (string $name): array |
58
|
|
|
{ |
59
|
|
|
return [ |
60
|
|
|
substr ($name, strrpos ($name, '.') + 1), |
61
|
|
|
($name = substr ($name, 0, strrpos ($name, '.'))) .', '. substr ($name, 0, strrpos ($name, '.')) |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
function enum_parse (string $baseType, string $value) |
66
|
|
|
{ |
67
|
|
|
try |
68
|
|
|
{ |
69
|
|
|
return \VoidCore::callMethod (\VoidCore::getClass ('System.Enum',''), ['parse', 'object'], \VoidCore::typeof ($baseType), $value, true); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
catch (\WinFormsException $e) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
return (new WFClass ($baseType))->$value; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
function getNetArray (string $type, array $items = []): WFObject |
79
|
|
|
{ |
80
|
|
|
$array = (new WFClass ('System.Array', null)) |
81
|
|
|
->createInstance (\VoidCore::typeof ($type), $size = sizeof ($items)); |
|
|
|
|
82
|
|
|
|
83
|
|
|
for ($i = 0; $i < $size; ++$i) |
84
|
|
|
$array[$i] = array_shift ($items); |
85
|
|
|
|
86
|
|
|
return $array; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
function dir_create (string $path, int $mode = 0777): void |
90
|
|
|
{ |
91
|
|
|
if (!is_dir ($path)) |
92
|
|
|
mkdir ($path, $mode, true); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
function dir_delete (string $path): bool |
96
|
|
|
{ |
97
|
|
|
if (!is_dir ($path)) |
98
|
|
|
return false; |
99
|
|
|
|
100
|
|
|
foreach (array_slice (scandir ($path), 2) as $file) |
|
|
|
|
101
|
|
|
if (is_dir ($file = $path .'/'. $file)) |
102
|
|
|
{ |
103
|
|
|
dir_delete ($file); |
104
|
|
|
|
105
|
|
|
if (is_dir ($file)) |
106
|
|
|
rmdir ($file); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
else unlink ($file); |
110
|
|
|
|
111
|
|
|
rmdir ($path); |
112
|
|
|
|
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
function dir_clean (string $path): void |
117
|
|
|
{ |
118
|
|
|
dir_delete ($path); |
119
|
|
|
dir_create ($path); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
function dir_copy (string $from, string $to): bool |
123
|
|
|
{ |
124
|
|
|
if (!is_dir ($from)) |
125
|
|
|
return false; |
126
|
|
|
|
127
|
|
|
if (!is_dir ($to)) |
128
|
|
|
dir_create ($to); |
129
|
|
|
|
130
|
|
|
foreach (array_slice (scandir ($from), 2) as $file) |
|
|
|
|
131
|
|
|
if (is_dir ($f = $from .'/'. $file)) |
132
|
|
|
dir_copy ($f, $to .'/'. $file); |
133
|
|
|
|
134
|
|
|
else copy ($f, $to .'/'. $file); |
135
|
|
|
|
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
function getARGBColor (string $color) |
140
|
|
|
{ |
141
|
|
|
return (new WFClass ('System.Drawing.ColorTranslator'))->fromHtml ($color); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
function replaceSl (string $string): string |
145
|
|
|
{ |
146
|
|
|
return str_replace ('\\', '/', $string); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
function replaceSr (string $string): string |
150
|
|
|
{ |
151
|
|
|
return str_replace ('/', '\\', $string); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
function basenameNoExt (string $path): string |
155
|
|
|
{ |
156
|
|
|
return pathinfo ($path, PATHINFO_FILENAME); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
function file_ext (string $path): string |
160
|
|
|
{ |
161
|
|
|
return strtolower (pathinfo ($path, PATHINFO_EXTENSION)); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
function filepathNoExt (string $path): string |
165
|
|
|
{ |
166
|
|
|
return dirname ($path) .'/'. basenameNoExt ($path); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
function array_first (array $array) |
170
|
|
|
{ |
171
|
|
|
return array_shift ($array); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
function array_end (array $array) |
175
|
|
|
{ |
176
|
|
|
return array_pop ($array); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
function substr_icount (string $haystack, string $needle, ...$params): int |
180
|
|
|
{ |
181
|
|
|
return substr_count (strtolower ($haystack), strtolower ($needle), ...$params); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
function str_replace_assoc (string $subject, array $replacements): string |
185
|
|
|
{ |
186
|
|
|
return str_replace (array_keys ($replacements), array_values ($replacements), $subject); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
function pre (...$args): void |
190
|
|
|
{ |
191
|
|
|
message (sizeof ($args) < 2 ? current ($args) : $args); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
function messageBox (string $message, string $caption = '', ...$args) |
195
|
|
|
{ |
196
|
|
|
return (new MessageBox)->show ($message, $caption, ...$args); |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
class Components |
200
|
|
|
{ |
201
|
|
|
static array $components = []; |
202
|
|
|
|
203
|
|
|
public static function addComponent (int $selector, object $object): void |
204
|
|
|
{ |
205
|
|
|
self::$components[$selector] = $object; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public static function getComponent (int $selector) |
209
|
|
|
{ |
210
|
|
|
return isset (self::$components[$selector]) ? |
211
|
|
|
self::$components[$selector] : false; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public static function componentExists (int $selector): bool |
215
|
|
|
{ |
216
|
|
|
return isset (self::$components[$selector]); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public static function removeComponent (int $selector): void |
220
|
|
|
{ |
221
|
|
|
unset (self::$components[$selector]); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public static function cleanJunk (): void |
225
|
|
|
{ |
226
|
|
|
// TODO: более строгие правила очистки мусорных объектов |
227
|
|
|
|
228
|
|
|
foreach (self::$components as $selector => $object) |
229
|
|
|
{ |
230
|
|
|
try |
231
|
|
|
{ |
232
|
|
|
if ($object->getType ()->isSubclassOf (\VoidCore::typeof ('System.Windows.Forms.Form', 'System.Windows.Forms'))) |
233
|
|
|
continue; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
catch (\Exception $e) {} |
|
|
|
|
237
|
|
|
|
238
|
|
|
\VoidCore::destructObject ($selector); |
239
|
|
|
|
240
|
|
|
if (!\VoidCore::objectExists ($selector)) |
241
|
|
|
unset (self::$components[$selector]); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
function _c (int $selector) |
247
|
|
|
{ |
248
|
|
|
return Components::getComponent ($selector); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
function c ($name, bool $returnAllSimilarObjects = false) |
252
|
|
|
{ |
253
|
|
|
if (is_int ($name) && is_object ($object = _c ($name))) |
254
|
|
|
return $object; |
255
|
|
|
|
256
|
|
|
else |
257
|
|
|
{ |
258
|
|
|
$path = explode ('->', $name); |
259
|
|
|
$similar = []; |
260
|
|
|
|
261
|
|
|
foreach (Components::$components as $object) |
262
|
|
|
try |
263
|
|
|
{ |
264
|
|
|
if ($object->name == end ($path)) |
265
|
|
|
{ |
266
|
|
|
if (sizeof ($path) > 1) |
267
|
|
|
try |
268
|
|
|
{ |
269
|
|
|
if (is_object ($parent = _c($object->parent->selector))) |
270
|
|
|
{ |
271
|
|
|
if (c(join ('->', array_slice ($path, 0, -1))) == $parent) |
272
|
|
|
{ |
273
|
|
|
if ($returnAllSimilarObjects) |
274
|
|
|
$similar[] = $object; |
275
|
|
|
|
276
|
|
|
else return $object; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
else continue; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
else continue; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
catch (\Throwable $e) |
286
|
|
|
{ |
287
|
|
|
continue; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
else |
291
|
|
|
{ |
292
|
|
|
if ($returnAllSimilarObjects) |
293
|
|
|
$similar[] = $object; |
294
|
|
|
|
295
|
|
|
else return $object; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
catch (\Exception $e) |
301
|
|
|
{ |
302
|
|
|
continue; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
if (sizeof ($path) == 2) |
306
|
|
|
{ |
307
|
|
|
$objects = c($path[1], true); |
308
|
|
|
|
309
|
|
|
if (is_array ($objects)) |
310
|
|
|
{ |
311
|
|
|
foreach ($objects as $id => $object) |
312
|
|
|
try |
313
|
|
|
{ |
314
|
|
|
while (is_object ($parent = _c($object->parent->selector))) |
315
|
|
|
{ |
316
|
|
|
if ($parent->getType ()->isSubclassOf (\VoidCore::typeof ('System.Windows.Forms.Form', 'System.Windows.Forms')) && $parent->name == $path[0]) |
317
|
|
|
return $objects[$id]; |
318
|
|
|
|
319
|
|
|
else $object = $parent; |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
catch (\Throwable $e) |
324
|
|
|
{ |
325
|
|
|
continue; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
return false; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
else return false; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
else return $returnAllSimilarObjects && sizeof ($similar) > 0 ? |
335
|
|
|
$similar : false; |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
function setTimer (int $interval, callable $function): Timer |
340
|
|
|
{ |
341
|
|
|
$timer = new Timer; |
342
|
|
|
$timer->interval = $interval; |
|
|
|
|
343
|
|
|
|
344
|
|
|
$timer->tickEvent = function ($self) use ($function) |
|
|
|
|
345
|
|
|
{ |
346
|
|
|
call_user_func ($function, $self); |
347
|
|
|
}; |
348
|
|
|
|
349
|
|
|
$timer->start (); |
|
|
|
|
350
|
|
|
|
351
|
|
|
return $timer; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
// FIXME: выполняется несколько раз, а не единожды |
355
|
|
|
function setTimeout (int $timeout, callable $function): Timer |
356
|
|
|
{ |
357
|
|
|
$timer = new Timer; |
358
|
|
|
$timer->interval = $timeout; |
|
|
|
|
359
|
|
|
|
360
|
|
|
$timer->tickEvent = function ($self) use ($function) |
|
|
|
|
361
|
|
|
{ |
362
|
|
|
call_user_func ($function, $self); |
363
|
|
|
|
364
|
|
|
$self->stop (); |
365
|
|
|
}; |
366
|
|
|
|
367
|
|
|
$timer->start (); |
368
|
|
|
|
369
|
|
|
return $timer; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
class Clipboard |
373
|
|
|
{ |
374
|
|
|
public static function getText () |
375
|
|
|
{ |
376
|
|
|
return (new WFClass ('System.Windows.Forms.Clipboard'))->getText (); |
|
|
|
|
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
public static function setText (string $text): void |
380
|
|
|
{ |
381
|
|
|
(new WFClass ('System.Windows.Forms.Clipboard'))->setText ($text); |
|
|
|
|
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
public static function getFiles (): array |
385
|
|
|
{ |
386
|
|
|
return (new WFClass ('System.Windows.Forms.Clipboard'))->getFileDropList ()->list; |
|
|
|
|
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
public static function setFiles (array $files): void |
390
|
|
|
{ |
391
|
|
|
$collection = new WFObject ('System.Collections.Specialized.StringCollection'); |
392
|
|
|
|
393
|
|
|
foreach ($files as $file) |
394
|
|
|
$collection->add ((string) $file); |
|
|
|
|
395
|
|
|
|
396
|
|
|
(new WFClass ('System.Windows.Forms.Clipboard'))->setFileDropList ($collection); |
|
|
|
|
397
|
|
|
\VoidCore::removeObjects ($collection->selector); |
|
|
|
|
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
class Cursor |
402
|
|
|
{ |
403
|
|
|
protected $cursor; |
404
|
|
|
|
405
|
|
|
public function __construct (int $handle = null) |
406
|
|
|
{ |
407
|
|
|
$handle !== null ? |
408
|
|
|
$this->cursor = new WFObject ('System.Windows.Forms.Cursor', false, $handle) : |
409
|
|
|
$this->cursor = new WFClass ('System.Windows.Forms.Cursor'); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
public function getPosition (): array |
413
|
|
|
{ |
414
|
|
|
$pos = $this->cursor->position; |
|
|
|
|
415
|
|
|
|
416
|
|
|
return [ |
417
|
|
|
$pos->x, |
|
|
|
|
418
|
|
|
$pos->y |
|
|
|
|
419
|
|
|
]; |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
function get_cursor_x (int $handle = null): int |
424
|
|
|
{ |
425
|
|
|
return (new Cursor ($handle))->getPosition ()[0]; |
|
|
|
|
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
function get_cursor_y (int $handle = null): int |
429
|
|
|
{ |
430
|
|
|
return (new Cursor ($handle))->getPosition ()[1]; |
|
|
|
|
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
function get_cursor_pos (int $handle = null): array |
434
|
|
|
{ |
435
|
|
|
return (new Cursor ($handle))->getPosition (); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
set_error_handler (function ($no, $str, $file, $line) |
439
|
|
|
{ |
440
|
|
|
// Мог ли я здесь сделать более адекватный код с использованием pow/sqrt? Да, мог |
441
|
|
|
// Почему не сделал? Скорость важнее |
442
|
|
|
static $errarr = [ |
443
|
|
|
1 => 'E_ERROR', |
444
|
|
|
2 => 'E_WARNING', |
445
|
|
|
4 => 'E_PARSE', |
446
|
|
|
8 => 'E_NOTICE', |
447
|
|
|
16 => 'E_CORE_ERROR', |
448
|
|
|
32 => 'E_CORE_WARNING', |
449
|
|
|
64 => 'E_COMPILE_ERROR', |
450
|
|
|
128 => 'E_COMPILE_WARNING', |
451
|
|
|
256 => 'E_USER_ERROR', |
452
|
|
|
512 => 'E_USER_WARNING', |
453
|
|
|
1024 => 'E_USER_NOTICE', |
454
|
|
|
2048 => 'E_STRICT', |
455
|
|
|
4096 => 'E_RECOVERABLE_ERROR', |
456
|
|
|
8192 => 'E_DEPRECATED', |
457
|
|
|
16384 => 'E_USER_DEPRECATED' |
458
|
|
|
]; |
459
|
|
|
|
460
|
|
|
if ($GLOBALS['__debug']['error_status']) |
461
|
|
|
message ([ |
|
|
|
|
462
|
|
|
'type' => $errarr[$no], |
463
|
|
|
'text' => $str, |
464
|
|
|
'file' => $file, |
465
|
|
|
'line' => $line |
466
|
|
|
], 'PHP Script Error'); |
467
|
|
|
}); |
468
|
|
|
|