Skip to content

Commit

Permalink
libretro: handle m3u format as gamepath.
Browse files Browse the repository at this point in the history
Implement retro_set_initial_image.

Signed-off-by: Andrea Odetti <mariofutire@gmail.com>
  • Loading branch information
audetto committed Nov 28, 2021
1 parent 30dbfad commit 02740ce
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 18 deletions.
57 changes: 52 additions & 5 deletions source/frontends/libretro/diskcontrol.cpp
Expand Up @@ -7,6 +7,8 @@
#include "Disk.h"
#include "Harddisk.h"

#include <fstream>

namespace ra2
{

Expand All @@ -17,11 +19,6 @@ namespace ra2

bool DiskControl::insertDisk(const std::string & filename)
{
if (filename.empty())
{
return false;
}

if (insertFloppyDisk(filename))
{
myIndex = 0;
Expand All @@ -34,6 +31,45 @@ namespace ra2
return insertHardDisk(filename);
}

bool DiskControl::insertPlaylist(const std::string & filename)
{
std::ifstream playlist(filename);
if (!playlist)
{
return false;
}

myImages.clear();

std::string line;
while (std::getline(playlist, line))
{
// should we trim initial spaces?
if (!line.empty() && line[0] != '#')
{
myImages.push_back(line);
}
}

// if we have an initial disk image, let's try to honour it
if (!ourInitialPath.empty() && ourInitialIndex < myImages.size() && myImages[ourInitialIndex] == ourInitialPath)
{
myIndex = ourInitialIndex;
// do we need to reset for next time?
ourInitialPath.clear();
ourInitialIndex = 0;
}
else
{
// insert the first image
myIndex = 0;
}

// this is safe even if myImages is empty
myEjected = true;
return setEjectedState(false);
}

bool DiskControl::insertFloppyDisk(const std::string & filename) const
{
CardManager & cardManager = GetCardMgr();
Expand Down Expand Up @@ -206,4 +242,15 @@ namespace ra2
}
}

unsigned DiskControl::ourInitialIndex = 0;
std::string DiskControl::ourInitialPath;
void DiskControl::setInitialPath(unsigned index, const char *path)
{
if (path && *path)
{
ourInitialIndex = index;
ourInitialPath = path;
}
}

}
7 changes: 7 additions & 0 deletions source/frontends/libretro/diskcontrol.h
Expand Up @@ -21,11 +21,15 @@ namespace ra2
bool removeImageIndex(size_t index);
bool addImageIndex();

// these 2 functions update the images for the Disc Control Interface
bool insertDisk(const std::string & filename);
bool insertPlaylist(const std::string & filename);

bool getImagePath(unsigned index, char *path, size_t len) const;
bool getImageLabel(unsigned index, char *label, size_t len) const;

static void setInitialPath(unsigned index, const char *path);

private:
std::vector<std::string> myImages;

Expand All @@ -36,6 +40,9 @@ namespace ra2
bool insertHardDisk(const std::string & filename) const;

void checkState() const;

static unsigned ourInitialIndex;
static std::string ourInitialPath;
};

}
6 changes: 0 additions & 6 deletions source/frontends/libretro/game.cpp
Expand Up @@ -258,12 +258,6 @@ namespace ra2
return myMouse[i].position;
}

bool Game::loadGame(const std::string & path)
{
const bool ok = myDiskControl.insertDisk(path);
return ok;
}

bool Game::loadSnapshot(const std::string & path)
{
common2::setSnapshotFilename(path, true);
Expand Down
1 change: 0 additions & 1 deletion source/frontends/libretro/game.h
Expand Up @@ -20,7 +20,6 @@ namespace ra2
Game();
~Game();

bool loadGame(const std::string & path);
bool loadSnapshot(const std::string & path);

void executeOneFrame();
Expand Down
25 changes: 19 additions & 6 deletions source/frontends/libretro/libretro.cpp
Expand Up @@ -85,8 +85,8 @@ namespace
bool retro_set_initial_image(unsigned index, const char *path)
{
ra2::log_cb(RETRO_LOG_INFO, "RA2: %s (%d) = %s\n", __FUNCTION__, index, path);
// I have not been able to trigger this yet.
return false;
ra2::DiskControl::setInitialPath(index, path);
return true;
}

bool retro_get_image_path(unsigned index, char *path, size_t len)
Expand Down Expand Up @@ -304,16 +304,29 @@ bool retro_load_game(const retro_game_info *info)
std::unique_ptr<ra2::Game> game(new ra2::Game());

const std::string snapshotEnding = ".aws.yaml";
const std::string gamePath = info->path;
const std::string playlistEnding = ".m3u";

bool ok;
if (endsWith(gamePath, snapshotEnding))

if (info->path && *info->path)
{
ok = game->loadSnapshot(gamePath);
const std::string gamePath = info->path;
if (endsWith(gamePath, snapshotEnding))
{
ok = game->loadSnapshot(gamePath);
}
else if (endsWith(gamePath, playlistEnding))
{
ok = game->getDiskControl().insertPlaylist(gamePath);
}
else
{
ok = game->getDiskControl().insertDisk(gamePath);
}
}
else
{
ok = game->loadGame(gamePath);
ok = false;
}

ra2::log_cb(RETRO_LOG_INFO, "Game path: %s -> %d\n", info->path, ok);
Expand Down

0 comments on commit 02740ce

Please sign in to comment.