dorfylegends/analyze/df/generate_events.go

56 lines
1.1 KiB
Go
Raw Permalink Normal View History

2022-04-18 11:36:29 +03:00
package df
import (
"bytes"
"encoding/json"
"fmt"
"go/format"
"io/ioutil"
"os"
"text/template"
)
var eventsTemplate = template.Must(template.New("").Parse(`// Code generated by legendsbrowser; DO NOT EDIT.
package model
{{- range $name, $obj := $.Objects }}
{{- if $obj.IsSubTypeOf "HistoricalEvent" }}
2023-11-16 23:16:23 +02:00
func (x *{{ $obj.Name }}) Html(c *context) string { return "UNKNOWN {{ $obj.Name }}" }
2022-04-18 11:36:29 +03:00
{{- end }}
{{- end }}
`))
2022-04-18 18:26:14 +03:00
func GenerateEventsCode(objects *Metadata) error {
2022-04-18 11:36:29 +03:00
file, _ := json.MarshalIndent(objects, "", " ")
_ = ioutil.WriteFile("model.json", file, 0644)
f, err := os.Create("../backend/model/events.go")
if err != nil {
return err
}
defer f.Close()
var buf bytes.Buffer
err = eventsTemplate.Execute(&buf, struct {
Objects *Metadata
Modes []bool
}{
Objects: objects,
Modes: []bool{false, true},
})
if err != nil {
return err
}
p, err := format.Source(buf.Bytes())
if err != nil {
fmt.Println("WARN: could not format source", err)
p = buf.Bytes()
}
_, err = f.Write(p)
return err
}
func (o Object) IsSubTypeOf(t string) bool {
return o.SubTypeOf != nil && *o.SubTypeOf == t
}