internal/gamepaddb: refactoring

Updates #2936
This commit is contained in:
Hajime Hoshi 2024-03-29 13:18:13 +09:00
parent 941c97eba2
commit 24238e16af
3 changed files with 10 additions and 22 deletions

View File

@ -16,10 +16,6 @@
package gamepaddb package gamepaddb
import (
_ "embed"
)
var additionalGLFWGamepads = []byte(` var additionalGLFWGamepads = []byte(`
78696e70757401000000000000000000,XInput Gamepad (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8, 78696e70757401000000000000000000,XInput Gamepad (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,
78696e70757402000000000000000000,XInput Wheel (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8, 78696e70757402000000000000000000,XInput Wheel (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,

View File

@ -1,7 +0,0 @@
78696e70757401000000000000000000,XInput Gamepad (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,
78696e70757402000000000000000000,XInput Wheel (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,
78696e70757403000000000000000000,XInput Arcade Stick (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,
78696e70757404000000000000000000,XInput Flight Stick (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,
78696e70757405000000000000000000,XInput Dance Pad (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,
78696e70757406000000000000000000,XInput Guitar (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,
78696e70757408000000000000000000,XInput Drum Kit (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,

View File

@ -79,8 +79,9 @@ func main() {
} }
func run() error { func run() error {
// Follow the standard comment rule (https://pkg.go.dev/cmd/go#hdr-Generate_Go_files_by_processing_source). // doNotEditor is a special comment for generated files.
doNotEdit := "// Code generated by gen.go using 'go generate'. DO NOT EDIT." // This follows the standard comment rule (https://pkg.go.dev/cmd/go#hdr-Generate_Go_files_by_processing_source).
const doNotEdit = "// Code generated by gen.go using 'go generate'. DO NOT EDIT."
type gamePadPlatform struct { type gamePadPlatform struct {
filenameSuffix string filenameSuffix string
@ -108,7 +109,7 @@ func run() error {
}, },
} }
controllerDBs, err := splitControllersByPlatform(gameControllerDB) controllerDBs, err := splitDBsByPlatform(gameControllerDB)
if err != nil { if err != nil {
return err return err
} }
@ -116,12 +117,11 @@ func run() error {
for sdlPlatformName, platform := range platforms { for sdlPlatformName, platform := range platforms {
controllerDB, ok := controllerDBs[sdlPlatformName] controllerDB, ok := controllerDBs[sdlPlatformName]
if !ok { if !ok {
return fmt.Errorf("failed to find controller db for platform %s in gamecontrollerdb_txt", sdlPlatformName) return fmt.Errorf("failed to find controller db for platform %s in gamecontrollerdb.txt", sdlPlatformName)
} }
// write each chunk into separate text file for embedding into respective generated files // Write each chunk into separate text file for embedding into respective generated files.
err = os.WriteFile(fmt.Sprintf("gamecontrollerdb_%s.txt", platform.filenameSuffix), []byte(controllerDB), 0666) if err = os.WriteFile(fmt.Sprintf("gamecontrollerdb_%s.txt", platform.filenameSuffix), []byte(controllerDB), 0666); err != nil {
if err != nil {
return err return err
} }
@ -137,7 +137,7 @@ func run() error {
} }
defer f.Close() defer f.Close()
err = tmpl.Execute(f, struct { if err := tmpl.Execute(f, struct {
License string License string
DoNotEdit string DoNotEdit string
BuildConstraints string BuildConstraints string
@ -147,8 +147,7 @@ func run() error {
DoNotEdit: doNotEdit, DoNotEdit: doNotEdit,
BuildConstraints: platform.buildConstraints, BuildConstraints: platform.buildConstraints,
FileNameSuffix: platform.filenameSuffix, FileNameSuffix: platform.filenameSuffix,
}) }); err != nil {
if err != nil {
return err return err
} }
} }
@ -156,7 +155,7 @@ func run() error {
return nil return nil
} }
func splitControllersByPlatform(controllerDB []byte) (map[string]string, error) { func splitDBsByPlatform(controllerDB []byte) (map[string]string, error) {
s := bufio.NewScanner(bytes.NewReader(controllerDB)) s := bufio.NewScanner(bytes.NewReader(controllerDB))
dbs := map[string]string{} dbs := map[string]string{}