mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
internal/gamepaddb: use scanner for parsing controller db, idiomatic Go
This commit is contained in:
parent
7a4a2a4994
commit
c47aa9ee7f
@ -1,4 +1,3 @@
|
||||
|
||||
// Copyright 2024 The Ebitengine Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@ -13,20 +12,28 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
|
||||
// Code generated by gen.go using 'go generate'. DO NOT EDIT.
|
||||
|
||||
//go:build android
|
||||
|
||||
package gamepaddb
|
||||
|
||||
import _ "embed"
|
||||
import (
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
//go:embed gamecontrollerdb_android.txt
|
||||
var controllerBytes []byte
|
||||
|
||||
//go:embed gamecontrollerdb_glfw.txt
|
||||
var glfwControllerBytes []byte
|
||||
|
||||
func init() {
|
||||
if err := Update(controllerBytes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := Update(glfwControllerBytes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
|
||||
// Copyright 2024 The Ebitengine Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@ -13,20 +12,28 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
|
||||
// Code generated by gen.go using 'go generate'. DO NOT EDIT.
|
||||
|
||||
//go:build ios
|
||||
|
||||
package gamepaddb
|
||||
|
||||
import _ "embed"
|
||||
import (
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
//go:embed gamecontrollerdb_ios.txt
|
||||
var controllerBytes []byte
|
||||
|
||||
//go:embed gamecontrollerdb_glfw.txt
|
||||
var glfwControllerBytes []byte
|
||||
|
||||
func init() {
|
||||
if err := Update(controllerBytes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := Update(glfwControllerBytes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
|
||||
// Copyright 2024 The Ebitengine Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@ -13,20 +12,28 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
|
||||
// Code generated by gen.go using 'go generate'. DO NOT EDIT.
|
||||
|
||||
//go:build (freebsd || (linux && !android) || netbsd || openbsd) && !nintendosdk && !playstation5
|
||||
|
||||
package gamepaddb
|
||||
|
||||
import _ "embed"
|
||||
import (
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
//go:embed gamecontrollerdb_linbsd.txt
|
||||
var controllerBytes []byte
|
||||
|
||||
//go:embed gamecontrollerdb_glfw.txt
|
||||
var glfwControllerBytes []byte
|
||||
|
||||
func init() {
|
||||
if err := Update(controllerBytes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := Update(glfwControllerBytes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
|
||||
// Copyright 2024 The Ebitengine Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@ -13,20 +12,28 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
|
||||
// Code generated by gen.go using 'go generate'. DO NOT EDIT.
|
||||
|
||||
//go:build darwin && !ios
|
||||
|
||||
package gamepaddb
|
||||
|
||||
import _ "embed"
|
||||
import (
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
//go:embed gamecontrollerdb_macos_darwin.txt
|
||||
var controllerBytes []byte
|
||||
|
||||
//go:embed gamecontrollerdb_glfw.txt
|
||||
var glfwControllerBytes []byte
|
||||
|
||||
func init() {
|
||||
if err := Update(controllerBytes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := Update(glfwControllerBytes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
|
||||
// Copyright 2024 The Ebitengine Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@ -13,18 +12,19 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
|
||||
// Code generated by gen.go using 'go generate'. DO NOT EDIT.
|
||||
|
||||
//go:build windows && !microsoftgdk
|
||||
|
||||
package gamepaddb
|
||||
|
||||
import _ "embed"
|
||||
import (
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
//go:embed gamecontrollerdb_windows.txt
|
||||
var controllerBytes []byte
|
||||
|
||||
|
||||
//go:embed gamecontrollerdb_glfw.txt
|
||||
var glfwControllerBytes []byte
|
||||
|
||||
@ -32,8 +32,8 @@ func init() {
|
||||
if err := Update(controllerBytes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err := Update(glfwControllerBytes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := Update(glfwControllerBytes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
@ -287,4 +287,3 @@
|
||||
050000005e040000130b0000ffff3f00,Xbox Series Controller,a:b0,b:b1,back:b15,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b9,leftstick:b7,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,platform:Android,
|
||||
65633038363832353634653836396239,Xbox Series Controller,a:b0,b:b1,back:b15,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,platform:Android,
|
||||
050000001727000044310000ffff3f00,XiaoMi Controller,a:b0,b:b1,back:b4,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,leftshoulder:b9,leftstick:b7,lefttrigger:a7,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:a6,rightx:a2,righty:a5,start:b6,x:b2,y:b3,platform:Android,
|
||||
|
||||
|
@ -610,4 +610,3 @@
|
||||
xinput,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Linux,
|
||||
03000000120c0000100e000011010000,Zeroplus P4,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,platform:Linux,
|
||||
03000000120c0000101e000011010000,Zeroplus P4,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,platform:Linux,
|
||||
|
||||
|
@ -284,4 +284,3 @@
|
||||
03000000172700004431000029010000,XiaoMi Controller,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b15,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a6,rightx:a2,righty:a5,start:b11,x:b3,y:b4,platform:Mac OS X,
|
||||
03000000120c0000100e000000010000,Zeroplus P4,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,platform:Mac OS X,
|
||||
03000000120c0000101e000000010000,Zeroplus P4,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,platform:Mac OS X,
|
||||
|
||||
|
@ -798,4 +798,3 @@ xinput,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,
|
||||
03000000790000004f18000000000000,ZDT Android Controller,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a3,righty:a4,start:b11,x:b3,y:b4,platform:Windows,
|
||||
03000000120c00000500000000000000,Zeroplus Adapter,a:b2,b:b1,back:b11,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b9,lefttrigger:b4,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b0,righttrigger:b5,rightx:a3,righty:a2,start:b8,x:b3,y:b0,platform:Windows,
|
||||
03000000120c0000101e000000000000,Zeroplus P4 Gamepad,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,platform:Windows,
|
||||
|
||||
|
@ -13,6 +13,8 @@
|
||||
// limitations under the License.
|
||||
|
||||
//go:generate go run gen.go
|
||||
//go:generate gofmt -s -w .
|
||||
|
||||
package gamepaddb
|
||||
|
||||
import (
|
||||
|
@ -14,11 +14,10 @@
|
||||
|
||||
//go:build ignore
|
||||
|
||||
// This program generates platform-specific controller dbs. It can be invoked by running
|
||||
// go generate
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
@ -50,134 +49,170 @@ const license = `// Copyright 2024 The Ebitengine Authors
|
||||
// curl --location --remote-name https://raw.githubusercontent.com/mdqinc/SDL_GameControllerDB/master/gamecontrollerdb.txt
|
||||
|
||||
//go:embed gamecontrollerdb.txt
|
||||
var gamecontrollerdb_txt []byte
|
||||
var gameControllerDB []byte
|
||||
|
||||
const db_template string = `
|
||||
{{.License}}
|
||||
const dbTemplate string = `{{.License}}
|
||||
|
||||
{{.DoNotEdit}}
|
||||
|
||||
{{.BuildTag}}
|
||||
{{.BuildConstraints}}
|
||||
|
||||
package gamepaddb
|
||||
|
||||
import _ "embed"
|
||||
import (
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
//go:embed gamecontrollerdb_{{.Platform}}.txt
|
||||
var controllerBytes []byte
|
||||
{{if eq .Platform "windows" }}
|
||||
//go:embed gamecontrollerdb_glfw.txt
|
||||
var glfwControllerBytes []byte
|
||||
{{end}}
|
||||
|
||||
func init() {
|
||||
if err := Update(controllerBytes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
{{if eq .Platform "windows" }}
|
||||
|
||||
if err := Update(glfwControllerBytes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
{{end}}}`
|
||||
}
|
||||
}`
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
// Follow the standard comment rule (https://pkg.go.dev/cmd/go#hdr-Generate_Go_files_by_processing_source).
|
||||
doNotEdit := "// Code generated by gen.go using 'go generate'. DO NOT EDIT."
|
||||
|
||||
type gamePadPlatform struct {
|
||||
name string
|
||||
buildTag string
|
||||
filenameSuffix string
|
||||
buildConstraints string
|
||||
}
|
||||
|
||||
supported := map[string]gamePadPlatform{
|
||||
"Windows": {
|
||||
name: "windows",
|
||||
buildTag: "//go:build windows && !microsoftgdk",
|
||||
filenameSuffix: "windows",
|
||||
buildConstraints: "//go:build windows && !microsoftgdk",
|
||||
},
|
||||
"Mac OS X": {
|
||||
name: "macos_darwin",
|
||||
buildTag: "//go:build darwin && !ios",
|
||||
filenameSuffix: "macos_darwin",
|
||||
buildConstraints: "//go:build darwin && !ios",
|
||||
},
|
||||
"Linux": {
|
||||
name: "linbsd",
|
||||
buildTag: "//go:build (freebsd || (linux && !android) || netbsd || openbsd) && !nintendosdk && !playstation5",
|
||||
filenameSuffix: "linbsd",
|
||||
buildConstraints: "//go:build (freebsd || (linux && !android) || netbsd || openbsd) && !nintendosdk && !playstation5",
|
||||
},
|
||||
"iOS": {
|
||||
name: "ios",
|
||||
buildTag: "//go:build ios",
|
||||
filenameSuffix: "ios",
|
||||
buildConstraints: "//go:build ios",
|
||||
},
|
||||
"Android": {
|
||||
name: "android",
|
||||
buildTag: "//go:build android",
|
||||
filenameSuffix: "android",
|
||||
buildConstraints: "//go:build android",
|
||||
},
|
||||
}
|
||||
|
||||
// divide controller dbs into chunks by platform
|
||||
controllerDbs := splitControllerDb(gamecontrollerdb_txt)
|
||||
controllerDBs, err := splitControllersByPlatform(gameControllerDB)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for sdlName, platform := range supported {
|
||||
controllerDb, ok := controllerDbs[sdlName]
|
||||
controllerDb, ok := controllerDBs[sdlName]
|
||||
if !ok {
|
||||
log.Fatalf("failed to find controller db for platform %s in gamecontrollerdb_txt", sdlName)
|
||||
return fmt.Errorf("failed to find controller db for platform %s in gamecontrollerdb_txt", sdlName)
|
||||
}
|
||||
|
||||
// write each chunk into separate text file for embedding into respective generated files
|
||||
txtFile, err := os.Create(fmt.Sprintf("gamecontrollerdb_%s.txt", platform.name))
|
||||
txtFile, err := os.Create(fmt.Sprintf("gamecontrollerdb_%s.txt", platform.filenameSuffix))
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open file: %v", err)
|
||||
return fmt.Errorf("failed to open file: %v", err)
|
||||
}
|
||||
defer txtFile.Close()
|
||||
written, err := txtFile.Write(controllerDb)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to write controller db for %s, expected to write %d bytes, wrote %d: %v", sdlName, len(controllerDb), written, err)
|
||||
return fmt.Errorf("failed to write controller db for %s, expected to write %d bytes, wrote %d: %v", sdlName, len(controllerDb), written, err)
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("db_%s.go", platform.name)
|
||||
tmpl, err := template.New(path).Parse(db_template)
|
||||
path := fmt.Sprintf("db_%s.go", platform.filenameSuffix)
|
||||
tmpl, err := template.New(path).Parse(dbTemplate)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to parse template: %v", err)
|
||||
return fmt.Errorf("failed to parse template: %v", err)
|
||||
}
|
||||
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
err = tmpl.Execute(f, struct {
|
||||
License string
|
||||
DoNotEdit string
|
||||
BuildTag string
|
||||
Platform string
|
||||
License string
|
||||
DoNotEdit string
|
||||
BuildConstraints string
|
||||
Platform string
|
||||
}{
|
||||
License: license,
|
||||
DoNotEdit: doNotEdit,
|
||||
BuildTag: platform.buildTag,
|
||||
Platform: platform.name,
|
||||
License: license,
|
||||
DoNotEdit: doNotEdit,
|
||||
BuildConstraints: platform.buildConstraints,
|
||||
Platform: platform.filenameSuffix,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// splitControllerDb maps the game controller db into respective controller definition byte slices for
|
||||
// each platform
|
||||
func splitControllerDb(controllerDb []byte) map[string][]byte {
|
||||
func splitControllersByPlatform(controllerDB []byte) (map[string][]byte, error) {
|
||||
s := bufio.NewScanner(bytes.NewReader(controllerDB))
|
||||
dbs := map[string][]byte{}
|
||||
// split at #, skip first 3 header chunks
|
||||
chunks := bytes.Split(controllerDb, []byte{'#'})[3:]
|
||||
|
||||
for _, chunk := range chunks {
|
||||
// find each platform string
|
||||
lines := bytes.Split(chunk, []byte{'\n'})
|
||||
if len(lines) == 0 {
|
||||
var currentPlatform string
|
||||
buf := bytes.Buffer{}
|
||||
for s.Scan() {
|
||||
chunk := s.Bytes()
|
||||
if len(chunk) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
platform := strings.TrimSpace(string(lines[0]))
|
||||
// count platform string len + space + newline
|
||||
bytesRead := len(lines[0]) + 1
|
||||
dbs[platform] = chunk[bytesRead:]
|
||||
if chunk[0] == '#' {
|
||||
if currentPlatform != "" && buf.Len() > 0 {
|
||||
dbs[currentPlatform] = bytes.Clone(buf.Bytes())
|
||||
buf.Reset()
|
||||
}
|
||||
|
||||
platform := strings.Replace(string(chunk), "# ", "", 1)
|
||||
switch platform {
|
||||
case "Windows":
|
||||
currentPlatform = "Windows"
|
||||
case "Mac OS X":
|
||||
currentPlatform = "Mac OS X"
|
||||
case "iOS":
|
||||
currentPlatform = "iOS"
|
||||
case "Android":
|
||||
currentPlatform = "Android"
|
||||
case "Linux":
|
||||
currentPlatform = "Linux"
|
||||
default:
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
buf.Write(chunk)
|
||||
buf.WriteByte('\n')
|
||||
}
|
||||
}
|
||||
return dbs
|
||||
if currentPlatform != "" && buf.Len() > 0 {
|
||||
dbs[currentPlatform] = bytes.Clone(buf.Bytes())
|
||||
}
|
||||
if err := s.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return dbs, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user