Created
January 31, 2026 15:23
-
-
Save gphg/8a303461a4f87e21a2f71218593c3b2a to your computer and use it in GitHub Desktop.
Lua custom loader utilizing , because why not?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Register the GZIP loader for .lua.gz files | |
| local function load_gzip_lua(module_name) | |
| -- Convert "a.b.c" to "a/b/c.lua.gz" | |
| local path = module_name:gsub("%.", "/") .. ".lua.gz" | |
| if not love.filesystem.getInfo(path) then | |
| return "\n\tno file '" .. path .. "' (gzip loader)" | |
| end | |
| local data, read_err = love.filesystem.read("data", path) | |
| if not data then | |
| error("Could not read .lua.gz file: " .. read_err) | |
| end | |
| -- Decompress using GZIP format | |
| local decompressed_str = love.data.decompress("string", "gzip", data) | |
| -- Load the chunk. We pass module_name so errors show the correct origin. | |
| local chunk, load_err = loadstring(decompressed_str, module_name) | |
| if not chunk then | |
| error("Error compiling chunk from " .. path .. ":\n" .. load_err) | |
| end | |
| return chunk | |
| end | |
| -- Insert into the searchers table (position 2) | |
| table.insert(package.loaders, 2, load_gzip_lua) | |
| -- --- TEST INFRASTRUCTURE --- | |
| local test_results = {} | |
| local logs = {} | |
| local function log(msg) | |
| print(msg) | |
| table.insert(logs, msg) | |
| end | |
| local function assert_test(name, condition, err) | |
| local status = condition and "PASS" or "FAIL" | |
| table.insert(test_results, { name = name, status = status, err = err }) | |
| log(string.format("[%s] %s %s", status, name, err or "")) | |
| end | |
| -- Helper to safely write compressed modules, even in subdirectories | |
| local function create_compressed_module(mod_path, content) | |
| -- mod_path is dot notation, e.g., "entities.player" | |
| local filepath = mod_path:gsub("%.", "/") .. ".lua.gz" | |
| -- Ensure the directory exists | |
| local dir = filepath:match("(.-)([^/]+)$") | |
| if dir ~= "" then | |
| love.filesystem.createDirectory(dir) | |
| end | |
| local compressed = love.data.compress("string", "gzip", content) | |
| local success, msg = love.filesystem.write(filepath, compressed) | |
| if not success then error("Failed to write test file: " .. msg) end | |
| end | |
| function love.load() | |
| log("--- Initializing Tests ---") | |
| -- Test 1: Root level module | |
| create_compressed_module("root_mod", "return 'hello from root'") | |
| local s1, r1 = pcall(require, "root_mod") | |
| assert_test("Root Module Load", s1 and r1 == "hello from root", r1) | |
| -- Test 2: Deeply nested module (Testing directory logic) | |
| -- This tests if love.filesystem.createDirectory worked and if gsub handled dots | |
| create_compressed_module("scripts.engine.core", "return { version = '1.0.0' }") | |
| local s2, r2 = pcall(require, "scripts.engine.core") | |
| assert_test("Nested Module Load", s2 and type(r2) == "table" and r2.version == "1.0.0", r2) | |
| -- Test 3: Standard Lua fallback | |
| -- Ensure we didn't break the default loader | |
| love.filesystem.write("normal.lua", "return 'uncompressed'") | |
| local s3, r3 = pcall(require, "normal") | |
| assert_test("Standard .lua Fallback", s3 and r3 == "uncompressed", r3) | |
| -- Test 4: Missing module error reporting | |
| -- We want to see if our custom error string appears in the trace | |
| local s4, r4 = pcall(require, "non_existent_module") | |
| local found_error_msg = tostring(r4):find("gzip loader") ~= nil | |
| assert_test("Error String Reporting", not s4 and found_error_msg, "Custom error string not found in stack trace") | |
| log("--- Tests Completed ---") | |
| end | |
| function love.draw() | |
| love.graphics.setBackgroundColor(0.1, 0.1, 0.1) | |
| local y = 20 | |
| love.graphics.setColor(1, 1, 1) | |
| love.graphics.print("LÖVE .lua.gz Loader Results:", 20, y) | |
| y = y + 40 | |
| for _, res in ipairs(test_results) do | |
| if res.status == "PASS" then | |
| love.graphics.setColor(0.4, 1, 0.4) | |
| else | |
| love.graphics.setColor(1, 0.4, 0.4) | |
| end | |
| love.graphics.print(string.format("%s: %s", res.status, res.name), 20, y) | |
| y = y + 20 | |
| if res.status == "FAIL" then | |
| love.graphics.setColor(0.8, 0.8, 0.8) | |
| love.graphics.print(" -> " .. tostring(res.err), 20, y) | |
| y = y + 20 | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment