Rework soundIDs and packetIDs generator code to not be platform specific

This commit is contained in:
Scott Binns
2021-01-08 21:27:57 -07:00
parent 8b93d44f86
commit b299065c78
4 changed files with 200 additions and 186 deletions

View File

@ -28,31 +28,38 @@ func main() {
os.Exit(1)
}
fmt.Println("package data")
fmt.Println()
fmt.Println("//go:generate bash -c \"go run gen_soundIDs.go > soundIDs.go\"")
fmt.Println()
fmt.Println("// This file is automatically generated by gen_soundIDs.go. DO NOT EDIT.")
fmt.Println()
fmt.Println("// SoundID represents a sound ID used in the minecraft protocol.")
fmt.Println("type SoundID int32")
fmt.Println()
fmt.Println("// SoundNames - map of ids to names for sounds.")
fmt.Println("var SoundNames map[SoundID]string = map[SoundID]string{")
for _, v := range sounds {
fmt.Printf(` %d: "%s",`, v.ID, v.Name)
fmt.Println()
f, err := os.Create("soundIDs.go")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
fmt.Println("}")
fmt.Println()
defer f.Close()
fmt.Println("// GetSoundNameByID helper method")
fmt.Println("func GetSoundNameByID(id SoundID) (string,bool) {")
fmt.Println(" name, ok := SoundNames[id]")
fmt.Println(" return name, ok")
fmt.Println("}")
fmt.Fprintln(f, "// This file is automatically generated by gen_soundIDs.go. DO NOT EDIT.")
fmt.Fprintln(f)
fmt.Fprintln(f, "package data")
fmt.Fprintln(f)
fmt.Fprintln(f, "//go:generate go run gen_soundIDs.go")
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)
}
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) {
@ -72,13 +79,13 @@ func downloadSoundInfo() ([]sound, error) {
out := []sound{}
for i := range x {
if sounds, ok := x[i]["sounds"]; ok {
// fmt.Printf("sounds: %#v\n", sounds)
// fmt.Fprintf("sounds: %#v\n", sounds)
for _, value := range sounds.(map[string]interface{}) {
// fmt.Printf("%d: %s\n", int64(value.(map[string]interface{})["id"].(float64)), value.(map[string]interface{})["name"])
// 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)})
}
} else {
// fmt.Printf("NO SOUNDS FOUND IN DATA!")
// fmt.Fprintf("NO SOUNDS FOUND IN DATA!")
return nil, fmt.Errorf("No sounds found in data from %s", protocolURL)
}
}