-- This Script is Part of the Prometheus Obfuscator by levno-710 -- -- cli.lua -- -- This Script contains the Code for the Prometheus CLI. -- Configure package.path for requiring Prometheus. local function script_path() local str = debug.getinfo(2, "S").source:sub(2) return str:match("(.*[/%\\])") end package.path = script_path() .. "?.lua;" .. package.path local INSTALL_SCRIPT_URL = "https://raw.githubusercontent.com/prometheus-lua/Prometheus/master/install.sh" local function run_shell(command) local ok = os.execute(command) if type(ok) == "number" then return ok == 0 end if type(ok) == "boolean" then return ok end return false end local function get_version() local version = os.getenv("PROMETHEUS_LUA_VERSION") if version and version ~= "" then return version end return "dev" end local function print_help() print("Prometheus Lua CLI") print("Usage: prometheus-lua [command] [options] ") print("") print("Commands:") print(" update Install latest release via installer script") print(" --version, -v Print CLI version") print("") print("Obfuscation options:") print(" --preset, --p Use preset (e.g. Minify, Medium, High)") print(" --config, --c Use custom config Lua file") print(" --out, --o Set output path") print(" --Lua51 Force Lua 5.1 target") print(" --LuaU Force LuaU target") print(" --pretty Pretty print output") print(" --nocolors Disable colored logs") print(" --saveerrors Persist parser errors to file") end local function run_update() local command if run_shell("command -v curl >/dev/null 2>&1") then command = string.format("curl -fsSL '%s' | sh", INSTALL_SCRIPT_URL) elseif run_shell("command -v wget >/dev/null 2>&1") then command = string.format("wget -qO- '%s' | sh", INSTALL_SCRIPT_URL) else io.stderr:write("Neither curl nor wget was found. Please install one of them and retry.\n") os.exit(1) end print("Updating prometheus-lua using official installer") if not run_shell(command) then io.stderr:write("Update failed\n") os.exit(1) end print("Update completed") end if arg[1] == "update" then run_update() os.exit(0) end if arg[1] == "--version" or arg[1] == "-v" then print(get_version()) os.exit(0) end if arg[1] == "--help" or arg[1] == "-h" or arg[1] == "help" then print_help() os.exit(0) end ---@diagnostic disable-next-line: different-requires local Prometheus = require("prometheus") Prometheus.Logger.logLevel = Prometheus.Logger.LogLevel.Info Prometheus.Logger.errorCallback = function(...) local args = { ... } local message = table.concat(args, " ") io.stderr:write(Prometheus.colors(Prometheus.Config.NameUpper .. ": " .. message, "red") .. "\n") os.exit(1) end -- Check if the file exists local function file_exists(file) local f = io.open(file, "rb") if f then f:close() end return f ~= nil end string.split = function(str, sep) local fields = {} local pattern = string.format("([^%s]+)", sep) str:gsub(pattern, function(c) fields[#fields + 1] = c end) return fields end -- get all lines from a file, returns an empty -- list/table if the file does not exist local function lines_from(file) if not file_exists(file) then return {} end local lines = {} for line in io.lines(file) do lines[#lines + 1] = line end return lines end local function load_chunk(content, chunkName, environment) if type(loadstring) == "function" then local func, err = loadstring(content, chunkName) if not func then return nil, err end if environment and type(setfenv) == "function" then setfenv(func, environment) elseif environment and type(load) == "function" then return load(content, chunkName, "t", environment) end return func end if type(load) ~= "function" then return nil, "No load function available" end return load(content, chunkName, "t", environment) end local function run_cli() -- CLI local config, sourceFile, outFile, luaVersion, prettyPrint Prometheus.colors.enabled = true -- Parse Arguments local i = 1 while i <= #arg do local curr = arg[i] if curr:sub(1, 2) == "--" then if curr == "--preset" or curr == "--p" then if config then Prometheus.Logger:warn("The config was set multiple times") end i = i + 1 local preset = Prometheus.Presets[arg[i]] if not preset then Prometheus.Logger:error(string.format('A Preset with the name "%s" was not found!', tostring(arg[i]))) end config = preset elseif curr == "--config" or curr == "--c" then i = i + 1 local filename = tostring(arg[i]) if not file_exists(filename) then Prometheus.Logger:error(string.format('The config file "%s" was not found!', filename)) end local content = table.concat(lines_from(filename), "\n") -- Load Config from File local func, err = load_chunk(content, "@" .. filename, {}) if not func then Prometheus.Logger:error(string.format('Failed to parse config file "%s": %s', filename, tostring(err))) end config = func() elseif curr == "--out" or curr == "--o" then i = i + 1 if outFile then Prometheus.Logger:warn("The output file was specified multiple times!") end outFile = arg[i] elseif curr == "--nocolors" then Prometheus.colors.enabled = false elseif curr == "--Lua51" then luaVersion = "Lua51" elseif curr == "--LuaU" then luaVersion = "LuaU" elseif curr == "--pretty" then prettyPrint = true elseif curr == "--saveerrors" then -- Override error callback Prometheus.Logger.errorCallback = function(...) local args = { ... } local message = table.concat(args, " ") io.stderr:write(Prometheus.colors(Prometheus.Config.NameUpper .. ": " .. message, "red") .. "\n") local fileName = sourceFile:sub(-4) == ".lua" and sourceFile:sub(0, -5) .. ".error.txt" or sourceFile .. ".error.txt" local handle = io.open(fileName, "w") handle:write(message) handle:close() os.exit(1) end else Prometheus.Logger:warn(string.format('The option "%s" is not valid and therefore ignored', curr)) end else if sourceFile then Prometheus.Logger:error(string.format('Unexpected argument "%s"', arg[i])) end sourceFile = tostring(arg[i]) end i = i + 1 end if not sourceFile then Prometheus.Logger:error("No input file was specified!") end if not config then Prometheus.Logger:warn("No config was specified, falling back to Minify preset") config = Prometheus.Presets.Minify end -- Add Option to override Lua Version config.LuaVersion = luaVersion or config.LuaVersion config.PrettyPrint = prettyPrint ~= nil and prettyPrint or config.PrettyPrint if not file_exists(sourceFile) then Prometheus.Logger:error(string.format('The File "%s" was not found!', sourceFile)) end if not outFile then if sourceFile:sub(-4) == ".lua" then outFile = sourceFile:sub(0, -5) .. ".obfuscated.lua" else outFile = sourceFile .. ".obfuscated.lua" end end local source = table.concat(lines_from(sourceFile), "\n") local pipeline = Prometheus.Pipeline:fromConfig(config) local out = pipeline:apply(source, sourceFile) Prometheus.Logger:info(string.format('Writing output to "%s"', outFile)) -- Write Output local handle = io.open(outFile, "w") handle:write(out) handle:close() end local ok, err = xpcall(run_cli, function(e) return tostring(e) end) if not ok then local message = tostring(err):gsub("^.-:%d+:%s*", "") io.stderr:write(Prometheus.colors(Prometheus.Config.NameUpper .. ": " .. message, "red") .. "\n") os.exit(1) end