From 23383a8a635e6a71fe0c972d942f40ad1af7f776 Mon Sep 17 00:00:00 2001 From: moderndevslulw Date: Tue, 8 Jul 2025 14:05:24 +0500 Subject: fix: build metadata for scripts only when there are changes --- lib/utils.php | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'lib/utils.php') diff --git a/lib/utils.php b/lib/utils.php index 6de3dfb..639eed4 100644 --- a/lib/utils.php +++ b/lib/utils.php @@ -1,4 +1,7 @@ 1 ? "s" : ""); } +} + +function parse_lua_script($value) +{ + if ($value instanceof \Raudius\Luar\Interpreter\LuarObject\Literal) { + return $value->getValue(); + } else if ($value instanceof \Raudius\Luar\Interpreter\LuarObject\Table) { + return parse_lua_script($value->getValue()); + } + + if (is_array($value)) { + $out = []; + foreach ($value as $k => $v) { + if ($k == 'handle') { + continue; + } + $out[$k] = parse_lua_script($v); + } + return $out; + } + + return $value; +} + +function build_script_metadata(string $folder_path) +{ + $file_path = $_SERVER["DOCUMENT_ROOT"] . "/../scriptmetadata.json"; + $should_create = is_dir($folder_path); + $files = glob("$folder_path/*.lua"); + + if (file_exists($file_path)) { + $file_last_change = filemtime($file_path); + $script_last_change = 0; + foreach ($files as $file) { + $l = filemtime($file); + if ($l > $script_last_change) { + $l = $script_last_change; + } + } + $should_create = $script_last_change > $file_last_change; + } + + if ($should_create) { + $scripts = []; + + $luar = new Luar(); + $parsedown = new Parsedown(); + + foreach ($files as $file) { + $contents = file_get_contents($file); + + $result = $luar->eval($contents); + $script = parse_lua_script($result); + if (!isset($script["description"])) { + $script["description"] = "_No description provided._"; + } + $script['description_text'] = str_replace(PHP_EOL, ' ', $script['description']); + $script['description_formatted'] = $parsedown->text($script['description']); + + array_push($scripts, $script); + } + + file_put_contents($file_path, json_encode($scripts, JSON_UNESCAPED_SLASHES)); + } + + return true; } \ No newline at end of file -- cgit v1.2.3