From bf45accfcb94c5dd1769fe99fedae4fc2759d91b Mon Sep 17 00:00:00 2001 From: Ankur Kotwal Date: Tue, 21 Nov 2023 14:03:20 +1100 Subject: [PATCH 1/5] Update domain name to mrc.metadawn.com --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2480477..021068e 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ designed to be flexible across many different controllers and many different games. ![Build Status](https://github.com/ankurkotwal/metarefcard/actions/workflows/main.yml/badge.svg) [![Go Report Card](https://goreportcard.com/badge/github.com/ankurkotwal/metarefcard)](https://goreportcard.com/report/github.com/ankurkotwal/metarefcard) -## Hosted at [metarefcard.com](https://metarefcard.com) +## Hosted at [mrc.metadawn.com](https://mrc.metadawn.com) Here are some example images: ## Flight Simulator 2020 - Alpha Flight Controls From fcc3da7bd570d32849885be0e4bcbecfdc62fb23 Mon Sep 17 00:00:00 2001 From: Ankur Kotwal Date: Tue, 21 Nov 2023 15:45:24 +1100 Subject: [PATCH 2/5] Ignore all __deug_bin files --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ae85650..c4aeb97 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ ##### MetaRef files ##### debug.xml debug.profile -__debug_bin +__debug_bin* scratch metarefcard From 8be57f20f8f1adc996d56dcb1e32c9cf23d6f7b8 Mon Sep 17 00:00:00 2001 From: Ankur Kotwal Date: Tue, 21 Nov 2023 15:45:39 +1100 Subject: [PATCH 3/5] Build script hide which output --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 40f17fc..318bab7 100755 --- a/build.sh +++ b/build.sh @@ -1,7 +1,7 @@ #!/bin/bash if [ `uname` == "Darwin" ]; then - brew_path=`which brew` + brew_path=`which brew 2>/dev/null` if [ $? -eq 0 ]; then jpeg_turbo_path=`brew --prefix jpeg-turbo 2>/dev/null` jpeg_turbo_version=`brew list --versions jpeg-turbo 2>/dev/null | sed -E 's/^jpeg-turbo[[:space:]]+//'` From 4df035b33dd6c204d3e088565e87157f15563b85 Mon Sep 17 00:00:00 2001 From: Ankur Kotwal Date: Tue, 21 Nov 2023 15:46:13 +1100 Subject: [PATCH 4/5] VSCode go.lintOnSave to workspace --- .vscode/settings.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 8075fd4..ab9ae44 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -12,5 +12,6 @@ "comments": false, "strings": true } - } + }, + "go.lintOnSave": "workspace" } \ No newline at end of file From b694f14ff37835665b16c8a7009840e0da50412c Mon Sep 17 00:00:00 2001 From: Ankur Kotwal Date: Tue, 21 Nov 2023 15:46:51 +1100 Subject: [PATCH 5/5] Domain name update, fix deprecated code --- config/config.yaml | 5 +++-- go.mod | 2 +- mrc/common/config.go | 5 +++++ mrc/common/game.go | 10 ++++++++++ mrc/common/image.go | 12 +++++++----- mrc/common/logger.go | 4 ++-- mrc/common/util.go | 6 +++--- mrc/fs2020/fs2020.go | 15 ++++++--------- mrc/metarefcard.go | 17 +++++++++-------- 9 files changed, 46 insertions(+), 30 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index 640cfa6..ec3ad1c 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -1,6 +1,7 @@ --- AppName: MetaRefCard -Version: 0.3.10 +Version: 0.3.11 +Domain: mrc.metadawn.com DebugOutput: false VerboseOutput: false DevicesFile: config/devices.yaml @@ -28,7 +29,7 @@ ImageHeader: BackgroundColour: "#2780e3ff" Watermark: - Text: MetaRefCard.com + Text: MetaRefCard TextColour: "#ffffffff" BackgroundColour: "#373a3cff" Font: Dirga.ttf diff --git a/go.mod b/go.mod index 021ec0b..0f069cf 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 github.com/pixiv/go-libjpeg v0.0.0-20190822045933-3da21a74767d golang.org/x/image v0.14.0 + golang.org/x/text v0.14.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -35,6 +36,5 @@ require ( golang.org/x/crypto v0.15.0 // indirect golang.org/x/net v0.18.0 // indirect golang.org/x/sys v0.14.0 // indirect - golang.org/x/text v0.14.0 // indirect google.golang.org/protobuf v1.31.0 // indirect ) diff --git a/mrc/common/config.go b/mrc/common/config.go index 2f91208..309ff43 100644 --- a/mrc/common/config.go +++ b/mrc/common/config.go @@ -1,9 +1,12 @@ package common +import "golang.org/x/text/cases" + // Config contains all the configuration data for the app type Config struct { AppName string `yaml:"AppName"` Version string `yaml:"Version"` + Domain string `yaml:"Domain"` DebugOutput bool `yaml:"DebugOutput"` VerboseOutput bool `yaml:"VerboseOutput"` @@ -31,6 +34,8 @@ type Config struct { LightColour string `yaml:"LightColour"` DarkColour string `yaml:"DarkColour"` AlternateColours []string `yaml:"AlternateColours"` + + LangTitleCase cases.Caser } // HeaderData contains necessary data to generate header diff --git a/mrc/common/game.go b/mrc/common/game.go index 2a1ac90..48e4602 100644 --- a/mrc/common/game.go +++ b/mrc/common/game.go @@ -4,6 +4,9 @@ import ( "fmt" "regexp" "strings" + + "golang.org/x/text/cases" + "golang.org/x/text/language" ) // GameData holds the game's parsed data @@ -31,6 +34,13 @@ const ( ProfileDefault = "default_metarefcard" ) +// Caser that returns Title case for a string. +var titleCaser = cases.Title(language.AmericanEnglish) + +func TitleCaser(text string) string { + return titleCaser.String(text) +} + // RegexByName - map of named regex strings type RegexByName map[string]*regexp.Regexp diff --git a/mrc/common/image.go b/mrc/common/image.go index a156ba6..bdc2e31 100644 --- a/mrc/common/image.go +++ b/mrc/common/image.go @@ -49,7 +49,7 @@ func GenerateImages(overlaysByProfile OverlaysByProfile, config.Devices.DeviceLabelsByImage[imageName], xOffset, pixelMultiplier, config.FontsDir, config.InputMinFontSize) - addMRCLogo(dc, &config.Watermark, config.Version, + addMRCLogo(dc, &config.Watermark, config.Version, config.Domain, xOffset, float64(config.InputPixelXInset), pixelMultiplier, config.FontsDir) @@ -158,8 +158,10 @@ func populateImage(dc *gg.Context, imageFilename string, imgSize image.Point, } var imgBytes bytes.Buffer - jpeg.Encode(&imgBytes, dc.Image(), - &jpeg.EncoderOptions{Quality: config.JpgQuality}) + err := jpeg.Encode(&imgBytes, dc.Image(), &jpeg.EncoderOptions{Quality: config.JpgQuality}) + if err != nil { + log.Err("jpeg encode failed: %v", err) + } return imgBytes } @@ -301,11 +303,11 @@ func addImageHeader(dc *gg.Context, imageHeader *HeaderData, profile string, imageHeader.Inset.Y*pixelMultiplier) } -func addMRCLogo(dc *gg.Context, watermark *WatermarkData, version string, +func addMRCLogo(dc *gg.Context, watermark *WatermarkData, version string, domain string, xOffset float64, xInset float64, pixelMultiplier float64, fontsDir string) { fontSize := int(math.Round(watermark.FontSize * pixelMultiplier)) // Generate watermark - text := fmt.Sprintf("%s v%s", watermark.Text, version) + text := fmt.Sprintf("%s v%s (%s)", watermark.Text, version, domain) drawTextWithBackgroundRec(dc, text, xOffset, watermark.Location, 0, 0, fontSize, pixelMultiplier, loadFont(fontsDir, watermark.Font, fontSize), diff --git a/mrc/common/logger.go b/mrc/common/logger.go index 8072bf4..2e13766 100644 --- a/mrc/common/logger.go +++ b/mrc/common/logger.go @@ -10,7 +10,7 @@ type Logger []*LogEntry // Dbg prints an informational message func (l *Logger) Dbg(format string, v ...interface{}) { - log.Println(fmt.Sprintf(format, v...)) + log.Printf("%s\n", fmt.Sprintf(format, v...)) } // Msg logs an informational message @@ -23,7 +23,7 @@ func (l *Logger) Msg(format string, v ...interface{}) { // Err logs an error message func (l *Logger) Err(format string, v ...interface{}) { msg := fmt.Sprintf(format, v...) - log.Println(fmt.Sprintf("Error: %s", msg)) + log.Printf("%s\n", fmt.Sprintf("Error: %s", msg)) *l = append(*l, &LogEntry{true, msg}) } diff --git a/mrc/common/util.go b/mrc/common/util.go index b1832ee..ea82d2c 100644 --- a/mrc/common/util.go +++ b/mrc/common/util.go @@ -2,8 +2,8 @@ package common import ( "fmt" - "io/ioutil" "log" + "os" "sync" "github.com/golang/freetype/truetype" @@ -25,7 +25,7 @@ func (m Set) Keys() []string { // LoadYaml loads Yaml file and prints any errors func LoadYaml(filename string, out interface{}, label string, log *Logger) { - yamlData, err := ioutil.ReadFile(filename) + yamlData, err := os.ReadFile(filename) if err != nil { log.Fatal("yaml ioutil.ReadFile %v ", err) } @@ -58,7 +58,7 @@ func loadFont(dir string, name string, size int) font.Face { font = v.(*truetype.Font) } else { fontPath := fmt.Sprintf("%s/%s", dir, name) - fontBytes, err := ioutil.ReadFile(fontPath) + fontBytes, err := os.ReadFile(fontPath) if err != nil { panic(err) } diff --git a/mrc/fs2020/fs2020.go b/mrc/fs2020/fs2020.go index 91180c3..fc7f1de 100644 --- a/mrc/fs2020/fs2020.go +++ b/mrc/fs2020/fs2020.go @@ -8,7 +8,6 @@ import ( "io" "regexp" "strconv" - "strings" "sync" "github.com/ankurkotwal/metarefcard/mrc/common" @@ -25,10 +24,10 @@ const ( // GetGameInfo returns the info needed to fit into MetaRefCard // Returns: -// * Game label / name -// * User friendly command line description -// * Func handler for incoming request -// * Func that matches the game input format to MRC's model +// - Game label / name +// - User friendly command line description +// - Func handler for incoming request +// - Func that matches the game input format to MRC's model func GetGameInfo() (string, string, common.FuncRequestHandler, common.FuncMatchGameInputToModel) { return label, desc, handleRequest, matchGameInputToModel } @@ -98,10 +97,8 @@ func loadInputFiles(files [][]byte, deviceShortNameMap common.DeviceNameFullToSh // Found new device var aDevice string for _, attr := range ty.Attr { - switch attr.Name.Local { - case "DeviceName": + if attr.Name.Local == "DeviceName" { aDevice = attr.Value - break } } var found bool @@ -287,7 +284,7 @@ func matchGameInputToModelByRegex(deviceName string, action string, } matches = sharedRegexes.Pov.FindAllStringSubmatch(action, -1) if matches != nil { - direction := strings.Title(strings.ToLower(matches[0][2])) + direction := common.TitleCaser(matches[0][2]) pov := fmt.Sprintf("POV%s%s", "1", direction) if len(matches[0][1]) > 0 { pov = fmt.Sprintf("POV%s%s", matches[0][1], direction) diff --git a/mrc/metarefcard.go b/mrc/metarefcard.go index ba81d91..7156639 100644 --- a/mrc/metarefcard.go +++ b/mrc/metarefcard.go @@ -5,7 +5,7 @@ import ( "encoding/base64" "fmt" "html/template" - "io/ioutil" + "io" "log" "net/http" "os" @@ -22,10 +22,10 @@ var config *common.Config // GameInfo is the info needed to fit into MetaRefCard // Returns: -// * Game label / name -// * User friendly command line description -// * Func handler for incoming request -// * Func that matches the game input format to MRC's model +// - Game label / name +// - User friendly command line description +// - Func handler for incoming request +// - Func that matches the game input format to MRC's model type GameInfo func() (string, string, common.FuncRequestHandler, common.FuncMatchGameInputToModel) @@ -65,6 +65,7 @@ func GetServer(debugMode bool, gameArgs GameToInputFiles) (*gin.Engine, string) c.HTML(http.StatusOK, fmt.Sprintf("%s.html", label), gin.H{ "Title": config.AppName, "Version": config.Version, + "Domain": config.Domain, }) }) // Flight simulator endpoint @@ -93,7 +94,7 @@ func GetServer(debugMode bool, gameArgs GameToInputFiles) (*gin.Engine, string) func loadLocalFiles(files []string, log *common.Logger) [][]byte { var inputFiles [][]byte for _, filename := range files { - file, err := ioutil.ReadFile(filename) + file, err := os.ReadFile(filename) if err != nil { log.Err("Error reading file. %s", err) } @@ -117,7 +118,7 @@ func loadFormFiles(c *gin.Context, log *common.Logger) [][]byte { log.Err("Error opening multipart file %s - %s", file.Filename, err) continue } - contents, err := ioutil.ReadAll(multipart) + contents, err := io.ReadAll(multipart) if err != nil { log.Err("Error reading multipart file %s - %s", file.Filename, err) continue @@ -203,7 +204,7 @@ func (i *Filenames) Set(value string) error { // GetFilesFromDir returns a list of file names from a directory func GetFilesFromDir(path string) *Filenames { - files, err := ioutil.ReadDir(path) + files, err := os.ReadDir(path) if err != nil { log.Fatal(err) }