Compare commits

..

11 Commits

Author SHA1 Message Date
Kenny Goodin
466f0c000c internal/gamepaddb: rename template variable for consistency, fix
initialism
2024-03-27 11:18:44 -04:00
Kenny Goodin
c47aa9ee7f internal/gamepaddb: use scanner for parsing controller db, idiomatic Go 2024-03-27 11:15:11 -04:00
Kenny Goodin
7a4a2a4994 internal/gamepaddb: pr feedback
this commit will be squashed upon review

- rename gen_dbs.go->gen.go
- add go:generate directive to gamepaddb.go
- output each game controller db into platform-specific text files and
  embed in each db_<platform>.go
- move template into literal in gen.go
2024-03-27 11:15:11 -04:00
Kenny Goodin
5fd91e2750 internal/gamepaddb: generate respective controller dbs for each platform
Replace singular embedded controller database file with separate generated
files for each supported platform. The controller database file is some
~500KB, so this change should reduce the overall binary size per
platform.
2024-03-27 11:15:11 -04:00
Hajime Hoshi
dc77c655af all: update Oto and PureGo 2024-03-27 23:09:12 +09:00
Hajime Hoshi
013e235628 cmd/ebitenmobile, exp/textinput: typo 2024-03-27 15:40:37 +09:00
Hajime Hoshi
59521d715b all: update go-text 2024-03-27 14:55:02 +09:00
Hajime Hoshi
11398f3bb5 ebiten: rename BuildTag -> BuildConstraints 2024-03-27 14:50:09 +09:00
Hajime Hoshi
5c0c084b6e cmd/ebitenmobile: update the default Android SDK version to 21 2024-03-27 14:23:00 +09:00
Hajime Hoshi
25a814d561 cmd/ebitenmobile: bug fix: wrong path was used
Updates #2937
2024-03-27 12:30:12 +09:00
Hajime Hoshi
0ab4538fa1 all: update gomobile
Closes #2937
2024-03-27 11:55:08 +09:00
16 changed files with 194 additions and 119 deletions

View File

