Adding support for multiple config base directories

This commit is contained in:
Pavel Shevaev 2023-05-25 18:04:29 +03:00
parent 63e05c1c73
commit c38b78e9d3
1 changed files with 57 additions and 16 deletions

View File

@ -29,12 +29,49 @@ task('config_worker', function(array $args)
}
});
function config_base_dir()
function config_base_dirs()
{
if(is("CONFIGS_BASE_DIR"))
return get("CONFIGS_BASE_DIR");
else
return get("UNITY_ASSETS_DIR") . "/Configs";
static $dirs = null;
if($dirs === null)
{
if(is("CONFIGS_BASE_DIR"))
{
$dirs = get("CONFIGS_BASE_DIR");
if(is_string($dirs))
$dirs = array($dirs);
}
else
$dirs = array(get("UNITY_ASSETS_DIR") . "/Configs");
$dirs = array_map(function($d) { return normalize_path($d); }, $dirs);
}
return $dirs;
}
function config_map_base_dir($file, $normalized = false, $strict = true, $dirs = null)
{
if(!is_array($dirs))
$dirs = config_base_dirs();
if(!$normalized)
$file = normalize_path($file);
foreach($dirs as $dir)
if(strpos($file, $dir) === 0)
return $dir;
if($strict)
throw new Exception("File '$file' is not mapped to any base dir");
return null;
}
function config_real_path($rel_path, $strict = true)
{
$dirs = config_base_dirs();
foreach($dirs as $dir)
if(is_file($dir . '/' . $rel_path))
return $dir . '/' . $rel_path;
if($strict)
throw new Exception("No file for relative path '$rel_path'");
return null;
}
function config_build_dir()
@ -79,7 +116,7 @@ function config_get_bundle_ext_path()
function config_scan_files()
{
return scan_files_rec(array(config_base_dir()), array('conf.js'));
return scan_files_rec(config_base_dirs(), array('conf.js'));
}
function config_pack_bundle(array $cache_entries, $use_lz4 = false, $use_config_refs = false)
@ -231,16 +268,18 @@ function config_make_standalone_ext_bundle(array $configs, $file_path)
function config_bench_load($file)
{
list($proto_id, $_) = config_ensure_header(config_base_dir(), $file);
$base_dir = config_map_base_dir($file);
list($proto_id, $_) = config_ensure_header($base_dir, $file);
if(!$proto_id)
throw new Exception("Bad proto_id: {$proto_id}");
$t = microtime(true);
$parse_res = config_parse(config_base_dir(), $file);
$parse_res = config_parse(config_base_dirs(), $file);
if($parse_res->error !== 0)
throw new Exception("Error({$parse_res->error}) while loading JSON from {$file}:\n" . $parse_res->error_descr);
echo "PARSE: " . (microtime(true) - $t) . "\n";
$t = microtime(true);
$config = config_load_ex(config_base_dir(), $file, $parse_res->parsed_arr, $proto_id);
$config = config_load_ex($base_dir, $file, $parse_res->parsed_arr, $proto_id);
echo "LOAD: " . (microtime(true) - $t) . "\n";
}
@ -260,7 +299,7 @@ function config_load_includes_map($file = null)
{
$file = $file ? $file : config_get_includes_map_path();
$includes_map = array(array(), array());
$includes_map = array(array(), array());
if(is_file($file))
{
@ -433,7 +472,10 @@ function _config_invalidate_cache($file, $cache_file) : ConfigCacheEntry
{
$cache_payload_file = config_get_cache_payload_path($file);
list($proto_id, $_) = config_ensure_header(config_base_dir(), $file);
//TODO: pass it from above?
$base_dir = config_map_base_dir($file);
list($proto_id, $_) = config_ensure_header($base_dir, $file);
if(!$proto_id)
throw new Exception("Bad proto_id: {$proto_id}");
@ -441,13 +483,13 @@ function _config_invalidate_cache($file, $cache_file) : ConfigCacheEntry
$GLOBALS['CONFIG_EXTRAS'] = ConfigCacheEntryExtras::create();
$GLOBALS['CONFIG_BHL_FUNCS'] = array();
$pres = config_parse(config_base_dir(), $file);
$pres = config_parse(config_base_dirs(), $file);
if($pres->error !== 0)
throw new Exception("Error({$pres->error}) while loading JSON in {$file}:\n" . $pres->error_descr);
$includes = config_get_module_includes($pres->jsm_module);
$config = config_load_ex(config_base_dir(), $file, $pres->parsed_arr, $proto_id);
$config = config_load_ex($base_dir, $file, $pres->parsed_arr, $proto_id);
$payload_data = config_msgpack_pack($config->export());
$cache_entry = new ConfigCacheEntry();
@ -778,15 +820,14 @@ class ConfigParseResult
public $jsm_module;
}
function config_parse($conf_dir, $file) : ConfigParseResult
function config_parse(array $base_dirs, $file) : ConfigParseResult
{
$res = new ConfigParseResult();
$normalized_jzon = '';
try
{
$jsm = new \JSM($conf_dir, $file);
$jsm = new \JSM($base_dirs, $file);
list($normalized_jzon, $jsm_module) = $jsm->process();
$res->normalized_jzon = $normalized_jzon;
$res->jsm_module = $jsm_module;