inc_dirs) return $this->inc_dirs; return $this->src_dirs; } } function bhl_proj_file() : string { 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"); $proj = new BhlProj(); foreach($arr as $k => $v) $proj->{$k} = $v; //NOTE: adding path to the file for further convenience $proj->file_path = $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)) { 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); return $proj; } 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; else return $path; } function bhl_result_file() : string { return bhl_proj()->result_file; } function bhl_dir() : string { global $GAME_ROOT; return "$GAME_ROOT/composer/vendor/bit/bhl"; } function bhl_shell(string $cmd, &$ret_var, &$ret_out) { shell_try(bhl_dir()."/bhl $cmd", $ret_var, $ret_out); } 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() : array { return scan_files_rec(bhl_proj()->src_dirs, array('.bhl')); } function bhl_run(bool $debug = true, bool $force = false, bool $exit_on_err = true) { global $GAME_ROOT; $bhl_proj = bhl_proj(); $result_file = $bhl_proj->result_file; if($force || need_to_regen($result_file, bhl_scan_files())) { bhl_shell("compile -p " . $bhl_proj->file_path . " " . ($debug ? " -d" : "") . " " . ($force || !getor("BHL_USE_CACHE", true) ? " -C" : ""), $ret_var, $ret_out ); if($ret_var != 0) { bhl_handle_error_result($ret_out, $bhl_proj->error_file, $exit_on_err); if(!$exit_on_err) return false; } else 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, string $err_file, bool $exit = true) { if(!is_file($err_file)) { stderr("Something went wrong: " . implode("\n", $ret_out)); if($exit) exit(1); } $history = array(); $err_lines = file($err_file); $err_count = 0; foreach($err_lines as $err) { $jerr = json_decode($err); if(!$jerr) { stderr("Something went wrong, bhl error is in invalid format: $err\n"); continue; } $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; ++$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) exit(1); } function bhl_clean() { bhl_clean_cache(); bhl_shell_ensure("clean"); } function bhl_clean_cache() { ensure_rm(bhl_proj()->tmp_dir); } function bhl_show_position(int $line, int $row, array $lines) : string { if($line > 0 && $line <= count($lines)) { //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; } else return "??? @($line:$row)"; } function bhl_line_row_to_pos(string $file, int $line, int $row) : ?int { $pos = 0; $lines = file($file); //something went wrong if($line <= 0 || $line > count($lines)) return null; for($i=0;$i<($line-1);++$i) $pos += strlen($lines[$i]); $pos += $row; return $pos; } function bhl_map_module_to_file(string $module) : ?string { $bhl_proj = bhl_proj(); foreach($bhl_proj->getIncPath() as $dir) { $tmp = $dir.'/'.$module.'.bhl'; if(file_exists($tmp)) return $tmp; } return null; } 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}'"); $module_chunks = array(); if($namespace) { $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) throw new Exception("Namespace '$namespace' for func '$func_full_name' not found in '$module_file'"); } else $module_chunks[] = $module_src; $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 .= '~'; foreach($module_chunks as $module_chunk_src) { 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*(?