@ -54,11 +54,6 @@ func goEnv(name string) string {
return strings.TrimSpace(string(val))
}
const (
// Copied from gomobile.
minAndroidAPI = 15
)
var (
buildA bool // -a
buildI bool // -i
@ -99,6 +94,11 @@ func main() {
flag.Usage()
}
// minAndroidAPI specifies the minimum API version for Android.
// Now Google Player v23.30.99+ drops API levels that are older than 21.
// See https://apilevels.com/.
const minAndroidAPI = 21
var flagset flag.FlagSet
flagset.StringVar(&buildO, "o", "", "")
flagset.StringVar(&buildGcflags, "gcflags", "", "")
@ -150,6 +150,20 @@ func main() {
log.Fatal(err)
}
// If args doesn't include '-androidapi', set it to args explicitly.
// It's because ebitenmobile's default API level is different from gomobile's one.
if buildTarget == "android" && buildAndroidAPI == minAndroidAPI {
var found bool
flag.Visit(func(f *flag.Flag) {
if f.Name == "androidapi" {
found = true
}
})
if !found {
args = append([]string{args[0], "-androidapi", fmt.Sprintf("%d", minAndroidAPI)}, args[1:]...)
}
}
if err := doBind(args, &flagset, buildTarget); err != nil {
log.Fatal(err)
}
@ -251,7 +265,7 @@ func doBind(args []string, flagset *flag.FlagSet, buildOS string) error {
// TODO: strings.Title is used here for the consistency with gomobile (see cmd/gomobile/bind_iosapp.go).
// As strings.Title is deprecated, golang.org/x/text/cases should be used.
frameworkNameBase = strings.Title(frameworkNameBase)
dir := filepath.Join(buildO, name, frameworkNameBase+".framework", "Versions", "A")
dir := filepath.Join(buildO, name, frameworkNameBase+".framework")
if err := os.WriteFile(filepath.Join(dir, "Headers", prefixUpper+"EbitenViewController.h"), []byte(replacePrefixes(objcH)), 0644); err != nil {
return err

View File

@ -47,7 +47,7 @@ type State struct {
// Start starts text inputting.
// Start returns a channel to send the state repeatedly, and a function to end the text inputting.
//
// Start is the low-leve API. For most use cases, Field is easier to use.
// Start is the low-level API. For most use cases, Field is easier to use.
//
// Start returns nil and nil if the current environment doesn't support this package.
func Start(x, y int) (states chan State, close func()) {

View File

@ -576,7 +576,7 @@ const uiGLFWKeysTmpl = `{{.License}}
{{.DoNotEdit}}
{{.BuildTag}}
{{.BuildConstraints}}
package ui
@ -594,7 +594,7 @@ const uiJSKeysTmpl = `{{.License}}
{{.DoNotEdit}}
{{.BuildTag}}
{{.BuildConstraints}}
package ui
@ -612,7 +612,7 @@ const glfwKeysTmpl = `{{.License}}
{{.DoNotEdit}}
{{.BuildTag}}
{{.BuildConstraints}}
package glfw
@ -626,7 +626,7 @@ const mobileAndroidKeysTmpl = `{{.License}}
{{.DoNotEdit}}
{{.BuildTag}}
{{.BuildConstraints}}
package ebitenmobileview
@ -644,7 +644,7 @@ const mobileIOSKeysTmpl = `{{.License}}
{{.DoNotEdit}}
{{.BuildTag}}
{{.BuildConstraints}}
package ebitenmobileview
@ -799,20 +799,20 @@ func main() {
// The build tag can't be included in the templates because of `go vet`.
// Pass the build tag and extract this in the template to make `go vet` happy.
buildTag := ""
buildConstraints := ""
switch path {
case filepath.Join("internal", "glfw", "keys.go"):
buildTag = "//go:build darwin || freebsd || linux || netbsd || openbsd || windows"
buildConstraints = "//go:build darwin || freebsd || linux || netbsd || openbsd || windows"
case filepath.Join("internal", "ui", "keys_mobile.go"):
buildTag = "//go:build android || ios"
buildConstraints = "//go:build android || ios"
case filepath.Join("internal", "ui", "keys_glfw.go"):
buildTag = "//go:build !android && !ios && !js && !nintendosdk && !playstation5"
buildConstraints = "//go:build !android && !ios && !js && !nintendosdk && !playstation5"
}
// NOTE: According to godoc, maps are automatically sorted by key.
if err := tmpl.Execute(f, struct {
License string
DoNotEdit string
BuildTag string
BuildConstraints string
UIKeyNameToJSCode map[string]string
EbitengineKeyNames []string
EbitengineKeyNamesWithoutOld []string
@ -826,7 +826,7 @@ func main() {
}{
License: license,
DoNotEdit: doNotEdit,
BuildTag: buildTag,
BuildConstraints: buildConstraints,
UIKeyNameToJSCode: uiKeyNameToJSCode,
EbitengineKeyNames: ebitengineKeyNames,
EbitengineKeyNamesWithoutOld: ebitengineKeyNamesWithoutOld,

8
go.mod
View File

@ -3,11 +3,11 @@ module github.com/hajimehoshi/ebiten/v2
go 1.18
require (
github.com/ebitengine/gomobile v0.0.0-20240320163605-0b2c67584a2b
github.com/ebitengine/gomobile v0.0.0-20240327025426-b04d93b1a983
github.com/ebitengine/hideconsole v1.0.0
github.com/ebitengine/oto/v3 v3.2.0-alpha.4
github.com/ebitengine/purego v0.7.0-alpha.3
github.com/go-text/typesetting v0.1.1-0.20240319115800-09fd9aa0cd1b
github.com/ebitengine/oto/v3 v3.3.0-alpha
github.com/ebitengine/purego v0.8.0-alpha
github.com/go-text/typesetting v0.1.1-0.20240325125605-c7936fe59984
github.com/hajimehoshi/bitmapfont/v3 v3.0.0
github.com/hajimehoshi/go-mp3 v0.3.4
github.com/jakecoffman/cp v1.2.1

16
go.sum
View File

@ -1,13 +1,13 @@
github.com/ebitengine/gomobile v0.0.0-20240320163605-0b2c67584a2b h1:ccykWd0kO+TooxL9yKfKsI3RB/yK+k/a4vdlA4Q9th8=
github.com/ebitengine/gomobile v0.0.0-20240320163605-0b2c67584a2b/go.mod h1:tWboRRNagZwwwis4QIgEFG1ZNFwBJ3LAhSLAXAAxobQ=
github.com/ebitengine/gomobile v0.0.0-20240327025426-b04d93b1a983 h1:hHib1v7Z+pXVENSB5x3EF8eQJThPhRfj35RymJFVgm0=
github.com/ebitengine/gomobile v0.0.0-20240327025426-b04d93b1a983/go.mod h1:tWboRRNagZwwwis4QIgEFG1ZNFwBJ3LAhSLAXAAxobQ=
github.com/ebitengine/hideconsole v1.0.0 h1:5J4U0kXF+pv/DhiXt5/lTz0eO5ogJ1iXb8Yj1yReDqE=
github.com/ebitengine/hideconsole v1.0.0/go.mod h1:hTTBTvVYWKBuxPr7peweneWdkUwEuHuB3C1R/ielR1A=
github.com/ebitengine/oto/v3 v3.2.0-alpha.4 h1:aaUdcbEDUV1oErHDv/Cd0IAjQaQPChZuvO8Cn/kQHE8=
github.com/ebitengine/oto/v3 v3.2.0-alpha.4/go.mod h1:JtMbxJHZBDXfS8BmVYwzWk9Z6r7jsjwsHzOuZrEkfs4=
github.com/ebitengine/purego v0.7.0-alpha.3 h1:9hH1aneqLaM3sM+PMUgRJVsMe2SqfVjZtV3DEzxBDJU=
github.com/ebitengine/purego v0.7.0-alpha.3/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/go-text/typesetting v0.1.1-0.20240319115800-09fd9aa0cd1b h1:CPNyzp2f5+D9FDN7yT6pXWTDAmpnpjnWi5jt3bKPJyA=
github.com/go-text/typesetting v0.1.1-0.20240319115800-09fd9aa0cd1b/go.mod h1:2+owI/sxa73XA581LAzVuEBZ3WEEV2pXeDswCH/3i1I=
github.com/ebitengine/oto/v3 v3.3.0-alpha h1:XxKkEhWl1w5zK9TjPwod+vZeTU91pXYYSooAGk9ya8o=
github.com/ebitengine/oto/v3 v3.3.0-alpha/go.mod h1:T2/VV0UWG97GEEf4kORMU2nCneYT/YmwSTxPutSVaUg=
github.com/ebitengine/purego v0.8.0-alpha h1:xWsUo8xQZLH8f9TzXXSvs/qjc2s6rhNGYxEo5Qd10so=
github.com/ebitengine/purego v0.8.0-alpha/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/go-text/typesetting v0.1.1-0.20240325125605-c7936fe59984 h1:NwCC36eQsDf1xVZG9jD7ngXNNjsvk8KXky15ogA1Vo0=
github.com/go-text/typesetting v0.1.1-0.20240325125605-c7936fe59984/go.mod h1:2+owI/sxa73XA581LAzVuEBZ3WEEV2pXeDswCH/3i1I=
github.com/go-text/typesetting-utils v0.0.0-20240317173224-1986cbe96c66 h1:GUrm65PQPlhFSKjLPGOZNPNxLCybjzjYBzjfoBGaDUY=
github.com/hajimehoshi/bitmapfont/v3 v3.0.0 h1:r2+6gYK38nfztS/et50gHAswb9hXgxXECYgE8Nczmi4=
github.com/hajimehoshi/bitmapfont/v3 v3.0.0/go.mod h1:+CxxG+uMmgU4mI2poq944i3uZ6UYFfAkj9V6WqmuvZA=

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -13,6 +13,8 @@
// limitations under the License.
//go:generate go run gen.go
//go:generate gofmt -s -w .
package gamepaddb
import (

View File

@ -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
//go:embed gamecontrollerdb_{{.FileNameSuffix}}.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)
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
FileNameSuffix string
}{
License: license,
DoNotEdit: doNotEdit,
BuildTag: platform.buildTag,
Platform: platform.name,
License: license,
DoNotEdit: doNotEdit,
BuildConstraints: platform.buildConstraints,
FileNameSuffix: 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
}