File size: 5,216 Bytes
d7c5875
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
-- Copyright (c) 2018 FYP <https://gitlab.com/THE-FYP>
-- Distributed under the terms of MIT license.

-- on_keypress(key1, key2, key3, ..., func)                   -- press key combination
-- on_keydown(key1, key2, ..., func)                          -- hold key combination
-- on_cheatcode(cheatcode, func)                              -- game cheat-code
-- on_sampcommand(command, [pattern], func(args, arg1, arg2)) -- samp chat command
-- on_sfcommand(command, [pattern], func(args, arg1, arg2))   -- sampfuncs's console command
-- on_command(command, [pattern], func(args, arg1, arg2))     -- samp and sampfuncs command
-- on_timer(delay, func, ...)                                 -- infinite loop with delay
-- on_timeout(delay, func, ...)                               -- run once when timer expired
-- bind:unbind()                                              -- destroy bind

local module = {
	_VERSION = '1.0.0'
}
local task_create, task_create_suspended = lua_thread.create, lua_thread.create_suspended

local function typecheck(...)
	local args = {...}
	for i = 1, #args, 2 do
		if args[i + 1] == '*' or args[i + 1] ~= type(args[i]) then
			local name = debug.getinfo(2).name
			error(("bad argument #%d to '%s' (%s expected, got %s)"):format((i + 1) / 2, name, args[i + 1], type(args[i])))
		end
	end
end

local function init_bind(bind, runner, ...)
	function bind:unbind()
		self.task:terminate()
	end
	local task = task_create_suspended(runner)
	bind.task = task
	task:run(bind, ...)
end

local function is_key_input_accessible()
    if not isSampfuncsLoaded() then return true end
    local result = not isSampfuncsConsoleActive() and not isPauseMenuActive()
    if isSampLoaded() and isSampAvailable() then
        result = result and not sampIsChatInputActive() and not sampIsDialogActive()
    end
    return result
end

local function is_keycombo_activated(keys, press)
	if not is_key_input_accessible() then
		return false
	end
	for i = 1, #keys do
		if (i == #keys and press and not wasKeyPressed(keys[i])) or not isKeyDown(keys[i]) then
			return false
		end
	end
	return true
end

local function keypress_bind(keys, func, press)
	local bind = {keys = keys}
	init_bind(bind, function(bind)
		while true do
			if is_keycombo_activated(bind.keys, press) then
				func()
			end
			wait(0)
		end
	end)
	return bind
end

local function command_bind(cmd, pat_or_fn, fn, register, unregister)
	local func, pattern
	if fn then
		typecheck(cmd, 'string', pat_or_fn, 'string', fn, 'function')
		func, pattern = fn, pat_or_fn
	else
		typecheck(cmd, 'string', pat_or_fn, 'function')
		func = pat_or_fn
	end
	local bind = {command = cmd, pattern = pattern}
	function bind:unbind()
		unregister(self.command)
	end
	register(cmd, function(params)
		local values = {}
		if bind.pattern then
			values = {string.match(params, bind.pattern)}
		else -- split string
			for v in string.gmatch(params, "%S+") do
			   table.insert(values, v)
			end
		end
		task_create(func, params, unpack(values))
	end)
	return bind
end

function module.on_keydown(...)
	local args = {...}
	local func = args[#args]
	args[#args] = nil
	return keypress_bind(args, func, false)
end

function module.on_keypress(...)
	local args = {...}
	local func = args[#args]
	args[#args] = nil
	return keypress_bind(args, func, true)
end

function module.on_cheatcode(cheat, func)
	typecheck(cheat, 'string', func, 'function')
	local bind = {cheatcode = cheat}
	init_bind(bind, function(bind)
		while true do
			if testCheat(bind.cheatcode) and is_key_input_accessible() then
				func()
			end
			wait(0)
		end
	end)
	return bind
end

function module.on_timeout(time, func, ...)
	typecheck(time, 'number', func, 'function')
	local bind = {}
	init_bind(bind, function(bind, ...)
		wait(time)
		func(...)
	end, ...)
	return bind
end

function module.on_timer(time, func, ...)
	typecheck(time, 'number', func, 'function')
	local bind = {}
	init_bind(bind, function(bind, ...)
		while true do
			wait(time)
			func(...)
		end
	end, ...)
	return bind
end

function module.on_sampcommand(cmd, pat_or_fn, fn)
	if not isSampLoaded() or not isSampfuncsLoaded() then
		return nil
	end
	return command_bind(cmd, pat_or_fn, fn, sampRegisterChatCommand, sampUnregisterChatCommand)
end

function module.on_sfcommand(cmd, pat_or_fn, fn)
	if not isSampfuncsLoaded() then
		return nil
	end
	return command_bind(cmd, pat_or_fn, fn, sampfuncsRegisterConsoleCommand, sampfuncsUnregisterConsoleCommand)
end

function module.on_command(cmd, pat_or_fn, fn)
	local b1 = module.on_sfcommand(cmd, pat_or_fn, fn)
	local b2 = module.on_sampcommand(cmd, pat_or_fn, fn)
	if not b1 and not b2 then
		return nil
	end
	local bind = {sfbind = b1, sampbind = b2}
	function bind:unbind()
		if self.sfbind then
			self.sfbind:unbind()
		end
		if self.sampbind then
			self.sampbind:unbind()
		end
	end
	setmetatable(bind, {
		__index = function(self, key)
			if self.sfbind then
				return self.sfbind[key]
			elseif self.sampbind then
				return self.sfbind[key]
			end
		end,
		__newindex = function(self, key, val)
			if self.sfbind then
				self.sfbind[key] = val
			end
			if self.sampbind then
				self.sampbind[key] = val
			end
		end,
	})
	return bind
end

return module