···1+# Created by https://www.toptal.com/developers/gitignore/api/rust
2+# Edit at https://www.toptal.com/developers/gitignore?templates=rust
34+### Rust ###
5+# Generated by Cargo
6+# will have compiled files and executables
7+debug/
8+target/
910+# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
11+# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
12+Cargo.lock
01314+# These are backup files generated by rustfmt
15+**/*.rs.bk
00001617+# MSVC Windows builds of rustc generate these, which store debugging information
18+*.pdb
01920+# End of https://www.toptal.com/developers/gitignore/api/rust
00000000000000000000000
···2728## Usage
2930-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.
31-32-After loading everything, call the following code:
33-34-```lua
35-Hooks:emit("init")
36-```
37-38-This will call every function callback under the `init` event.
···2728## Usage
2930+**TODO**: Need to add this later for Rust and MessagePack00000000
-47
class.lua
···1--- class.lua
2--- Compatible with Lua 5.1 (not 5.0).
3-function class(base, init)
4- local c = {} -- a new class instance
5- if not init and type(base) == 'function' then
6- init = base
7- base = nil
8- elseif type(base) == 'table' then
9- -- our new class is a shallow copy of the base class!
10- for i,v in pairs(base) do
11- c[i] = v
12- end
13- c._base = base
14- end
15- -- the class will be the metatable for all its objects,
16- -- and they will look up their methods in it.
17- c.__index = c
18-19- -- expose a constructor which can be called by <classname>(<args>)
20- local mt = {}
21- mt.__call = function(class_tbl, ...)
22- local obj = {}
23- setmetatable(obj,c)
24- if init then
25- init(obj,...)
26- else
27- -- make sure that any stuff from the base class is initialized!
28- if base and base.init then
29- base.init(obj, ...)
30- end
31- end
32- return obj
33- end
34- c.init = init
35- c.is_a = function(self, klass)
36- local m = getmetatable(self)
37- while m do
38- if m == klass then return true end
39- m = m._base
40- end
41- return false
42- end
43- setmetatable(c, mt)
44- return c
45-end
46-47-return class
···1-hooks = class(function(self)
2- self.events = {}
3-end)
4-5-function hooks:on(event, callback)
6- if not self.events[event] then
7- self.events[event] = {}
8- end
9-10- table.insert(self.events[event], callback)
11-end
12-13-function hooks:once(event, callback)
14- local wrapper
15- wrapper = function(...)
16- callback(...)
17- self:off(event, wrapper)
18- end
19-20- self:on(event, wrapper)
21-end
22-23-function hooks:off(event, callback)
24- local list = self.events[event]
25- if not list then return end
26- for i, cb in ipairs(list) do
27- if cb == callback then
28- table.remove(list, i)
29- return
30- end
31- end
32-end
33-34-function hooks:emit(event, ...)
35- local list = self.events[event]
36- if not list then return end
37- for _, cb in ipairs(list) do
38- cb(...)
39- end
40-end
41-42-local Hooks = hooks()
43-return Hooks