First commit.

Oh my. It's been such a long time since I wrote hook and plugin and
general system wankery in Lua. Ah, the good ol' Garry's Mod days. I kind
of miss them. Or maybe not.
This commit is contained in:
q3k 2013-09-21 23:33:14 +02:00
commit d23fb2b07f
4 changed files with 78 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*swp

31
core/hook.lua Normal file
View file

@ -0,0 +1,31 @@
hook = {}
hook.Hooks = {}
function hook.Add(event_name, hook_name, callback)
if hook.Hooks[event_name] == nil then
hook.Hooks[event_name] = {}
end
hook.Hooks[event_name][hook_name] = callback
end
function hook.Call(event_name, ...)
local Args = {...}
if hook.Hooks[event_name] == nil then
return
end
for K, Function in pairs(hook.Hooks[event_name]) do
if type(Function) == 'function' then
local Return = Function(unpack(Args))
if Return == false then
return
end
end
end
end
function hook.Remove(event_name, hook_name)
if hook.Hooks[event_name] == nil then
return
end
hook.Hooks[event_name][hook_name] = nil
end

41
core/reactor.lua Normal file
View file

@ -0,0 +1,41 @@
local socket = require('socket')
reactor = {}
function reactor:Initialize(quantum)
self._read_sockets = {}
self._write_sockets = {}
self._quantum = quantum or 0.1
end
function reactor:Run()
local read = {}
for Socket, V in pairs(self._read_sockets) do
read[#read+1] = Socket
end
local write = {}
for Socket, V in pairs(self._write_sockets) do
read[#read+1] = Socket
end
local r, w, e = socket.select(read, write, self._quantum)
if e == nil then
-- we actually got something on our sockets
for Socket, Data in pairs(self._read_sockets) do
if r[Socket] ~= nil then
local Callback = Data[1]
local Args = Data[2]
Callback(unpack(Args))
hook.Call('SocketDataReceived', Socket)
end
end
for Socket, Data in pairs(self._write_sockets) do
if w[Socket] ~= nil then
local Callback = Data[1]
local Args = Data[2]
Callback(unpack(Args))
end
end
end
hook.Call('ReactorTick')
end

5
start.lua Normal file
View file

@ -0,0 +1,5 @@
require('core.hook')
require('core.reactor')
reactor:Initialize()