Skip to content

Commit

Permalink
Fixing some edge cases with mister tracker (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
wizzomafizzo authored Jan 5, 2025
1 parent 05d4357 commit 9b34020
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 46 deletions.
28 changes: 25 additions & 3 deletions pkg/platforms/mister/launchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ import (
)

func launch(cfg *config.Instance, path string) error {
return mister.LaunchGenericFile(UserConfigToMrext(cfg), path)
err := mister.LaunchGenericFile(UserConfigToMrext(cfg), path)
if err != nil {
return err
}

log.Debug().Msgf("setting active game: %s", path)
return mister.SetActiveGame(path)
}

func launchSinden(
Expand All @@ -30,12 +36,20 @@ func launchSinden(
if err != nil {
return err
}

sn := *s
sn.Rbf = "_Sinden/" + rbfName + "_Sinden"
sn.SetName = rbfName + "_Sinden"
sn.SetNameSameDir = true

log.Debug().Str("rbf", sn.Rbf).Msgf("launching Sinden: %v", sn)
return mister.LaunchGame(UserConfigToMrext(cfg), sn, path)

err = mister.LaunchGame(UserConfigToMrext(cfg), sn, path)
if err != nil {
return err
}

return mister.SetActiveGame(path)
}
}

Expand All @@ -48,10 +62,18 @@ func launchAltCore(
if err != nil {
return err
}

sn := *s
sn.Rbf = rbfPath

log.Debug().Str("rbf", sn.Rbf).Msgf("launching alt core: %v", sn)
return mister.LaunchGame(UserConfigToMrext(cfg), sn, path)

err = mister.LaunchGame(UserConfigToMrext(cfg), sn, path)
if err != nil {
return err
}

return mister.SetActiveGame(path)
}
}

