Last active
October 4, 2025 19:38
-
-
Save Hyperboid/b9b1c2962ba3f23c49c31c05746fd603 to your computer and use it in GitHub Desktop.
Partially automate mass-conversion to Utils.hookScript
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
| -- function\(orig, self(.*)\) (.*) end\) | |
| -- function(orig, self$1)\n $2\nend) | |
| -- Utils\.hook\((\w*), "(\w*)", function\(orig, self(.*)\) end\) | |
| -- Utils.hook($1, "$2", function(orig, self$3)\nend) | |
| local START_TIME = love.timer.getTime() | |
| local function infloopcheck() | |
| assert((love.timer.getTime() - START_TIME) < 5, "5s time limit exceeded! Possible infinite loop?") | |
| end | |
| local lines = Utils.splitFast(love.filesystem.read("mods/magical-glass-redux/libraries/magical-glass-redux/scripts/stubid.lua"), "\n") | |
| local current_class | |
| ---@class StubidScript.MethodData | |
| ---@field name string | |
| ---@field paramaters string[] | |
| ---@field body string[] | |
| ---@type table<string, table<string, StubidScript.MethodData>> | |
| local hooks = {} | |
| local line_number = 0 | |
| local function consumeLine() | |
| line_number = line_number + 1 | |
| return lines[line_number] | |
| end | |
| while line_number < #lines do | |
| infloopcheck() | |
| local toplevelline = consumeLine() | |
| local is_hook, hookline = Utils.startsWith(toplevelline, "Utils.hook(") | |
| if is_hook then | |
| local splithookline = Utils.splitFast(hookline, ",") | |
| for i = 1, #splithookline do | |
| splithookline[i] = Utils.trim(splithookline[i]) | |
| end | |
| ---@type StubidScript.MethodData | |
| local hook_content = { | |
| name = splithookline[2]:sub(2,-2), | |
| body = {}, | |
| paramaters = {}, | |
| } | |
| for i = 3, #splithookline do | |
| local param = select(2, Utils.endsWith(splithookline[i], ")")) | |
| param = select(2, Utils.startsWith(param, "function(")) | |
| table.insert(hook_content.paramaters, param) | |
| end | |
| while true do | |
| infloopcheck() | |
| if toplevelline == "end)" then break end | |
| local line = consumeLine() | |
| if line == "end)" then break end | |
| if hook_content.paramaters[1] then | |
| line = line:gsub(hook_content.paramaters[1], "super."..hook_content.name) | |
| end | |
| table.insert(hook_content.body, line) | |
| end | |
| hooks[splithookline[1]] = hooks[splithookline[1]] or {} | |
| hooks[splithookline[1]][hook_content.name] = hook_content | |
| end | |
| end | |
| for classname, hookgroup in pairs(hooks) do | |
| local string = '---@class '..classname..' : '..classname..'\nlocal '..classname..', super = Utils.hookScript('..classname..')\n' | |
| local function write(str) string = string .. str end | |
| local function writeln(str) write(str.."\n") end | |
| writeln("") | |
| for _, hook in pairs(hookgroup) do | |
| local args = Utils.copy(hook.paramaters) | |
| table.remove(args, 1) | |
| table.remove(args, 1) | |
| writeln("function "..classname..":"..hook.name.."("..table.concat(args,", ")..")") | |
| for _, line in ipairs(hook.body) do | |
| writeln(line) | |
| end | |
| writeln("end") | |
| writeln("") | |
| end | |
| writeln("return "..classname) | |
| love.filesystem.write("mods/magical-glass-redux/libraries/magical-glass-redux/scripts/hooks/"..classname..".lua", string) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment