taskman_bhl/bhl.inc.php

325 lines
7.4 KiB
PHP
Raw Normal View History

2022-05-18 12:17:58 +03:00
<?php
namespace taskman;
2022-05-18 12:28:35 +03:00
use Exception;
2023-11-09 13:02:47 +03:00
use stdClass;
2022-05-18 12:17:58 +03:00
task('bhl_clean', function()
{
bhl_clean();
});
task('bhl_clean_cache', function()
{
bhl_clean_cache();
});
2023-05-29 16:22:38 +03:00
task('bhl_make_upm_package', function() {
bhl_make_upm_package();
});
2023-04-18 13:13:45 +03:00
function bhl_proj_file()
{
return get('BHL_PROJ_FILE');
2023-04-18 13:13:45 +03:00
}
2023-11-09 13:02:47 +03:00
function bhl_proj(?string $proj_file = null)
2023-04-18 13:13:45 +03:00
{
global $GAME_ROOT;
2023-11-09 13:02:47 +03:00
static $projs = [];
$proj_file ??= bhl_proj_file();
if(!isset($projs[$proj_file]))
2023-04-18 13:13:45 +03:00
{
$proj = json_decode(ensure_read($proj_file));
if(!$proj)
throw new Exception("Bad bhl project file: $proj_file");
2023-11-09 13:02:47 +03:00
$proj->file_path = $proj_file; //NOTE: adding path to the file for convenience
2023-04-18 13:13:45 +03:00
foreach($proj->src_dirs as $k => $v)
$proj->src_dirs[$k] = _bhl_make_abs_path($proj_file, $v);
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
if(isset($proj->postproc_sources))
{
foreach($proj->postproc_sources as $k => $v)
$proj->postproc_sources[$k] = _bhl_make_abs_path($proj_file, $v);
}
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
$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
$projs[$proj_file] = $proj;
2023-04-18 13:13:45 +03:00
}
2023-11-09 13:02:47 +03:00
return $projs[$proj_file];
2023-04-18 13:13:45 +03:00
}
function _bhl_make_abs_path($proj_file, $path)
{
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-09 13:02:47 +03:00
function bhl_result_file(stdClass $bhl_proj = null)
2022-05-18 12:17:58 +03:00
{
2023-11-09 13:02:47 +03:00
$bhl_proj ??= bhl_proj();
return $bhl_proj->result_file;
2022-05-18 12:17:58 +03:00
}
function bhl_dir()
{
global $GAME_ROOT;
return "$GAME_ROOT/composer/vendor/bit/bhl";
}
function bhl_shell($cmd, &$ret_var, &$ret_out)
{
$prev_path = mono_try_override_path();
try
{
shell_try(bhl_dir()."/bhl $cmd", $ret_var, $ret_out);
}
finally
{
mono_try_restore_path($prev_path);
}
}
function bhl_shell_ensure($cmd)
{
bhl_shell($cmd, $ret_var, $ret_out);
if($ret_var !== 0)
throw new Exception("Error executing shell cmd: $ret_var");
}
2023-11-09 13:02:47 +03:00
function bhl_scan_files(stdClass $bhl_proj = null)
{
2023-11-09 13:02:47 +03:00
$bhl_proj ??= bhl_proj();
return scan_files_rec($bhl_proj->src_dirs, array('.bhl'));
}
2023-11-09 13:02:47 +03:00
function bhl_run($debug = true, $force = false, $exit_on_err = true, stdClass $bhl_proj = null)
2022-05-18 12:17:58 +03:00
{
global $GAME_ROOT;
2023-11-09 13:02:47 +03:00
$bhl_proj ??= bhl_proj();
$result_file = $bhl_proj->result_file;
2022-05-18 12:17:58 +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
echo "BHL BUNDLE: total " . kb_len(filesize($result_file)) . "\n";
}
@touch($result_file);
}
function bhl_handle_error_result(array $ret_out, $err_file, $exit = true)
{
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);
}
$history = array();
$err_lines = file($err_file);
2023-03-28 17:39:21 +03:00
$err_count = 0;
foreach($err_lines as $err)
2022-05-18 12:17:58 +03:00
{
$jerr = json_decode($err);
if(!$jerr)
{
stderr("Something went wrong, bhl error is in invalid format: $err\n");
continue;
}
2022-05-18 12:17:58 +03:00
$file = $jerr->file;
//let's show only one error per file's line
if(isset($history[$file.$jerr->line]))
continue;
$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");
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-09 13:02:47 +03:00
function bhl_clean(stdClass $bhl_proj = null)
2022-05-18 12:17:58 +03:00
{
2023-11-09 13:02:47 +03:00
bhl_clean_cache($bhl_proj);
2022-05-18 12:17:58 +03:00
bhl_shell_ensure("clean");
}
2023-11-09 13:02:47 +03:00
function bhl_clean_cache(stdClass $bhl_proj = null)
2022-05-18 12:17:58 +03:00
{
2023-11-09 13:02:47 +03:00
$bhl_proj ??= bhl_proj();
ensure_rm($bhl_proj->tmp_dir);
2022-05-18 12:17:58 +03:00
}
function bhl_show_position($line, $row, array $lines)
{
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
return "??? @($line:$row)";
2022-05-18 12:17:58 +03:00
}
function bhl_line_row_to_pos($file, $line, $row)
{
$pos = 0;
$lines = file($file);
//something went wrong
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-09 13:02:47 +03:00
function bhl_map_module_to_file($module, stdClass $bhl_proj = null)
2023-05-18 15:09:35 +03:00
{
2023-11-09 13:02:47 +03:00
$bhl_proj ??= bhl_proj();
foreach($bhl_proj->src_dirs as $dir)
2023-05-18 15:09:35 +03:00
{
$tmp = $dir.'/'.$module.'.bhl';
if(file_exists($tmp))
return $tmp;
}
return null;
}
function bhl_validate_func_ref($module_file, $func, array $signature)
2023-05-18 15:09:35 +03:00
{
if(sizeof($signature) == 0)
throw new Exception("Signature is invalid");
$module_src = file_get_contents($module_file);
if(!$module_src)
throw new Exception("Bad module file '{$module_file}'");
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 .= '~';
if(!preg_match($signature_pattern, $module_src))
throw new Exception("Func '$func' signature '".implode(',', $signature)."' not found in module '$module_file'");
2023-05-18 15:09:35 +03:00
}
2023-05-29 16:22:38 +03:00
function bhl_upm_path()
{
global $GAME_ROOT;
$pkg_dir = get("UNITY_ASSETS_DIR") . '/../Packages/bhl';
return $pkg_dir;
}
function bhl_clean_upm_package()
{
ensure_rm(bhl_upm_path() . '/Runtime/code/');
}
function bhl_make_upm_package()
{
global $GAME_ROOT;
$pkg_dir = bhl_upm_path();
ensure_mkdir($pkg_dir);
$srcs = array();
$lines = file(bhl_dir() . '/bhl.cs');
$started = false;
foreach($lines as $line)
{
if(!$started && strpos($line, 'static readonly string[] VM_SRC = new string[] {') !== false)
{
$started = true;
continue;
}
else if($started && strpos($line, '};') !== false)
break;
else if($started)
{
$mask = trim($line);
$mask = str_replace('$"{BHL_ROOT}/', '', $mask);
$mask = str_replace('",', '', $mask);
$srcs = array_merge($srcs, glob(bhl_dir() . '/' . $mask));
}
}
$trgs = array();
$changed = false;
foreach($srcs as $src)
{
$trg = $pkg_dir . '/Runtime/code/' . str_replace(bhl_dir(), '', $src);
if(!is_file($trg) || file_get_contents($src) != file_get_contents($trg))
$changed = true;
$trgs[$src] = $trg;
}
if($changed)
{
bhl_clean_upm_package();
foreach($trgs as $src => $trg)
ensure_copy($src, $trg);
}
}