Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
sergystepanov committed Aug 30, 2023
1 parent 8a3ad08 commit 70c8439
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 38 deletions.
49 changes: 27 additions & 22 deletions pkg/worker/caged/libretro/caged.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

type Caged struct {
Emulator

base *Frontend // maintains the root for mad embedding
conf CagedConf
log *logger.Logger
w, h int
Expand All @@ -33,46 +35,49 @@ func (c *Caged) Init() error {
return nil
}

func (c *Caged) PreLoad(uid string, saveClose bool, storage cloud.Storage, rec bool, recUser string, recGame string) {
nan, err := NewFrontend(c.conf.Emulator, c.log)
func (c *Caged) ReloadFrontend() {
frontend, err := NewFrontend(c.conf.Emulator, c.log)
if err != nil {
c.log.Fatal().Err(err).Send()
}
nan.SaveOnClose = saveClose
nan.SetMainSaveName(uid)
c.Emulator = frontend
c.base = frontend
}

var nano Emulator = nan
func (c *Caged) Load(game games.GameMetadata, path string) error {
c.Emulator.LoadCore(game.System)
if err := c.Emulator.LoadGame(game.FullPath(path)); err != nil {
return err
}
w, h := c.ViewportCalc()
c.SetViewport(w, h, c.conf.Emulator.Scale)

return nil
}

func (c *Caged) EnableRecording(nowait bool, user string, game string) {
if c.conf.Recording.Enabled {
nan.DisableCanvasPool = true
nano = WithRecording(nano, rec, recUser, recGame, c.conf.Recording, c.log)
// !to fix races with canvas pool when recording
c.base.DisableCanvasPool = true
c.Emulator = WithRecording(c.Emulator, nowait, user, game, c.conf.Recording, c.log)
}
}

func (c *Caged) EnableCloudStorage(uid string, storage cloud.Storage) {
if storage != nil {
wc, err := WithCloud(nano, uid, storage)
wc, err := WithCloud(c.Emulator, uid, storage)
if err != nil {
c.log.Error().Err(err).Msgf("couldn't init %v", wc.HashPath())
} else {
c.log.Info().Msgf("cloud state %v has been initialized", wc.HashPath())
nano = wc
c.Emulator = wc
}
}

c.Emulator = nano
}

func (c *Caged) Load(game games.GameMetadata, path string) error {
c.Emulator.LoadCore(game.System)
if err := c.Emulator.LoadGame(game.FullPath(path)); err != nil {
return err
}
w, h := c.ViewportCalc()
c.SetViewport(w, h, c.conf.Emulator.Scale)

return nil
}

func (c *Caged) AudioSampleRate() int { return c.Emulator.AudioSampleRate() }
func (c *Caged) ViewportSize() (int, int) { return c.Emulator.ViewportSize() }
func (c *Caged) Start() { go c.Emulator.Start() }
func (c *Caged) SetSaveOnClose(v bool) { c.base.SaveOnClose = v }
func (c *Caged) SetSessionId(name string) { c.base.SetSessionId(name) }
func (c *Caged) Close() { c.Emulator.Close() }
7 changes: 4 additions & 3 deletions pkg/worker/caged/libretro/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type Emulator interface {
ViewportCalc() (nw int, nh int)
ViewportSize() (w, h int)
RestoreGameState() error
// SetMainSaveName sets distinct name for saves naming
SetMainSaveName(name string)
// SetSessionId sets distinct name for the game session (in order to save/load it later)
SetSessionId(name string)
SaveGameState() error
// HashPath returns the path emulator will save state to
HashPath() string
Expand Down Expand Up @@ -256,6 +256,7 @@ func (f *Frontend) linkNano(nano *nanoarch.Nanoarch) {
func (f *Frontend) Start() {
f.log.Debug().Msgf("Frontend start")

f.done = make(chan struct{})
f.nano.LastFrameTime = time.Now().UnixNano()
defer f.Shutdown()

Expand Down Expand Up @@ -304,7 +305,7 @@ func (f *Frontend) IsPortrait() bool { return f.nano.IsPortrait() }
func (f *Frontend) SaveGameState() error { return f.Save() }
func (f *Frontend) Scale(factor int) { w, h := f.ViewportSize(); f.SetViewport(w, h, factor) }
func (f *Frontend) SetAudio(ff func(*GameAudio)) { f.onAudio = ff }
func (f *Frontend) SetMainSaveName(name string) { f.storage.SetMainSaveName(name) }
func (f *Frontend) SetSessionId(name string) { f.storage.SetMainSaveName(name) }
func (f *Frontend) SetViewport(width int, height int, scale int) {
f.mu.Lock()
f.vw, f.vh = width, height
Expand Down
7 changes: 6 additions & 1 deletion pkg/worker/coordinatorhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ func (c *coordinator) HandleGameStart(rq api.StartGameRequest[com.Uid], w *Worke

// start the emulator
app := emulator(w.mana.Get(caged.Libretro))
app.PreLoad(uid, true, w.storage, rq.Record, rq.RecordUser, rq.Game.Name)
app.ReloadFrontend()
app.SetSessionId(uid)
app.SetSaveOnClose(true)
app.EnableCloudStorage(uid, w.storage)
app.EnableRecording(rq.Record, rq.RecordUser, rq.Game.Name)

game := games.GameMetadata(rq.Game)
if err := app.Load(game, w.conf.Worker.Library.BasePath); err != nil {
c.log.Error().Err(err).Msgf("couldn't load the game %v", game)
Expand Down
2 changes: 1 addition & 1 deletion pkg/worker/recorder/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (r *Recording) Stop() (err error) {

func (r *Recording) Set(enable bool, user string) {
r.Lock()
r.meta.UserName = user
if !r.enabled && enable {
r.Unlock()
r.Start()
Expand All @@ -149,7 +150,6 @@ func (r *Recording) Set(enable bool, user string) {
}
}
r.enabled = enable
r.meta.UserName = user
r.Unlock()
}

Expand Down
4 changes: 0 additions & 4 deletions pkg/worker/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ func (r *Room) initAudio(srcHz int, conf config.Audio) {
}
frameDur := time.Duration(conf.Frame) * time.Millisecond

pa := emulator(r.app).Audio()
emulator(r.app).SetAudio(func(raw *libretro.GameAudio) {
pa(raw)
buf.write(*raw.Data, func(pcm samples) {
data, err := opus_.Encode(pcm)
audioPool.Put((*[]int16)(&pcm))
Expand Down Expand Up @@ -129,9 +127,7 @@ func (r *Room) initVideo(w, h int, conf config.Video) {

r.vEncoder = encoder.NewVideoEncoder(enc, w, h, conf.Concurrency, r.log)

pv := emulator(r.app).Video()
emulator(r.app).SetVideo(func(raw *libretro.GameFrame) {
pv(raw)
data := r.vEncoder.Encode(raw.Data.RGBA)
if data == nil {
r.log.Warn().Msgf("no data after video encoding")
Expand Down
11 changes: 4 additions & 7 deletions pkg/worker/room_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,13 @@ func getRoomMock(cfg roomMockConfig) roomMock {
}

emu := emulator(manager.Get("libretro"))

emu.PreLoad(roomId, false, nil, false, "", "")

game := cfg.game
if err := emu.Load(game, conf.Worker.Library.BasePath); err != nil {
l.Fatal().Err(err).Msgf("couldn't load the game %v", game)
emu.ReloadFrontend()
emu.SetSessionId(roomId)
if err := emu.Load(cfg.game, conf.Worker.Library.BasePath); err != nil {
l.Fatal().Err(err).Msgf("couldn't load the game %v", cfg.game)
}

room := NewRoom(roomId, emu, conf, l)

if !cfg.dontStartEmulator {
room.StartApp()
}
Expand Down

0 comments on commit 70c8439

Please sign in to comment.