Passed
Push — master ( 232fa2...89a9e2 )
by Observer
01:17
created

messageBox()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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