Skip to content

Commit

Permalink
Fix multiple profile playlists/favourites/goals
Browse files Browse the repository at this point in the history
  • Loading branch information
nico-abram committed Feb 10, 2018
1 parent fd6b34d commit 5594f6c
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 36 deletions.
6 changes: 3 additions & 3 deletions src/DBProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void DBProfile::LoadPlayLists(SQLite::Database* db)
"INNER JOIN songs ON songs.id = charts.songid "
"INNER JOIN chartkeys ON charts.chartkeyid = chartkeys.id "
"ORDER BY playlists.name, chartkeys.chartkey, chartplaylists.rate");
auto& pls = SONGMAN->allplaylists;
auto& pls = loadingProfile->allplaylists;

Playlist *tmp;

Expand Down Expand Up @@ -653,9 +653,9 @@ void DBProfile::SavePlayLists(SQLite::Database* db, const Profile* profile) cons
"CONSTRAINT fk_courserunid FOREIGN KEY (courserunid) REFERENCES courseruns(id))");


auto& pls = SONGMAN->allplaylists;
auto& pls = profile->allplaylists;
if (!pls.empty()) {
FOREACHM(string, Playlist, pls, pl)
FOREACHM_CONST(string, Playlist, pls, pl)
{
if (pl->first != "" && pl->first != "Favorites")
{
Expand Down
2 changes: 1 addition & 1 deletion src/MusicWheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ bool MusicWheel::SelectSong( const Song *p )
SetOpenSection( from[i]->m_sText );

// skip any playlist groups
if (!SONGMAN->allplaylists.count(GetExpandedSectionName()))
if (!SONGMAN->GetPlaylists().count(GetExpandedSectionName()))
break;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/Profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
#include "LuaReference.h"
#include "XMLProfile.h"
#include "DBProfile.h"
#include "SongManager.h"
#include "arch/LoadingWindow/LoadingWindow.h"

#include <unordered_map>

class XNode;
struct lua_State;
class Character;
struct Playlist;

// Current file versions
extern const RString STATS_XML;
Expand Down Expand Up @@ -186,6 +188,8 @@ class Profile
ProfileType m_Type{ProfileType_Normal};
// Profiles of the same type and priority are sorted by dir name.
int m_ListPriority{0};
// Profile Playlists
map<string, Playlist> allplaylists;

// Editable data
RString m_sDisplayName;
Expand Down
4 changes: 2 additions & 2 deletions src/ScreenGameplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ void ScreenGameplay::InitSongQueues()
FOREACH_EnabledPlayerInfo(m_vPlayerInfo, pi)
pi->m_vpStepsQueue.clear();

Playlist& pl = SONGMAN->allplaylists[SONGMAN->playlistcourse];
Playlist& pl = SONGMAN->GetPlaylists()[SONGMAN->playlistcourse];
FOREACH(Chart, pl.chartlist, ch) {
m_apSongsQueue.emplace_back(ch->songptr);
FOREACH_EnabledPlayerInfo(m_vPlayerInfo, pi)
Expand Down Expand Up @@ -2260,7 +2260,7 @@ void ScreenGameplay::HandleScreenMessage( const ScreenMessage SM )
MESSAGEMAN->Broadcast(msg);

if (GAMESTATE->IsPlaylistCourse()) {
SONGMAN->allplaylists[SONGMAN->playlistcourse].courseruns.emplace_back(playlistscorekeys);
SONGMAN->GetPlaylists()[SONGMAN->playlistcourse].courseruns.emplace_back(playlistscorekeys);
}

TweenOffScreen();
Expand Down
13 changes: 9 additions & 4 deletions src/ScreenSelectMusic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ void ScreenSelectMusic::BeginScreen()
g_ScreenStartedLoadingAt.Touch();
m_timerIdleComment.GetDeltaTime();


SONGMAN->MakeSongGroupsFromPlaylists();
SONGMAN->SetFavoritedStatus(PROFILEMAN->GetProfile(PLAYER_1)->FavoritedCharts);
if (CommonMetrics::AUTO_SET_STYLE)
{
GAMESTATE->SetCompatibleStylesForPlayers();
Expand Down Expand Up @@ -477,6 +480,8 @@ bool ScreenSelectMusic::Input(const InputEventPlus &input)
if (!fav_me_biatch->IsFavorited()) {
fav_me_biatch->SetFavorited(true);
pProfile->AddToFavorites(GAMESTATE->m_pCurSteps[PLAYER_1]->GetChartKey());
pProfile->allplaylists.erase("Favorites");
SONGMAN->MakePlaylistFromFavorites(pProfile->FavoritedCharts, pProfile->allplaylists);
}
else {
fav_me_biatch->SetFavorited(false);
Expand Down Expand Up @@ -541,10 +546,10 @@ bool ScreenSelectMusic::Input(const InputEventPlus &input)
}
else if (bHoldingCtrl && c == 'A' && m_MusicWheel.IsSettled() && input.type == IET_FIRST_PRESS)
{
if (SONGMAN->allplaylists.empty())
if (SONGMAN->GetPlaylists().empty())
return true;

SONGMAN->allplaylists[SONGMAN->activeplaylist].AddChart(GAMESTATE->m_pCurSteps[PLAYER_1]->GetChartKey());
SONGMAN->GetPlaylists()[SONGMAN->activeplaylist].AddChart(GAMESTATE->m_pCurSteps[PLAYER_1]->GetChartKey());
MESSAGEMAN->Broadcast("DisplaySinglePlaylist");
SCREENMAN->SystemMessage(ssprintf("Added chart: %s to playlist: %s", GAMESTATE->m_pCurSong->GetDisplayMainTitle().c_str(), SONGMAN->activeplaylist.c_str()));
return true;
Expand Down Expand Up @@ -1176,7 +1181,7 @@ void ScreenSelectMusic::HandleScreenMessage(const ScreenMessage SM)
Playlist pl;
pl.name = ScreenTextEntry::s_sLastAnswer;
if (pl.name != "") {
SONGMAN->allplaylists.emplace(pl.name, pl);
SONGMAN->GetPlaylists().emplace(pl.name, pl);
SONGMAN->activeplaylist = pl.name;
Message msg("DisplayAll");
MESSAGEMAN->Broadcast(msg);
Expand Down Expand Up @@ -1825,7 +1830,7 @@ class LunaScreenSelectMusic : public Luna<ScreenSelectMusic>
static int StartPlaylistAsCourse(T* p, lua_State *L)
{
string name = SArg(1);
Playlist& pl = SONGMAN->allplaylists[name];
Playlist& pl = SONGMAN->GetPlaylists()[name];

// don't allow empty playlists to be started as a course
if (pl.chartlist.empty())
Expand Down
42 changes: 27 additions & 15 deletions src/SongManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,11 @@ void Playlist::LoadFromNode(const XNode* node) {
}
}

void SongManager::MakeSongGroupsFromPlaylists() {
for (auto& p : allplaylists) {
void SongManager::MakeSongGroupsFromPlaylists(map<string, Playlist>& playlists) {
for(auto& plName : playlistGroups)
groupderps.erase(plName);
playlistGroups.clear();
for (auto& p : playlists) {
vector<Song*> playlistgroup;
for (auto& n : p.second.chartlist)
if (n.loaded)
Expand All @@ -373,6 +376,7 @@ void SongManager::MakeSongGroupsFromPlaylists() {
groupderps.erase(p.first);
groupderps[p.first] = playlistgroup;
SongUtil::SortSongPointerArrayByTitle(groupderps[p.first]);
playlistGroups.emplace_back(p.first);
}
}

Expand All @@ -399,18 +403,18 @@ vector<string> Playlist::GetKeys() {
return o;
}

void SongManager::DeletePlaylist(const string& pl) {
allplaylists.erase(pl);
void SongManager::DeletePlaylist(const string& pl, map<string, Playlist> &playlists) {
playlists.erase(pl);

// stuff gets weird if all playlists have been deleted and a chart is added - mina
if(allplaylists.size() > 0)
activeplaylist = allplaylists.begin()->first;
if(playlists.size() > 0)
activeplaylist = playlists.begin()->first;

// clear out the entry for the music wheel as well or it'll crash -mina
groupderps.erase(pl);
}

void SongManager::MakePlaylistFromFavorites(set<string>& favs) {
void SongManager::MakePlaylistFromFavorites(set<string>& favs, map<string, Playlist>& playlists) {
Playlist pl;
pl.name = "Favorites";
for (auto& n : favs)
Expand All @@ -421,7 +425,7 @@ void SongManager::MakePlaylistFromFavorites(set<string>& favs) {
if (!pl.chartlist[i].loaded)
pl.DeleteChart(i);

allplaylists.emplace("Favorites", pl);
playlists.emplace("Favorites", pl);
}

string SongManager::ReconcileBustedKeys(const string& ck) {
Expand Down Expand Up @@ -706,10 +710,13 @@ bool SongManager::IsGroupNeverCached(const RString& group) const
}

void SongManager::SetFavoritedStatus(set<string>& favs) {
FOREACH(Song*, m_pSongs, song)
FOREACH(Song*, m_pSongs, song) {
bool fav = false;
FOREACH_CONST(Steps*, (*song)->GetAllSteps(), steps)
if (favs.count((*steps)->GetChartKey()))
(*song)->SetFavorited(true);
if (favs.count((*steps)->GetChartKey()))
fav = true;
(*song)->SetFavorited(fav);
}
}

void SongManager::SetPermaMirroredStatus(set<string>& pmir) {
Expand Down Expand Up @@ -764,11 +771,11 @@ bool SongManager::DoesSongGroupExist( const RString &sSongGroup ) const
return find( m_sSongGroupNames.begin(), m_sSongGroupNames.end(), sSongGroup ) != m_sSongGroupNames.end();
}

RageColor SongManager::GetSongGroupColor( const RString &sSongGroup ) const
RageColor SongManager::GetSongGroupColor( const RString &sSongGroup, map<string, Playlist>& playlists) const
{
for( unsigned i=0; i<m_sSongGroupNames.size(); i++ )
{
if( m_sSongGroupNames[i] == sSongGroup || allplaylists.count(sSongGroup))
if( m_sSongGroupNames[i] == sSongGroup || playlists.count(sSongGroup))
{
return SONG_GROUP_COLOR.GetValue( i%NUM_SONG_GROUP_COLORS );
}
Expand Down Expand Up @@ -948,6 +955,11 @@ void SongManager::Invalidate( const Song *pStaleSong )
UpdateShuffled();
}

map<string, Playlist>& SongManager::GetPlaylists()
{
return PROFILEMAN->GetProfile(PLAYER_1)->allplaylists;
}

void SongManager::SetPreferences()
{
}
Expand Down Expand Up @@ -1447,7 +1459,7 @@ class LunaSongManager: public Luna<SongManager>

static int GetActivePlaylist(T* p, lua_State *L)
{
p->allplaylists[p->activeplaylist].PushSelf(L);
p->GetPlaylists()[p->activeplaylist].PushSelf(L);
return 1;
}

Expand All @@ -1467,7 +1479,7 @@ class LunaSongManager: public Luna<SongManager>
{
int idx = 1;
lua_newtable(L);
FOREACHM(string, Playlist, p->allplaylists, pl) {
FOREACHM(string, Playlist, p->GetPlaylists(), pl) {
pl->second.PushSelf(L);
lua_rawseti(L, -2, idx);
++idx;
Expand Down
13 changes: 7 additions & 6 deletions src/SongManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Style;
class Steps;
class PlayerOptions;
struct lua_State;
struct GoalsForChart;

#include "RageTypes.h"
#include "GameConstantsAndTypes.h"
Expand Down Expand Up @@ -83,7 +84,7 @@ class SongManager
void Cleanup();

void Invalidate( const Song *pStaleSong );

static map<string, Playlist>& GetPlaylists();
void SetPreferences();
void SaveEnabledSongsToPref();
void LoadEnabledSongsFromPref();
Expand All @@ -107,7 +108,7 @@ class SongManager
void GetSongGroupNames( vector<RString> &AddTo ) const;
const vector<RString>& GetSongGroupNames() const;
bool DoesSongGroupExist( const RString &sSongGroup ) const;
RageColor GetSongGroupColor( const RString &sSongGroupName ) const;
RageColor GetSongGroupColor( const RString &sSongGroupName, map<string, Playlist>& playlists = GetPlaylists()) const;
RageColor GetSongColor( const Song* pSong ) const;

// temporary solution to reorganizing the entire songid/stepsid system - mina
Expand Down Expand Up @@ -182,16 +183,16 @@ class SongManager
// Lua
void PushSelf( lua_State *L );

map<string, Playlist> allplaylists;
string activeplaylist = "";
string playlistcourse = "";
string ReconcileBustedKeys(const string& ck);
map<string, string> keyconversionmap;
void MakeSongGroupsFromPlaylists();
void DeletePlaylist(const string& ck);
void MakePlaylistFromFavorites(set<string>& favs);
void MakeSongGroupsFromPlaylists(map<string, Playlist>& playlists = GetPlaylists());
void DeletePlaylist(const string& ck, map<string, Playlist>& playlists = GetPlaylists());
void MakePlaylistFromFavorites(set<string>& favs, map<string, Playlist>& playlists = GetPlaylists());

map<string, vector<Song*>> groupderps;
vector<string> playlistGroups; // To delete from groupderps when rebuilding playlist groups
protected:
void LoadStepManiaSongDir( RString sDir, LoadingWindow *ld );
void LoadDWISongDir( const RString &sDir );
Expand Down
10 changes: 5 additions & 5 deletions src/XMLProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,8 @@ XNode* XMLProfile::SavePlaylistsCreateNode(const Profile* profile) const {
CHECKPOINT_M("Saving the playlists node.");

XNode* playlists = new XNode("Playlists");
auto& pls = SONGMAN->allplaylists;
FOREACHM(string, Playlist, pls, i)
auto& pls = profile->allplaylists;
FOREACHM_CONST(string, Playlist, pls, i)
if (i->first != "" && i->first != "Favorites")
playlists->AppendChild(i->second.CreateNode());
return playlists;
Expand All @@ -458,7 +458,7 @@ void XMLProfile::LoadFavoritesFromNode(const XNode *pNode) {
loadingProfile->FavoritedCharts.emplace(SONGMAN->ReconcileBustedKeys(ck->GetName()));

SONGMAN->SetFavoritedStatus(loadingProfile->FavoritedCharts);
SONGMAN->MakePlaylistFromFavorites(loadingProfile->FavoritedCharts);
SONGMAN->MakePlaylistFromFavorites(loadingProfile->FavoritedCharts, loadingProfile->allplaylists);
}

void XMLProfile::LoadPermaMirrorFromNode(const XNode *pNode) {
Expand Down Expand Up @@ -493,7 +493,7 @@ void XMLProfile::LoadScoreGoalsFromNode(const XNode *pNode) {
void XMLProfile::LoadPlaylistsFromNode(const XNode *pNode) {
CHECKPOINT_M("Loading the playlists node.");

auto& pls = SONGMAN->allplaylists;
auto& pls = loadingProfile->allplaylists;
FOREACH_CONST_Child(pNode, pl) {
Playlist tmp;
tmp.LoadFromNode(pl);
Expand Down Expand Up @@ -1146,7 +1146,7 @@ XNode *XMLProfile::SaveEttXmlCreateNode(const Profile* profile) const
if (!profile->PermaMirrorCharts.empty())
xml->AppendChild(SavePermaMirrorCreateNode(profile));

if (!SONGMAN->allplaylists.empty())
if (!loadingProfile->allplaylists.empty())
xml->AppendChild(SavePlaylistsCreateNode(profile));

if (!profile->goalmap.empty())
Expand Down

0 comments on commit 5594f6c

Please sign in to comment.