jsm_macros/macro.inc.php

182 lines
3.0 KiB
PHP
Raw Permalink Normal View History

2023-05-26 13:38:03 +03:00
<?php
/**
* @global
*/
function macro_DATE($proc, $spec)
{
$stamp = strtotime($spec);
if($stamp === false)
throw new Exception("Bad date: $spec");
return $stamp;
}
/**
* @global
*/
function macro_INTERVAL($proc, $spec)
{
$in = DateInterval::createFromDateString($spec);
if(!$in)
throw new Exception("Bad time interval: $spec");
$sec = ($in->y * 365 * 24 * 60 * 60) +
($in->m * 30 * 24 * 60 * 60) +
($in->d * 24 * 60 * 60) +
($in->h * 60 * 60) +
($in->i * 60) +
$in->s;
if($sec <= 0)
throw new Exception("Bad resulting interval in seconds '$sec' for spec '$spec'");
return $sec;
}
/**
* @global
*/
function macro_ENUM_OR($proc)
{
global $GAME_ROOT;
$args = func_get_args();
array_shift($args);
$class = array_shift($args);
$opts = 0;
for($i=0;$i<sizeof($args);++$i)
{
$opts |= constant("$class::" . $args[$i]);
}
return $opts;
}
/**
* @global
*/
function macro_PRINTF($proc)
{
$args = func_get_args();
array_shift($args);
return '"'.call_user_func_array("sprintf", $args).'"';
}
/**
* @global
*/
function macro_FMT($proc)
{
$args = func_get_args();
array_shift($args);
$res = array();
foreach($args as $arg)
$res[] = $arg;
if(preg_match_all('~\{(\d+)[^\}]*\}~', $res[0], $matches))
{
$indices = $matches[1];
if(count($indices) != (count($res)-1))
throw new Exception("Too few args for: '{$res[0]}'");
sort($indices);
$indices = array_unique($indices);
$prev = -1;
foreach($indices as $idx)
{
$idx = 1*$idx;
if(($idx - $prev) > 1)
throw new Exception("Format indices gap: '{$res[0]}'");
$prev = $idx;
}
}
return $res;
}
/**
* @raw_args
*/
function macro_Q($proc, $value)
{
if(strpos(ltrim($value), '[') === 0)
return $value;
else
return "\"$value\"";
}
/**
* @global
*/
function macro_SELF_CONF_ID($jsm)
{
$file = $jsm->getRootFile();
$proto_id = 0;
$alias = "";
if(!\taskman\config_get_header($file, $proto_id, $alias))
2023-06-02 15:33:30 +03:00
throw new Exception("No valid header for config: $file");
2023-05-26 13:38:03 +03:00
return $proto_id;
}
/**
* @global
*/
function macro_SELF_SHORT_NAME($jsm)
{
$file = $jsm->getRootFile();
return str_replace(".conf.js", "", basename($file));
}
/**
* @global @raw_args
*/
function macro_BOOL($jsm, $expr)
{
$res = jsm_eval_string_value($jsm, $expr);
return $res == 0 ? "false" : "true";
}
/**
* @global
*/
function macro_IS_DEV($jsm)
{
return \taskman\getor("GAME_IS_DEV", 0);
2023-05-26 13:38:03 +03:00
}
2023-06-20 19:32:41 +03:00
/**
* @global
*/
function macro_PROP($jsm, $expr)
{
return \taskman\get($expr);
}
2023-05-26 13:38:03 +03:00
/**
* @global
*/
function macro_VCLASS($proc, $class)
{
return '"vclass__" : ' . constant("$class::CLASS_ID");
}
/**
* @global
*/
function macro_ITEM($proc, $proto_id, $amount)
{
return array('proto_id' => $proto_id, 'amount' => $amount);
}
/**
* @global @raw_args
*/
function macro_UNWRAP($jsm, $content)
{
$content = trim($content);
$content = ltrim($content, '{');
$content = rtrim($content, '}');
return $content;
}