···11-# Created by https://www.toptal.com/developers/gitignore/api/lua
22-# Edit at https://www.toptal.com/developers/gitignore?templates=lua
11+# Created by https://www.toptal.com/developers/gitignore/api/rust
22+# Edit at https://www.toptal.com/developers/gitignore?templates=rust
3344-### Lua ###
55-# Compiled Lua sources
66-luac.out
44+### Rust ###
55+# Generated by Cargo
66+# will have compiled files and executables
77+debug/
88+target/
7988-# luarocks build files
99-*.src.rock
1010-*.zip
1111-*.tar.gz
1010+# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
1111+# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
1212+Cargo.lock
12131313-# Object files
1414-*.o
1515-*.os
1616-*.ko
1717-*.obj
1818-*.elf
1414+# These are backup files generated by rustfmt
1515+**/*.rs.bk
19162020-# Precompiled Headers
2121-*.gch
2222-*.pch
1717+# MSVC Windows builds of rustc generate these, which store debugging information
1818+*.pdb
23192424-# Libraries
2525-*.lib
2626-*.a
2727-*.la
2828-*.lo
2929-*.def
3030-*.exp
3131-3232-# Shared objects (inc. Windows DLLs)
3333-*.dll
3434-*.so
3535-*.so.*
3636-*.dylib
3737-3838-# Executables
3939-*.exe
4040-*.out
4141-*.app
4242-*.i*86
4343-*.x86_64
4444-*.hex
4545-4646-4747-# End of https://www.toptal.com/developers/gitignore/api/lua
2020+# End of https://www.toptal.com/developers/gitignore/api/rust
···27272828## Usage
29293030-You can, of course, just use this as a usual Lua program. The code assumes you load the class file, call it, and assign the resulting function returned to a global variable `class`. You can then load the `hooks` file, along with any other libraries you'd like (i.e. the different systems). Due to the nature of this framework, I will not be writing code to load a system's code, but it shouldn't be too difficult to do so.
3131-3232-After loading everything, call the following code:
3333-3434-```lua
3535-Hooks:emit("init")
3636-```
3737-3838-This will call every function callback under the `init` event.3030+**TODO**: Need to add this later for Rust and MessagePack
-47
class.lua
···11--- class.lua
22--- Compatible with Lua 5.1 (not 5.0).
33-function class(base, init)
44- local c = {} -- a new class instance
55- if not init and type(base) == 'function' then
66- init = base
77- base = nil
88- elseif type(base) == 'table' then
99- -- our new class is a shallow copy of the base class!
1010- for i,v in pairs(base) do
1111- c[i] = v
1212- end
1313- c._base = base
1414- end
1515- -- the class will be the metatable for all its objects,
1616- -- and they will look up their methods in it.
1717- c.__index = c
1818-1919- -- expose a constructor which can be called by <classname>(<args>)
2020- local mt = {}
2121- mt.__call = function(class_tbl, ...)
2222- local obj = {}
2323- setmetatable(obj,c)
2424- if init then
2525- init(obj,...)
2626- else
2727- -- make sure that any stuff from the base class is initialized!
2828- if base and base.init then
2929- base.init(obj, ...)
3030- end
3131- end
3232- return obj
3333- end
3434- c.init = init
3535- c.is_a = function(self, klass)
3636- local m = getmetatable(self)
3737- while m do
3838- if m == klass then return true end
3939- m = m._base
4040- end
4141- return false
4242- end
4343- setmetatable(c, mt)
4444- return c
4545-end
4646-4747-return class
-43
hooks.lua
···11-hooks = class(function(self)
22- self.events = {}
33-end)
44-55-function hooks:on(event, callback)
66- if not self.events[event] then
77- self.events[event] = {}
88- end
99-1010- table.insert(self.events[event], callback)
1111-end
1212-1313-function hooks:once(event, callback)
1414- local wrapper
1515- wrapper = function(...)
1616- callback(...)
1717- self:off(event, wrapper)
1818- end
1919-2020- self:on(event, wrapper)
2121-end
2222-2323-function hooks:off(event, callback)
2424- local list = self.events[event]
2525- if not list then return end
2626- for i, cb in ipairs(list) do
2727- if cb == callback then
2828- table.remove(list, i)
2929- return
3030- end
3131- end
3232-end
3333-3434-function hooks:emit(event, ...)
3535- local list = self.events[event]
3636- if not list then return end
3737- for _, cb in ipairs(list) do
3838- cb(...)
3939- end
4040-end
4141-4242-local Hooks = hooks()
4343-return Hooks