2022-12-08 19:14:25 +03:00
|
|
|
<?php
|
2024-04-11 20:16:32 +03:00
|
|
|
require_once(__DIR__ . '/parser1.inc.php');
|
|
|
|
require_once(__DIR__ . '/parser2.inc.php');
|
2022-12-08 19:14:25 +03:00
|
|
|
|
2024-04-11 20:16:32 +03:00
|
|
|
define('MTG_PARSER_TYPE_1', "1.0");
|
|
|
|
define('MTG_PARSER_TYPE_2', "2.0a");
|
2022-12-08 19:14:25 +03:00
|
|
|
|
2024-04-11 20:16:32 +03:00
|
|
|
interface mtgIMetaInfoParser
|
|
|
|
{
|
|
|
|
function parse(mtgMetaInfo $meta, string $file);
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
2024-04-04 19:20:38 +03:00
|
|
|
class mtgMetaParsedModule implements mtgScope
|
2023-08-03 18:03:37 +03:00
|
|
|
{
|
2024-03-14 12:50:54 +03:00
|
|
|
public string $file;
|
2023-08-03 18:03:37 +03:00
|
|
|
public $units = array();
|
|
|
|
public $includes = array();
|
|
|
|
|
2024-03-14 12:50:54 +03:00
|
|
|
function __construct(string $file)
|
2023-08-03 18:03:37 +03:00
|
|
|
{
|
|
|
|
$this->file = $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addUnit(mtgMetaInfoUnit $unit)
|
|
|
|
{
|
2023-08-16 14:16:13 +03:00
|
|
|
$this->units[$unit->object->getMetaId()] = $unit;
|
2023-08-03 18:03:37 +03:00
|
|
|
}
|
|
|
|
|
2024-04-04 19:20:38 +03:00
|
|
|
function findSymbol($name)
|
|
|
|
{
|
|
|
|
$unit = $this->findUnit($name);
|
|
|
|
if(!$unit)
|
|
|
|
return null;
|
|
|
|
return $unit->object;
|
|
|
|
}
|
|
|
|
|
2023-08-03 18:03:37 +03:00
|
|
|
function findUnit($id)
|
|
|
|
{
|
|
|
|
if(isset($this->units[$id]))
|
|
|
|
return $this->units[$id];
|
|
|
|
|
|
|
|
foreach($this->includes as $include)
|
|
|
|
{
|
|
|
|
if(isset($include->units[$id]))
|
|
|
|
return $include->units[$id];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addInclude(mtgMetaParsedModule $include)
|
|
|
|
{
|
|
|
|
$this->includes[] = $include;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-11 20:16:32 +03:00
|
|
|
function mtg_parse_meta(array $meta_srcs, $valid_tokens = null, $inc_path = null, $version = null)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2022-12-09 12:37:55 +03:00
|
|
|
if($inc_path === null)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2022-12-09 12:37:55 +03:00
|
|
|
//let's autodetect include path
|
|
|
|
$inc_path = array();
|
|
|
|
foreach($meta_srcs as $src)
|
|
|
|
{
|
|
|
|
if(is_dir($src))
|
|
|
|
$inc_path[] = $src;
|
|
|
|
else if(is_file($src))
|
|
|
|
$inc_path[] = dirname($src);
|
|
|
|
}
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
2024-04-11 20:16:32 +03:00
|
|
|
$meta_parser = null;
|
|
|
|
|
|
|
|
if($version === null || $version === MTG_PARSER_TYPE_1)
|
|
|
|
$meta_parser = new mtgMetaInfoParser(
|
|
|
|
array(
|
|
|
|
'include_path' => $inc_path,
|
|
|
|
'valid_tokens' => $valid_tokens
|
|
|
|
)
|
|
|
|
);
|
|
|
|
else if($version === MTG_PARSER_TYPE_2)
|
|
|
|
$meta_parser = new mtgMetaInfoParser2(
|
|
|
|
array(
|
|
|
|
'include_path' => $inc_path,
|
|
|
|
'valid_tokens' => $valid_tokens
|
|
|
|
)
|
|
|
|
);
|
|
|
|
else
|
|
|
|
throw new Exception("Unknown meta parser version: $version");
|
|
|
|
|
2022-12-08 19:14:25 +03:00
|
|
|
$meta = new mtgMetaInfo();
|
2024-04-11 20:16:32 +03:00
|
|
|
|
2022-12-08 19:14:25 +03:00
|
|
|
foreach($meta_srcs as $src)
|
|
|
|
mtg_load_meta($meta, $meta_parser, $src);
|
2023-09-27 00:15:44 +03:00
|
|
|
|
|
|
|
$meta->validate();
|
|
|
|
|
|
|
|
mtgTypeRef::checkAllResolved();
|
|
|
|
|
2022-12-08 19:14:25 +03:00
|
|
|
return $meta;
|
|
|
|
}
|
|
|
|
|
2024-09-30 20:27:31 +03:00
|
|
|
function mtg_load_meta(
|
|
|
|
mtgMetaInfo $meta, mtgIMetaInfoParser $meta_parser,
|
|
|
|
string $dir_or_file, string $extension = '.meta'
|
|
|
|
)
|
2024-04-11 20:16:32 +03:00
|
|
|
{
|
2024-09-30 20:27:31 +03:00
|
|
|
$files = mtg_resolve_files($dir_or_file, $extension);
|
2024-04-11 20:16:32 +03:00
|
|
|
foreach($files as $file)
|
|
|
|
$meta_parser->parse($meta, $file);
|
|
|
|
}
|
|
|
|
|
2024-09-30 20:27:31 +03:00
|
|
|
function mtg_resolve_files(string $dir_or_file, string $extension = '.meta') : array
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
$files = array();
|
|
|
|
if(is_dir($dir_or_file))
|
2024-09-30 20:27:31 +03:00
|
|
|
$files = mtg_find_meta_files($dir_or_file, $extension);
|
2022-12-08 19:14:25 +03:00
|
|
|
else if(is_file($dir_or_file))
|
|
|
|
$files[] = $dir_or_file;
|
|
|
|
else
|
|
|
|
throw new Exception("Bad meta source '$dir_or_file'");
|
2024-04-11 20:16:32 +03:00
|
|
|
return $files;
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
2024-09-30 20:27:31 +03:00
|
|
|
function mtg_find_meta_files($dir, $extension = '.meta')
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
$items = scandir($dir);
|
|
|
|
if($items === false)
|
|
|
|
throw new Exception("Directory '$dir' is invalid");
|
|
|
|
|
|
|
|
$files = array();
|
|
|
|
foreach($items as $item)
|
|
|
|
{
|
|
|
|
if($item[0] == '.')
|
|
|
|
continue;
|
|
|
|
|
2024-09-30 20:27:31 +03:00
|
|
|
if(strpos($item, $extension) !== (strlen($item)-strlen($extension)))
|
2022-12-08 19:14:25 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
$file = $dir . '/' . $item;
|
|
|
|
if(is_file($file) && !is_dir($file))
|
|
|
|
$files[] = $file;
|
|
|
|
}
|
|
|
|
return $files;
|
|
|
|
}
|