Expand Down
77 changes: 35 additions & 42 deletions pkg/platforms/mister/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ZaparooProject/zaparoo-core/pkg/assets"
config2 "github.com/ZaparooProject/zaparoo-core/pkg/config"
"github.com/ZaparooProject/zaparoo-core/pkg/database/gamesdb"
"github.com/ZaparooProject/zaparoo-core/pkg/platforms"
utils2 "github.com/ZaparooProject/zaparoo-core/pkg/utils"
"os"
"path/filepath"
Expand Down Expand Up @@ -41,7 +42,7 @@ type Tracker struct {
Config *config.UserConfig
mu sync.Mutex
ns chan<- models.Notification
pl *Platform
pl platforms.Platform
cfg *config2.Instance
ActiveCore string
ActiveSystem string
Expand Down Expand Up @@ -90,7 +91,7 @@ func generateNameMap() []NameMapping {
return nameMap
}

func NewTracker(cfg *config.UserConfig, ns chan<- models.Notification, pl *Platform, cfg2 *config2.Instance) (*Tracker, error) {
func NewTracker(cfg *config.UserConfig, ns chan<- models.Notification, pl platforms.Platform, cfg2 *config2.Instance) (*Tracker, error) {
log.Info().Msg("starting tracker")

nameMap := generateNameMap()
Expand Down Expand Up @@ -121,9 +122,8 @@ func (tr *Tracker) ReloadNameMap() {
tr.NameMap = nameMap
}

func (tr *Tracker) LookupCoreName(name string, path string) NameMapping {
log.Debug().Msgf("looking up name: %s", name)
log.Debug().Msgf("file path: %s", path)
func (tr *Tracker) LookupCoreName(name string) (NameMapping, bool) {
log.Debug().Msgf("looking up core name: %s", name)

for _, mapping := range tr.NameMap {
if len(mapping.CoreName) != len(name) {
Expand All @@ -134,25 +134,20 @@ func (tr *Tracker) LookupCoreName(name string, path string) NameMapping {
continue
} else if mapping.ArcadeName != "" {
log.Debug().Msgf("arcade name: %s", mapping.ArcadeName)
return mapping
return mapping, true
}

sys, err := games.BestSystemMatch(tr.Config, path)
_, err := gamesdb.LookupSystem(name)
if err != nil {
log.Debug().Msgf("error finding system for game %s, %s: %s", name, path, err)
continue
}

if sys.Id == "" {
log.Debug().Msgf("no system found for game: %s, %s", name, path)
log.Error().Msgf("error getting system %s", err)
continue
}

log.Info().Msgf("found mapping: %s -> %s", name, mapping.Name)
return mapping
return mapping, true
}

return NameMapping{}
return NameMapping{}, false
}

func (tr *Tracker) stopCore() bool {
Expand Down Expand Up @@ -193,32 +188,31 @@ func (tr *Tracker) LoadCore() {
}
}

if coreName != tr.ActiveCore {
tr.stopCore()

tr.ActiveCore = coreName
if coreName == tr.ActiveCore {
return
}

if coreName == config.MenuCore {
log.Debug().Msg("in menu, stopping game")
tr.stopGame()
return
}
tr.stopCore()
tr.ActiveCore = coreName

result := tr.LookupCoreName(coreName, tr.ActiveGamePath)
if result != (NameMapping{}) {
if result.ArcadeName != "" {
err := mister.SetActiveGame(result.CoreName)
if err != nil {
log.Warn().Err(err).Msg("error setting active game")
}
if coreName == config.MenuCore {
log.Debug().Msg("in menu, stopping game")
tr.stopGame()
return
}

tr.ActiveGameId = coreName
tr.ActiveGameName = result.ArcadeName
tr.ActiveGamePath = "" // TODO: any way to find this?
tr.ActiveSystem = ArcadeSystem
tr.ActiveSystemName = ArcadeSystem
}
// set arcade core details
if result, ok := tr.LookupCoreName(coreName); ok && result.ArcadeName != "" {
err := mister.SetActiveGame(result.CoreName)
if err != nil {
log.Warn().Err(err).Msg("error setting active game")
}

tr.ActiveGameId = coreName
tr.ActiveGameName = result.ArcadeName
tr.ActiveGamePath = "" // no way to find mra path from CORENAME
tr.ActiveSystem = ArcadeSystem
tr.ActiveSystemName = ArcadeSystem
}
}

Expand All @@ -243,14 +237,13 @@ func (tr *Tracker) loadGame() {
log.Error().Msgf("error getting active game: %s", err)
tr.stopGame()
return
} else if !filepath.IsAbs(activeGame) {
// arcade game, ignore handling
// TODO: will this work ok long term?
return
} else if activeGame == "" {
log.Debug().Msg("active game is empty, stopping game")
tr.stopGame()
return
} else if !filepath.IsAbs(activeGame) {
log.Debug().Msgf("active game is not absolute, assuming arcade: %s", activeGame)
return
}

path := mister.ResolvePath(activeGame)
Expand Down Expand Up @@ -463,7 +456,7 @@ func StartFileWatch(tr *Tracker) (*fsnotify.Watcher, error) {
return watcher, nil
}

func StartTracker(cfg config.UserConfig, ns chan<- models.Notification, cfg2 *config2.Instance, pl *Platform) (*Tracker, func() error, error) {
func StartTracker(cfg config.UserConfig, ns chan<- models.Notification, cfg2 *config2.Instance, pl platforms.Platform) (*Tracker, func() error, error) {
tr, err := NewTracker(&cfg, ns, pl, cfg2)
if err != nil {
log.Error().Msgf("error creating tracker: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/platforms/mistex/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (p *Platform) StartPre(_ *config.Instance) error {
}

func (p *Platform) StartPost(cfg *config.Instance, ns chan<- models.Notification) error {
tr, stopTr, err := mister.StartTracker(*mister.UserConfigToMrext(cfg), ns)
tr, stopTr, err := mister.StartTracker(*mister.UserConfigToMrext(cfg), ns, cfg, p)
if err != nil {
return err
}
Expand Down

0 comments on commit 9b34020

Please sign in to comment.