Convert to templates

Signed-off-by: jolheiser <john.olheiser@gmail.com>
This commit is contained in:
jolheiser
2021-08-06 14:05:10 -05:00
parent ba26dc6adc
commit 782f70ba21
10 changed files with 21461 additions and 4677 deletions

View File

@ -9,19 +9,39 @@ import (
"net/http"
"os"
"sort"
"text/template"
)
const (
protocolURL = "https://pokechu22.github.io/Burger/1.17.1.json"
//language=gohtml
soundTmpl = `// Code generated by gen_soundid.go. DO NOT EDIT.
package soundid
// SoundID represents a sound ID used in the minecraft protocol.
type SoundID int32
// SoundNames - map of ids to names for sounds.
var SoundNames = map[SoundID]string{ {{range .}}
{{.ID}}: "{{.Name}}",{{end}}
}
// GetSoundNameByID helper method
func GetSoundNameByID(id SoundID) (string, bool) {
name, ok := SoundNames[id]
return name, ok
}`
)
type sound struct {
type Sound struct {
ID int64
Name string
}
//go:generate go run $GOFILE
func main() {
fmt.Println("generating soundid.go")
sounds, err := downloadSoundInfo()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
@ -35,56 +55,35 @@ func main() {
}
defer f.Close()
fmt.Fprintln(f, "// Code generated by gen_soundIDs.go. DO NOT EDIT.")
fmt.Fprintln(f)
fmt.Fprintln(f, "package soundid")
fmt.Fprintln(f)
fmt.Fprintln(f, "// SoundID represents a sound ID used in the minecraft protocol.")
fmt.Fprintln(f, "type SoundID int32")
fmt.Fprintln(f)
fmt.Fprintln(f, "// SoundNames - map of ids to names for sounds.")
fmt.Fprintln(f, "var SoundNames map[SoundID]string = map[SoundID]string{")
for _, v := range sounds {
fmt.Fprintf(f, ` %d: "%s",`, v.ID, v.Name)
fmt.Fprintln(f)
if err := template.Must(template.New("").Parse(soundTmpl)).Execute(f, sounds); err != nil {
panic(err)
}
fmt.Fprintln(f, "}")
fmt.Fprintln(f)
fmt.Fprintln(f, "// GetSoundNameByID helper method")
fmt.Fprintln(f, "func GetSoundNameByID(id SoundID) (string,bool) {")
fmt.Fprintln(f, " name, ok := SoundNames[id]")
fmt.Fprintln(f, " return name, ok")
fmt.Fprintln(f, "}")
}
func downloadSoundInfo() ([]sound, error) {
func downloadSoundInfo() ([]*Sound, error) {
resp, err := http.Get(protocolURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
x := []map[string]interface{}{}
// I'm not sure why the response returns a list, it appears to ever only have a single object...
var data []struct {
Sounds map[string]Sound `json:"sounds"`
}
// if err := json.Unmarshal([]byte(data), &x); err != nil {
if err := json.NewDecoder(resp.Body).Decode(&x); err != nil {
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
out := []sound{}
for i := range x {
if sounds, ok := x[i]["sounds"]; ok {
// fmt.Fprintf("sounds: %#v\n", sounds)
for _, value := range sounds.(map[string]interface{}) {
// fmt.Fprintf("%d: %s\n", int64(value.(map[string]interface{})["id"].(float64)), value.(map[string]interface{})["name"])
out = append(out, sound{ID: int64(value.(map[string]interface{})["id"].(float64)), Name: value.(map[string]interface{})["name"].(string)})
out := make([]*Sound, 0)
for _, d := range data {
if has := len(d.Sounds); has > 0 {
for _, val := range d.Sounds {
out = append(out, &Sound{ID: val.ID, Name: val.Name})
}
} else {
// fmt.Fprintf("NO SOUNDS FOUND IN DATA!")
return nil, fmt.Errorf("No sounds found in data from %s", protocolURL)
return nil, fmt.Errorf("no sounds found in data from %s", protocolURL)
}
}

File diff suppressed because it is too large Load Diff