meta_filters/filters.inc.php

166 lines
4.2 KiB
PHP
Raw Normal View History

2023-05-26 17:50:05 +03:00
<?php
namespace metagen_php;
use Exception;
function flt_str2num($val, $name, $data, $args)
2023-05-26 17:50:05 +03:00
{
$config_globals = $GLOBALS['CONFIG_GLOBALS'];
$config_extra = $GLOBALS['CONFIG_EXTRAS'];
if(!property_exists($config_extra, 'flt_str2num_entries'))
$config_extra->flt_str2num_entries = array();
2023-05-26 17:50:05 +03:00
if(is_string($val))
{
if(strlen($val) === 0)
throw new Exception("Bad value, string empty, crc28 can't be generated");
//special case
if(strpos($val, '@') !== 0)
throw new Exception("@ expected");
$name = substr($val, 1) . '.conf.js';
$path = \taskman\config_real_path($config_globals->base_dirs, $name);
2023-05-26 17:50:05 +03:00
if(\taskman\config_get_header($path, $proto_id, $alias))
{
if($alias !== $val)
throw new Exception("Config file header alias doesn't match: $alias vs $val");
$val = $proto_id;
}
else
throw new Exception("Config file header is not valid: $path");
}
if(!is_numeric($val))
throw new Exception("Bad value, not a number(" . serialize($val) . ")");
$config_extra->flt_str2num_entries[1*$val] = true;
2023-05-26 17:50:05 +03:00
return 1*$val;
}
function validate_flt_str2num(\taskman\ConfigFetchResult $fetch_result, \taskman\ConfigCacheEntry $entry)
{
$config_extra = $entry->extras;
if(!property_exists($config_extra, 'flt_str2num_entries'))
return;
foreach($config_extra->flt_str2num_entries as $conf_id => $_)
{
if($conf_id == 0)
continue;
$fetch_result->getById($conf_id);
}
}
function flt_range($val, $name, $data, $args)
2023-05-26 17:50:05 +03:00
{
if(count($args) != 2)
throw new Exception("Wrong usage of \"flt_range\". Use \"@flt_range:[0,1]\" where 0 - min, 1 - max");
if(!is_numeric($args[0]) || !is_numeric($args[1]))
throw new Exception("Wrong usage of \"flt_range\". Use \"@flt_range:[0,1]\" where 0 - min, 1 - max");
if(is_numeric($val))
{
if($val < $args[0] || $val > $args[1])
throw new Exception("Value ".$val.", not in range(" . $args[0] . "," . $args[1] . ")");
}
return $val;
}
function flt_time($val, $name, $data, $args)
2023-05-26 17:50:05 +03:00
{
$str = "";
if(is_string($val))
{
if(strlen($val) === 0)
throw new Exception("Bad value, string empty, crc28 can't be generated");
$reg = "/^([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})$/";
if(!preg_match($reg, $val, $matches))
throw new Exception("Bad value, pattern 00:00:00");
2023-08-16 14:14:10 +03:00
$h = intval($matches[1]);
$m = intval($matches[2]);
$s = intval($matches[3]);
2023-05-26 17:50:05 +03:00
return $h * 3600 + $m * 60 + $s;
}
return 0;
}
function flt_class($val, $name, $data, $args)
2023-05-26 17:50:05 +03:00
{
global $GAME_ROOT;
$config_extra = $GLOBALS['CONFIG_EXTRAS'];
if(!property_exists($config_extra, 'flt_class_entries'))
$config_extra->flt_class_entries = array();
$target_class = $args;
if(is_array($target_class))
$target_class = $target_class[0];
if(!class_exists($target_class))
throw new Exception($val.". Target class \"".$target_class."\" is not valid");
$config_extra->flt_class_entries[$val] = $target_class;
2023-05-26 17:50:05 +03:00
return $val;
}
function validate_flt_class(\taskman\ConfigFetchResult $fetch_result, \taskman\ConfigCacheEntry $entry)
{
$config_extra = $entry->extras;
if(!property_exists($config_extra, 'flt_class_entries'))
return;
$config_globals = $GLOBALS['CONFIG_GLOBALS'];
foreach($config_extra->flt_class_entries as $conf_ref => $target_class)
2023-05-26 17:50:05 +03:00
{
$conf_cache = null;
2023-05-26 17:50:05 +03:00
if(is_string($conf_ref))
{
if(empty($conf_ref))
continue;
2023-09-25 16:55:12 +03:00
$conf_path = $conf_ref;
if(strpos($conf_path, '@') === 0)
$conf_path = substr($conf_path, 1) . '.conf.js';
2024-09-26 18:38:23 +03:00
2025-02-27 18:43:18 +03:00
$conf_path = \taskman\normalize_path(\taskman\config_real_path($config_globals->base_dirs, $conf_path));
$conf_cache = $fetch_result->getByPath($conf_path);
}
else if(is_int($conf_ref))
{
if($conf_ref == 0)
continue;
$conf_cache = $fetch_result->getById($conf_ref);
}
else
throw new Exception("Invalid config ref: '$conf_ref'");
if($conf_cache == null)
throw new Exception("Config not found by ref: '$conf_ref'");
$conf_class = $conf_cache->class;
2024-09-26 18:38:23 +03:00
if($conf_class != $target_class && !is_subclass_of($conf_class, $target_class))
throw new Exception("Config class is '$conf_class'. Must be '$target_class' or its child");
}
2024-09-26 18:38:23 +03:00
}