2022-05-18 12:17:58 +03:00
|
|
|
<?php
|
|
|
|
namespace taskman;
|
2022-05-18 12:28:35 +03:00
|
|
|
use Exception;
|
2022-05-18 12:17:58 +03:00
|
|
|
|
|
|
|
task('bhl_clean', function()
|
|
|
|
{
|
|
|
|
bhl_clean();
|
|
|
|
});
|
|
|
|
|
|
|
|
task('bhl_clean_cache', function()
|
|
|
|
{
|
|
|
|
bhl_clean_cache();
|
|
|
|
});
|
|
|
|
|
2023-11-16 09:30:06 +03:00
|
|
|
class BhlProj
|
|
|
|
{
|
2023-11-24 13:43:39 +03:00
|
|
|
public static ?BhlProj $active = null;
|
|
|
|
|
2023-11-16 09:30:06 +03:00
|
|
|
public string $file_path;
|
2023-11-24 13:43:39 +03:00
|
|
|
public array $inc_dirs = array();
|
2023-11-16 09:30:06 +03:00
|
|
|
public array $src_dirs = array();
|
2024-03-14 12:52:07 +03:00
|
|
|
public array $defines = array();
|
2023-11-16 09:30:06 +03:00
|
|
|
public array $bindings_sources;
|
|
|
|
public array $postproc_sources;
|
|
|
|
public string $postproc_dll;
|
2023-11-16 11:20:40 +03:00
|
|
|
public string $bindings_dll;
|
2023-11-16 09:30:06 +03:00
|
|
|
public string $result_file;
|
|
|
|
public string $error_file;
|
|
|
|
public string $tmp_dir;
|
|
|
|
public int $max_threads;
|
|
|
|
public bool $deterministic;
|
2023-11-24 13:43:39 +03:00
|
|
|
|
|
|
|
function getIncPath() : array
|
|
|
|
{
|
|
|
|
if($this->inc_dirs)
|
|
|
|
return $this->inc_dirs;
|
|
|
|
return $this->src_dirs;
|
|
|
|
}
|
2023-11-16 09:30:06 +03:00
|
|
|
}
|
|
|
|
|
2023-11-24 13:43:39 +03:00
|
|
|
function bhl_proj_file() : string
|
2023-04-18 13:13:45 +03:00
|
|
|
{
|
2023-11-24 13:43:39 +03:00
|
|
|
return get('BHL_PROJ_FILE');
|
|
|
|
}
|
|
|
|
|
|
|
|
function bhl_proj_load(string $proj_file) : BhlProj
|
|
|
|
{
|
|
|
|
$arr = json_decode(ensure_read($proj_file), true);
|
|
|
|
if(!$arr)
|
|
|
|
throw new Exception("Bad bhl project file: $proj_file");
|
2023-04-18 13:13:45 +03:00
|
|
|
|
2023-11-24 13:43:39 +03:00
|
|
|
$proj = new BhlProj();
|
|
|
|
foreach($arr as $k => $v)
|
|
|
|
$proj->{$k} = $v;
|
2023-11-09 13:02:47 +03:00
|
|
|
|
2023-11-24 13:43:39 +03:00
|
|
|
//NOTE: adding path to the file for further convenience
|
|
|
|
$proj->file_path = $proj_file;
|
2023-11-09 13:02:47 +03:00
|
|
|
|
2023-11-24 13:43:39 +03:00
|
|
|
foreach($proj->inc_dirs as $k => $v)
|
|
|
|
$proj->inc_dirs[$k] = _bhl_make_abs_path($proj_file, $v);
|
2023-04-18 13:13:45 +03:00
|
|
|
|
2023-11-24 13:43:39 +03:00
|
|
|
foreach($proj->src_dirs as $k => $v)
|
|
|
|
$proj->src_dirs[$k] = _bhl_make_abs_path($proj_file, $v);
|
2023-11-16 09:30:06 +03:00
|
|
|
|
2023-11-24 13:43:39 +03:00
|
|
|
foreach($proj->bindings_sources as $k => $v)
|
|
|
|
$proj->bindings_sources[$k] = _bhl_make_abs_path($proj_file, $v);
|
|
|
|
$proj->bindings_dll = _bhl_make_abs_path($proj_file, $proj->bindings_dll);
|
2023-11-09 13:02:47 +03:00
|
|
|
|
2023-11-24 13:43:39 +03:00
|
|
|
if(isset($proj->postproc_sources))
|
|
|
|
{
|
|
|
|
foreach($proj->postproc_sources as $k => $v)
|
|
|
|
$proj->postproc_sources[$k] = _bhl_make_abs_path($proj_file, $v);
|
|
|
|
}
|
2023-04-18 13:13:45 +03:00
|
|
|
|
2023-11-24 13:43:39 +03:00
|
|
|
if(isset($proj->postproc_dll))
|
|
|
|
$proj->postproc_dll = _bhl_make_abs_path($proj_file, $proj->postproc_dll);
|
2023-04-18 13:13:45 +03:00
|
|
|
|
2023-11-24 13:43:39 +03:00
|
|
|
$proj->result_file = _bhl_make_abs_path($proj_file, $proj->result_file);
|
|
|
|
$proj->error_file = _bhl_make_abs_path($proj_file, $proj->error_file);
|
|
|
|
$proj->tmp_dir = _bhl_make_abs_path($proj_file, $proj->tmp_dir);
|
2023-11-09 13:02:47 +03:00
|
|
|
|
2023-11-24 13:43:39 +03:00
|
|
|
return $proj;
|
|
|
|
}
|
2023-04-18 13:13:45 +03:00
|
|
|
|
2023-11-24 13:43:39 +03:00
|
|
|
function bhl_proj_set_active(BhlProj $proj) : ?BhlProj
|
|
|
|
{
|
|
|
|
$prev = BhlProj::$active;
|
|
|
|
BhlProj::$active = $proj;
|
|
|
|
return $prev;
|
|
|
|
}
|
2023-11-09 13:02:47 +03:00
|
|
|
|
2023-11-24 13:43:39 +03:00
|
|
|
function bhl_proj() : BhlProj
|
|
|
|
{
|
|
|
|
if(BhlProj::$active == null)
|
|
|
|
{
|
|
|
|
$proj = bhl_proj_load(bhl_proj_file());
|
|
|
|
BhlProj::$active = $proj;
|
2023-04-18 13:13:45 +03:00
|
|
|
}
|
2023-11-24 13:43:39 +03:00
|
|
|
return BhlProj::$active;
|
2023-04-18 13:13:45 +03:00
|
|
|
}
|
|
|
|
|
2023-11-24 16:32:58 +03:00
|
|
|
function _bhl_make_abs_path(string $proj_file, string $path) : string
|
2023-04-18 13:13:45 +03:00
|
|
|
{
|
2023-04-18 13:42:35 +03:00
|
|
|
if($path && $path[0] == '.')
|
2023-04-18 13:13:45 +03:00
|
|
|
return dirname($proj_file) . '/' . $path;
|
|
|
|
else
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
2023-11-24 16:32:58 +03:00
|
|
|
function bhl_result_file() : string
|
2022-05-18 12:17:58 +03:00
|
|
|
{
|
2023-11-24 13:43:39 +03:00
|
|
|
return bhl_proj()->result_file;
|
2022-05-18 12:17:58 +03:00
|
|
|
}
|
|
|
|
|
2023-11-24 16:32:58 +03:00
|
|
|
function bhl_dir() : string
|
2022-05-18 12:17:58 +03:00
|
|
|
{
|
|
|
|
global $GAME_ROOT;
|
|
|
|
return "$GAME_ROOT/composer/vendor/bit/bhl";
|
|
|
|
}
|
|
|
|
|
2023-11-24 16:32:58 +03:00
|
|
|
function bhl_shell(string $cmd, &$ret_var, &$ret_out)
|
2022-05-18 12:17:58 +03:00
|
|
|
{
|
2024-09-20 17:48:53 +03:00
|
|
|
shell_try(bhl_dir()."/bhl $cmd", $ret_var, $ret_out);
|
2022-05-18 12:17:58 +03:00
|
|
|
}
|
|
|
|
|
2023-11-24 16:32:58 +03:00
|
|
|
function bhl_shell_ensure(string $cmd)
|
2022-05-18 12:17:58 +03:00
|
|
|
{
|
|
|
|
bhl_shell($cmd, $ret_var, $ret_out);
|
|
|
|
if($ret_var !== 0)
|
|
|
|
throw new Exception("Error executing shell cmd: $ret_var");
|
|
|
|
}
|
|
|
|
|
2023-11-24 16:32:58 +03:00
|
|
|
function bhl_scan_files() : array
|
2023-05-25 18:07:27 +03:00
|
|
|
{
|
2023-11-24 13:43:39 +03:00
|
|
|
return scan_files_rec(bhl_proj()->src_dirs, array('.bhl'));
|
2023-05-25 18:07:27 +03:00
|
|
|
}
|
|
|
|
|
2023-11-24 16:32:58 +03:00
|
|
|
function bhl_run(bool $debug = true, bool $force = false, bool $exit_on_err = true)
|
2022-05-18 12:17:58 +03:00
|
|
|
{
|
|
|
|
global $GAME_ROOT;
|
|
|
|
|
2023-11-24 13:43:39 +03:00
|
|
|
$bhl_proj = bhl_proj();
|
2023-11-09 13:02:47 +03:00
|
|
|
$result_file = $bhl_proj->result_file;
|
2022-05-18 12:17:58 +03:00
|
|
|
|
2023-11-24 13:43:39 +03:00
|
|
|
if($force || need_to_regen($result_file, bhl_scan_files()))
|
2022-05-18 12:17:58 +03:00
|
|
|
{
|
2023-11-09 13:02:47 +03:00
|
|
|
bhl_shell("compile -p " . $bhl_proj->file_path . " " .
|
2022-05-18 12:17:58 +03:00
|
|
|
($debug ? " -d" : "") . " " .
|
2023-04-18 13:13:45 +03:00
|
|
|
($force || !getor("BHL_USE_CACHE", true) ? " -C" : ""),
|
2022-05-18 12:17:58 +03:00
|
|
|
$ret_var, $ret_out
|
|
|
|
);
|
|
|
|
|
|
|
|
if($ret_var != 0)
|
|
|
|
{
|
2023-11-09 13:02:47 +03:00
|
|
|
bhl_handle_error_result($ret_out, $bhl_proj->error_file, $exit_on_err);
|
2022-05-18 12:17:58 +03:00
|
|
|
if(!$exit_on_err)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
2023-11-28 11:00:19 +03:00
|
|
|
echo "BHL BUNDLE: total " . kb_len(filesize($result_file)) . ", CRC " . hexdec(hash_file('CRC32', $result_file, false)) . "\n";
|
2022-05-18 12:17:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@touch($result_file);
|
|
|
|
}
|
|
|
|
|
2023-11-24 16:32:58 +03:00
|
|
|
function bhl_handle_error_result(array $ret_out, string $err_file, bool $exit = true)
|
2022-05-18 12:17:58 +03:00
|
|
|
{
|
|
|
|
if(!is_file($err_file))
|
|
|
|
{
|
2023-08-16 14:10:53 +03:00
|
|
|
stderr("Something went wrong: " . implode("\n", $ret_out));
|
2022-05-18 12:17:58 +03:00
|
|
|
if($exit)
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2023-03-02 19:52:11 +03:00
|
|
|
$history = array();
|
2023-03-02 18:54:49 +03:00
|
|
|
$err_lines = file($err_file);
|
2023-03-28 17:39:21 +03:00
|
|
|
$err_count = 0;
|
2023-03-02 18:54:49 +03:00
|
|
|
foreach($err_lines as $err)
|
2022-05-18 12:17:58 +03:00
|
|
|
{
|
2023-03-02 18:54:49 +03:00
|
|
|
$jerr = json_decode($err);
|
|
|
|
if(!$jerr)
|
|
|
|
{
|
2023-03-02 19:52:11 +03:00
|
|
|
stderr("Something went wrong, bhl error is in invalid format: $err\n");
|
2023-03-02 18:54:49 +03:00
|
|
|
continue;
|
|
|
|
}
|
2022-05-18 12:17:58 +03:00
|
|
|
|
2023-03-02 18:54:49 +03:00
|
|
|
$file = $jerr->file;
|
2023-03-16 17:29:38 +03:00
|
|
|
//let's show only one error per file's line
|
|
|
|
if(isset($history[$file.$jerr->line]))
|
2023-03-02 19:52:11 +03:00
|
|
|
continue;
|
2023-03-16 17:29:38 +03:00
|
|
|
$history[$file.$jerr->line] = true;
|
2023-03-28 17:39:21 +03:00
|
|
|
++$err_count;
|
|
|
|
stderr("BHL ERROR (#$err_count): " . $jerr->error . " \nin '$file', line {$jerr->line}\n");
|
2023-03-02 18:54:49 +03:00
|
|
|
if(file_exists($file))
|
|
|
|
stderr(bhl_show_position($jerr->line, $jerr->column, file($file)) . "\n");
|
|
|
|
}
|
|
|
|
if($err_lines && $exit)
|
2022-05-18 12:17:58 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2023-11-24 13:43:39 +03:00
|
|
|
function bhl_clean()
|
2022-05-18 12:17:58 +03:00
|
|
|
{
|
2023-11-24 13:43:39 +03:00
|
|
|
bhl_clean_cache();
|
2022-05-18 12:17:58 +03:00
|
|
|
bhl_shell_ensure("clean");
|
|
|
|
}
|
|
|
|
|
2023-11-24 13:43:39 +03:00
|
|
|
function bhl_clean_cache()
|
2022-05-18 12:17:58 +03:00
|
|
|
{
|
2023-11-24 13:43:39 +03:00
|
|
|
ensure_rm(bhl_proj()->tmp_dir);
|
2022-05-18 12:17:58 +03:00
|
|
|
}
|
|
|
|
|
2023-11-24 16:32:58 +03:00
|
|
|
function bhl_show_position(int $line, int $row, array $lines) : string
|
2022-05-18 12:17:58 +03:00
|
|
|
{
|
2022-08-25 14:49:22 +03:00
|
|
|
if($line > 0 && $line <= count($lines))
|
2022-08-25 16:05:11 +03:00
|
|
|
{
|
|
|
|
//handling tabs
|
|
|
|
$hint = str_replace("\t", " ", $lines[$line-1]);
|
|
|
|
for($c=0;$c<$row;++$c)
|
|
|
|
{
|
|
|
|
if($lines[$line-1][$c] === "\t")
|
|
|
|
$hint .= "----";
|
|
|
|
else
|
|
|
|
$hint .= "-";
|
|
|
|
}
|
|
|
|
$hint .= "^";
|
|
|
|
return $hint;
|
|
|
|
}
|
2022-05-18 12:17:58 +03:00
|
|
|
else
|
2022-08-25 14:49:22 +03:00
|
|
|
return "??? @($line:$row)";
|
2022-05-18 12:17:58 +03:00
|
|
|
}
|
|
|
|
|
2023-11-24 16:32:58 +03:00
|
|
|
function bhl_line_row_to_pos(string $file, int $line, int $row) : ?int
|
2022-05-18 12:17:58 +03:00
|
|
|
{
|
|
|
|
$pos = 0;
|
|
|
|
$lines = file($file);
|
|
|
|
//something went wrong
|
2022-08-25 14:49:22 +03:00
|
|
|
if($line <= 0 || $line > count($lines))
|
|
|
|
return null;
|
2022-05-18 12:17:58 +03:00
|
|
|
for($i=0;$i<($line-1);++$i)
|
|
|
|
$pos += strlen($lines[$i]);
|
|
|
|
$pos += $row;
|
|
|
|
return $pos;
|
|
|
|
}
|
2023-05-18 15:09:35 +03:00
|
|
|
|
2023-11-24 16:32:58 +03:00
|
|
|
function bhl_map_module_to_file(string $module) : ?string
|
2023-05-18 15:09:35 +03:00
|
|
|
{
|
2023-11-24 13:43:39 +03:00
|
|
|
$bhl_proj = bhl_proj();
|
|
|
|
foreach($bhl_proj->getIncPath() as $dir)
|
2023-05-18 15:09:35 +03:00
|
|
|
{
|
|
|
|
$tmp = $dir.'/'.$module.'.bhl';
|
|
|
|
if(file_exists($tmp))
|
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-11-24 16:32:58 +03:00
|
|
|
function bhl_validate_func_ref(string $module_file, string $func_full_name, array $signature)
|
2023-05-18 15:09:35 +03:00
|
|
|
{
|
|
|
|
if(sizeof($signature) == 0)
|
|
|
|
throw new Exception("Signature is invalid");
|
|
|
|
|
2023-11-24 16:32:58 +03:00
|
|
|
$name_items = explode('.', $func_full_name);
|
2024-04-24 16:29:13 +03:00
|
|
|
$func = array_pop($name_items);
|
|
|
|
$namespace = implode('.', $name_items);
|
2023-11-24 16:32:58 +03:00
|
|
|
|
2023-05-18 15:09:35 +03:00
|
|
|
$module_src = file_get_contents($module_file);
|
2024-10-04 13:34:22 +03:00
|
|
|
$module_src = _bhl_remove_comments($module_src);
|
2023-05-18 15:09:35 +03:00
|
|
|
|
2023-05-30 16:34:59 +03:00
|
|
|
if(!$module_src)
|
|
|
|
throw new Exception("Bad module file '{$module_file}'");
|
|
|
|
|
2024-04-24 16:29:13 +03:00
|
|
|
$module_chunks = array();
|
|
|
|
if($namespace)
|
|
|
|
{
|
2024-04-24 19:39:01 +03:00
|
|
|
$ns_chunks = _bhl_split_by_namespaces($module_src);
|
|
|
|
if(count($ns_chunks) == 0)
|
|
|
|
throw new Exception("No namespaces found in '$module_file'");
|
|
|
|
|
|
|
|
foreach($ns_chunks as $ns => $ns_src)
|
|
|
|
{
|
|
|
|
if($ns === $namespace)
|
|
|
|
$module_chunks[] = $ns_src;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(count($module_chunks) == 0)
|
2024-04-24 16:51:02 +03:00
|
|
|
throw new Exception("Namespace '$namespace' for func '$func_full_name' not found in '$module_file'");
|
2024-04-24 16:29:13 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
$module_chunks[] = $module_src;
|
|
|
|
|
2023-05-18 15:09:35 +03:00
|
|
|
$signature_pattern = '';
|
|
|
|
$signature_pattern .= '~func\s+';
|
|
|
|
if($signature[0] !== 'void')
|
|
|
|
{
|
|
|
|
$signature_pattern .= preg_quote($signature[0]);
|
|
|
|
$signature_pattern .= '\s+';
|
|
|
|
}
|
|
|
|
$signature_pattern .= $func;
|
|
|
|
$signature_pattern .= '\s*\(';
|
|
|
|
array_shift($signature);
|
|
|
|
foreach($signature as $type)
|
|
|
|
{
|
|
|
|
$signature_pattern .= '\s*';
|
|
|
|
$signature_pattern .= preg_quote($type);
|
|
|
|
$signature_pattern .= '\s*\S+';
|
|
|
|
$signature_pattern .= '\s*,';
|
|
|
|
}
|
|
|
|
$signature_pattern = rtrim($signature_pattern, '\s*,');
|
|
|
|
$signature_pattern .= '\s*\)';
|
|
|
|
$signature_pattern .= '~';
|
|
|
|
|
2024-04-24 16:29:13 +03:00
|
|
|
foreach($module_chunks as $module_chunk_src)
|
|
|
|
{
|
|
|
|
if(preg_match($signature_pattern, $module_chunk_src))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-24 16:51:02 +03:00
|
|
|
throw new Exception("Func '$func_full_name(".implode(',', $signature).")' not found in '$module_file'");
|
2023-05-18 15:09:35 +03:00
|
|
|
}
|
2023-05-29 16:22:38 +03:00
|
|
|
|
2024-10-04 13:34:22 +03:00
|
|
|
function _bhl_remove_comments(string $txt) : string
|
|
|
|
{
|
|
|
|
//block comments
|
|
|
|
if(strpos($txt, '/*') !== false)
|
|
|
|
{
|
|
|
|
$regex = '~/\*.*?\*/~s';
|
|
|
|
$txt = preg_replace_callback(
|
|
|
|
$regex,
|
|
|
|
//preserve the new lines for better error reporting
|
|
|
|
function($m) { return str_repeat("\n", substr_count($m[0], "\n")); },
|
|
|
|
$txt);
|
|
|
|
}
|
|
|
|
//line comments
|
|
|
|
$txt = preg_replace("~\s*(?<!:)//.*~", "\n", $txt);
|
|
|
|
return $txt;
|
|
|
|
}
|
|
|
|
|
2024-04-24 19:39:01 +03:00
|
|
|
function _bhl_split_by_namespaces(string $src) : array
|
|
|
|
{
|
|
|
|
$nss = array();
|
|
|
|
|
|
|
|
$chunks = preg_split('~^\s*(namespace\s+[^\{]+){~m', $src, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
|
|
|
|
|
|
|
|
for($i=0;$i<count($chunks);)
|
|
|
|
{
|
|
|
|
$chunk = $chunks[$i];
|
|
|
|
if(strpos(ltrim($chunk), 'namespace ') === 0)
|
|
|
|
{
|
|
|
|
$ns = trim(substr(ltrim($chunk), 9));
|
|
|
|
$body = $chunks[$i+1];
|
|
|
|
$nss[$ns] = $body;
|
|
|
|
$i+=2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++$i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $nss;
|
|
|
|
}
|