|
| 1 | +#!/usr/bin/env bash |
| 2 | +# ============================================================================= |
| 3 | +# bin/cfxlua — CfxLua Standalone Interpreter Wrapper |
| 4 | +# ============================================================================= |
| 5 | +# Install: copy or symlink this file into a directory on your PATH. |
| 6 | +# sudo ln -s "$(pwd)/bin/cfxlua" /usr/local/bin/cfxlua |
| 7 | +# |
| 8 | +# Usage: |
| 9 | +# cfxlua <script.lua> [arg1 arg2 ...] |
| 10 | +# cfxlua --version |
| 11 | +# cfxlua --help |
| 12 | +# |
| 13 | +# Environment variables: |
| 14 | +# CFXLUA_VM Override path to the cfxlua-vm binary (default: auto-detect) |
| 15 | +# CFXLUA_RUNTIME Override path to the runtime/ directory |
| 16 | +# CFXLUA_RESOURCE Override the resource name (default: script basename) |
| 17 | +# ============================================================================= |
| 18 | + |
| 19 | +set -euo pipefail |
| 20 | + |
| 21 | +# --------------------------------------------------------------------------- |
| 22 | +# Resolve the directory this script lives in, following symlinks. |
| 23 | +# This is the canonical way to locate co-installed files in POSIX sh. |
| 24 | +# --------------------------------------------------------------------------- |
| 25 | +_self_dir() { |
| 26 | + local source="${BASH_SOURCE[0]}" |
| 27 | + while [ -L "$source" ]; do |
| 28 | + local dir |
| 29 | + dir="$(cd -P "$(dirname "$source")" && pwd)" |
| 30 | + source="$(readlink "$source")" |
| 31 | + [[ "$source" != /* ]] && source="$dir/$source" |
| 32 | + done |
| 33 | + cd -P "$(dirname "$source")" && pwd |
| 34 | +} |
| 35 | + |
| 36 | +SCRIPT_DIR="$(_self_dir)" |
| 37 | +PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 38 | + |
| 39 | +# --------------------------------------------------------------------------- |
| 40 | +# Locate the cfxlua-vm binary. |
| 41 | +# Build system places it at: vm/build/lua (renamed via Makefile install target) |
| 42 | +# We check several locations in priority order. |
| 43 | +# --------------------------------------------------------------------------- |
| 44 | +if [ -n "${CFXLUA_VM:-}" ]; then |
| 45 | + VM_BIN="$CFXLUA_VM" |
| 46 | +elif [ -x "$PROJECT_DIR/vm/build/cfxlua-vm" ]; then |
| 47 | + VM_BIN="$PROJECT_DIR/vm/build/cfxlua-vm" |
| 48 | +elif [ -x "$PROJECT_DIR/vm/build/lua" ]; then |
| 49 | + VM_BIN="$PROJECT_DIR/vm/build/lua" |
| 50 | +elif command -v cfxlua-vm &>/dev/null; then |
| 51 | + VM_BIN="$(command -v cfxlua-vm)" |
| 52 | +else |
| 53 | + # Fall back to the system Lua 5.4 for development/testing without a built VM. |
| 54 | + # NOTE: Without LuaGLM, vector types and power-patches won't be available. |
| 55 | + if command -v lua5.4 &>/dev/null; then |
| 56 | + VM_BIN="$(command -v lua5.4)" |
| 57 | + echo "[cfxlua] WARNING: cfxlua-vm not found; using system lua5.4 (no LuaGLM extensions)" >&2 |
| 58 | + elif command -v lua &>/dev/null; then |
| 59 | + VM_BIN="$(command -v lua)" |
| 60 | + echo "[cfxlua] WARNING: cfxlua-vm not found; using system lua (no LuaGLM extensions)" >&2 |
| 61 | + else |
| 62 | + echo "[cfxlua] FATAL: no Lua interpreter found." >&2 |
| 63 | + echo " Build the VM: cd $PROJECT_DIR && make" >&2 |
| 64 | + echo " Or set: export CFXLUA_VM=/path/to/lua" >&2 |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | +fi |
| 68 | + |
| 69 | +# --------------------------------------------------------------------------- |
| 70 | +# Locate the runtime directory. |
| 71 | +# --------------------------------------------------------------------------- |
| 72 | +RUNTIME_DIR="${CFXLUA_RUNTIME:-$PROJECT_DIR/runtime}" |
| 73 | +BOOTSTRAP="$RUNTIME_DIR/bootstrap.lua" |
| 74 | + |
| 75 | +if [ ! -f "$BOOTSTRAP" ]; then |
| 76 | + echo "[cfxlua] FATAL: bootstrap.lua not found at '$BOOTSTRAP'" >&2 |
| 77 | + echo " Set CFXLUA_RUNTIME to the correct runtime directory." >&2 |
| 78 | + exit 1 |
| 79 | +fi |
| 80 | + |
| 81 | +# --------------------------------------------------------------------------- |
| 82 | +# Handle special flags. |
| 83 | +# --------------------------------------------------------------------------- |
| 84 | +if [ "${1:-}" = "--version" ] || [ "${1:-}" = "-v" ]; then |
| 85 | + case "${LC_ALL:-${LC_CTYPE:-${LANG:-}}}" in |
| 86 | + *[Uu][Tt][Ff]-8*|*[Uu][Tt][Ff]8*) |
| 87 | + printf "CfxLua 1.0.1 · © 2026 Polaris Naz\nLuaGLM 5.4 · Cfx.re" |
| 88 | + ;; |
| 89 | + *) |
| 90 | + printf "CfxLua 1.0.1 - (c) 2026 Polaris Naz\nLuaGLM 5.4 - Cfx.re" |
| 91 | + ;; |
| 92 | + esac |
| 93 | + exit 0 |
| 94 | +fi |
| 95 | + |
| 96 | +if [ "${1:-}" = "--help" ] || [ "${1:-}" = "-h" ] || [ $# -eq 0 ]; then |
| 97 | + case "${LC_ALL:-${LC_CTYPE:-${LANG:-}}}" in |
| 98 | + *[Uu][Tt][Ff]-8*|*[Uu][Tt][Ff]8*) |
| 99 | + printf "CfxLua 1.0.1 · © 2026 Polaris Naz\nLuaGLM 5.4 · Cfx.re\n\n" |
| 100 | + ;; |
| 101 | + *) |
| 102 | + printf "CfxLua 1.0.1 - (c) 2026 Polaris Naz\nLuaGLM 5.4 - Cfx.re\n\n" |
| 103 | + ;; |
| 104 | + esac |
| 105 | + cat <<EOF |
| 106 | +Usage: |
| 107 | + cfxlua <script.lua> [arg1 arg2 ...] |
| 108 | + cfxlua --version |
| 109 | + cfxlua -v |
| 110 | + cfxlua --help |
| 111 | + cfxlua -h |
| 112 | +
|
| 113 | +Arguments: |
| 114 | + <script.lua> Run a Lua script via runtime/bootstrap.lua |
| 115 | + [arg1 arg2 ...] Passed through to the script as arg[1..n] |
| 116 | + --version, -v Print interpreter and VM version banner |
| 117 | + --help, -h Print this help message |
| 118 | +
|
| 119 | +Environment: |
| 120 | + CFXLUA_VM Path to the cfxlua-vm binary (default: auto-detect) |
| 121 | + CFXLUA_RUNTIME Path to the runtime/ directory (default: $PROJECT_DIR/runtime) |
| 122 | + CFXLUA_RESOURCE Resource name override (default: script basename) |
| 123 | +
|
| 124 | +Examples: |
| 125 | + cfxlua my_resource/server.lua |
| 126 | + cfxlua tests/run_tests.lua |
| 127 | + CFXLUA_RESOURCE=my-resource cfxlua server.lua |
| 128 | +
|
| 129 | +Build the VM: |
| 130 | + cd $PROJECT_DIR && make |
| 131 | +EOF |
| 132 | + exit 0 |
| 133 | +fi |
| 134 | + |
| 135 | +# --------------------------------------------------------------------------- |
| 136 | +# Derive resource name from the script path (strip directory and extension). |
| 137 | +# Resources that use GetCurrentResourceName() get a sensible value. |
| 138 | +# --------------------------------------------------------------------------- |
| 139 | +SCRIPT_PATH="$1" |
| 140 | +SCRIPT_BASENAME="$(basename "$SCRIPT_PATH")" |
| 141 | +SCRIPT_RESOURCE="${SCRIPT_BASENAME%.*}" # strip extension |
| 142 | + |
| 143 | +export CFXLUA_RESOURCE_NAME="${CFXLUA_RESOURCE:-$SCRIPT_RESOURCE}" |
| 144 | +export CFXLUA_BOOTSTRAP_PATH="$PROJECT_DIR" |
| 145 | + |
| 146 | +# --------------------------------------------------------------------------- |
| 147 | +# Inject the bootstrap path into Lua via a synthetic global. |
| 148 | +# We prepend a tiny chunk that sets __cfx_bootstrapPath before bootstrap.lua |
| 149 | +# runs, using Lua's -e flag. |
| 150 | +# --------------------------------------------------------------------------- |
| 151 | +INJECT="__cfx_bootstrapPath = '$(echo "$PROJECT_DIR" | sed "s/'/\\\\'/g")'" |
| 152 | + |
| 153 | +# Execute. |
| 154 | +# The `--` separator ensures script args aren't consumed by the Lua interpreter. |
| 155 | +exec "$VM_BIN" \ |
| 156 | + -e "$INJECT" \ |
| 157 | + "$BOOTSTRAP" \ |
| 158 | + "$@" |
0 commit comments