2020-05-23 23:16:17 +00:00
|
|
|
package hookgen
|
|
|
|
|
|
|
|
import (
|
2021-03-24 11:45:18 +00:00
|
|
|
"bytes"
|
2020-05-23 23:16:17 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HookVars struct {
|
|
|
|
Imports []string
|
2021-03-24 11:45:18 +00:00
|
|
|
Hooks []Hook
|
2020-05-23 23:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Hook struct {
|
2021-03-24 11:45:18 +00:00
|
|
|
Name string
|
|
|
|
Params string
|
|
|
|
Params2 string
|
|
|
|
Ret string
|
|
|
|
Type string
|
|
|
|
Any bool
|
|
|
|
MultiHook bool
|
|
|
|
Skip bool
|
2020-05-26 07:53:56 +00:00
|
|
|
DefaultRet string
|
2021-03-24 11:45:18 +00:00
|
|
|
Pure string
|
2020-05-23 23:16:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 07:53:56 +00:00
|
|
|
func AddHooks(add func(name, params, ret, htype string, multiHook, skip bool, defaultRet, pure string)) {
|
2020-05-23 23:16:17 +00:00
|
|
|
vhookskip := func(name, params string) {
|
2021-03-24 11:45:18 +00:00
|
|
|
add(name, params, "(bool,RouteError)", "VhookSkippable_", false, true, "false,nil", "")
|
2020-05-23 23:16:17 +00:00
|
|
|
}
|
2021-05-12 02:04:55 +00:00
|
|
|
vhookskip("simple_forum_check_pre_perms", "w http.ResponseWriter,r *http.Request,u *User,fid *int,h *HeaderLite")
|
2021-03-24 11:45:18 +00:00
|
|
|
vhookskip("forum_check_pre_perms", "w http.ResponseWriter,r *http.Request,u *User,fid *int,h *Header")
|
|
|
|
vhookskip("router_after_filters", "w http.ResponseWriter,r *http.Request,prefix string")
|
|
|
|
vhookskip("router_pre_route", "w http.ResponseWriter,r *http.Request,u *User,prefix string")
|
|
|
|
vhookskip("route_forum_list_start", "w http.ResponseWriter,r *http.Request,u *User,h *Header")
|
|
|
|
vhookskip("route_topic_list_start", "w http.ResponseWriter,r *http.Request,u *User,h *Header")
|
|
|
|
vhookskip("route_attach_start", "w http.ResponseWriter,r *http.Request,u *User,fname string")
|
|
|
|
vhookskip("route_attach_post_get", "w http.ResponseWriter,r *http.Request,u *User,a *Attachment")
|
2021-05-12 02:04:55 +00:00
|
|
|
|
2020-05-23 23:16:17 +00:00
|
|
|
vhooknoret := func(name, params string) {
|
2021-03-24 11:45:18 +00:00
|
|
|
add(name, params, "", "Vhooks", false, false, "false,nil", "")
|
2020-05-23 23:16:17 +00:00
|
|
|
}
|
2021-03-24 11:45:18 +00:00
|
|
|
vhooknoret("router_end", "w http.ResponseWriter,r *http.Request,u *User,prefix string,extraData string")
|
|
|
|
vhooknoret("topic_reply_row_assign", "r *ReplyUser")
|
2021-04-07 14:23:11 +00:00
|
|
|
vhooknoret("counters_perf_tick_row", "low int64,high int64,avg int64")
|
2020-05-26 07:53:56 +00:00
|
|
|
//forums_frow_assign
|
|
|
|
//Hook(name string, data interface{}) interface{}
|
|
|
|
/*hook := func(name, params, ret, pure string) {
|
|
|
|
add(name,params,ret,"Hooks",true,false,ret,pure)
|
|
|
|
}*/
|
2021-05-12 02:04:55 +00:00
|
|
|
|
2020-05-26 07:53:56 +00:00
|
|
|
hooknoret := func(name, params string) {
|
2021-03-24 11:45:18 +00:00
|
|
|
add(name, params, "", "HooksNoRet", true, false, "", "")
|
2020-05-26 07:53:56 +00:00
|
|
|
}
|
2021-03-24 11:45:18 +00:00
|
|
|
hooknoret("forums_frow_assign", "f *Forum")
|
2021-05-12 02:04:55 +00:00
|
|
|
|
2020-05-26 07:53:56 +00:00
|
|
|
hookskip := func(name, params string) {
|
2021-03-24 11:45:18 +00:00
|
|
|
add(name, params, "(skip bool)", "HooksSkip", true, true, "", "")
|
2020-05-26 07:53:56 +00:00
|
|
|
}
|
|
|
|
//hookskip("forums_frow_assign","f *Forum")
|
2021-03-24 11:45:18 +00:00
|
|
|
hookskip("topic_create_frow_assign", "f *Forum")
|
2021-05-12 02:04:55 +00:00
|
|
|
|
2020-06-08 02:18:17 +00:00
|
|
|
hookss := func(name string) {
|
2021-03-24 11:45:18 +00:00
|
|
|
add(name, "d string", "string", "Sshooks", true, false, "", "d")
|
2020-06-08 02:18:17 +00:00
|
|
|
}
|
|
|
|
hookss("topic_ogdesc_assign")
|
2020-05-23 23:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Write(hookVars HookVars) {
|
|
|
|
fileData := `// Code generated by Gosora's Hook Generator. DO NOT EDIT.
|
|
|
|
/* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */
|
|
|
|
package common
|
|
|
|
import ({{range .Imports}}
|
|
|
|
"{{.}}"{{end}}
|
|
|
|
)
|
|
|
|
{{range .Hooks}}
|
2020-05-26 07:53:56 +00:00
|
|
|
func H_{{.Name}}_hook(t *HookTable,{{.Params}}) {{.Ret}} { {{if .Any}}
|
|
|
|
{{if .MultiHook}}for _, hook := range t.{{.Type}}["{{.Name}}"] {
|
|
|
|
{{if .Skip}}if skip = hook({{.Params2}}); skip {
|
|
|
|
break
|
|
|
|
}{{else}}{{if .Pure}}{{.Pure}} = {{else if .Ret}}return {{end}}hook({{.Params2}}){{end}}
|
|
|
|
}{{else}}hook := t.{{.Type}}["{{.Name}}"]
|
2020-05-23 23:16:17 +00:00
|
|
|
if hook != nil {
|
|
|
|
{{if .Ret}}return {{end}}hook({{.Params2}})
|
2020-05-26 07:53:56 +00:00
|
|
|
} {{end}}{{end}}{{if .Pure}}
|
|
|
|
return {{.Pure}}{{else if .Ret}}
|
|
|
|
return {{.DefaultRet}}{{end}}
|
2020-05-23 23:16:17 +00:00
|
|
|
}{{end}}
|
|
|
|
`
|
|
|
|
tmpl := template.Must(template.New("hooks").Parse(fileData))
|
|
|
|
var b bytes.Buffer
|
|
|
|
if e := tmpl.Execute(&b, hookVars); e != nil {
|
|
|
|
log.Fatal(e)
|
|
|
|
}
|
2021-03-24 11:45:18 +00:00
|
|
|
|
|
|
|
err := writeFile("./common/gen_extend.go", b.String())
|
2020-05-23 23:16:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeFile(name, body string) error {
|
|
|
|
f, e := os.Create(name)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
if _, e = f.WriteString(body); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
if e = f.Sync(); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return f.Close()
|
2021-03-24 11:45:18 +00:00
|
|
|
}
|