[manifest]
version = "1.0.0"
dump_lua = true
priority = -5

# Check all registered keybinds
# inserted inside Controller:key_press_update

[[patches]]
[patches.pattern]
target = 'engine/controller.lua'
pattern = "if not _RELEASE_MODE then"
position = "before"
payload = '''
for _, keybind in pairs(SMODS.Keybinds) do
    if keybind.action and keybind.key_pressed == key and keybind.event == 'pressed' then
        local execute = true
        for _, other_key in pairs(keybind.held_keys) do
            if not self.held_keys[other_key] then
                execute = false
                break
            end
        end
        if execute then
            keybind:action()
        end
    end
end
'''
match_indent = true

# Controller:key_release_update
[[patches]]
[patches.pattern]
target = 'engine/controller.lua'
pattern = "function Controller:key_release_update(key, dt)"
position = "after"
payload = '''
for _, keybind in pairs(SMODS.Keybinds) do
    if keybind.action and keybind.key_pressed == key and keybind.event == 'released' then
        local execute = true
        for _, other_key in pairs(keybind.held_keys) do
            if not self.held_keys[other_key] then
                execute = false
                break
            end
        end
        if execute then
            keybind:action()
        end
    end
end
'''
match_indent = true

[[patches]]
[patches.regex]
target = 'engine/controller.lua'
pattern = 'if key == "r"'
position = 'before'
line_prepend = '$indent'
payload = '''
for _, keybind in pairs(SMODS.Keybinds) do
    if keybind.key_pressed == key and keybind.event == 'held' and keybind.held_duration then
        if self.held_key_times[key] > keybind.held_duration then
            local execute = true
            for _, other_key in pairs(keybind.held_keys) do
                if not self.held_keys[other_key] then
                    execute = false
                    break
                end
            end
            if execute then
                keybind:action()
                self.held_key_times[key] = nil
            end
        else
            self.held_key_times[key] = self.held_key_times[key] + dt
        end
    end
end
'''
