taskman_bhl/bhl.inc.php

400 lines
8.7 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;
2022-05-18 12:17:58 +03:00
task('bhl_clean', function()
{
bhl_clean();
});
task('bhl_clean_cache', function()
{
bhl_clean_cache();
});
task('bhl_try', function($args = [])
{
global $GAME_ROOT;
$all_files = array($args[0]);
$bhl_inc_dir = config_base_dir();
$res_file = "$GAME_ROOT/build/bhl.try";
bhl_run_ex(
$bhl_inc_dir,
$all_files,
$res_file,
false/*not debug*/,
true/*force*/
);
});
task('bhl_callstack', ['alias' => 'bhc'], function(array $args)
{
global $GAME_ROOT;
if(count($args) < 1)
{
echo "Usage: ./gamectl bhl_callstack \"<callstack>\"";
return;
}
$items = explode("\n", $args[0]);
foreach($items as $item)
{
$item = trim($item);
if(!$item)
continue;
if(preg_match('~(\d+)\s+\(\)\s+\(at (\d+):(\d+)\)~', $item, $m))
{
$func_hash = (int)$m[1];
$file_hash = (int)$m[2];
$line_num = (int)$m[3];
$func_name = key(bhl_find_name_hash($func_hash));
$file_name = bhl_find_module($file_hash);
if(!$file_name)
$file_name = "?";
else
$file_name = normalize_path($file_name, true);
echo ($func_name ? $func_name : $func_hash) . "() in $file_name@$line_num \n";
}
}
});
task('bhl_callstack_file', function(array $args)
{
global $GAME_ROOT;
if(count($args) < 1)
{
echo "Usage: ./gamectl bhl_callstack_file \"<path_to_callstack_file>\"";
return;
}
$callstack = ensure_read($args[0]);
run('bhl_callstack', [$callstack]);
});
2022-05-24 17:26:41 +03:00
task('bhl_find_module', function(array $args)
{
if(count($args) < 1)
{
echo "Usage: ./gamectl bhl_find_module <mod_id>";
return;
}
$mod_id = (int)$args[0];
$bhl_file = bhl_find_module($mod_id);
if($bhl_file)
echo "Found '$bhl_file'\n";
});
2022-05-18 12:17:58 +03:00
function bhl_find_name_hash($hash)
{
global $GAME_ROOT;
$result = array();
$files = scan_files_rec(array(config_base_dir()), array('bhl'));
foreach($files as $file)
{
$cont = file_get_contents($file);
if(preg_match_all('/(\w+)/', $cont, $ms))
{
foreach($ms[1] as $name)
{
$name = trim($name, '"');
$var_hash = crc32($name) & 0xFFFFFFF;
if($var_hash === $hash)
{
if(!isset($result[$name]))
$result[$name] = array();
$result[$name][$file] = true;
}
}
}
}
return $result;
}
function bhl_find_module($mod_id)
{
global $GAME_ROOT;
$files = scan_files_rec(array(config_base_dir()), array('.bhl'));
$conf_dir = normalize_path(config_base_dir(), true);
foreach($files as $bhl_file)
{
$module_path = str_replace($conf_dir, '', $bhl_file);
$module_path = ltrim(substr($module_path, 0, strlen($module_path)-4), '/');
if(crc32($module_path) === $mod_id)
return $bhl_file;
}
}
function bhl_result_file()
{
global $GAME_ROOT;
return "$GAME_ROOT/build/ext_config/bhl.bytes";
}
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");
}
function bhl_run($debug = true, $force = false, $check_deps = true, $exit_on_err = true)
{
global $GAME_ROOT;
$bhl_inc_dir = config_base_dir();
$all_files = scan_files_rec(array(config_base_dir()), array('.bhl'));
$res_file = bhl_result_file();
_bhl_shuffle_files($all_files);
$res_file = bhl_result_file();
$res = bhl_run_ex($bhl_inc_dir, $all_files, $res_file, $debug, $force, $check_deps, $exit_on_err);
return $res;
}
function _bhl_shuffle_files(&$files)
{
shuffle($files);
}
function bhl_run_ex($bhl_inc_dir, array $bhl_files, $result_file, $debug = true, $force = false, $check_deps = true, $exit_on_err = true)
{
global $GAME_ROOT;
ensure_mkdir("$GAME_ROOT/build");
$post_proc_file = "$GAME_ROOT/build/bhl.post.".crc32($result_file);
$err_file = "$GAME_ROOT/build/bhl.error";
ensure_rm($err_file);
2022-05-24 17:26:41 +03:00
$user_sources = getor("BHL_USER_SOURCES", array(
2022-05-18 15:23:00 +03:00
get("UNITY_ASSETS_DIR")."/Scripts/bhl/register.cs",
get("UNITY_ASSETS_DIR")."/Scripts/bhl/autobind.cs"
2022-05-24 17:26:41 +03:00
));
2022-05-18 12:17:58 +03:00
2022-05-24 17:26:41 +03:00
$postproc_sources = getor("BHL_POSTPROC_SOURCES", array(
2022-05-24 10:44:17 +03:00
get("UNITY_ASSETS_DIR")."/Scripts/bhl/postproc.cs",
2022-05-24 17:26:41 +03:00
));
2022-05-18 12:17:58 +03:00
$bhl_deps = $bhl_files;
$bhl_deps = array_merge($bhl_deps, $user_sources);
$bhl_deps = array_merge($bhl_deps, $postproc_sources);
$bhl_deps = array_merge($bhl_deps, scan_files_rec(array(bhl_dir()), array(".cs")));
if($force || need_to_regen($result_file, $bhl_deps) || need_to_regen($post_proc_file, $bhl_deps))
{
$bhl_file_list = "$GAME_ROOT/build/bhl/file_list.tmp";
ensure_write($bhl_file_list, implode("\n", $bhl_files));
putenv("POSTPROC_PROJ_ROOT=$GAME_ROOT");
putenv("POSTPROC_RESULT=$post_proc_file");
if($force)
putenv("POSTPROC_FORCE=1");
$bhl_dir = bhl_dir();
2022-05-18 13:04:03 +03:00
bhl_shell("compile " .
2022-05-18 12:17:58 +03:00
"--user-sources=".implode(",",$user_sources)." --postproc-sources=".implode(",",$postproc_sources)." " .
"--dir=$bhl_inc_dir --files=$bhl_file_list --result=$result_file " .
2022-05-18 13:04:03 +03:00
"--tmp-dir=$GAME_ROOT/build/bhl/ " .
2022-05-18 12:17:58 +03:00
"--error=$GAME_ROOT/build/bhl.error " .
"--deterministic " .
"--threads=" . getor("BHL_MAX_THREADS", is_win() ? 1 : 4) . " " .
2022-05-18 12:17:58 +03:00
($debug ? " -d" : "") . " " .
($force || !getor("BHL_USE_CACHE", true) ? " -C" : "") . " ".
2022-05-18 12:17:58 +03:00
(!$check_deps ? " -N" : "") . " ",
$ret_var, $ret_out
);
if($ret_var != 0)
{
bhl_handle_error_result($ret_out, $err_file, $exit_on_err);
if(!$exit_on_err)
return false;
}
else
echo "BHL BUNDLE: total " . kb_len(filesize($result_file)) . "\n";
}
@touch($result_file);
@touch($post_proc_file);
$res = bhl_ast_post_proc($result_file, $post_proc_file);
return $res;
}
function bhl_handle_error_result(array $ret_out, $err_file, $exit = true)
{
if(!is_file($err_file))
{
stderr("Something went wrong: " . (is_array($ret_out) ? implode("\n", $ret_out) : "????"));
if($exit)
exit(1);
}
$history = array();
$err_lines = file($err_file);
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
if(isset($history[$file]))
continue;
$history[$file] = true;
stderr("BHL ERROR: " . $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);
}
class BhlAstProcResult
{
var $res_file;
var $func2assets_deep = array();
var $func2refs_deep = array();
}
function bhl_ast_post_proc($res_file, $post_proc_file)
{
$res = new BhlAstProcResult();
$res->res_file = $res_file;
2022-05-25 16:42:39 +03:00
$data = ensure_read($post_proc_file);
if(!$data)
return $res;
$data = config_msgpack_unpack($data);
2022-05-18 12:17:58 +03:00
if(!$data)
return $res;
$func2assets = $data[0];
foreach($func2assets as $item)
{
list($func1, $func2, $assets) = $item;
$func = ($func2 << 31) | $func1;
$res->func2assets_deep[$func] = array_flip($assets);
}
$func2crefs = $data[1];
foreach($func2crefs as $item)
{
list($func1, $func2, $crefs) = $item;
$func = ($func2 << 31) | $func1;
$res->func2refs_deep[$func] = array_flip($crefs);
}
return $res;
}
function bhl_clean()
{
global $GAME_ROOT;
$files = scan_files_rec(array(config_base_dir()), array('js', 'gen.bhl'));
foreach($files as $file)
{
$is_conf = strpos($file, '.gen.bhl') === false;
if($is_conf)
{
//skipping .def.js files
if(strpos($file, '.def.js') !== false)
continue;
@touch($file);
}
else if(!$is_conf)
{
ensure_rm($file);
}
}
bhl_clean_cache();
bhl_shell_ensure("clean");
}
function bhl_clean_cache()
{
global $GAME_ROOT;
ensure_rm("$GAME_ROOT/build/bhl/");
}
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;
}