Skip to content

Commit

Permalink
Merge pull request #275 from autobleem/feature/add_RA_history_playlist
Browse files Browse the repository at this point in the history
[feature/add_RA_history_playlist]: add RA history playlist
  • Loading branch information
ChocoNutDancer committed Dec 15, 2019
2 parents 0a85ced + 3bcefdd commit 5d58e1b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/code/launcher/gui_launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ void GuiLauncher::switchSet(int newSet, bool noForce) {
getGames_SET_APPS(&gamesList);
}

sort(gamesList.begin(), gamesList.end(), sortByTitle);
if (!(currentSet == SET_RETROARCH && currentRAPlaylistName == raIntegrator->historyDisplayName))
sort(gamesList.begin(), gamesList.end(), sortByTitle);
cout << "Games Sorted" << endl;
// copy the gamesList into the carousel
carouselGames.clear();
Expand Down
65 changes: 64 additions & 1 deletion src/code/launcher/ra_integrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ string RAIntegrator::findFavoritesPlaylistPath() {
return "";
}

//********************
// RAIntegrator::findHistoryPlaylistPath
//********************
// returns "" if not found
string RAIntegrator::findHistoryPlaylistPath() {
string defaultPath {Env::getPathToRetroarchDir() + sep + "content_history.lpl"};
if (DirEntry::exists(defaultPath))
return defaultPath;
else
return "";
}

//********************
// RAIntegrator::reloadFavorites
//********************
Expand All @@ -125,6 +137,10 @@ void RAIntegrator::reloadFavorites() {
}
}

// remove any games in the favorites playlist that no longer exist
auto it = remove_if(begin(favGames), end(favGames), [&] (PsGamePtr &game)
{ return game->title == "" || game->db_name == ""; });
favGames.erase(it, end(favGames));

std::tuple<bool,int> restuple = playlistNameToIndex(favoritesDisplayName);
bool found = std::get<0>(restuple);
Expand All @@ -141,6 +157,51 @@ void RAIntegrator::reloadFavorites() {
}
}

//********************
// RAIntegrator::reloadHistory
//********************
// after running RA history may have been added or removed
void RAIntegrator::reloadHistory() {
string histPath = findHistoryPlaylistPath();
if (histPath != "") {
auto histGames = readGamesFromPlaylistFile(histPath);

// when RA adds a game to history for some reason the title, crc32, and db_name fields are empty.
// we need the title and db_path to compute the path to the boxart.
// find the original game in the playlists and copy the title and db_name to the history entry for the same game
for (auto & histGame : histGames) {
for (auto const & info : playlistInfos) {
if (info.displayName != historyDisplayName) {
auto it = find_if(begin(info.psGames), end(info.psGames), [&] (PsGamePtr game) { return (game->image_path == histGame->image_path); } );
if (it != end(info.psGames)) {
histGame->title = (*it)->title;
histGame->db_name = (*it)->db_name;
break;
}
}
}
}

// remove any games in the history playlist that no longer exist
auto it = remove_if(begin(histGames), end(histGames), [&] (PsGamePtr &game)
{ return game->title == "" || game->db_name == ""; });
histGames.erase(it, end(histGames));

std::tuple<bool,int> restuple = playlistNameToIndex(historyDisplayName);
bool found = std::get<0>(restuple);
int index = std::get<1>(restuple);
if (found) {
// the prior history is in the list. modify the existing entry
RAPlaylistInfo & hist = playlistInfos[index];
hist.path = histPath;
hist.psGames = histGames;
} else {
// the history list didn't exist before and isn't in the list. add a new entry.
playlistInfos.emplace_back(historyDisplayName, histPath, histGames);
}
}
}

//********************
// RAIntegrator::readGamesFromAllPlaylists
//********************
Expand All @@ -149,6 +210,7 @@ void RAIntegrator::readGamesFromAllPlaylists() {
assert(playlistInfos.size() == 0);
if (playlistInfos.size() != 0) {
reloadFavorites(); // playlists already read. only need to update favorites in case changed in RA.
reloadHistory(); // playlists already read. only need to update history in case changed in RA.
return;
}

Expand All @@ -165,7 +227,7 @@ void RAIntegrator::readGamesFromAllPlaylists() {
playlistNames.emplace_back(entry.name);
}

// sort the playlist names and if any favorites add favorites at the end
// sort the playlist names and if any favorites or history add them at the end
sort(begin(playlistNames), end(playlistNames));

for (auto & playlistName : playlistNames) {
Expand All @@ -183,6 +245,7 @@ void RAIntegrator::readGamesFromAllPlaylists() {
cout << "Invalid Playlist: " << playlistName << endl;
}
reloadFavorites(); // since it isn't already in the list, reloadFavorites() will add favorites at the end
reloadHistory(); // since it isn't already in the list, reloadHistory() will add history at the end
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/code/launcher/ra_integrator.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,17 @@ class RAIntegrator {
set<string> databases;

std::vector<RAPlaylistInfo> playlistInfos;
//std::string favoritesFileName;
std::string favoritesDisplayName { "Favorites" };
std::string historyDisplayName { "History" };
std::tuple<bool,int> playlistNameToIndex(const std::string& name); // returns true if name found, and the index

PsGames readGamesFromPlaylistFile(const std::string& path); // read one lpl file
void readGamesFromAllPlaylists(); // reads all the playlist info into playlistInfos

std::string findFavoritesPlaylistPath(); // returns "" if not found
std::string findHistoryPlaylistPath(); // returns "" if not found
void reloadFavorites(); // after running RA, favorites may have been added or removed
void reloadHistory(); // after running RA, history may have changed

vector<string> getPlaylists();
PsGames getGames(string playlist);
Expand Down
18 changes: 10 additions & 8 deletions src/code/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,16 @@ int main(int argc, char *argv[]) {
interceptor->memcardOut(gui->runningGame);
delete (interceptor);

bool reloadFav {false};
bool reloadFavAndHist {false};
if (gui->runningGame->foreign)
reloadFav = true;
reloadFavAndHist = true;
else if (gui->emuMode != EMU_PCSX)
reloadFav = true;
reloadFavAndHist = true;

if (reloadFav) {
if (reloadFavAndHist) {
auto ra = RAIntegrator::getInstance();
ra->reloadFavorites(); // they could have changed
ra->reloadHistory(); // they could have changed
}
gui->runningGame.reset(); // replace with shared_ptr pointing to nullptr
gui->startingGame = false;
Expand Down Expand Up @@ -329,15 +330,16 @@ int main(int argc, char *argv[]) {
interceptor->memcardOut(gui->runningGame);
delete (interceptor);

bool reloadFav {false};
bool reloadFavHist {false};
if (gui->runningGame->foreign)
reloadFav = true;
reloadFavHist = true;
else if (gui->emuMode != EMU_PCSX)
reloadFav = true;
reloadFavHist = true;

if (reloadFav) {
if (reloadFavHist) {
auto ra = RAIntegrator::getInstance();
ra->reloadFavorites(); // they could have changed
ra->reloadHistory(); // they could have changed
}

SDL_InitSubSystem(SDL_INIT_JOYSTICK);
Expand Down

0 comments on commit 5d58e1b

Please sign in to comment.