2016-12-11 16:06:17 +00:00
|
|
|
/* Copyright Azareal 2016 - 2017 */
|
|
|
|
package main
|
2017-01-01 15:45:43 +00:00
|
|
|
import "log"
|
2016-12-11 16:06:17 +00:00
|
|
|
|
|
|
|
var plugins map[string]Plugin = make(map[string]Plugin)
|
|
|
|
var hooks map[string]func(interface{})interface{} = make(map[string]func(interface{})interface{})
|
2016-12-13 02:14:14 +00:00
|
|
|
var vhooks map[string]func(...interface{})interface{} = make(map[string]func(...interface{})interface{})
|
2016-12-11 16:06:17 +00:00
|
|
|
|
|
|
|
type Plugin struct
|
|
|
|
{
|
|
|
|
UName string
|
|
|
|
Name string
|
|
|
|
Author string
|
|
|
|
URL string
|
|
|
|
Settings string
|
|
|
|
Active bool
|
2016-12-16 10:37:42 +00:00
|
|
|
Tag string
|
2016-12-11 16:06:17 +00:00
|
|
|
Type string
|
|
|
|
Init func()
|
2016-12-16 10:37:42 +00:00
|
|
|
Activate func()error
|
2016-12-13 02:14:14 +00:00
|
|
|
Deactivate func()
|
2016-12-11 16:06:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-18 12:56:06 +00:00
|
|
|
/*func add_hook(name string, handler func(interface{})interface{}) {
|
2016-12-11 16:06:17 +00:00
|
|
|
hooks[name] = handler
|
2016-12-18 12:56:06 +00:00
|
|
|
}*/
|
|
|
|
|
2017-01-01 15:45:43 +00:00
|
|
|
func init_plugins() {
|
|
|
|
for name, body := range plugins {
|
|
|
|
log.Print("Added plugin " + name)
|
|
|
|
if body.Active {
|
|
|
|
log.Print("Initialised plugin " + name)
|
|
|
|
plugins[name].Init()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 12:56:06 +00:00
|
|
|
func add_hook(name string, handler interface{}) {
|
|
|
|
switch h := handler.(type) {
|
|
|
|
case func(interface{})interface{}:
|
|
|
|
hooks[name] = h
|
|
|
|
case func(...interface{}) interface{}:
|
|
|
|
vhooks[name] = h
|
|
|
|
default:
|
|
|
|
panic("I don't recognise this kind of handler!") // Should this be an error for the plugin instead of a panic()?
|
|
|
|
}
|
2016-12-11 16:06:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 02:30:32 +00:00
|
|
|
func remove_hook(name string/*, plugin string */) {
|
2016-12-13 02:14:14 +00:00
|
|
|
delete(hooks, name)
|
|
|
|
}
|
|
|
|
|
2016-12-11 16:06:17 +00:00
|
|
|
func run_hook(name string, data interface{}) interface{} {
|
|
|
|
return hooks[name](data)
|
2016-12-13 02:14:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 10:37:42 +00:00
|
|
|
func remove_vhook(name string) {
|
|
|
|
delete(vhooks, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func run_vhook(name string, data ...interface{}) interface{} {
|
|
|
|
return vhooks[name](data...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func run_vhook_noreturn(name string, data ...interface{}) {
|
|
|
|
_ = vhooks[name](data...)
|
2016-12-11 16:06:17 +00:00
|
|
|
}
|