Introducing typed BhlProj instead of stdClass

This commit is contained in:
Pavel Shevaev 2023-11-16 09:30:06 +03:00
parent 3e064092c7
commit 9cfff757c3
1 changed files with 27 additions and 10 deletions

View File

@ -1,7 +1,6 @@
<?php <?php
namespace taskman; namespace taskman;
use Exception; use Exception;
use stdClass;
task('bhl_clean', function() task('bhl_clean', function()
{ {
@ -22,7 +21,21 @@ function bhl_proj_file()
return get('BHL_PROJ_FILE'); return get('BHL_PROJ_FILE');
} }
function bhl_proj(?string $proj_file = null) class BhlProj
{
public string $file_path;
public array $src_dirs = array();
public array $bindings_sources;
public array $postproc_sources;
public string $postproc_dll;
public string $result_file;
public string $error_file;
public string $tmp_dir;
public int $max_threads;
public bool $deterministic;
}
function bhl_proj(?string $proj_file = null) : BhlProj
{ {
global $GAME_ROOT; global $GAME_ROOT;
@ -32,10 +45,14 @@ function bhl_proj(?string $proj_file = null)
if(!isset($projs[$proj_file])) if(!isset($projs[$proj_file]))
{ {
$proj = json_decode(ensure_read($proj_file)); $arr = json_decode(ensure_read($proj_file), true);
if(!$proj) if(!$arr)
throw new Exception("Bad bhl project file: $proj_file"); throw new Exception("Bad bhl project file: $proj_file");
$proj = new BhlProj();
foreach($arr as $k => $v)
$proj->{$k} = $v;
$proj->file_path = $proj_file; //NOTE: adding path to the file for convenience $proj->file_path = $proj_file; //NOTE: adding path to the file for convenience
foreach($proj->src_dirs as $k => $v) foreach($proj->src_dirs as $k => $v)
@ -71,7 +88,7 @@ function _bhl_make_abs_path($proj_file, $path)
return $path; return $path;
} }
function bhl_result_file(stdClass $bhl_proj = null) function bhl_result_file(BhlProj $bhl_proj = null)
{ {
$bhl_proj ??= bhl_proj(); $bhl_proj ??= bhl_proj();
return $bhl_proj->result_file; return $bhl_proj->result_file;
@ -104,13 +121,13 @@ function bhl_shell_ensure($cmd)
throw new Exception("Error executing shell cmd: $ret_var"); throw new Exception("Error executing shell cmd: $ret_var");
} }
function bhl_scan_files(stdClass $bhl_proj = null) function bhl_scan_files(BhlProj $bhl_proj = null)
{ {
$bhl_proj ??= bhl_proj(); $bhl_proj ??= bhl_proj();
return scan_files_rec($bhl_proj->src_dirs, array('.bhl')); return scan_files_rec($bhl_proj->src_dirs, array('.bhl'));
} }
function bhl_run($debug = true, $force = false, $exit_on_err = true, stdClass $bhl_proj = null) function bhl_run($debug = true, $force = false, $exit_on_err = true, BhlProj $bhl_proj = null)
{ {
global $GAME_ROOT; global $GAME_ROOT;
@ -173,13 +190,13 @@ function bhl_handle_error_result(array $ret_out, $err_file, $exit = true)
exit(1); exit(1);
} }
function bhl_clean(stdClass $bhl_proj = null) function bhl_clean(BhlProj $bhl_proj = null)
{ {
bhl_clean_cache($bhl_proj); bhl_clean_cache($bhl_proj);
bhl_shell_ensure("clean"); bhl_shell_ensure("clean");
} }
function bhl_clean_cache(stdClass $bhl_proj = null) function bhl_clean_cache(BhlProj $bhl_proj = null)
{ {
$bhl_proj ??= bhl_proj(); $bhl_proj ??= bhl_proj();
ensure_rm($bhl_proj->tmp_dir); ensure_rm($bhl_proj->tmp_dir);
@ -218,7 +235,7 @@ function bhl_line_row_to_pos($file, $line, $row)
return $pos; return $pos;
} }
function bhl_map_module_to_file($module, stdClass $bhl_proj = null) function bhl_map_module_to_file($module, BhlProj $bhl_proj = null)
{ {
$bhl_proj ??= bhl_proj(); $bhl_proj ??= bhl_proj();
foreach($bhl_proj->src_dirs as $dir) foreach($bhl_proj->src_dirs as $dir)