|
|
|
@ -1,7 +1,6 @@
|
|
|
|
|
<?php
|
|
|
|
|
namespace taskman;
|
|
|
|
|
use Exception;
|
|
|
|
|
use stdClass;
|
|
|
|
|
|
|
|
|
|
task('bhl_clean', function()
|
|
|
|
|
{
|
|
|
|
@ -13,57 +12,94 @@ task('bhl_clean_cache', function()
|
|
|
|
|
bhl_clean_cache();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
task('bhl_make_upm_package', function() {
|
|
|
|
|
bhl_make_upm_package();
|
|
|
|
|
});
|
|
|
|
|
class BhlProj
|
|
|
|
|
{
|
|
|
|
|
public static ?BhlProj $active = null;
|
|
|
|
|
|
|
|
|
|
function bhl_proj_file()
|
|
|
|
|
public string $file_path;
|
|
|
|
|
public array $inc_dirs = array();
|
|
|
|
|
public array $src_dirs = array();
|
|
|
|
|
public array $defines = array();
|
|
|
|
|
public array $bindings_sources;
|
|
|
|
|
public array $postproc_sources;
|
|
|
|
|
public string $postproc_dll;
|
|
|
|
|
public string $bindings_dll;
|
|
|
|
|
public string $result_file;
|
|
|
|
|
public string $error_file;
|
|
|
|
|
public string $tmp_dir;
|
|
|
|
|
public int $max_threads;
|
|
|
|
|
public bool $deterministic;
|
|
|
|
|
|
|
|
|
|
function getIncPath() : array
|
|
|
|
|
{
|
|
|
|
|
if($this->inc_dirs)
|
|
|
|
|
return $this->inc_dirs;
|
|
|
|
|
return $this->src_dirs;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function bhl_proj_file() : string
|
|
|
|
|
{
|
|
|
|
|
return get('BHL_PROJ_FILE');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function bhl_proj(?string $proj_file = null)
|
|
|
|
|
function bhl_proj_load(string $proj_file) : BhlProj
|
|
|
|
|
{
|
|
|
|
|
global $GAME_ROOT;
|
|
|
|
|
$arr = json_decode(ensure_read($proj_file), true);
|
|
|
|
|
if(!$arr)
|
|
|
|
|
throw new Exception("Bad bhl project file: $proj_file");
|
|
|
|
|
|
|
|
|
|
static $projs = [];
|
|
|
|
|
$proj = new BhlProj();
|
|
|
|
|
foreach($arr as $k => $v)
|
|
|
|
|
$proj->{$k} = $v;
|
|
|
|
|
|
|
|
|
|
$proj_file ??= bhl_proj_file();
|
|
|
|
|
//NOTE: adding path to the file for further convenience
|
|
|
|
|
$proj->file_path = $proj_file;
|
|
|
|
|
|
|
|
|
|
if(!isset($projs[$proj_file]))
|
|
|
|
|
foreach($proj->inc_dirs as $k => $v)
|
|
|
|
|
$proj->inc_dirs[$k] = _bhl_make_abs_path($proj_file, $v);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
if(isset($proj->postproc_sources))
|
|
|
|
|
{
|
|
|
|
|
$proj = json_decode(ensure_read($proj_file));
|
|
|
|
|
if(!$proj)
|
|
|
|
|
throw new Exception("Bad bhl project file: $proj_file");
|
|
|
|
|
|
|
|
|
|
$proj->file_path = $proj_file; //NOTE: adding path to the file for convenience
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
|
|
$projs[$proj_file] = $proj;
|
|
|
|
|
foreach($proj->postproc_sources as $k => $v)
|
|
|
|
|
$proj->postproc_sources[$k] = _bhl_make_abs_path($proj_file, $v);
|
|
|
|
|
}
|
|
|
|
|
return $projs[$proj_file];
|
|
|
|
|
|
|
|
|
|
if(isset($proj->postproc_dll))
|
|
|
|
|
$proj->postproc_dll = _bhl_make_abs_path($proj_file, $proj->postproc_dll);
|
|
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
|
|
return $proj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _bhl_make_abs_path($proj_file, $path)
|
|
|
|
|
function bhl_proj_set_active(BhlProj $proj) : ?BhlProj
|
|
|
|
|
{
|
|
|
|
|
$prev = BhlProj::$active;
|
|
|
|
|
BhlProj::$active = $proj;
|
|
|
|
|
return $prev;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function bhl_proj() : BhlProj
|
|
|
|
|
{
|
|
|
|
|
if(BhlProj::$active == null)
|
|
|
|
|
{
|
|
|
|
|
$proj = bhl_proj_load(bhl_proj_file());
|
|
|
|
|
BhlProj::$active = $proj;
|
|
|
|
|
}
|
|
|
|
|
return BhlProj::$active;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _bhl_make_abs_path(string $proj_file, string $path) : string
|
|
|
|
|
{
|
|
|
|
|
if($path && $path[0] == '.')
|
|
|
|
|
return dirname($proj_file) . '/' . $path;
|
|
|
|
@ -71,53 +107,42 @@ function _bhl_make_abs_path($proj_file, $path)
|
|
|
|
|
return $path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function bhl_result_file(stdClass $bhl_proj = null)
|
|
|
|
|
function bhl_result_file() : string
|
|
|
|
|
{
|
|
|
|
|
$bhl_proj ??= bhl_proj();
|
|
|
|
|
return $bhl_proj->result_file;
|
|
|
|
|
return bhl_proj()->result_file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function bhl_dir()
|
|
|
|
|
function bhl_dir() : string
|
|
|
|
|
{
|
|
|
|
|
global $GAME_ROOT;
|
|
|
|
|
return "$GAME_ROOT/composer/vendor/bit/bhl";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function bhl_shell($cmd, &$ret_var, &$ret_out)
|
|
|
|
|
function bhl_shell(string $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);
|
|
|
|
|
}
|
|
|
|
|
shell_try(bhl_dir()."/bhl $cmd", $ret_var, $ret_out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function bhl_shell_ensure($cmd)
|
|
|
|
|
function bhl_shell_ensure(string $cmd)
|
|
|
|
|
{
|
|
|
|
|
bhl_shell($cmd, $ret_var, $ret_out);
|
|
|
|
|
if($ret_var !== 0)
|
|
|
|
|
throw new Exception("Error executing shell cmd: $ret_var");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function bhl_scan_files(stdClass $bhl_proj = null)
|
|
|
|
|
function bhl_scan_files() : array
|
|
|
|
|
{
|
|
|
|
|
$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(bool $debug = true, bool $force = false, bool $exit_on_err = true)
|
|
|
|
|
{
|
|
|
|
|
global $GAME_ROOT;
|
|
|
|
|
|
|
|
|
|
$bhl_proj ??= bhl_proj();
|
|
|
|
|
$bhl_proj = bhl_proj();
|
|
|
|
|
$result_file = $bhl_proj->result_file;
|
|
|
|
|
|
|
|
|
|
if($force || need_to_regen($result_file, bhl_scan_files($bhl_proj)))
|
|
|
|
|
if($force || need_to_regen($result_file, bhl_scan_files()))
|
|
|
|
|
{
|
|
|
|
|
bhl_shell("compile -p " . $bhl_proj->file_path . " " .
|
|
|
|
|
($debug ? " -d" : "") . " " .
|
|
|
|
@ -131,14 +156,12 @@ function bhl_run($debug = true, $force = false, $exit_on_err = true, stdClass $b
|
|
|
|
|
if(!$exit_on_err)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
echo "BHL BUNDLE: total " . kb_len(filesize($result_file)) . "\n";
|
|
|
|
|
else if($debug)
|
|
|
|
|
echo "BHL BUNDLE: total " . kb_len(filesize($result_file)) . ", CRC " . hexdec(hash_file('CRC32', $result_file, false)) . "\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@touch($result_file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function bhl_handle_error_result(array $ret_out, $err_file, $exit = true)
|
|
|
|
|
function bhl_handle_error_result(array $ret_out, string $err_file, bool $exit = true)
|
|
|
|
|
{
|
|
|
|
|
if(!is_file($err_file))
|
|
|
|
|
{
|
|
|
|
@ -173,19 +196,18 @@ function bhl_handle_error_result(array $ret_out, $err_file, $exit = true)
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function bhl_clean(stdClass $bhl_proj = null)
|
|
|
|
|
function bhl_clean()
|
|
|
|
|
{
|
|
|
|
|
bhl_clean_cache($bhl_proj);
|
|
|
|
|
bhl_shell_ensure("clean");
|
|
|
|
|
bhl_clean_cache();
|
|
|
|
|
bhl_shell("clean", $ret, $out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function bhl_clean_cache(stdClass $bhl_proj = null)
|
|
|
|
|
function bhl_clean_cache()
|
|
|
|
|
{
|
|
|
|
|
$bhl_proj ??= bhl_proj();
|
|
|
|
|
ensure_rm($bhl_proj->tmp_dir);
|
|
|
|
|
ensure_rm(bhl_proj()->tmp_dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function bhl_show_position($line, $row, array $lines)
|
|
|
|
|
function bhl_show_position(int $line, int $row, array $lines) : string
|
|
|
|
|
{
|
|
|
|
|
if($line > 0 && $line <= count($lines))
|
|
|
|
|
{
|
|
|
|
@ -205,7 +227,7 @@ function bhl_show_position($line, $row, array $lines)
|
|
|
|
|
return "??? @($line:$row)";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function bhl_line_row_to_pos($file, $line, $row)
|
|
|
|
|
function bhl_line_row_to_pos(string $file, int $line, int $row) : ?int
|
|
|
|
|
{
|
|
|
|
|
$pos = 0;
|
|
|
|
|
$lines = file($file);
|
|
|
|
@ -218,10 +240,10 @@ function bhl_line_row_to_pos($file, $line, $row)
|
|
|
|
|
return $pos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function bhl_map_module_to_file($module, stdClass $bhl_proj = null)
|
|
|
|
|
function bhl_map_module_to_file(string $module) : ?string
|
|
|
|
|
{
|
|
|
|
|
$bhl_proj ??= bhl_proj();
|
|
|
|
|
foreach($bhl_proj->src_dirs as $dir)
|
|
|
|
|
$bhl_proj = bhl_proj();
|
|
|
|
|
foreach($bhl_proj->getIncPath() as $dir)
|
|
|
|
|
{
|
|
|
|
|
$tmp = $dir.'/'.$module.'.bhl';
|
|
|
|
|
if(file_exists($tmp))
|
|
|
|
@ -230,16 +252,41 @@ function bhl_map_module_to_file($module, stdClass $bhl_proj = null)
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function bhl_validate_func_ref($module_file, $func, array $signature)
|
|
|
|
|
function bhl_validate_func_ref(string $module_file, string $func_full_name, array $signature)
|
|
|
|
|
{
|
|
|
|
|
if(sizeof($signature) == 0)
|
|
|
|
|
throw new Exception("Signature is invalid");
|
|
|
|
|
|
|
|
|
|
$name_items = explode('.', $func_full_name);
|
|
|
|
|
$func = array_pop($name_items);
|
|
|
|
|
$namespace = implode('.', $name_items);
|
|
|
|
|
|
|
|
|
|
$module_src = file_get_contents($module_file);
|
|
|
|
|
$module_src = _bhl_remove_comments($module_src);
|
|
|
|
|
|
|
|
|
|
if(!$module_src)
|
|
|
|
|
throw new Exception("Bad module file '{$module_file}'");
|
|
|
|
|
|
|
|
|
|
$ns_chunks = _bhl_split_by_namespaces($module_src);
|
|
|
|
|
|
|
|
|
|
$module_chunks = array();
|
|
|
|
|
if($namespace)
|
|
|
|
|
{
|
|
|
|
|
if(!isset($ns_chunks[$namespace]))
|
|
|
|
|
throw new Exception("No namespace '$namespace' found in '$module_file'");
|
|
|
|
|
|
|
|
|
|
foreach($ns_chunks[$namespace] as $ns_src)
|
|
|
|
|
$module_chunks[] = $ns_src;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(!isset($ns_chunks['']))
|
|
|
|
|
throw new Exception("No global namespace found in '$module_file'");
|
|
|
|
|
|
|
|
|
|
foreach($ns_chunks[''] as $ns_src)
|
|
|
|
|
$module_chunks[] = $ns_src;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$signature_pattern = '';
|
|
|
|
|
$signature_pattern .= '~func\s+';
|
|
|
|
|
if($signature[0] !== 'void')
|
|
|
|
@ -261,64 +308,58 @@ function bhl_validate_func_ref($module_file, $func, array $signature)
|
|
|
|
|
$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'");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
foreach($module_chunks as $module_chunk_src)
|
|
|
|
|
{
|
|
|
|
|
if(!$started && strpos($line, 'static readonly string[] VM_SRC = new string[] {') !== false)
|
|
|
|
|
if(preg_match($signature_pattern, $module_chunk_src))
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Exception("Func '$func_full_name(".implode(',', $signature).")' not found in '$module_file'");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
$started = true;
|
|
|
|
|
continue;
|
|
|
|
|
$ns = trim(substr(ltrim($chunk), 9));
|
|
|
|
|
$body = $chunks[$i+1];
|
|
|
|
|
if(!isset($nss[$ns]))
|
|
|
|
|
$nss[$ns] = array();
|
|
|
|
|
$nss[$ns][] = $body;
|
|
|
|
|
$i+=2;
|
|
|
|
|
}
|
|
|
|
|
else if($started && strpos($line, '};') !== false)
|
|
|
|
|
break;
|
|
|
|
|
else if($started)
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$mask = trim($line);
|
|
|
|
|
$mask = str_replace('$"{BHL_ROOT}/', '', $mask);
|
|
|
|
|
$mask = str_replace('",', '', $mask);
|
|
|
|
|
$srcs = array_merge($srcs, glob(bhl_dir() . '/' . $mask));
|
|
|
|
|
if(!isset($nss['']))
|
|
|
|
|
$nss[''] = array();
|
|
|
|
|
$nss[''][] = $chunk;
|
|
|
|
|
++$i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
return $nss;
|
|
|
|
|
}
|
|
|
|
|