Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add game aliases #473

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type (
Wid string `json:"wid"`
}
AppMeta struct {
Alias string `json:"alias,omitempty"`
Title string `json:"title"`
System string `json:"system"`
}
Expand Down
1 change: 1 addition & 0 deletions pkg/api/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type (
PlayerIndex int `json:"player_index"`
}
GameInfo struct {
Alias string `json:"alias"`
Base string `json:"base"`
Name string `json:"name"`
Path string `json:"path"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/coordinator/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (h *Hub) handleUserConnection() http.HandlerFunc {
apps := h.launcher.GetAppNames()
list := make([]api.AppMeta, len(apps))
for i := range apps {
list[i] = api.AppMeta{Title: apps[i].Name, System: apps[i].System}
list[i] = api.AppMeta{Alias: apps[i].Alias, Title: apps[i].Name, System: apps[i].System}
}
user.InitSession(worker.Id().String(), h.conf.Webrtc.IceServers, list)
log.Info().Str(logger.DirectionField, logger.MarkPlus).Msgf("user %s", user.Id())
Expand Down
3 changes: 2 additions & 1 deletion pkg/games/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Launcher interface {
}

type AppMeta struct {
Alias string
Base string
Name string
Path string
Expand All @@ -39,7 +40,7 @@ func (gl GameLauncher) ExtractAppNameFromUrl(name string) string { return Extrac

func (gl GameLauncher) GetAppNames() (apps []AppMeta) {
for _, game := range gl.lib.GetAll() {
apps = append(apps, AppMeta{Name: game.Name, System: game.System})
apps = append(apps, AppMeta{Alias: game.Alias, Name: game.Name, System: game.System})
}
return
}
Expand Down
60 changes: 57 additions & 3 deletions pkg/games/library.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package games

import (
"bufio"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
Expand Down Expand Up @@ -57,6 +60,7 @@ type WithEmulatorInfo interface {
}

type GameMetadata struct {
Alias string
Base string
Name string // the display name of the game
Path string // the game path relative to the library base path
Expand Down Expand Up @@ -123,6 +127,37 @@ func (lib *library) FindGameByName(name string) GameMetadata {
return game
}

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) {
return nil
}

// read
f, err := os.Open(path)
if err != nil {
lib.log.Error().Msgf("couldn't open alias file, %v", err)
return nil
}
defer func() { _ = f.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
}
}
if err = s.Err(); err != nil {
lib.log.Error().Msgf("alias file read error, %v", err)
}

return m
}

func (lib *library) Scan() {
if !lib.hasSource {
lib.log.Info().Msg("Lib scan... skipped (no source)")
Expand All @@ -142,6 +177,14 @@ func (lib *library) Scan() {

lib.log.Debug().Msg("Lib scan... started")

// game name aliases
aliases := lib.AliasFileMaybe()

if aliases != nil {
lib.log.Debug().Msgf("Lib game alises found")
lib.log.Debug().Msgf(">>> %v", aliases)
}

start := time.Now()
var games []GameMetadata
dir := lib.config.path
Expand All @@ -155,6 +198,13 @@ func (lib *library) Scan() {

meta.System = lib.emuConf.GetEmulator(meta.Type, meta.Path)

if aliases != nil {
k, ok := aliases[meta.Name]
if ok {
meta.Alias = k
}
}

if _, ok := lib.config.ignored[meta.Name]; !ok {
games = append(games, meta)
}
Expand Down Expand Up @@ -247,12 +297,12 @@ func (lib *library) isExtAllowed(path string) bool {
// getMetadata returns game info from a path
func getMetadata(path string, basePath string) GameMetadata {
name := filepath.Base(path)
ext := strings.ToLower(filepath.Ext(name))
ext := filepath.Ext(name)
relPath, _ := filepath.Rel(basePath, path)

return GameMetadata{
Name: strings.TrimSuffix(name, ext),
Type: ext[1:],
Type: strings.ToLower(ext[1:]),
Path: relPath,
}
}
Expand All @@ -270,7 +320,11 @@ func (lib *library) dumpLibrary() {

for _, k := range keys {
game := lib.games[k]
gameList.WriteString(fmt.Sprintf(" %7s %s (%s)\n", game.System, game.Name, game.Path))
alias := game.Alias
if alias != "" {
alias = fmt.Sprintf("[%s] ", game.Alias)
}
gameList.WriteString(fmt.Sprintf(" %7s %s %s(%s)\n", game.System, game.Name, alias, game.Path))
}

lib.log.Debug().Msgf("Lib dump\n"+
Expand Down
2 changes: 1 addition & 1 deletion web/js/gameList.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ const ui = (() => {
const render = () => {
rootEl.innerHTML = games.list.map(game =>
`<div class="menu-item">` +
`<div><span>${game.title}</span></div>` +
`<div><span>${game.alias ? game.alias : game.title}</span></div>` +
`<div class="menu-item__info">${game.system}</div>` +
`</div>`)
.join('')
Expand Down
Loading