diff --git a/pkg/config/config.yaml b/pkg/config/config.yaml index 62d7f8cd..d1402f3c 100644 --- a/pkg/config/config.yaml +++ b/pkg/config/config.yaml @@ -29,6 +29,8 @@ coordinator: selector: # games library library: + # optional alias file for overriding game names from the basePath path + aliasFile: alias.txt # root folder for the library (where games are stored) basePath: assets/games # an explicit list of supported file extensions diff --git a/pkg/config/shared.go b/pkg/config/shared.go index 2847048a..e49eb3ce 100644 --- a/pkg/config/shared.go +++ b/pkg/config/shared.go @@ -5,6 +5,8 @@ import "flag" type Version int type Library struct { + // filename of the alias' file + AliasFile string // some directory which is going to be // the root folder for the library BasePath string diff --git a/pkg/games/library.go b/pkg/games/library.go index abf30b41..d4ef214e 100644 --- a/pkg/games/library.go +++ b/pkg/games/library.go @@ -2,7 +2,6 @@ package games import ( "bufio" - "errors" "fmt" "io/fs" "os" @@ -19,6 +18,7 @@ import ( // libConf is an optimized internal library configuration type libConf struct { + aliasFile string path string supported map[string]struct{} ignored map[string]struct{} @@ -89,6 +89,7 @@ func NewLib(conf config.Library, emu WithEmulatorInfo, log *logger.Logger) GameL library := &library{ config: libConf{ + aliasFile: conf.AliasFile, path: dir, supported: toMap(conf.Supported), ignored: toMap(conf.Ignored), @@ -128,34 +129,36 @@ func (lib *library) FindGameByName(name string) GameMetadata { } func (lib *library) AliasFileMaybe() map[string]string { - dir := lib.config.path - path := dir + "/alias.txt" - - if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { + if lib.config.aliasFile == "" { return nil } - // read - f, err := os.Open(path) + path := filepath.Join(lib.config.path, lib.config.aliasFile) + + if _, err := os.Stat(path); os.IsNotExist(err) { + return nil + } + + file, err := os.Open(path) if err != nil { lib.log.Error().Msgf("couldn't open alias file, %v", err) return nil } - defer func() { _ = f.Close() }() + defer func() { _ = file.Close() }() - m := make(map[string]string) - s := bufio.NewScanner(f) - for s.Scan() { - id, alias, ok := strings.Cut(s.Text(), "=") - if ok { - m[id] = alias + aliases := make(map[string]string) + scanner := bufio.NewScanner(file) + for scanner.Scan() { + if id, alias, found := strings.Cut(scanner.Text(), "="); found { + aliases[id] = alias } } - if err = s.Err(); err != nil { + + if err := scanner.Err(); err != nil { lib.log.Error().Msgf("alias file read error, %v", err) } - return m + return aliases } func (lib *library) Scan() { diff --git a/pkg/games/library_test.go b/pkg/games/library_test.go index a0d250cf..dc85fbbf 100644 --- a/pkg/games/library_test.go +++ b/pkg/games/library_test.go @@ -1,6 +1,9 @@ package games import ( + "os" + "path/filepath" + "reflect" "testing" "github.com/giongto35/cloud-game/v3/pkg/config" @@ -60,6 +63,52 @@ func TestLibraryScan(t *testing.T) { } } +func TestAliasFileMaybe(t *testing.T) { + lib := &library{ + config: libConf{ + aliasFile: "alias", + path: os.TempDir(), + }, + log: logger.NewConsole(false, "w", false), + } + + contents := "a=b\nc=d\n" + + path := filepath.Join(lib.config.path, lib.config.aliasFile) + if err := os.WriteFile(path, []byte(contents), 0644); err != nil { + t.Error(err) + } + defer func() { + if err := os.RemoveAll(path); err != nil { + t.Error(err) + } + }() + + want := map[string]string{} + want["a"] = "b" + want["c"] = "d" + + aliases := lib.AliasFileMaybe() + + if !reflect.DeepEqual(aliases, want) { + t.Errorf("AliasFileMaybe() = %v, want %v", aliases, want) + } +} + +func TestAliasFileMaybeNot(t *testing.T) { + lib := &library{ + config: libConf{ + path: os.TempDir(), + }, + log: logger.NewConsole(false, "w", false), + } + + aliases := lib.AliasFileMaybe() + if aliases != nil { + t.Errorf("should be nil, but %v", aliases) + } +} + func Benchmark(b *testing.B) { log := logger.Default() logger.SetGlobalLevel(logger.Disabled)